From bf60303ddc88ad9ede708874e70a9b8293af3340 Mon Sep 17 00:00:00 2001 From: faris Aljohari <83524184+farisaljohari@users.noreply.github.com> Date: Sun, 14 Apr 2024 10:47:54 +0300 Subject: [PATCH] Add endpoint to rename unit by UUID --- src/unit/controllers/unit.controller.ts | 27 ++++++++++++++ src/unit/dtos/update.unit.dto.ts | 16 +++++++++ src/unit/interface/unit.interface.ts | 5 +++ src/unit/services/unit.service.ts | 48 ++++++++++++++++++++++++- 4 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 src/unit/dtos/update.unit.dto.ts diff --git a/src/unit/controllers/unit.controller.ts b/src/unit/controllers/unit.controller.ts index 40f35a1..cc746c8 100644 --- a/src/unit/controllers/unit.controller.ts +++ b/src/unit/controllers/unit.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 { AddUnitDto } from '../dtos/add.unit.dto'; import { GetUnitChildDto } from '../dtos/get.unit.dto'; +import { UpdateUnitNameDto } from '../dtos/update.unit.dto'; @ApiTags('Unit Module') @Controller({ @@ -96,4 +98,29 @@ export class UnitController { } } } + + @ApiBearerAuth() + @UseGuards(JwtAuthGuard) + @Put('rename/:unitUuid') + async renameUnitByUuid( + @Param('unitUuid') unitUuid: string, + @Body() updateUnitNameDto: UpdateUnitNameDto, + ) { + try { + const unit = await this.unitService.renameUnitByUuid( + unitUuid, + updateUnitNameDto, + ); + return unit; + } catch (error) { + if (error.status === 404) { + throw new HttpException('Unit not found', HttpStatus.NOT_FOUND); + } else { + throw new HttpException( + error.message || 'Internal server error', + error.status || HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + } + } } diff --git a/src/unit/dtos/update.unit.dto.ts b/src/unit/dtos/update.unit.dto.ts new file mode 100644 index 0000000..2d69902 --- /dev/null +++ b/src/unit/dtos/update.unit.dto.ts @@ -0,0 +1,16 @@ +import { ApiProperty } from '@nestjs/swagger'; +import { IsNotEmpty, IsString } from 'class-validator'; + +export class UpdateUnitNameDto { + @ApiProperty({ + description: 'unitName', + required: true, + }) + @IsString() + @IsNotEmpty() + public unitName: string; + + constructor(dto: Partial) { + Object.assign(this, dto); + } +} diff --git a/src/unit/interface/unit.interface.ts b/src/unit/interface/unit.interface.ts index 8db6bfa..8502a86 100644 --- a/src/unit/interface/unit.interface.ts +++ b/src/unit/interface/unit.interface.ts @@ -19,3 +19,8 @@ export interface UnitParentInterface { type: string; parent?: UnitParentInterface; } +export interface RenameUnitByUuidInterface { + uuid: string; + name: string; + type: string; +} diff --git a/src/unit/services/unit.service.ts b/src/unit/services/unit.service.ts index 6969717..1bff975 100644 --- a/src/unit/services/unit.service.ts +++ b/src/unit/services/unit.service.ts @@ -1,14 +1,21 @@ import { GetUnitChildDto } from '../dtos/get.unit.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 { AddUnitDto } from '../dtos'; import { UnitChildInterface, UnitParentInterface, GetUnitByUuidInterface, + RenameUnitByUuidInterface, } from '../interface/unit.interface'; import { SpaceEntity } from '@app/common/modules/space/entities'; +import { UpdateUnitNameDto } from '../dtos/update.unit.dto'; @Injectable() export class UnitService { @@ -179,4 +186,43 @@ export class UnitService { ); } } + + async renameUnitByUuid( + unitUuid: string, + updateUnitNameDto: UpdateUnitNameDto, + ): Promise { + try { + const unit = await this.spaceRepository.findOneOrFail({ + where: { uuid: unitUuid }, + relations: ['spaceType'], + }); + + if (!unit || !unit.spaceType || unit.spaceType.type !== 'unit') { + throw new BadRequestException('Invalid unit UUID'); + } + + await this.spaceRepository.update( + { uuid: unitUuid }, + { spaceName: updateUnitNameDto.unitName }, + ); + + // Fetch the updated unit + const updatedUnit = await this.spaceRepository.findOneOrFail({ + where: { uuid: unitUuid }, + relations: ['spaceType'], + }); + + return { + uuid: updatedUnit.uuid, + name: updatedUnit.spaceName, + type: updatedUnit.spaceType.type, + }; + } catch (err) { + if (err instanceof BadRequestException) { + throw err; // Re-throw BadRequestException + } else { + throw new HttpException('Unit not found', HttpStatus.NOT_FOUND); + } + } + } }