add space linking when create or update space

This commit is contained in:
faris Aljohari
2025-03-06 02:33:48 +03:00
parent 0d4afc40f2
commit 6864e000b8
3 changed files with 81 additions and 10 deletions

View File

@ -40,6 +40,9 @@ import {
ORPHAN_SPACE_NAME,
} from '@app/common/constants/orphan-constant';
import { DeviceRepository } from '@app/common/modules/device/repositories';
import { SpaceProductAllocationEntity } from '@app/common/modules/space/entities/space-product-allocation.entity';
import { SubspaceEntity } from '@app/common/modules/space/entities/subspace/subspace.entity';
import { SubspaceProductAllocationEntity } from '@app/common/modules/space/entities/subspace/subspace-product-allocation.entity';
@Injectable()
export class SpaceModelService {
@ -387,10 +390,15 @@ export class SpaceModelService {
async linkToSpace(
space: SpaceEntity,
spaceModel: SpaceModelEntity,
queryRunner?: QueryRunner, // Make queryRunner optional
): Promise<void> {
try {
space.spaceModel = spaceModel;
await this.spaceRepository.save(space);
if (queryRunner) {
await queryRunner.manager.save(SpaceEntity, space);
} else {
await this.spaceRepository.save(space);
}
const spaceProductAllocations = spaceModel.productAllocations.map(
(modelAllocation) =>
@ -401,8 +409,17 @@ export class SpaceModelService {
tags: modelAllocation.tags,
}),
);
await this.spaceProductAllocationRepository.save(spaceProductAllocations);
if (queryRunner) {
await queryRunner.manager.save(
SpaceProductAllocationEntity,
spaceProductAllocations,
);
} else {
await this.spaceProductAllocationRepository.save(
spaceProductAllocations,
);
}
await Promise.all(
spaceModel.subspaceModels.map(async (subspaceModel) => {
const subspace = this.subspaceRepository.create({
@ -411,7 +428,11 @@ export class SpaceModelService {
space: space,
});
await this.subspaceRepository.save(subspace);
if (queryRunner) {
await queryRunner.manager.save(SubspaceEntity, subspace);
} else {
await this.subspaceRepository.save(subspace);
}
const subspaceAllocations = subspaceModel.productAllocations.map(
(modelAllocation) =>
@ -424,9 +445,16 @@ export class SpaceModelService {
);
if (subspaceAllocations.length) {
await this.subspaceProductAllocationRepository.save(
subspaceAllocations,
);
if (queryRunner) {
await queryRunner.manager.save(
SubspaceProductAllocationEntity,
subspaceAllocations,
);
} else {
await this.subspaceProductAllocationRepository.save(
subspaceAllocations,
);
}
}
}),
);