mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-10 15:17:41 +00:00
added space model delete
This commit is contained in:
@ -281,6 +281,10 @@ export class ControllerRoute {
|
||||
public static readonly UPDATE_SPACE_MODEL_SUMMARY = 'Update Space Model';
|
||||
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';
|
||||
|
||||
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.';
|
||||
};
|
||||
};
|
||||
|
||||
|
@ -12,7 +12,8 @@ export const PermissionMapping = {
|
||||
'ADD',
|
||||
'UPDATE',
|
||||
'DELETE',
|
||||
'MODULE_ADD',
|
||||
'MODEL_ADD',
|
||||
'MODEL_DELETE',
|
||||
'MODEL_VIEW',
|
||||
'ASSIGN_USER_TO_SPACE',
|
||||
'DELETE_USER_FROM_SPACE',
|
||||
|
@ -16,9 +16,10 @@ export const RolePermissions = {
|
||||
'SPACE_ADD',
|
||||
'SPACE_UPDATE',
|
||||
'SPACE_DELETE',
|
||||
'SPACE_MODULE_ADD',
|
||||
'SPACE_MODEL_ADD',
|
||||
'SPACE_MODEL_VIEW',
|
||||
'SPACE_MODEL_UPDATE',
|
||||
'SPACE_MODEL_DELETE',
|
||||
'ASSIGN_USER_TO_SPACE',
|
||||
'DELETE_USER_FROM_SPACE',
|
||||
'SUBSPACE_VIEW',
|
||||
@ -61,9 +62,10 @@ export const RolePermissions = {
|
||||
'SPACE_ADD',
|
||||
'SPACE_UPDATE',
|
||||
'SPACE_DELETE',
|
||||
'SPACE_MODULE_ADD',
|
||||
'SPACE_MODEL_ADD',
|
||||
'SPACE_MODEL_VIEW',
|
||||
'SPACE_MODEL_UPDATE',
|
||||
'SPACE_MODEL_DELETE',
|
||||
'ASSIGN_USER_TO_SPACE',
|
||||
'DELETE_USER_FROM_SPACE',
|
||||
'SUBSPACE_VIEW',
|
||||
|
@ -2,6 +2,7 @@ import { ControllerRoute } from '@app/common/constants/controller-route';
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Delete,
|
||||
Get,
|
||||
Param,
|
||||
Post,
|
||||
@ -32,7 +33,7 @@ export class SpaceModelController {
|
||||
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(PermissionsGuard)
|
||||
@Permissions('SPACE_MODULE_ADD')
|
||||
@Permissions('SPACE_MODEL_ADD')
|
||||
@ApiOperation({
|
||||
summary: ControllerRoute.SPACE_MODEL.ACTIONS.CREATE_SPACE_MODEL_SUMMARY,
|
||||
description:
|
||||
@ -80,4 +81,17 @@ export class SpaceModelController {
|
||||
): Promise<BaseResponseDto> {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -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> {
|
||||
const isModelExist = await this.spaceModelRepository.findOne({
|
||||
where: { modelName, project: { uuid: projectUuid }, disabled: false },
|
||||
|
Reference in New Issue
Block a user