fix delete space and sub space

This commit is contained in:
faris Aljohari
2025-03-07 03:16:24 +03:00
parent 5e6c7bef9e
commit dd56300b96
3 changed files with 52 additions and 41 deletions

View File

@ -334,17 +334,23 @@ export class SpaceProductAllocationService {
} }
async clearAllAllocations(spaceUuid: string, queryRunner: QueryRunner) { async clearAllAllocations(spaceUuid: string, queryRunner: QueryRunner) {
try { try {
const allocationUuids = await queryRunner.manager
.createQueryBuilder(SpaceProductAllocationEntity, 'allocation')
.select('allocation.uuid')
.where('allocation.space_uuid = :spaceUuid', { spaceUuid })
.getRawMany()
.then((results) => results.map((r) => r.allocation_uuid));
if (allocationUuids.length === 0) {
return;
}
await queryRunner.manager await queryRunner.manager
.createQueryBuilder() .createQueryBuilder()
.delete() .delete()
.from('space_product_tags') .from('space_product_tags')
.where('space_product_allocation_uuid IN (:...allocationUuids)', { .where('space_product_allocation_uuid IN (:...allocationUuids)', {
allocationUuids: await queryRunner.manager allocationUuids,
.createQueryBuilder(SpaceProductAllocationEntity, 'allocation')
.select('allocation.uuid')
.where('allocation.spaceUuid = :spaceUuid', { spaceUuid })
.getRawMany()
.then((results) => results.map((r) => r.allocation_uuid)),
}) })
.execute(); .execute();
@ -352,7 +358,7 @@ export class SpaceProductAllocationService {
.createQueryBuilder() .createQueryBuilder()
.delete() .delete()
.from(SpaceProductAllocationEntity) .from(SpaceProductAllocationEntity)
.where('spaceUuid = :spaceUuid', { spaceUuid }) .where('space_uuid = :spaceUuid', { spaceUuid })
.execute(); .execute();
} catch (error) { } catch (error) {
throw this.handleError(error, 'Failed to clear all allocations'); throw this.handleError(error, 'Failed to clear all allocations');

View File

@ -359,6 +359,10 @@ export class SpaceService {
} }
async delete(params: GetSpaceParam): Promise<BaseResponseDto> { async delete(params: GetSpaceParam): Promise<BaseResponseDto> {
const queryRunner = this.dataSource.createQueryRunner();
await queryRunner.connect();
await queryRunner.startTransaction();
try { try {
const { communityUuid, spaceUuid, projectUuid } = params; const { communityUuid, spaceUuid, projectUuid } = params;
@ -385,48 +389,43 @@ export class SpaceService {
spaceName: ORPHAN_SPACE_NAME, spaceName: ORPHAN_SPACE_NAME,
}, },
}); });
const queryRunner = this.dataSource.createQueryRunner();
await queryRunner.connect();
await queryRunner.startTransaction();
try { await this.spaceProductAllocationService.clearAllAllocations(
await this.spaceProductAllocationService.clearAllAllocations( spaceUuid,
spaceUuid, queryRunner,
queryRunner, );
);
const subspaces = await queryRunner.manager.find(SubspaceEntity, { const subspaces = await queryRunner.manager.find(SubspaceEntity, {
where: { space: { uuid: spaceUuid } }, where: { space: { uuid: spaceUuid } },
}); });
const subspaceUuids = subspaces.map((subspace) => subspace.uuid); const subspaceUuids = subspaces.map((subspace) => subspace.uuid);
if (subspaceUuids.length > 0) { if (subspaceUuids.length > 0) {
await this.subSpaceService.clearSubspaces(subspaceUuids, queryRunner); 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();
} }
await this.disableSpace(space, orphanSpace);
await queryRunner.commitTransaction();
return new SuccessResponseDto({ return new SuccessResponseDto({
message: `Space with ID ${spaceUuid} successfully deleted`, message: `Space with ID ${spaceUuid} successfully deleted`,
statusCode: HttpStatus.OK, statusCode: HttpStatus.OK,
}); });
} catch (error) { } catch (error) {
await queryRunner.rollbackTransaction();
console.error('Error deleting space:', error);
if (error instanceof HttpException) { if (error instanceof HttpException) {
throw error; throw error;
} }
throw new HttpException( throw new HttpException(
`An error occurred while deleting the space ${error.message}`, `An error occurred while deleting the space: ${error.message}`,
HttpStatus.INTERNAL_SERVER_ERROR, HttpStatus.INTERNAL_SERVER_ERROR,
); );
} finally {
await queryRunner.release();
} }
} }

View File

@ -507,19 +507,25 @@ export class SubspaceProductAllocationService {
} }
async clearAllAllocations(subspaceUuids: string[], queryRunner: QueryRunner) { async clearAllAllocations(subspaceUuids: string[], queryRunner: QueryRunner) {
try { try {
const allocationUuids = await queryRunner.manager
.createQueryBuilder(SubspaceProductAllocationEntity, 'allocation')
.select('allocation.uuid')
.where('allocation.subspace_uuid IN (:...subspaceUuids)', {
subspaceUuids,
})
.getRawMany()
.then((results) => results.map((r) => r.allocation_uuid));
if (allocationUuids.length === 0) {
return;
}
await queryRunner.manager await queryRunner.manager
.createQueryBuilder() .createQueryBuilder()
.delete() .delete()
.from('subspace_product_tags') .from('subspace_product_tags')
.where('subspace_product_allocation_uuid IN (:...allocationUuids)', { .where('subspace_product_allocation_uuid IN (:...allocationUuids)', {
allocationUuids: await queryRunner.manager allocationUuids,
.createQueryBuilder(SubspaceProductAllocationEntity, 'allocation')
.select('allocation.uuid')
.where('allocation.subspaceUuid IN (:...subspaceUuids)', {
subspaceUuids,
})
.getRawMany()
.then((results) => results.map((r) => r.allocation_uuid)),
}) })
.execute(); .execute();
@ -527,7 +533,7 @@ export class SubspaceProductAllocationService {
.createQueryBuilder() .createQueryBuilder()
.delete() .delete()
.from(SubspaceProductAllocationEntity) .from(SubspaceProductAllocationEntity)
.where('subspaceUuid IN (:...subspaceUuids)', { subspaceUuids }) .where('subspace_uuid IN (:...subspaceUuids)', { subspaceUuids })
.execute(); .execute();
} catch (error) { } catch (error) {
throw new HttpException( throw new HttpException(