mirror of
https://github.com/SyncrowIOT/backend.git
synced 2026-10-25 14:47:55 +00:00
95 lines
2.9 KiB
TypeScript
95 lines
2.9 KiB
TypeScript
import { 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 { ProjectParam } from 'src/community/dtos';
|
|
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
|
import { SubSpaceModelService } from './subspace-model.service';
|
|
import { SpaceProductModelService } from './space-product-model.service';
|
|
|
|
@Injectable()
|
|
export class SpaceModelService {
|
|
constructor(
|
|
private readonly spaceModelRepository: SpaceModelRepository,
|
|
private readonly projectRepository: ProjectRepository,
|
|
private readonly subSpaceModelService: SubSpaceModelService,
|
|
private readonly spaceProductModelService: SpaceProductModelService,
|
|
) {}
|
|
|
|
async createSpaceModel(
|
|
createSpaceModelDto: CreateSpaceModelDto,
|
|
params: ProjectParam,
|
|
) {
|
|
const { modelName, subspaceModels, spaceProductModels } =
|
|
createSpaceModelDto;
|
|
const project = await this.validateProject(params.projectUuid);
|
|
|
|
try {
|
|
const isModelExist = await this.validateName(
|
|
modelName,
|
|
params.projectUuid,
|
|
);
|
|
if (isModelExist) {
|
|
throw new HttpException(
|
|
`Model name "${modelName}" already exists in this project ${project.name}.`,
|
|
HttpStatus.CONFLICT,
|
|
);
|
|
}
|
|
|
|
const spaceModel = this.spaceModelRepository.create({
|
|
modelName,
|
|
project,
|
|
});
|
|
const savedSpaceModel = await this.spaceModelRepository.save(spaceModel);
|
|
|
|
await this.subSpaceModelService.createSubSpaceModels(
|
|
subspaceModels,
|
|
savedSpaceModel,
|
|
);
|
|
|
|
await this.spaceProductModelService.createSpaceProductModels(
|
|
spaceProductModels,
|
|
savedSpaceModel,
|
|
);
|
|
|
|
return new SuccessResponseDto({
|
|
message: `Successfully created new space model with uuid ${savedSpaceModel.uuid}`,
|
|
data: savedSpaceModel,
|
|
statusCode: HttpStatus.CREATED,
|
|
});
|
|
} catch (error) {
|
|
if (error instanceof HttpException) {
|
|
throw error;
|
|
}
|
|
|
|
throw new HttpException(
|
|
error.message || `An unexpected error occurred`,
|
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
|
);
|
|
}
|
|
}
|
|
|
|
async validateProject(projectUuid: string) {
|
|
const project = await this.projectRepository.findOne({
|
|
where: {
|
|
uuid: projectUuid,
|
|
},
|
|
});
|
|
|
|
if (!project) {
|
|
throw new HttpException(
|
|
`Project with uuid ${projectUuid} not found`,
|
|
HttpStatus.NOT_FOUND,
|
|
);
|
|
}
|
|
return project;
|
|
}
|
|
|
|
async validateName(modelName: string, projectUuid: string): Promise<boolean> {
|
|
const isModelExist = await this.spaceModelRepository.exists({
|
|
where: { modelName, project: { uuid: projectUuid } },
|
|
});
|
|
return isModelExist;
|
|
}
|
|
}
|