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

View File

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