mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-10 15:17:41 +00:00
adding tag in space model will add tag to spaces
This commit is contained in:
@ -13,12 +13,17 @@ import { ModifySubspaceModelDto, ModifyTagModelDto } from '../dtos';
|
||||
import { ModifyAction } from '@app/common/constants/modify-action.enum';
|
||||
import { NewTagEntity } from '@app/common/modules/tag';
|
||||
import { ProductEntity } from '@app/common/modules/product/entities';
|
||||
import { SpaceRepository } from '@app/common/modules/space';
|
||||
import { SpaceProductAllocationService } from 'src/space/services/space-product-allocation.service';
|
||||
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
|
||||
|
||||
@Injectable()
|
||||
export class SpaceModelProductAllocationService {
|
||||
constructor(
|
||||
private readonly tagService: NewTagService,
|
||||
private readonly spaceModelProductAllocationRepository: SpaceModelProductAllocationRepoitory,
|
||||
private readonly spaceRepository: SpaceRepository,
|
||||
private readonly spaceProductAllocationService: SpaceProductAllocationService,
|
||||
) {}
|
||||
|
||||
async createProductAllocations(
|
||||
@ -27,6 +32,7 @@ export class SpaceModelProductAllocationService {
|
||||
tags: ProcessTagDto[],
|
||||
queryRunner?: QueryRunner,
|
||||
modifySubspaceModels?: ModifySubspaceModelDto[],
|
||||
spaces?: SpaceEntity[],
|
||||
): Promise<SpaceModelProductAllocationEntity[]> {
|
||||
try {
|
||||
if (!tags.length) return [];
|
||||
@ -102,12 +108,23 @@ export class SpaceModelProductAllocationService {
|
||||
} else if (!allocation.tags.some((t) => t.uuid === tag.uuid)) {
|
||||
allocation.tags.push(tag);
|
||||
await this.saveAllocation(allocation, queryRunner);
|
||||
await this.spaceProductAllocationService.addTagToAllocationFromModel(
|
||||
allocation,
|
||||
queryRunner,
|
||||
tag,
|
||||
spaces,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (productAllocations.length > 0) {
|
||||
await this.saveAllocations(productAllocations, queryRunner);
|
||||
await this.spaceProductAllocationService.createAllocationFromModel(
|
||||
productAllocations,
|
||||
queryRunner,
|
||||
spaces,
|
||||
);
|
||||
}
|
||||
|
||||
return productAllocations;
|
||||
@ -122,6 +139,7 @@ export class SpaceModelProductAllocationService {
|
||||
spaceModel: SpaceModelEntity,
|
||||
queryRunner: QueryRunner,
|
||||
modifySubspaceModels?: ModifySubspaceModelDto[],
|
||||
spaces?: SpaceEntity[],
|
||||
): Promise<void> {
|
||||
try {
|
||||
const addDtos = dtos.filter((dto) => dto.action === ModifyAction.ADD);
|
||||
@ -172,6 +190,7 @@ export class SpaceModelProductAllocationService {
|
||||
spaceModel,
|
||||
queryRunner,
|
||||
modifySubspaceModels,
|
||||
spaces,
|
||||
),
|
||||
this.processDeleteActions(filteredDtos, queryRunner),
|
||||
]);
|
||||
@ -186,6 +205,7 @@ export class SpaceModelProductAllocationService {
|
||||
spaceModel: SpaceModelEntity,
|
||||
queryRunner: QueryRunner,
|
||||
modifySubspaceModels?: ModifySubspaceModelDto[],
|
||||
spaces?: SpaceEntity[],
|
||||
): Promise<void> {
|
||||
const addDtos: ProcessTagDto[] = dtos
|
||||
.filter((dto) => dto.action === ModifyAction.ADD)
|
||||
@ -202,6 +222,7 @@ export class SpaceModelProductAllocationService {
|
||||
addDtos,
|
||||
queryRunner,
|
||||
modifySubspaceModels,
|
||||
spaces,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -198,6 +198,7 @@ export class SpaceModelService {
|
||||
|
||||
try {
|
||||
await queryRunner.startTransaction();
|
||||
const spaces = await this.fetchModelSpaces(spaceModel);
|
||||
|
||||
const { modelName } = dto;
|
||||
if (modelName) {
|
||||
@ -230,6 +231,7 @@ export class SpaceModelService {
|
||||
spaceModel,
|
||||
queryRunner,
|
||||
dto.subspaceModels,
|
||||
spaces,
|
||||
);
|
||||
}
|
||||
await queryRunner.commitTransaction();
|
||||
@ -318,6 +320,30 @@ export class SpaceModelService {
|
||||
}
|
||||
}
|
||||
|
||||
async fetchModelSpaces(
|
||||
spaceModel: SpaceModelEntity,
|
||||
queryRunner?: QueryRunner,
|
||||
) {
|
||||
const spaces = await (queryRunner
|
||||
? queryRunner.manager.find(SpaceEntity, {
|
||||
where: {
|
||||
spaceModel: {
|
||||
uuid: spaceModel.uuid,
|
||||
},
|
||||
disabled: false,
|
||||
},
|
||||
})
|
||||
: this.spaceRepository.find({
|
||||
where: {
|
||||
spaceModel: {
|
||||
uuid: spaceModel.uuid,
|
||||
},
|
||||
disabled: false,
|
||||
},
|
||||
}));
|
||||
return spaces;
|
||||
}
|
||||
|
||||
async linkSpaceModel(
|
||||
params: SpaceModelParam,
|
||||
dto: LinkSpacesToModelDto,
|
||||
|
@ -12,6 +12,7 @@ import { ModifyTagDto } from '../dtos/tag/modify-tag.dto';
|
||||
import { ModifySubspaceDto } from '../dtos';
|
||||
import { ProcessTagDto } from 'src/tags/dtos';
|
||||
import { TagService as NewTagService } from 'src/tags/services';
|
||||
import { SpaceModelProductAllocationEntity } from '@app/common/modules/space-model';
|
||||
|
||||
@Injectable()
|
||||
export class SpaceProductAllocationService {
|
||||
@ -108,6 +109,71 @@ export class SpaceProductAllocationService {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async createAllocationFromModel(
|
||||
modelAllocations: SpaceModelProductAllocationEntity[],
|
||||
queryRunner: QueryRunner,
|
||||
spaces?: SpaceEntity[],
|
||||
) {
|
||||
if (!spaces || spaces.length === 0 || !modelAllocations.length) return;
|
||||
|
||||
const allocations: SpaceProductAllocationEntity[] = [];
|
||||
|
||||
for (const space of spaces) {
|
||||
for (const modelAllocation of modelAllocations) {
|
||||
const allocation = queryRunner.manager.create(
|
||||
SpaceProductAllocationEntity,
|
||||
{
|
||||
space,
|
||||
product: modelAllocation.product,
|
||||
tags: modelAllocation.tags,
|
||||
inheritedFromModel: modelAllocation,
|
||||
},
|
||||
);
|
||||
allocations.push(allocation);
|
||||
}
|
||||
}
|
||||
|
||||
if (allocations.length > 0) {
|
||||
await queryRunner.manager.save(SpaceProductAllocationEntity, allocations);
|
||||
}
|
||||
}
|
||||
|
||||
async addTagToAllocationFromModel(
|
||||
modelAllocation: SpaceModelProductAllocationEntity,
|
||||
queryRunner: QueryRunner,
|
||||
tag: NewTagEntity,
|
||||
spaces?: SpaceEntity[],
|
||||
) {
|
||||
try {
|
||||
if (!spaces || spaces.length === 0 || !modelAllocation) return;
|
||||
|
||||
const spaceAllocations = await queryRunner.manager.find(
|
||||
SpaceProductAllocationEntity,
|
||||
{
|
||||
where: { inheritedFromModel: { uuid: modelAllocation.uuid } },
|
||||
relations: ['tags'],
|
||||
},
|
||||
);
|
||||
|
||||
if (spaceAllocations.length === 0) return;
|
||||
|
||||
for (const allocation of spaceAllocations) {
|
||||
allocation.tags.push(tag);
|
||||
}
|
||||
|
||||
await queryRunner.manager.save(
|
||||
SpaceProductAllocationEntity,
|
||||
spaceAllocations,
|
||||
);
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
'Failed to add tag to allocation from model',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async updateSpaceProductAllocations(
|
||||
dtos: ModifyTagDto[],
|
||||
projectUuid: string,
|
||||
@ -285,7 +351,7 @@ export class SpaceProductAllocationService {
|
||||
});
|
||||
}
|
||||
|
||||
private createNewAllocation(
|
||||
createNewAllocation(
|
||||
space: SpaceEntity,
|
||||
tag: NewTagEntity,
|
||||
queryRunner?: QueryRunner,
|
||||
@ -312,7 +378,7 @@ export class SpaceProductAllocationService {
|
||||
: await this.spaceProductAllocationRepository.save(allocation);
|
||||
}
|
||||
|
||||
private async saveAllocations(
|
||||
async saveAllocations(
|
||||
allocations: SpaceProductAllocationEntity[],
|
||||
queryRunner?: QueryRunner,
|
||||
) {
|
||||
|
Reference in New Issue
Block a user