adding tag in space model will add tag to spaces

This commit is contained in:
hannathkadher
2025-03-06 15:49:14 +04:00
parent 772459a106
commit 1f18f50923
3 changed files with 115 additions and 2 deletions

View File

@ -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,
);
}
}

View File

@ -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,