Add endpoint to rename unit by UUID

This commit is contained in:
faris Aljohari
2024-04-14 10:47:54 +03:00
parent 8dca50b24d
commit bf60303ddc
4 changed files with 95 additions and 1 deletions

View File

@ -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);
}
}
}
}