mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 16:14:55 +00:00
Add endpoint to rename unit by UUID
This commit is contained in:
@ -7,6 +7,7 @@ import {
|
|||||||
HttpStatus,
|
HttpStatus,
|
||||||
Param,
|
Param,
|
||||||
Post,
|
Post,
|
||||||
|
Put,
|
||||||
Query,
|
Query,
|
||||||
UseGuards,
|
UseGuards,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
@ -14,6 +15,7 @@ import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
|||||||
import { JwtAuthGuard } from '../../../libs/common/src/guards/jwt.auth.guard';
|
import { JwtAuthGuard } from '../../../libs/common/src/guards/jwt.auth.guard';
|
||||||
import { AddUnitDto } from '../dtos/add.unit.dto';
|
import { AddUnitDto } from '../dtos/add.unit.dto';
|
||||||
import { GetUnitChildDto } from '../dtos/get.unit.dto';
|
import { GetUnitChildDto } from '../dtos/get.unit.dto';
|
||||||
|
import { UpdateUnitNameDto } from '../dtos/update.unit.dto';
|
||||||
|
|
||||||
@ApiTags('Unit Module')
|
@ApiTags('Unit Module')
|
||||||
@Controller({
|
@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,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
16
src/unit/dtos/update.unit.dto.ts
Normal file
16
src/unit/dtos/update.unit.dto.ts
Normal file
@ -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<UpdateUnitNameDto>) {
|
||||||
|
Object.assign(this, dto);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -19,3 +19,8 @@ export interface UnitParentInterface {
|
|||||||
type: string;
|
type: string;
|
||||||
parent?: UnitParentInterface;
|
parent?: UnitParentInterface;
|
||||||
}
|
}
|
||||||
|
export interface RenameUnitByUuidInterface {
|
||||||
|
uuid: string;
|
||||||
|
name: string;
|
||||||
|
type: string;
|
||||||
|
}
|
||||||
|
|||||||
@ -1,14 +1,21 @@
|
|||||||
import { GetUnitChildDto } from '../dtos/get.unit.dto';
|
import { GetUnitChildDto } from '../dtos/get.unit.dto';
|
||||||
import { SpaceTypeRepository } from '../../../libs/common/src/modules/space-type/repositories/space.type.repository';
|
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 { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||||
import { AddUnitDto } from '../dtos';
|
import { AddUnitDto } from '../dtos';
|
||||||
import {
|
import {
|
||||||
UnitChildInterface,
|
UnitChildInterface,
|
||||||
UnitParentInterface,
|
UnitParentInterface,
|
||||||
GetUnitByUuidInterface,
|
GetUnitByUuidInterface,
|
||||||
|
RenameUnitByUuidInterface,
|
||||||
} from '../interface/unit.interface';
|
} from '../interface/unit.interface';
|
||||||
import { SpaceEntity } from '@app/common/modules/space/entities';
|
import { SpaceEntity } from '@app/common/modules/space/entities';
|
||||||
|
import { UpdateUnitNameDto } from '../dtos/update.unit.dto';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class UnitService {
|
export class UnitService {
|
||||||
@ -179,4 +186,43 @@ export class UnitService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async renameUnitByUuid(
|
||||||
|
unitUuid: string,
|
||||||
|
updateUnitNameDto: UpdateUnitNameDto,
|
||||||
|
): Promise<RenameUnitByUuidInterface> {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user