mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-16 18:56:22 +00:00
added edit space module endpoint
This commit is contained in:
@ -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';
|
||||
};
|
||||
};
|
||||
|
||||
|
@ -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',
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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';
|
||||
|
@ -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',
|
||||
|
11
src/space-model/dtos/space-model-param.ts
Normal file
11
src/space-model/dtos/space-model-param.ts
Normal 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;
|
||||
}
|
12
src/space-model/dtos/update-space-model.dto.ts
Normal file
12
src/space-model/dtos/update-space-model.dto.ts
Normal 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;
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user