fix propagation

This commit is contained in:
hannathkadher
2025-03-09 13:56:06 +04:00
parent 1220ee395d
commit 36612a40b9
5 changed files with 104 additions and 31 deletions

View File

@ -0,0 +1,32 @@
import { SubspaceModelProductAllocationEntity } from '@app/common/modules/space-model';
import { NewTagEntity } from '@app/common/modules/tag';
export type IUpdatedAllocations = {
allocation?: SubspaceModelProductAllocationEntity;
tagsRemoved?: NewTagEntity[];
tagsAdded?: NewTagEntity[];
newAllocation?: SubspaceModelProductAllocationEntity;
deletedAllocation?: SubspaceModelProductAllocationEntity;
};
export interface ISubspaceProductAllocationUpdateResult {
createdAllocations: ICreatedAllocation[];
updatedAllocations: IUpdatedAllocation[];
deletedAllocations: IDeletedAllocation[];
}
export interface ICreatedAllocation {
allocation: SubspaceModelProductAllocationEntity;
tags: NewTagEntity[];
}
export interface IUpdatedAllocation {
allocation: SubspaceModelProductAllocationEntity;
tagsAdded: NewTagEntity[];
tagsRemoved: NewTagEntity[];
}
export interface IDeletedAllocation {
allocation: SubspaceModelProductAllocationEntity;
tagsRemoved: NewTagEntity[];
}

View File

@ -44,6 +44,8 @@ import { SpaceProductAllocationEntity } from '@app/common/modules/space/entities
import { SubspaceEntity } from '@app/common/modules/space/entities/subspace/subspace.entity';
import { SubspaceProductAllocationEntity } from '@app/common/modules/space/entities/subspace/subspace-product-allocation.entity';
import { DeviceEntity } from '@app/common/modules/device/entities';
import { ISingleSubspace } from 'src/space/interfaces/single-subspace.interface';
import { ISingleSubspaceModel } from '../interfaces';
@Injectable()
export class SpaceModelService {
@ -195,7 +197,7 @@ export class SpaceModelService {
param.projectUuid,
);
await queryRunner.connect();
let modifiedSubspaces: ISingleSubspaceModel[] = [];
try {
await queryRunner.startTransaction();
const spaces = await this.fetchModelSpaces(spaceModel);
@ -215,13 +217,14 @@ export class SpaceModelService {
}
if (dto.subspaceModels) {
await this.subSpaceModelService.modifySubspaceModels(
dto.subspaceModels,
spaceModel,
queryRunner,
param.projectUuid,
dto.tags,
);
modifiedSubspaces =
await this.subSpaceModelService.modifySubspaceModels(
dto.subspaceModels,
spaceModel,
queryRunner,
param.projectUuid,
dto.tags,
);
}
if (dto.tags) {

View File

@ -11,6 +11,7 @@ import { NewTagEntity } from '@app/common/modules/tag';
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { ModifyTagModelDto } from 'src/space-model/dtos';
import { ISingleSubspaceModel } from 'src/space-model/interfaces';
import { IUpdatedAllocations } from 'src/space-model/interfaces/subspace-product-allocation-update-result.interface';
import { ProcessTagDto } from 'src/tags/dtos';
import { TagService as NewTagService } from 'src/tags/services';
import { In, QueryRunner } from 'typeorm';
@ -29,8 +30,10 @@ export class SubspaceModelProductAllocationService {
tags: NewTagEntity[],
queryRunner?: QueryRunner,
spaceAllocationsToExclude?: SpaceModelProductAllocationEntity[],
): Promise<SubspaceModelProductAllocationEntity[]> {
): Promise<IUpdatedAllocations[]> {
try {
const updatedAllocations: IUpdatedAllocations[] = [];
const allocations: SubspaceModelProductAllocationEntity[] = [];
for (const tag of tags) {
@ -140,7 +143,6 @@ export class SubspaceModelProductAllocationService {
product: tag.product,
tags: [tag],
});
allocations.push(allocation);
} else {
//If allocation exists, add the tag to it
@ -155,6 +157,10 @@ export class SubspaceModelProductAllocationService {
existingAllocationsForProduct[0],
);
}
updatedAllocations.push({
allocation: existingAllocationsForProduct[0],
tagsAdded: [tag],
});
}
}
@ -168,9 +174,13 @@ export class SubspaceModelProductAllocationService {
} else {
await this.subspaceModelProductAllocationRepository.save(allocations);
}
allocations.forEach((allocation) => {
updatedAllocations.push({ newAllocation: allocation });
});
}
return allocations;
return updatedAllocations;
} catch (error) {
throw new HttpException(
`An unexpected error occurred while creating subspace product allocations ${error}`,
@ -267,6 +277,7 @@ export class SubspaceModelProductAllocationService {
spaceTagUpdateDtos?: ModifyTagModelDto[],
) {
const spaceAllocationToExclude: SpaceModelProductAllocationEntity[] = [];
const updatedAllocations: IUpdatedAllocations[] = [];
for (const subspaceModel of subspaceModels) {
const tagDtos = subspaceModel.tags;
@ -332,6 +343,11 @@ export class SubspaceModelProductAllocationService {
(tag) => tag.uuid !== deletedTag.tagUuid,
);
updatedAllocations.push({
allocation,
tagsRemoved: [tagEntity],
});
await queryRunner.manager.save(allocation);
const productAllocationExistInSubspace =
@ -350,6 +366,12 @@ export class SubspaceModelProductAllocationService {
if (productAllocationExistInSubspace) {
productAllocationExistInSubspace.tags.push(tagEntity);
updatedAllocations.push({
allocation: productAllocationExistInSubspace,
tagsAdded: [tagEntity],
});
await queryRunner.manager.save(
productAllocationExistInSubspace,
);
@ -363,6 +385,10 @@ export class SubspaceModelProductAllocationService {
},
);
updatedAllocations.push({
allocation: newProductAllocation,
});
await queryRunner.manager.save(newProductAllocation);
}
@ -406,6 +432,11 @@ export class SubspaceModelProductAllocationService {
),
);
updatedAllocations.push({
allocation: allocation,
tagsRemoved: repeatedTags,
});
await queryRunner.manager.save(allocation);
const productAllocationExistInSubspace =
@ -421,6 +452,11 @@ export class SubspaceModelProductAllocationService {
);
if (productAllocationExistInSubspace) {
updatedAllocations.push({
allocation: productAllocationExistInSubspace,
tagsAdded: repeatedTags,
});
productAllocationExistInSubspace.tags.push(...repeatedTags);
await queryRunner.manager.save(
productAllocationExistInSubspace,
@ -435,19 +471,12 @@ export class SubspaceModelProductAllocationService {
},
);
updatedAllocations.push({
newAllocation: newProductAllocation,
});
await queryRunner.manager.save(newProductAllocation);
}
const newAllocation = queryRunner.manager.create(
SubspaceModelProductAllocationEntity,
{
subspaceModel: subspaceModel.subspaceModel,
product: allocation.product,
tags: repeatedTags,
},
);
await queryRunner.manager.save(newAllocation);
}
}
}
@ -479,13 +508,14 @@ export class SubspaceModelProductAllocationService {
}
// Create new product allocations
await this.createProductAllocations(
const newAllocations = await this.createProductAllocations(
subspaceModel.subspaceModel,
spaceModel,
processedTags,
queryRunner,
spaceAllocationToExclude,
);
return [...updatedAllocations, ...newAllocations];
}
if (tagsToDeleteDto.length > 0) {
await this.processDeleteActions(tagsToDeleteDto, queryRunner);

View File

@ -115,7 +115,7 @@ export class SubSpaceModelService {
projectUuid: string,
spaceTagUpdateDtos?: ModifyTagModelDto[],
spaces?: SpaceEntity[],
) {
): Promise<ISingleSubspaceModel[]> {
try {
if (!dtos || dtos.length === 0) {
return;
@ -129,18 +129,18 @@ export class SubSpaceModelService {
(dto) => dto.action === ModifyAction.DELETE,
);
const updatedModels = await this.updateSubspaceModel(
const updatedSubspaces = await this.updateSubspaceModel(
combinedDtos,
spaceModel,
queryRunner,
);
const addedModels = await this.handleAddAction(
const createdSubspaces = await this.handleAddAction(
addDtos,
spaceModel,
queryRunner,
spaces,
);
const combineModels = [...addedModels, ...updatedModels];
const combineModels = [...createdSubspaces, ...updatedSubspaces];
await this.productAllocationService.updateAllocations(
combineModels,
@ -150,7 +150,12 @@ export class SubSpaceModelService {
spaceTagUpdateDtos,
);
await this.deleteSubspaceModels(deleteDtos, queryRunner, spaceModel);
const deletedSubspaces = await this.deleteSubspaceModels(
deleteDtos,
queryRunner,
spaceModel,
);
return [...createdSubspaces, ...updatedSubspaces, ...deletedSubspaces];
} catch (error) {
console.error('Error in modifySubspaceModels:', error);
throw new HttpException(
@ -168,7 +173,7 @@ export class SubSpaceModelService {
deleteDtos: ModifySubspaceModelDto[],
queryRunner: QueryRunner,
spaceModel: SpaceModelEntity,
) {
): Promise<ISingleSubspaceModel[]> {
try {
if (!deleteDtos || deleteDtos.length === 0) {
return;
@ -275,7 +280,11 @@ export class SubSpaceModelService {
deleteResults.push(...subspaceUuids.map((uuid) => ({ uuid })));
return deleteResults;
return subspaces.map((subspace) => ({
subspaceModel: subspace,
action: ModifyAction.DELETE,
tags: [],
}));
} catch (error) {
if (error instanceof QueryFailedError) {
throw new HttpException(

View File

@ -38,7 +38,6 @@ import { ProcessTagDto } from 'src/tags/dtos';
import { SpaceProductAllocationService } from './space-product-allocation.service';
import { SubspaceProductAllocationService } from './subspace/subspace-product-allocation.service';
import { SubspaceEntity } from '@app/common/modules/space/entities/subspace/subspace.entity';
import { ProjectEntity } from '@app/common/modules/project/entities';
import { DeviceRepository } from '@app/common/modules/device/repositories';
import { DeviceService } from 'src/device/services';
@Injectable()