Merge pull request #280 from SyncrowIOT/SP-1078-be-implement-api-to-assign-a-space-model-or-reassign-it-to-a-space

add space linking when create or update space
This commit is contained in:
faris Aljohari
2025-03-06 12:33:31 +03:00
committed by GitHub
3 changed files with 117 additions and 15 deletions

View File

@ -40,6 +40,10 @@ 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';
import { DeviceEntity } from '@app/common/modules/device/entities';
@Injectable()
export class SpaceModelService {
@ -387,10 +391,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 +410,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 +429,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 +446,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,
);
}
}
}),
);
@ -441,10 +470,31 @@ export class SpaceModelService {
async overwriteSpace(
space: SpaceEntity,
project: ProjectEntity,
queryRunner?: QueryRunner,
): Promise<void> {
try {
const spaceProductAllocationRepository = queryRunner
? queryRunner.manager.getRepository(SpaceProductAllocationEntity)
: this.spaceProductAllocationRepository;
const subspaceRepository = queryRunner
? queryRunner.manager.getRepository(SubspaceEntity)
: this.subspaceRepository;
const subspaceProductAllocationRepository = queryRunner
? queryRunner.manager.getRepository(SubspaceProductAllocationEntity)
: this.subspaceProductAllocationRepository;
const spaceRepository = queryRunner
? queryRunner.manager.getRepository(SpaceEntity)
: this.spaceRepository;
const deviceRepository = queryRunner
? queryRunner.manager.getRepository(DeviceEntity)
: this.deviceRepository;
if (space.productAllocations.length) {
await this.spaceProductAllocationRepository.delete({
await spaceProductAllocationRepository.delete({
uuid: In(
space.productAllocations.map((allocation) => allocation.uuid),
),
@ -453,13 +503,13 @@ export class SpaceModelService {
await Promise.all(
space.subspaces.map(async (subspace) => {
await this.subspaceRepository.update(
await subspaceRepository.update(
{ uuid: subspace.uuid },
{ disabled: true },
);
if (subspace.productAllocations.length) {
await this.subspaceProductAllocationRepository.delete({
await subspaceProductAllocationRepository.delete({
uuid: In(
subspace.productAllocations.map(
(allocation) => allocation.uuid,
@ -471,7 +521,7 @@ export class SpaceModelService {
);
if (space.devices.length > 0) {
const orphanSpace = await this.spaceRepository.findOne({
const orphanSpace = await spaceRepository.findOne({
where: {
community: {
name: `${ORPHAN_COMMUNITY_NAME}-${project.name}`,
@ -487,7 +537,7 @@ export class SpaceModelService {
);
}
await this.deviceRepository.update(
await deviceRepository.update(
{ uuid: In(space.devices.map((device) => device.uuid)) },
{ spaceDevice: orphanSpace },
);