mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 20:24:54 +00:00
Add endpoint to rename unit by UUID
This commit is contained in:
@ -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<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