diff --git a/src/space/services/space-product-allocation.service.ts b/src/space/services/space-product-allocation.service.ts index eb48b4f..469c96f 100644 --- a/src/space/services/space-product-allocation.service.ts +++ b/src/space/services/space-product-allocation.service.ts @@ -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'); diff --git a/src/space/services/space.service.ts b/src/space/services/space.service.ts index 11a94e8..be1cdaf 100644 --- a/src/space/services/space.service.ts +++ b/src/space/services/space.service.ts @@ -359,6 +359,10 @@ export class SpaceService { } async delete(params: GetSpaceParam): Promise { + 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(); } } diff --git a/src/space/services/subspace/subspace-product-allocation.service.ts b/src/space/services/subspace/subspace-product-allocation.service.ts index 8cfbd14..faaf393 100644 --- a/src/space/services/subspace/subspace-product-allocation.service.ts +++ b/src/space/services/subspace/subspace-product-allocation.service.ts @@ -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(