Merge pull request #277 from SyncrowIOT/SP-1228-be-update-the-delete-space-endpoint-with-the-changes-in-tags

Sp 1228 be update the delete space endpoint with the changes in tags
This commit is contained in:
hannathkadher
2025-03-04 12:50:07 +04:00
committed by GitHub
4 changed files with 113 additions and 1 deletions

View File

@ -331,4 +331,30 @@ export class SpaceProductAllocationService {
: HttpStatus.INTERNAL_SERVER_ERROR,
);
}
async clearAllAllocations(spaceUuid: string, queryRunner: QueryRunner) {
try {
await queryRunner.manager
.createQueryBuilder()
.delete()
.from('space_product_tags')
.where('space_product_allocation_uuid IN (:...allocationUuids)', {
allocationUuids: await queryRunner.manager
.createQueryBuilder(SpaceProductAllocationEntity, 'allocation')
.select('allocation.uuid')
.where('allocation.spaceUuid = :spaceUuid', { spaceUuid })
.getRawMany()
.then((results) => results.map((r) => r.allocation_uuid)),
})
.execute();
await queryRunner.manager
.createQueryBuilder()
.delete()
.from(SpaceProductAllocationEntity)
.where('spaceUuid = :spaceUuid', { spaceUuid })
.execute();
} catch (error) {
throw this.handleError(error, 'Failed to clear all allocations');
}
}
}

View File

@ -37,6 +37,7 @@ import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
import { ProcessTagDto } from 'src/tags/dtos';
import { SpaceProductAllocationService } from './space-product-allocation.service';
import { SubspaceProductAllocationService } from './subspace/subspace-product-allocation.service';
import { SubspaceEntity } from '@app/common/modules/space/entities/subspace/subspace.entity';
@Injectable()
export class SpaceService {
constructor(
@ -374,8 +375,35 @@ export class SpaceService {
spaceName: ORPHAN_SPACE_NAME,
},
});
const queryRunner = this.dataSource.createQueryRunner();
await queryRunner.connect();
await queryRunner.startTransaction();
await this.disableSpace(space, orphanSpace);
try {
await this.spaceProductAllocationService.clearAllAllocations(
spaceUuid,
queryRunner,
);
const subspaces = await queryRunner.manager.find(SubspaceEntity, {
where: { space: { uuid: spaceUuid } },
});
const subspaceUuids = subspaces.map((subspace) => subspace.uuid);
if (subspaceUuids.length > 0) {
await this.subSpaceService.clearSubspaces(subspaceUuids, queryRunner);
}
await this.disableSpace(space, orphanSpace);
await queryRunner.commitTransaction();
} catch (error) {
await queryRunner.rollbackTransaction();
throw error;
} finally {
await queryRunner.release();
}
return new SuccessResponseDto({
message: `Space with ID ${spaceUuid} successfully deleted`,

View File

@ -505,4 +505,39 @@ export class SubspaceProductAllocationService {
: HttpStatus.INTERNAL_SERVER_ERROR,
);
}
async clearAllAllocations(subspaceUuids: string[], queryRunner: QueryRunner) {
try {
await queryRunner.manager
.createQueryBuilder()
.delete()
.from('subspace_product_tags')
.where('subspace_product_allocation_uuid IN (:...allocationUuids)', {
allocationUuids: await queryRunner.manager
.createQueryBuilder(SubspaceProductAllocationEntity, 'allocation')
.select('allocation.uuid')
.where('allocation.subspaceUuid IN (:...subspaceUuids)', {
subspaceUuids,
})
.getRawMany()
.then((results) => results.map((r) => r.allocation_uuid)),
})
.execute();
await queryRunner.manager
.createQueryBuilder()
.delete()
.from(SubspaceProductAllocationEntity)
.where('subspaceUuid IN (:...subspaceUuids)', { subspaceUuids })
.execute();
} catch (error) {
throw new HttpException(
error instanceof HttpException
? error.message
: 'An unexpected error occurred while clearing allocations',
error instanceof HttpException
? error.getStatus()
: HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
}

View File

@ -574,4 +574,27 @@ export class SubSpaceService {
extractTagsFromSubspace(addSubspaceDto: AddSubspaceDto[]): ProcessTagDto[] {
return addSubspaceDto.flatMap((subspace) => subspace.tags || []);
}
async clearSubspaces(subspaceUuids: string[], queryRunner: QueryRunner) {
try {
await queryRunner.manager.update(
SubspaceEntity,
{ uuid: In(subspaceUuids) },
{ disabled: true },
);
await this.subspaceProductAllocationService.clearAllAllocations(
subspaceUuids,
queryRunner,
);
} catch (error) {
throw new HttpException(
error instanceof HttpException
? error.message
: 'An unexpected error occurred while clearing subspaces',
error instanceof HttpException
? error.getStatus()
: HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
}