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) {
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
.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)),
allocationUuids,
})
.execute();
@ -352,7 +358,7 @@ export class SpaceProductAllocationService {
.createQueryBuilder()
.delete()
.from(SpaceProductAllocationEntity)
.where('spaceUuid = :spaceUuid', { spaceUuid })
.where('space_uuid = :spaceUuid', { spaceUuid })
.execute();
} catch (error) {
throw this.handleError(error, 'Failed to clear all allocations');

View File

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

View File

@ -507,19 +507,25 @@ export class SubspaceProductAllocationService {
}
async clearAllAllocations(subspaceUuids: string[], queryRunner: QueryRunner) {
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
.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)),
allocationUuids,
})
.execute();
@ -527,7 +533,7 @@ export class SubspaceProductAllocationService {
.createQueryBuilder()
.delete()
.from(SubspaceProductAllocationEntity)
.where('subspaceUuid IN (:...subspaceUuids)', { subspaceUuids })
.where('subspace_uuid IN (:...subspaceUuids)', { subspaceUuids })
.execute();
} catch (error) {
throw new HttpException(