add overwrite when update spaces with spacesModel

This commit is contained in:
faris Aljohari
2025-03-06 11:13:14 +03:00
parent 6864e000b8
commit 61692c70d1
2 changed files with 39 additions and 8 deletions

View File

@ -43,6 +43,7 @@ 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 {
@ -469,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),
),
@ -481,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,
@ -499,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}`,
@ -515,7 +537,7 @@ export class SpaceModelService {
);
}
await this.deviceRepository.update(
await deviceRepository.update(
{ uuid: In(space.devices.map((device) => device.uuid)) },
{ spaceDevice: orphanSpace },
);

View File

@ -449,6 +449,9 @@ export class SpaceService {
try {
await queryRunner.connect();
await queryRunner.startTransaction();
const project = await this.spaceModelService.validateProject(
params.projectUuid,
);
const space =
await this.validationService.validateSpaceWithinCommunityAndProject(
@ -483,9 +486,15 @@ export class SpaceService {
queryRunner,
);
} else if (hasDependencies) {
throw new HttpException(
`Space cannot be linked to a model because it has existing dependencies (devices, subspaces, or product allocations).`,
HttpStatus.BAD_REQUEST,
await this.spaceModelService.overwriteSpace(
space,
project,
queryRunner,
);
await this.spaceModelService.linkToSpace(
space,
spaceModel,
queryRunner,
);
}
}