mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 21:54:54 +00:00
update space service
This commit is contained in:
@ -12,7 +12,6 @@ import {
|
||||
AddSpaceDto,
|
||||
AddSubspaceDto,
|
||||
CommunitySpaceParam,
|
||||
CreateTagDto,
|
||||
GetSpaceParam,
|
||||
UpdateSpaceDto,
|
||||
} from '../dtos';
|
||||
@ -29,11 +28,15 @@ import {
|
||||
} from '@app/common/constants/orphan-constant';
|
||||
import { CommandBus } from '@nestjs/cqrs';
|
||||
import { TagService } from './tag';
|
||||
import { TagService as NewTagService } from 'src/tags/services/tags.service';
|
||||
import { SpaceModelService } from 'src/space-model/services';
|
||||
import { DisableSpaceCommand } from '../commands';
|
||||
import { GetSpaceDto } from '../dtos/get.space.dto';
|
||||
import { removeCircularReferences } from '@app/common/helper/removeCircularReferences';
|
||||
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
|
||||
import { ProcessTagDto } from 'src/tags/dtos';
|
||||
import { SpaceProductAllocationService } from './space-product-allocation.service';
|
||||
import { SubspaceProductAllocationService } from './subspace/subspace-product-allocation.service';
|
||||
@Injectable()
|
||||
export class SpaceService {
|
||||
constructor(
|
||||
@ -44,8 +47,11 @@ export class SpaceService {
|
||||
private readonly subSpaceService: SubSpaceService,
|
||||
private readonly validationService: ValidationService,
|
||||
private readonly tagService: TagService,
|
||||
private readonly newTagService: NewTagService,
|
||||
private readonly spaceModelService: SpaceModelService,
|
||||
private commandBus: CommandBus,
|
||||
private readonly spaceProductAllocationService: SpaceProductAllocationService,
|
||||
private readonly subspaceProductAllocationService: SubspaceProductAllocationService,
|
||||
) {}
|
||||
|
||||
async createSpace(
|
||||
@ -93,6 +99,10 @@ export class SpaceService {
|
||||
});
|
||||
|
||||
const newSpace = await queryRunner.manager.save(space);
|
||||
const subspaceTags =
|
||||
this.subSpaceService.extractTagsFromSubspace(subspaces);
|
||||
const allTags = [...tags, ...subspaceTags];
|
||||
this.validateUniqueTags(allTags);
|
||||
|
||||
await Promise.all([
|
||||
spaceModelUuid &&
|
||||
@ -106,10 +116,17 @@ export class SpaceService {
|
||||
)
|
||||
: Promise.resolve(),
|
||||
subspaces?.length
|
||||
? this.createSubspaces(subspaces, newSpace, queryRunner, tags)
|
||||
? this.createSubspaces(
|
||||
subspaces,
|
||||
newSpace,
|
||||
queryRunner,
|
||||
tags,
|
||||
projectUuid,
|
||||
)
|
||||
: Promise.resolve(),
|
||||
|
||||
tags?.length
|
||||
? this.createTags(tags, queryRunner, newSpace)
|
||||
? this.createTags(tags, projectUuid, queryRunner, space)
|
||||
: Promise.resolve(),
|
||||
]);
|
||||
|
||||
@ -121,6 +138,8 @@ export class SpaceService {
|
||||
message: 'Space created successfully',
|
||||
});
|
||||
} catch (error) {
|
||||
console.log('error', error);
|
||||
|
||||
await queryRunner.rollbackTransaction();
|
||||
|
||||
if (error instanceof HttpException) {
|
||||
@ -131,7 +150,31 @@ export class SpaceService {
|
||||
await queryRunner.release();
|
||||
}
|
||||
}
|
||||
private validateUniqueTags(allTags: ProcessTagDto[]) {
|
||||
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,
|
||||
);
|
||||
}
|
||||
tagUuidSet.add(tag.uuid);
|
||||
} 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,
|
||||
);
|
||||
}
|
||||
tagNameProductSet.add(tagKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
async createFromModel(
|
||||
spaceModelUuid: string,
|
||||
queryRunner: QueryRunner,
|
||||
@ -408,6 +451,8 @@ export class SpaceService {
|
||||
modifiedSubspaces,
|
||||
queryRunner,
|
||||
space,
|
||||
projectUuid,
|
||||
updateSpaceDto.tags,
|
||||
);
|
||||
}
|
||||
|
||||
@ -423,7 +468,15 @@ export class SpaceService {
|
||||
space,
|
||||
);
|
||||
}
|
||||
|
||||
if (updateSpaceDto.tags) {
|
||||
await this.spaceProductAllocationService.updateSpaceProductAllocations(
|
||||
updateSpaceDto.tags,
|
||||
projectUuid,
|
||||
space,
|
||||
queryRunner,
|
||||
updateSpaceDto.subspace,
|
||||
);
|
||||
}
|
||||
await queryRunner.commitTransaction();
|
||||
|
||||
return new SuccessResponseDto({
|
||||
@ -603,21 +656,33 @@ export class SpaceService {
|
||||
subspaces: AddSubspaceDto[],
|
||||
space: SpaceEntity,
|
||||
queryRunner: QueryRunner,
|
||||
tags: CreateTagDto[],
|
||||
tags: ProcessTagDto[],
|
||||
projectUuid: string,
|
||||
): Promise<void> {
|
||||
space.subspaces = await this.subSpaceService.createSubspacesFromDto(
|
||||
subspaces,
|
||||
space,
|
||||
queryRunner,
|
||||
tags,
|
||||
projectUuid,
|
||||
);
|
||||
}
|
||||
|
||||
private async createTags(
|
||||
tags: CreateTagDto[],
|
||||
tags: ProcessTagDto[],
|
||||
projectUuid: string,
|
||||
queryRunner: QueryRunner,
|
||||
space: SpaceEntity,
|
||||
): Promise<void> {
|
||||
space.tags = await this.tagService.createTags(tags, queryRunner, space);
|
||||
const processedTags = await this.newTagService.processTags(
|
||||
tags,
|
||||
projectUuid,
|
||||
queryRunner,
|
||||
);
|
||||
await this.spaceProductAllocationService.createSpaceProductAllocations(
|
||||
space,
|
||||
processedTags,
|
||||
queryRunner,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user