mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-11 15:48:09 +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 { ModifyAction } from '@app/common/constants/modify-action.enum';
|
||||||
import { NewTagEntity } from '@app/common/modules/tag';
|
import { NewTagEntity } from '@app/common/modules/tag';
|
||||||
import { ProductEntity } from '@app/common/modules/product/entities';
|
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()
|
@Injectable()
|
||||||
export class SpaceModelProductAllocationService {
|
export class SpaceModelProductAllocationService {
|
||||||
constructor(
|
constructor(
|
||||||
private readonly tagService: NewTagService,
|
private readonly tagService: NewTagService,
|
||||||
private readonly spaceModelProductAllocationRepository: SpaceModelProductAllocationRepoitory,
|
private readonly spaceModelProductAllocationRepository: SpaceModelProductAllocationRepoitory,
|
||||||
|
private readonly spaceRepository: SpaceRepository,
|
||||||
|
private readonly spaceProductAllocationService: SpaceProductAllocationService,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async createProductAllocations(
|
async createProductAllocations(
|
||||||
@ -27,6 +32,7 @@ export class SpaceModelProductAllocationService {
|
|||||||
tags: ProcessTagDto[],
|
tags: ProcessTagDto[],
|
||||||
queryRunner?: QueryRunner,
|
queryRunner?: QueryRunner,
|
||||||
modifySubspaceModels?: ModifySubspaceModelDto[],
|
modifySubspaceModels?: ModifySubspaceModelDto[],
|
||||||
|
spaces?: SpaceEntity[],
|
||||||
): Promise<SpaceModelProductAllocationEntity[]> {
|
): Promise<SpaceModelProductAllocationEntity[]> {
|
||||||
try {
|
try {
|
||||||
if (!tags.length) return [];
|
if (!tags.length) return [];
|
||||||
@ -102,12 +108,23 @@ export class SpaceModelProductAllocationService {
|
|||||||
} else if (!allocation.tags.some((t) => t.uuid === tag.uuid)) {
|
} else if (!allocation.tags.some((t) => t.uuid === tag.uuid)) {
|
||||||
allocation.tags.push(tag);
|
allocation.tags.push(tag);
|
||||||
await this.saveAllocation(allocation, queryRunner);
|
await this.saveAllocation(allocation, queryRunner);
|
||||||
|
await this.spaceProductAllocationService.addTagToAllocationFromModel(
|
||||||
|
allocation,
|
||||||
|
queryRunner,
|
||||||
|
tag,
|
||||||
|
spaces,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (productAllocations.length > 0) {
|
if (productAllocations.length > 0) {
|
||||||
await this.saveAllocations(productAllocations, queryRunner);
|
await this.saveAllocations(productAllocations, queryRunner);
|
||||||
|
await this.spaceProductAllocationService.createAllocationFromModel(
|
||||||
|
productAllocations,
|
||||||
|
queryRunner,
|
||||||
|
spaces,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return productAllocations;
|
return productAllocations;
|
||||||
@ -122,6 +139,7 @@ export class SpaceModelProductAllocationService {
|
|||||||
spaceModel: SpaceModelEntity,
|
spaceModel: SpaceModelEntity,
|
||||||
queryRunner: QueryRunner,
|
queryRunner: QueryRunner,
|
||||||
modifySubspaceModels?: ModifySubspaceModelDto[],
|
modifySubspaceModels?: ModifySubspaceModelDto[],
|
||||||
|
spaces?: SpaceEntity[],
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
try {
|
try {
|
||||||
const addDtos = dtos.filter((dto) => dto.action === ModifyAction.ADD);
|
const addDtos = dtos.filter((dto) => dto.action === ModifyAction.ADD);
|
||||||
@ -172,6 +190,7 @@ export class SpaceModelProductAllocationService {
|
|||||||
spaceModel,
|
spaceModel,
|
||||||
queryRunner,
|
queryRunner,
|
||||||
modifySubspaceModels,
|
modifySubspaceModels,
|
||||||
|
spaces,
|
||||||
),
|
),
|
||||||
this.processDeleteActions(filteredDtos, queryRunner),
|
this.processDeleteActions(filteredDtos, queryRunner),
|
||||||
]);
|
]);
|
||||||
@ -186,6 +205,7 @@ export class SpaceModelProductAllocationService {
|
|||||||
spaceModel: SpaceModelEntity,
|
spaceModel: SpaceModelEntity,
|
||||||
queryRunner: QueryRunner,
|
queryRunner: QueryRunner,
|
||||||
modifySubspaceModels?: ModifySubspaceModelDto[],
|
modifySubspaceModels?: ModifySubspaceModelDto[],
|
||||||
|
spaces?: SpaceEntity[],
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const addDtos: ProcessTagDto[] = dtos
|
const addDtos: ProcessTagDto[] = dtos
|
||||||
.filter((dto) => dto.action === ModifyAction.ADD)
|
.filter((dto) => dto.action === ModifyAction.ADD)
|
||||||
@ -202,6 +222,7 @@ export class SpaceModelProductAllocationService {
|
|||||||
addDtos,
|
addDtos,
|
||||||
queryRunner,
|
queryRunner,
|
||||||
modifySubspaceModels,
|
modifySubspaceModels,
|
||||||
|
spaces,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -198,6 +198,7 @@ export class SpaceModelService {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
await queryRunner.startTransaction();
|
await queryRunner.startTransaction();
|
||||||
|
const spaces = await this.fetchModelSpaces(spaceModel);
|
||||||
|
|
||||||
const { modelName } = dto;
|
const { modelName } = dto;
|
||||||
if (modelName) {
|
if (modelName) {
|
||||||
@ -230,6 +231,7 @@ export class SpaceModelService {
|
|||||||
spaceModel,
|
spaceModel,
|
||||||
queryRunner,
|
queryRunner,
|
||||||
dto.subspaceModels,
|
dto.subspaceModels,
|
||||||
|
spaces,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
await queryRunner.commitTransaction();
|
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(
|
async linkSpaceModel(
|
||||||
params: SpaceModelParam,
|
params: SpaceModelParam,
|
||||||
dto: LinkSpacesToModelDto,
|
dto: LinkSpacesToModelDto,
|
||||||
|
@ -12,6 +12,7 @@ import { ModifyTagDto } from '../dtos/tag/modify-tag.dto';
|
|||||||
import { ModifySubspaceDto } from '../dtos';
|
import { ModifySubspaceDto } from '../dtos';
|
||||||
import { ProcessTagDto } from 'src/tags/dtos';
|
import { ProcessTagDto } from 'src/tags/dtos';
|
||||||
import { TagService as NewTagService } from 'src/tags/services';
|
import { TagService as NewTagService } from 'src/tags/services';
|
||||||
|
import { SpaceModelProductAllocationEntity } from '@app/common/modules/space-model';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class SpaceProductAllocationService {
|
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(
|
async updateSpaceProductAllocations(
|
||||||
dtos: ModifyTagDto[],
|
dtos: ModifyTagDto[],
|
||||||
projectUuid: string,
|
projectUuid: string,
|
||||||
@ -285,7 +351,7 @@ export class SpaceProductAllocationService {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private createNewAllocation(
|
createNewAllocation(
|
||||||
space: SpaceEntity,
|
space: SpaceEntity,
|
||||||
tag: NewTagEntity,
|
tag: NewTagEntity,
|
||||||
queryRunner?: QueryRunner,
|
queryRunner?: QueryRunner,
|
||||||
@ -312,7 +378,7 @@ export class SpaceProductAllocationService {
|
|||||||
: await this.spaceProductAllocationRepository.save(allocation);
|
: await this.spaceProductAllocationRepository.save(allocation);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async saveAllocations(
|
async saveAllocations(
|
||||||
allocations: SpaceProductAllocationEntity[],
|
allocations: SpaceProductAllocationEntity[],
|
||||||
queryRunner?: QueryRunner,
|
queryRunner?: QueryRunner,
|
||||||
) {
|
) {
|
||||||
|
Reference in New Issue
Block a user