added space model delete

This commit is contained in:
hannathkadher
2024-12-23 22:13:24 +04:00
parent 41c86b47eb
commit fef44b3c4f
5 changed files with 77 additions and 4 deletions

View File

@ -281,6 +281,10 @@ export class ControllerRoute {
public static readonly UPDATE_SPACE_MODEL_SUMMARY = 'Update Space Model'; public static readonly UPDATE_SPACE_MODEL_SUMMARY = 'Update Space Model';
public static readonly UPDATE_SPACE_MODEL_DESCRIPTION = public static readonly UPDATE_SPACE_MODEL_DESCRIPTION =
'This endpoint allows you to update a Space Model attributesas well as manage its associated Subspaces and Device'; 'This endpoint allows you to update a Space Model attributesas well as manage its associated Subspaces and Device';
public static readonly DELETE_SPACE_MODEL_SUMMARY = 'Delete Space Model';
public static readonly DELETE_SPACE_MODEL_DESCRIPTION =
'This endpoint allows you to delete a specified Space Model within a project. Deleting a Space Model disables the model and all its associated subspaces and tags, ensuring they are no longer active but remain in the system for auditing.';
}; };
}; };

View File

@ -12,7 +12,8 @@ export const PermissionMapping = {
'ADD', 'ADD',
'UPDATE', 'UPDATE',
'DELETE', 'DELETE',
'MODULE_ADD', 'MODEL_ADD',
'MODEL_DELETE',
'MODEL_VIEW', 'MODEL_VIEW',
'ASSIGN_USER_TO_SPACE', 'ASSIGN_USER_TO_SPACE',
'DELETE_USER_FROM_SPACE', 'DELETE_USER_FROM_SPACE',

View File

@ -16,9 +16,10 @@ export const RolePermissions = {
'SPACE_ADD', 'SPACE_ADD',
'SPACE_UPDATE', 'SPACE_UPDATE',
'SPACE_DELETE', 'SPACE_DELETE',
'SPACE_MODULE_ADD', 'SPACE_MODEL_ADD',
'SPACE_MODEL_VIEW', 'SPACE_MODEL_VIEW',
'SPACE_MODEL_UPDATE', 'SPACE_MODEL_UPDATE',
'SPACE_MODEL_DELETE',
'ASSIGN_USER_TO_SPACE', 'ASSIGN_USER_TO_SPACE',
'DELETE_USER_FROM_SPACE', 'DELETE_USER_FROM_SPACE',
'SUBSPACE_VIEW', 'SUBSPACE_VIEW',
@ -61,9 +62,10 @@ export const RolePermissions = {
'SPACE_ADD', 'SPACE_ADD',
'SPACE_UPDATE', 'SPACE_UPDATE',
'SPACE_DELETE', 'SPACE_DELETE',
'SPACE_MODULE_ADD', 'SPACE_MODEL_ADD',
'SPACE_MODEL_VIEW', 'SPACE_MODEL_VIEW',
'SPACE_MODEL_UPDATE', 'SPACE_MODEL_UPDATE',
'SPACE_MODEL_DELETE',
'ASSIGN_USER_TO_SPACE', 'ASSIGN_USER_TO_SPACE',
'DELETE_USER_FROM_SPACE', 'DELETE_USER_FROM_SPACE',
'SUBSPACE_VIEW', 'SUBSPACE_VIEW',

View File

@ -2,6 +2,7 @@ import { ControllerRoute } from '@app/common/constants/controller-route';
import { import {
Body, Body,
Controller, Controller,
Delete,
Get, Get,
Param, Param,
Post, Post,
@ -32,7 +33,7 @@ export class SpaceModelController {
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(PermissionsGuard) @UseGuards(PermissionsGuard)
@Permissions('SPACE_MODULE_ADD') @Permissions('SPACE_MODEL_ADD')
@ApiOperation({ @ApiOperation({
summary: ControllerRoute.SPACE_MODEL.ACTIONS.CREATE_SPACE_MODEL_SUMMARY, summary: ControllerRoute.SPACE_MODEL.ACTIONS.CREATE_SPACE_MODEL_SUMMARY,
description: description:
@ -80,4 +81,17 @@ export class SpaceModelController {
): Promise<BaseResponseDto> { ): Promise<BaseResponseDto> {
return await this.spaceModelService.update(dto, param); return await this.spaceModelService.update(dto, param);
} }
@ApiBearerAuth()
@UseGuards(PermissionsGuard)
@Permissions('SPACE_MODEL_DELETE')
@ApiOperation({
summary: ControllerRoute.SPACE_MODEL.ACTIONS.DELETE_SPACE_MODEL_SUMMARY,
description:
ControllerRoute.SPACE_MODEL.ACTIONS.DELETE_SPACE_MODEL_DESCRIPTION,
})
@Delete(':spaceModelUuid')
async delete(@Param() param: SpaceModelParam): Promise<BaseResponseDto> {
return await this.spaceModelService.deleteSpaceModel(param);
}
} }

View File

@ -169,6 +169,58 @@ export class SpaceModelService {
} }
} }
async deleteSpaceModel(param: SpaceModelParam): Promise<BaseResponseDto> {
const queryRunner = this.dataSource.createQueryRunner();
await queryRunner.connect();
await queryRunner.startTransaction();
try {
await this.validateProject(param.projectUuid);
const spaceModel = await this.validateSpaceModel(param.spaceModelUuid);
if (spaceModel.subspaceModels?.length) {
const deleteSubspaceDtos = spaceModel.subspaceModels.map(
(subspace) => ({
subspaceUuid: subspace.uuid,
}),
);
await this.subSpaceModelService.deleteSubspaceModels(
deleteSubspaceDtos,
queryRunner,
);
}
await queryRunner.manager.update(
this.spaceModelRepository.target,
{ uuid: param.spaceModelUuid },
{ disabled: true },
);
await queryRunner.commitTransaction();
return new SuccessResponseDto({
message: `SpaceModel with UUID ${param.spaceModelUuid} deleted successfully.`,
statusCode: HttpStatus.OK,
});
} catch (error) {
await queryRunner.rollbackTransaction();
const errorMessage =
error instanceof HttpException
? error.message
: 'An unexpected error occurred while deleting the SpaceModel';
const statusCode =
error instanceof HttpException
? error.getStatus()
: HttpStatus.INTERNAL_SERVER_ERROR;
throw new HttpException(errorMessage, statusCode);
} finally {
await queryRunner.release();
}
}
async validateName(modelName: string, projectUuid: string): Promise<void> { async validateName(modelName: string, projectUuid: string): Promise<void> {
const isModelExist = await this.spaceModelRepository.findOne({ const isModelExist = await this.spaceModelRepository.findOne({
where: { modelName, project: { uuid: projectUuid }, disabled: false }, where: { modelName, project: { uuid: projectUuid }, disabled: false },