mirror of
https://github.com/SyncrowIOT/backend.git
synced 2026-10-27 02:47:55 +00:00
update
This commit is contained in:
@@ -2,7 +2,7 @@ import { ApiProperty } from '@nestjs/swagger';
|
||||
import { IsNotEmpty, IsString, IsArray, ValidateNested } from 'class-validator';
|
||||
import { Type } from 'class-transformer';
|
||||
import { CreateSubspaceModelDto } from './subspaces-model-dtos/create-subspace-model.dto';
|
||||
import { CreateTagModelDto } from './tag-model-dtos/create-tag-model.dto';
|
||||
import { ProcessTagDto } from 'src/tags/dtos';
|
||||
|
||||
export class CreateSpaceModelDto {
|
||||
@ApiProperty({
|
||||
@@ -24,10 +24,10 @@ export class CreateSpaceModelDto {
|
||||
|
||||
@ApiProperty({
|
||||
description: 'List of tags associated with the space model',
|
||||
type: [CreateTagModelDto],
|
||||
type: [ProcessTagDto],
|
||||
})
|
||||
@IsArray()
|
||||
@ValidateNested({ each: true })
|
||||
@Type(() => CreateTagModelDto)
|
||||
tags?: CreateTagModelDto[];
|
||||
@Type(() => ProcessTagDto)
|
||||
tags?: ProcessTagDto[];
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { IsArray, IsNotEmpty, IsString, ValidateNested } from 'class-validator';
|
||||
import { CreateTagModelDto } from '../tag-model-dtos/create-tag-model.dto';
|
||||
import { Type } from 'class-transformer';
|
||||
import { CreateTagDto, ProcessTagDto } from 'src/tags/dtos';
|
||||
|
||||
export class CreateSubspaceModelDto {
|
||||
@ApiProperty({
|
||||
@@ -14,10 +14,10 @@ export class CreateSubspaceModelDto {
|
||||
|
||||
@ApiProperty({
|
||||
description: 'List of tag models associated with the subspace',
|
||||
type: [CreateTagModelDto],
|
||||
type: [ProcessTagDto],
|
||||
})
|
||||
@IsArray()
|
||||
@ValidateNested({ each: true })
|
||||
@Type(() => CreateTagModelDto)
|
||||
tags?: CreateTagModelDto[];
|
||||
@Type(() => ProcessTagDto)
|
||||
tags?: ProcessTagDto[];
|
||||
}
|
||||
|
||||
@@ -1,9 +1,15 @@
|
||||
import {
|
||||
SpaceModelEntity,
|
||||
SpaceModelProductAllocationEntity,
|
||||
SpaceModelProductAllocationRepoitory,
|
||||
SpaceModelRepository,
|
||||
} from '@app/common/modules/space-model';
|
||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||
import { CreateSpaceModelDto, UpdateSpaceModelDto } from '../dtos';
|
||||
import {
|
||||
CreateSpaceModelDto,
|
||||
CreateSubspaceModelDto,
|
||||
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';
|
||||
@@ -25,6 +31,10 @@ import {
|
||||
ModifySubspaceModelPayload,
|
||||
} from '../interfaces';
|
||||
import { SpaceModelDto } from '@app/common/modules/space-model/dtos';
|
||||
import { TagService as NewTagService } from 'src/tags/services';
|
||||
import { ProductRepository } from '@app/common/modules/product/repositories';
|
||||
import { NewTagEntity } from '@app/common/modules/tag';
|
||||
import { ProcessTagDto } from 'src/tags/dtos';
|
||||
|
||||
@Injectable()
|
||||
export class SpaceModelService {
|
||||
@@ -35,6 +45,9 @@ export class SpaceModelService {
|
||||
private readonly subSpaceModelService: SubSpaceModelService,
|
||||
private readonly tagModelService: TagModelService,
|
||||
private commandBus: CommandBus,
|
||||
private readonly tagService: NewTagService,
|
||||
private readonly spaceModelProductAllocationRepository: SpaceModelProductAllocationRepoitory,
|
||||
private readonly productRepository: ProductRepository,
|
||||
) {}
|
||||
|
||||
async createSpaceModel(
|
||||
@@ -62,23 +75,27 @@ export class SpaceModelService {
|
||||
|
||||
const savedSpaceModel = await queryRunner.manager.save(spaceModel);
|
||||
|
||||
this.validateUniqueTags(
|
||||
tags,
|
||||
this.subSpaceModelService.extractSubspaceTags(subspaceModels),
|
||||
);
|
||||
|
||||
if (subspaceModels?.length) {
|
||||
savedSpaceModel.subspaceModels =
|
||||
await this.subSpaceModelService.createSubSpaceModels(
|
||||
subspaceModels,
|
||||
await this.subSpaceModelService.createModels(
|
||||
savedSpaceModel,
|
||||
subspaceModels,
|
||||
queryRunner,
|
||||
tags,
|
||||
);
|
||||
}
|
||||
|
||||
if (tags?.length) {
|
||||
savedSpaceModel.tags = await this.tagModelService.createTags(
|
||||
const processedTags = await this.tagService.processTags(
|
||||
tags,
|
||||
queryRunner,
|
||||
savedSpaceModel,
|
||||
null,
|
||||
params.projectUuid,
|
||||
);
|
||||
|
||||
await this.createProductAllocations(spaceModel, processedTags);
|
||||
}
|
||||
|
||||
await queryRunner.commitTransaction();
|
||||
@@ -397,4 +414,83 @@ export class SpaceModelService {
|
||||
}
|
||||
return spaceModel;
|
||||
}
|
||||
|
||||
private async createProductAllocations(
|
||||
spaceModel: SpaceModelEntity,
|
||||
tags: NewTagEntity[],
|
||||
): Promise<void> {
|
||||
const productAllocations: SpaceModelProductAllocationEntity[] = [];
|
||||
|
||||
for (const tag of tags) {
|
||||
const product = await this.productRepository.findOne({
|
||||
where: { uuid: tag.product.uuid },
|
||||
});
|
||||
|
||||
if (!product) {
|
||||
throw new HttpException(
|
||||
`Product with UUID ${tag.product.uuid} not found.`,
|
||||
HttpStatus.NOT_FOUND,
|
||||
);
|
||||
}
|
||||
|
||||
const existingAllocation =
|
||||
await this.spaceModelProductAllocationRepository.findOne({
|
||||
where: { spaceModel, product },
|
||||
});
|
||||
|
||||
if (!existingAllocation) {
|
||||
const productAllocation =
|
||||
this.spaceModelProductAllocationRepository.create({
|
||||
spaceModel,
|
||||
product,
|
||||
tags: [tag],
|
||||
});
|
||||
|
||||
productAllocations.push(productAllocation);
|
||||
} else {
|
||||
if (
|
||||
!existingAllocation.tags.some(
|
||||
(existingTag) => existingTag.uuid === tag.uuid,
|
||||
)
|
||||
) {
|
||||
existingAllocation.tags.push(tag);
|
||||
await this.spaceModelProductAllocationRepository.save(
|
||||
existingAllocation,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (productAllocations.length > 0) {
|
||||
await this.spaceModelProductAllocationRepository.save(productAllocations);
|
||||
}
|
||||
}
|
||||
|
||||
private validateUniqueTags(
|
||||
spaceTags: ProcessTagDto[],
|
||||
subspaceTags: ProcessTagDto[],
|
||||
) {
|
||||
const allTags = [...spaceTags, ...subspaceTags];
|
||||
|
||||
const tagUuidSet = new Set<string>();
|
||||
const tagNameProductSet = new Set<string>();
|
||||
|
||||
for (const tag of allTags) {
|
||||
if (tag.uuid) {
|
||||
if (tagUuidSet.has(tag.uuid)) {
|
||||
throw new HttpException(
|
||||
`Duplicate tag UUID found: ${tag.uuid}`,
|
||||
HttpStatus.BAD_REQUEST,
|
||||
);
|
||||
}
|
||||
} else {
|
||||
const tagKey = `${tag.name}-${tag.productUuid}`;
|
||||
if (tagNameProductSet.has(tagKey)) {
|
||||
throw new HttpException(
|
||||
`Duplicate tag found with name "${tag.name}" and product "${tag.productUuid}".`,
|
||||
HttpStatus.BAD_REQUEST,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import {
|
||||
SpaceModelEntity,
|
||||
SubspaceModelEntity,
|
||||
SubspaceModelProductAllocationEntity,
|
||||
SubspaceModelProductAllocationRepoitory,
|
||||
SubspaceModelRepository,
|
||||
} from '@app/common/modules/space-model';
|
||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||
@@ -17,13 +19,57 @@ import {
|
||||
} from 'src/space-model/dtos/subspaces-model-dtos';
|
||||
import { TagModelService } from '../tag-model.service';
|
||||
import { ModifyAction } from '@app/common/constants/modify-action.enum';
|
||||
import { ProcessTagDto } from 'src/tags/dtos';
|
||||
import { TagService } from 'src/tags/services';
|
||||
import { NewTagEntity } from '@app/common/modules/tag';
|
||||
|
||||
@Injectable()
|
||||
export class SubSpaceModelService {
|
||||
constructor(
|
||||
private readonly subspaceModelRepository: SubspaceModelRepository,
|
||||
private readonly tagModelService: TagModelService,
|
||||
private readonly tagService: TagService,
|
||||
private readonly subspaceModelProductAllocationRepository: SubspaceModelProductAllocationRepoitory,
|
||||
) {}
|
||||
async createModels(
|
||||
spaceModel: SpaceModelEntity,
|
||||
dtos: CreateSubspaceModelDto[],
|
||||
queryRunner: QueryRunner,
|
||||
) {
|
||||
this.validateNamesInDTO(dtos.map((dto) => dto.subspaceName));
|
||||
|
||||
const subspaceEntities: SubspaceModelEntity[] = [];
|
||||
|
||||
for (const dto of dtos) {
|
||||
// Create Subspace Model entity
|
||||
const subspaceModel = queryRunner.manager.create(
|
||||
this.subspaceModelRepository.target,
|
||||
{
|
||||
subspaceName: dto.subspaceName,
|
||||
spaceModel,
|
||||
},
|
||||
);
|
||||
|
||||
subspaceEntities.push(subspaceModel);
|
||||
}
|
||||
|
||||
// Save all subspaces in bulk
|
||||
const savedSubspaces = await queryRunner.manager.save(subspaceEntities);
|
||||
|
||||
for (const [index, dto] of dtos.entries()) {
|
||||
const subspaceModel = savedSubspaces[index];
|
||||
|
||||
// Process tags for this subspace
|
||||
const processedTags = await this.tagService.processTags(
|
||||
dto.tags,
|
||||
spaceModel.project.uuid,
|
||||
);
|
||||
|
||||
// Create Product Allocations for Subspace Model
|
||||
await this.createSubspaceProductAllocations(subspaceModel, processedTags);
|
||||
}
|
||||
return savedSubspaces;
|
||||
}
|
||||
|
||||
async createSubSpaceModels(
|
||||
subSpaceModelDtos: CreateSubspaceModelDto[],
|
||||
@@ -327,10 +373,7 @@ export class SubSpaceModelService {
|
||||
}
|
||||
}
|
||||
|
||||
private async validateName(
|
||||
names: string[],
|
||||
spaceModel: SpaceModelEntity,
|
||||
): Promise<void> {
|
||||
private async validateNamesInDTO(names: string[]) {
|
||||
const seenNames = new Set<string>();
|
||||
const duplicateNames = new Set<string>();
|
||||
|
||||
@@ -346,7 +389,13 @@ export class SubSpaceModelService {
|
||||
HttpStatus.CONFLICT,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private async validateName(
|
||||
names: string[],
|
||||
spaceModel: SpaceModelEntity,
|
||||
): Promise<void> {
|
||||
this.validateNamesInDTO(names);
|
||||
for (const name of names) {
|
||||
await this.checkDuplicateNames(name, spaceModel.uuid);
|
||||
}
|
||||
@@ -368,4 +417,51 @@ export class SubSpaceModelService {
|
||||
await queryRunner.manager.save(subSpaceModel);
|
||||
}
|
||||
}
|
||||
|
||||
extractSubspaceTags(
|
||||
subspaceModels: CreateSubspaceModelDto[],
|
||||
): ProcessTagDto[] {
|
||||
return subspaceModels.flatMap((subspace) => subspace.tags || []);
|
||||
}
|
||||
|
||||
private async createSubspaceProductAllocations(
|
||||
subspaceModel: SubspaceModelEntity,
|
||||
tags: NewTagEntity[],
|
||||
): Promise<void> {
|
||||
const allocations: SubspaceModelProductAllocationEntity[] = [];
|
||||
|
||||
for (const tag of tags) {
|
||||
const existingAllocation =
|
||||
await this.subspaceModelProductAllocationRepository.findOne({
|
||||
where: { subspaceModel, product: tag.product },
|
||||
});
|
||||
|
||||
if (!existingAllocation) {
|
||||
const allocation = this.subspaceModelProductAllocationRepository.create(
|
||||
{
|
||||
subspaceModel,
|
||||
product: tag.product,
|
||||
tags: [tag],
|
||||
},
|
||||
);
|
||||
|
||||
allocations.push(allocation);
|
||||
} else {
|
||||
if (
|
||||
!existingAllocation.tags.some(
|
||||
(existingTag) => existingTag.uuid === tag.uuid,
|
||||
)
|
||||
) {
|
||||
existingAllocation.tags.push(tag);
|
||||
await this.subspaceModelProductAllocationRepository.save(
|
||||
existingAllocation,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (allocations.length > 0) {
|
||||
await this.subspaceModelProductAllocationRepository.save(allocations);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user