From 42a4d0cc11dbd009c17935515d67cab5af7f0a43 Mon Sep 17 00:00:00 2001 From: faris Aljohari <83524184+farisaljohari@users.noreply.github.com> Date: Sun, 14 Apr 2024 10:17:11 +0300 Subject: [PATCH] Add endpoint to rename community by UUID --- .../controllers/community.controller.ts | 27 ++++++++++ src/community/dtos/update.community.dto.ts | 16 ++++++ .../interface/community.interface.ts | 5 ++ src/community/services/community.service.ts | 51 ++++++++++++++++++- 4 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 src/community/dtos/update.community.dto.ts diff --git a/src/community/controllers/community.controller.ts b/src/community/controllers/community.controller.ts index 6b1a5a7..1a07791 100644 --- a/src/community/controllers/community.controller.ts +++ b/src/community/controllers/community.controller.ts @@ -7,6 +7,7 @@ import { HttpStatus, Param, Post, + Put, Query, UseGuards, } from '@nestjs/common'; @@ -14,6 +15,7 @@ import { ApiTags, ApiBearerAuth } from '@nestjs/swagger'; import { JwtAuthGuard } from '../../../libs/common/src/guards/jwt.auth.guard'; import { AddCommunityDto } from '../dtos/add.community.dto'; import { GetCommunityChildDto } from '../dtos/get.community.dto'; +import { UpdateCommunityNameDto } from '../dtos/update.community.dto'; @ApiTags('Community Module') @Controller({ @@ -82,4 +84,29 @@ export class CommunityController { } } } + + @ApiBearerAuth() + @UseGuards(JwtAuthGuard) + @Put('rename/:communityUuid') + async renameCommunityByUuid( + @Param('communityUuid') communityUuid: string, + @Body() updateCommunityDto: UpdateCommunityNameDto, + ) { + try { + const community = await this.communityService.renameCommunityByUuid( + communityUuid, + updateCommunityDto, + ); + return community; + } catch (error) { + if (error.status === 404) { + throw new HttpException('Community not found', HttpStatus.NOT_FOUND); + } else { + throw new HttpException( + error.message || 'Internal server error', + error.status || HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + } + } } diff --git a/src/community/dtos/update.community.dto.ts b/src/community/dtos/update.community.dto.ts new file mode 100644 index 0000000..6f15d43 --- /dev/null +++ b/src/community/dtos/update.community.dto.ts @@ -0,0 +1,16 @@ +import { ApiProperty } from '@nestjs/swagger'; +import { IsNotEmpty, IsString } from 'class-validator'; + +export class UpdateCommunityNameDto { + @ApiProperty({ + description: 'communityName', + required: true, + }) + @IsString() + @IsNotEmpty() + public communityName: string; + + constructor(dto: Partial) { + Object.assign(this, dto); + } +} diff --git a/src/community/interface/community.interface.ts b/src/community/interface/community.interface.ts index 7f769d3..ca18aca 100644 --- a/src/community/interface/community.interface.ts +++ b/src/community/interface/community.interface.ts @@ -13,3 +13,8 @@ export interface CommunityChildInterface { totalCount?: number; children?: CommunityChildInterface[]; } +export interface RenameCommunityByUuidInterface { + uuid: string; + name: string; + type: string; +} diff --git a/src/community/services/community.service.ts b/src/community/services/community.service.ts index a917d94..33cab98 100644 --- a/src/community/services/community.service.ts +++ b/src/community/services/community.service.ts @@ -1,13 +1,20 @@ import { GetCommunityChildDto } from './../dtos/get.community.dto'; import { SpaceTypeRepository } from './../../../libs/common/src/modules/space-type/repositories/space.type.repository'; -import { Injectable, HttpException, HttpStatus } from '@nestjs/common'; +import { + Injectable, + HttpException, + HttpStatus, + BadRequestException, +} from '@nestjs/common'; import { SpaceRepository } from '@app/common/modules/space/repositories'; import { AddCommunityDto } from '../dtos'; import { CommunityChildInterface, GetCommunityByUuidInterface, + RenameCommunityByUuidInterface, } from '../interface/community.interface'; import { SpaceEntity } from '@app/common/modules/space/entities'; +import { UpdateCommunityNameDto } from '../dtos/update.community.dto'; @Injectable() export class CommunityService { @@ -131,4 +138,46 @@ export class CommunityService { return childHierarchies; } + async renameCommunityByUuid( + communityUuid: string, + updateCommunityDto: UpdateCommunityNameDto, + ): Promise { + try { + const community = await this.spaceRepository.findOneOrFail({ + where: { uuid: communityUuid }, + relations: ['spaceType'], + }); + + if ( + !community || + !community.spaceType || + community.spaceType.type !== 'community' + ) { + throw new BadRequestException('Invalid community UUID'); + } + + await this.spaceRepository.update( + { uuid: communityUuid }, + { spaceName: updateCommunityDto.communityName }, + ); + + // Fetch the updated community + const updatedCommunity = await this.spaceRepository.findOneOrFail({ + where: { uuid: communityUuid }, + relations: ['spaceType'], + }); + + return { + uuid: updatedCommunity.uuid, + name: updatedCommunity.spaceName, + type: updatedCommunity.spaceType.type, + }; + } catch (err) { + if (err instanceof BadRequestException) { + throw err; // Re-throw BadRequestException + } else { + throw new HttpException('Community not found', HttpStatus.NOT_FOUND); + } + } + } }