added edit space module endpoint

This commit is contained in:
hannathkadher
2024-12-18 15:00:34 +04:00
parent 7833151b70
commit 85aa64ac36
8 changed files with 103 additions and 17 deletions

View File

@ -277,6 +277,10 @@ export class ControllerRoute {
public static readonly LIST_SPACE_MODEL_SUMMARY = 'List Space Models';
public static readonly LIST_SPACE_MODEL_DESCRIPTION =
'This endpoint allows you to retrieve a list of space models within a specified project. Each space model includes its structure, associated subspaces, products, and product items.';
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';
};
};

View File

@ -18,6 +18,7 @@ export const RolePermissions = {
'SPACE_DELETE',
'SPACE_MODULE_ADD',
'SPACE_MODEL_VIEW',
'SPACE_MODEL_UPDATE',
'ASSIGN_USER_TO_SPACE',
'DELETE_USER_FROM_SPACE',
'SUBSPACE_VIEW',
@ -62,6 +63,7 @@ export const RolePermissions = {
'SPACE_DELETE',
'SPACE_MODULE_ADD',
'SPACE_MODEL_VIEW',
'SPACE_MODEL_UPDATE',
'ASSIGN_USER_TO_SPACE',
'DELETE_USER_FROM_SPACE',
'SUBSPACE_VIEW',

View File

@ -5,12 +5,17 @@ import {
Get,
Param,
Post,
Put,
Query,
UseGuards,
} from '@nestjs/common';
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
import { SpaceModelService } from '../services';
import { CreateSpaceModelDto } from '../dtos';
import {
CreateSpaceModelDto,
SpaceModelParam,
UpdateSpaceModelDto,
} from '../dtos';
import { ProjectParam } from 'src/community/dtos';
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
import { PermissionsGuard } from 'src/guards/permissions.guard';
@ -59,4 +64,20 @@ export class SpaceModelController {
): Promise<BaseResponseDto> {
return await this.spaceModelService.list(projectParam, query);
}
@ApiBearerAuth()
@UseGuards(PermissionsGuard)
@Permissions('SPACE_MODEL_UPDATE')
@ApiOperation({
summary: ControllerRoute.SPACE_MODEL.ACTIONS.UPDATE_SPACE_MODEL_SUMMARY,
description:
ControllerRoute.SPACE_MODEL.ACTIONS.UPDATE_SPACE_MODEL_DESCRIPTION,
})
@Put(':spaceModelUuid')
async update(
@Body() dto: UpdateSpaceModelDto,
@Param() param: SpaceModelParam,
): Promise<BaseResponseDto> {
return await this.spaceModelService.update(dto, param);
}
}

View File

@ -3,3 +3,5 @@ export * from './create-space-product-item-model.dto';
export * from './create-space-product-model.dto';
export * from './create-subspace-model.dto';
export * from './project-param.dto';
export * from './update-space-model.dto';
export * from './space-model-param';

View File

@ -1,7 +1,7 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsUUID } from 'class-validator';
export class projectParam {
export class ProjectParam {
@ApiProperty({
description: 'UUID of the Project',
example: 'd290f1ee-6c54-4b01-90e6-d701748f0851',

View File

@ -0,0 +1,11 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsUUID } from 'class-validator';
import { ProjectParam } from './project-param.dto';
export class SpaceModelParam extends ProjectParam {
@ApiProperty({
description: 'UUID of the Space',
example: 'd290f1ee-6c54-4b01-90e6-d701748f0851',
})
@IsUUID()
spaceModelUuid: string;
}

View File

@ -0,0 +1,12 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsOptional, IsString } from 'class-validator';
export class UpdateSpaceModelDto {
@ApiProperty({
description: 'Updated name of the space model',
example: 'New Space Model Name',
})
@IsOptional()
@IsString()
modelName?: string;
}

View File

@ -1,7 +1,9 @@
import { SpaceModelRepository } from '@app/common/modules/space-model';
import {
SpaceModelEntity,
SpaceModelRepository,
} from '@app/common/modules/space-model';
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { CreateSpaceModelDto } from '../dtos';
import { ProjectRepository } from '@app/common/modules/project/repositiories';
import { CreateSpaceModelDto, UpdateSpaceModelDto } from '../dtos';
import { ProjectParam } from 'src/community/dtos';
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
import { SubSpaceModelService } from './subspace/subspace-model.service';
@ -13,13 +15,16 @@ import {
} from '@app/common/models/typeOrmCustom.model';
import { PageResponse } from '@app/common/dto/pagination.response.dto';
import { SpaceModelDto } from '@app/common/modules/space-model/dtos';
import { SpaceModelParam } from '../dtos/space-model-param';
import { ProjectService } from 'src/project/services';
import { ProjectEntity } from '@app/common/modules/project/entities';
@Injectable()
export class SpaceModelService {
constructor(
private readonly dataSource: DataSource,
private readonly spaceModelRepository: SpaceModelRepository,
private readonly projectRepository: ProjectRepository,
private readonly projectService: ProjectService,
private readonly subSpaceModelService: SubSpaceModelService,
private readonly spaceProductModelService: SpaceProductModelService,
) {}
@ -44,7 +49,7 @@ export class SpaceModelService {
);
if (isModelExist) {
throw new HttpException(
`Model name "${modelName}" already exists in this project ${project.name}.`,
`Model name "${modelName}" already exists in this project ${params.projectUuid}.`,
HttpStatus.CONFLICT,
);
}
@ -125,20 +130,37 @@ export class SpaceModelService {
}
}
async validateProject(projectUuid: string) {
const project = await this.projectRepository.findOne({
where: {
uuid: projectUuid,
},
});
async validateProject(projectUuid: string): Promise<ProjectEntity> {
return await this.projectService.findOne(projectUuid);
}
if (!project) {
async update(dto: UpdateSpaceModelDto, param: SpaceModelParam) {
await this.validateProject(param.projectUuid);
const spaceModel = await this.validateSpaceModel(param.spaceModelUuid);
const queryRunner = this.dataSource.createQueryRunner();
await queryRunner.connect();
await queryRunner.startTransaction();
try {
const { modelName } = dto;
if (modelName) spaceModel.modelName = modelName;
await queryRunner.manager.save(spaceModel);
await queryRunner.commitTransaction();
return new SuccessResponseDto({
message: 'SpaceModel updated successfully',
data: spaceModel,
});
} catch (error) {
await queryRunner.rollbackTransaction();
throw new HttpException(
`Project with uuid ${projectUuid} not found`,
HttpStatus.NOT_FOUND,
error.message || 'Failed to update SpaceModel',
HttpStatus.INTERNAL_SERVER_ERROR,
);
} finally {
await queryRunner.release();
}
return project;
}
async validateName(modelName: string, projectUuid: string): Promise<boolean> {
@ -147,4 +169,16 @@ export class SpaceModelService {
});
return isModelExist;
}
async validateSpaceModel(uuid: string): Promise<SpaceModelEntity> {
const spaceModel = await this.spaceModelRepository.findOne({
where: {
uuid,
},
});
if (!spaceModel) {
throw new HttpException('space model not found', HttpStatus.NOT_FOUND);
}
return spaceModel;
}
}