updated space product model allocation

This commit is contained in:
hannathkadher
2025-03-12 23:08:18 +04:00
parent 537f20f8de
commit 8cbbb3a8ba
10 changed files with 222 additions and 25 deletions
@@ -15,6 +15,7 @@ import { NewTagEntity } from '@app/common/modules/tag';
import { ProductEntity } from '@app/common/modules/product/entities';
import { SpaceRepository } from '@app/common/modules/space';
import { ProjectEntity } from '@app/common/modules/project/entities';
import { IUpdatedSpaceAllocations } from '../interfaces';
@Injectable()
export class SpaceModelProductAllocationService {
@@ -30,10 +31,12 @@ export class SpaceModelProductAllocationService {
tags: ProcessTagDto[],
queryRunner?: QueryRunner,
modifySubspaceModels?: ModifySubspaceModelDto[],
): Promise<SpaceModelProductAllocationEntity[]> {
): Promise<IUpdatedSpaceAllocations[]> {
try {
if (!tags.length) return [];
const allocationUpdates: IUpdatedSpaceAllocations[] = [];
const processedTags = await this.tagService.processTags(
tags,
projectUuid,
@@ -108,8 +111,15 @@ export class SpaceModelProductAllocationService {
if (!allocation) {
allocation = this.createNewAllocation(spaceModel, tag, queryRunner);
productAllocations.push(allocation);
allocationUpdates.push({
newAllocation: allocation,
});
} else if (!allocation.tags.some((t) => t.uuid === tag.uuid)) {
allocation.tags.push(tag);
allocationUpdates.push({
allocation: allocation,
tagsAdded: [tag],
});
await this.saveAllocation(allocation, queryRunner);
}
}
@@ -119,7 +129,7 @@ export class SpaceModelProductAllocationService {
await this.saveAllocations(productAllocations, queryRunner);
}
return productAllocations;
return allocationUpdates;
} catch (error) {
throw this.handleError(error, 'Failed to create product allocations');
}
@@ -131,7 +141,7 @@ export class SpaceModelProductAllocationService {
spaceModel: SpaceModelEntity,
queryRunner: QueryRunner,
modifySubspaceModels?: ModifySubspaceModelDto[],
): Promise<void> {
): Promise<IUpdatedSpaceAllocations[]> {
try {
const addDtos = dtos.filter((dto) => dto.action === ModifyAction.ADD);
const deleteDtos = dtos.filter(
@@ -144,11 +154,13 @@ export class SpaceModelProductAllocationService {
uuid: dto.newTagUuid,
}));
// Process added tags
const processedTags = await this.tagService.processTags(
addTagDtos,
project.uuid,
queryRunner,
);
const addTagUuidMap = new Map<string, ModifyTagModelDto>();
processedTags.forEach((tag, index) => {
addTagUuidMap.set(tag.uuid, addDtos[index]);
@@ -161,6 +173,7 @@ export class SpaceModelProductAllocationService {
[...addTagUuids].filter((uuid) => deleteTagUuids.has(uuid)),
);
// Filter out tags that are added and deleted in the same request
const filteredDtos = dtos.filter(
(dto) =>
!(
@@ -174,7 +187,8 @@ export class SpaceModelProductAllocationService {
),
);
await Promise.all([
// Process add and delete actions concurrently
const [updatedAllocations, deletedAllocations] = await Promise.all([
this.processAddActions(
filteredDtos,
project.uuid,
@@ -184,6 +198,9 @@ export class SpaceModelProductAllocationService {
),
this.processDeleteActions(filteredDtos, queryRunner, spaceModel),
]);
// Combine results and return
return [...updatedAllocations, ...deletedAllocations];
} catch (error) {
throw this.handleError(error, 'Error while updating product allocations');
}
@@ -195,7 +212,9 @@ export class SpaceModelProductAllocationService {
spaceModel: SpaceModelEntity,
queryRunner: QueryRunner,
modifySubspaceModels?: ModifySubspaceModelDto[],
): Promise<void> {
): Promise<IUpdatedSpaceAllocations[]> {
let allocationUpdates: IUpdatedSpaceAllocations[] = [];
const addDtos: ProcessTagDto[] = dtos
.filter((dto) => dto.action === ModifyAction.ADD)
.map((dto) => ({
@@ -205,7 +224,7 @@ export class SpaceModelProductAllocationService {
}));
if (addDtos.length > 0) {
await this.createProductAllocations(
allocationUpdates = await this.createProductAllocations(
projectUuid,
spaceModel,
addDtos,
@@ -213,6 +232,7 @@ export class SpaceModelProductAllocationService {
modifySubspaceModels,
);
}
return allocationUpdates;
}
private createNewAllocation(
@@ -292,11 +312,12 @@ export class SpaceModelProductAllocationService {
dtos: ModifyTagModelDto[],
queryRunner: QueryRunner,
spaceModel: SpaceModelEntity,
): Promise<SpaceModelProductAllocationEntity[]> {
): Promise<IUpdatedSpaceAllocations[]> {
try {
if (!dtos || dtos.length === 0) {
return;
}
let allocationUpdateToPropagate: IUpdatedSpaceAllocations[] = [];
const tagUuidsToDelete = dtos
.filter((dto) => dto.action === ModifyAction.DELETE && dto.tagUuid)
@@ -331,15 +352,26 @@ export class SpaceModelProductAllocationService {
(tag) => !tagUuidsToDelete.includes(tag.uuid),
);
const deletedTags = allocation.tags.filter((tag) =>
tagUuidsToDelete.includes(tag.uuid),
);
if (updatedTags.length === allocation.tags.length) {
continue;
}
if (updatedTags.length === 0) {
deletedAllocations.push(allocation);
allocationUpdateToPropagate.push({
deletedAllocation: allocation,
});
} else {
allocation.tags = updatedTags;
allocationUpdates.push(allocation);
allocationUpdateToPropagate.push({
allocation: allocation,
tagsRemoved: deletedTags,
});
}
}
@@ -372,7 +404,7 @@ export class SpaceModelProductAllocationService {
)
.execute();
return deletedAllocations;
return allocationUpdateToPropagate;
} catch (error) {
throw this.handleError(error, `Failed to delete tags in space model`);
}
@@ -28,6 +28,7 @@ import { SpaceModelProductAllocationService } from './space-model-product-alloca
import {
PropogateDeleteSpaceModelCommand,
PropogateUpdateSpaceModelCommand,
PropogateUpdateSpaceModelProductAllocationCommand,
} from '../commands';
import {
SpaceProductAllocationRepository,
@@ -47,7 +48,11 @@ 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 { ISingleSubspaceModel, ISubspaceModelUpdates } from '../interfaces';
import {
ISingleSubspaceModel,
ISubspaceModelUpdates,
IUpdatedSpaceAllocations,
} from '../interfaces';
@Injectable()
export class SpaceModelService {
@@ -200,6 +205,7 @@ export class SpaceModelService {
);
await queryRunner.connect();
let modifiedSubspaces: ISubspaceModelUpdates;
let modifiedProductAllocations: IUpdatedSpaceAllocations[];
try {
await queryRunner.startTransaction();
const spaces = await this.fetchModelSpaces(spaceModel);
@@ -230,24 +236,35 @@ export class SpaceModelService {
}
if (dto.tags) {
await this.spaceModelProductAllocationService.updateProductAllocations(
dto.tags,
project,
spaceModel,
queryRunner,
dto.subspaceModels,
);
modifiedProductAllocations =
await this.spaceModelProductAllocationService.updateProductAllocations(
dto.tags,
project,
spaceModel,
queryRunner,
dto.subspaceModels,
);
}
await queryRunner.commitTransaction();
await this.commandBus.execute(
new PropogateUpdateSpaceModelCommand({
spaceModel: spaceModel,
subspaceModels: modifiedSubspaces,
subspaceUpdates: modifiedSubspaces,
spaces: spaces,
}),
);
if (modifiedProductAllocations?.length > 0) {
await this.commandBus.execute(
new PropogateUpdateSpaceModelProductAllocationCommand({
spaceModel: spaceModel,
updatedAllocations: modifiedProductAllocations,
spaces: spaces,
}),
);
}
return new SuccessResponseDto({
message: 'SpaceModel updated successfully',
});