diff --git a/src/common/utils/business-cascade.util.ts b/src/common/utils/business-cascade.util.ts index 0d8a9ba..4a42576 100644 --- a/src/common/utils/business-cascade.util.ts +++ b/src/common/utils/business-cascade.util.ts @@ -4,26 +4,24 @@ import { AttributeDefinitionEntity } from '../../entities/attribute-definition.e import { BookingEntity } from '../../entities/booking.entity'; import { BusinessAttributeValueEntity } from '../../entities/business-attribute-value.entity'; import { BusinessCategoryEntity } from '../../entities/business-category.entity'; +import { BusinessServiceRequestEntity } from '../../entities/business-service-request.entity'; import { BusinessServiceEntity } from '../../entities/business-service.entity'; import { BusinessImageEntity } from '../../entities/business-image.entity'; import { BusinessAvailabilitySlotEntity } from '../../entities/business-availability-slot.entity'; import { BusinessEntity } from '../../entities/business.entity'; import { ServiceTemplateEntity } from '../../entities/service-template.entity'; -/** Remove a business and all rows that block FK deletes in production DB. */ +/** Remove a business and dependent rows that block FK deletes. */ export async function deleteBusinessCascade( manager: EntityManager, businessId: string, - onBeforeDeleteBusiness?: (businessId: string) => Promise, ): Promise { await manager.delete(BookingEntity, { businessId }); + await manager.delete(BusinessServiceRequestEntity, { businessId }); await manager.delete(BusinessAvailabilitySlotEntity, { businessId }); await manager.delete(BusinessServiceEntity, { businessId }); await manager.delete(BusinessAttributeValueEntity, { businessId }); await manager.delete(BusinessImageEntity, { businessId }); - if (onBeforeDeleteBusiness) { - await onBeforeDeleteBusiness(businessId); - } const result = await manager.delete(BusinessEntity, businessId); if (!result.affected) { @@ -36,10 +34,9 @@ export async function deleteCategoryCascade( manager: EntityManager, categoryId: string, businessIds: string[], - onBeforeDeleteBusiness?: (businessId: string) => Promise, ): Promise { for (const businessId of businessIds) { - await deleteBusinessCascade(manager, businessId, onBeforeDeleteBusiness); + await deleteBusinessCascade(manager, businessId); } await manager.delete(ServiceTemplateEntity, { categoryId }); diff --git a/src/modules/business-categories/business-categories.service.ts b/src/modules/business-categories/business-categories.service.ts index 6dd6ea2..a539f5f 100644 --- a/src/modules/business-categories/business-categories.service.ts +++ b/src/modules/business-categories/business-categories.service.ts @@ -89,10 +89,16 @@ export class BusinessCategoriesService { }); const businessIds = businesses.map((b) => b.id); + const imageUrls = ( + await Promise.all( + businessIds.map((businessId) => this.images.listUrlsForBusiness(businessId)), + ) + ).flat(); + await this.dataSource.transaction(async (manager) => { - await deleteCategoryCascade(manager, id, businessIds, async (businessId) => { - await this.images.removeAllForBusiness(businessId); - }); + await deleteCategoryCascade(manager, id, businessIds); }); + + this.images.deleteFiles(imageUrls); } } diff --git a/src/modules/businesses/business-images.service.ts b/src/modules/businesses/business-images.service.ts index 120ce86..ebfc1e9 100644 --- a/src/modules/businesses/business-images.service.ts +++ b/src/modules/businesses/business-images.service.ts @@ -116,11 +116,23 @@ export class BusinessImagesService { } } - async removeAllForBusiness(businessId: string): Promise { - const images = await this.imageRepo.find({ where: { businessId } }); - for (const image of images) { - this.deleteFile(image.url); + async listUrlsForBusiness(businessId: string): Promise { + const images = await this.imageRepo.find({ + where: { businessId }, + select: ['url'], + }); + return images.map((image) => image.url); + } + + deleteFiles(urls: string[]): void { + for (const url of urls) { + this.deleteFile(url); } + } + + async removeAllForBusiness(businessId: string): Promise { + const urls = await this.listUrlsForBusiness(businessId); + this.deleteFiles(urls); await this.imageRepo.delete({ businessId }); } diff --git a/src/modules/businesses/businesses.service.ts b/src/modules/businesses/businesses.service.ts index 7e195e7..88ba82d 100644 --- a/src/modules/businesses/businesses.service.ts +++ b/src/modules/businesses/businesses.service.ts @@ -148,11 +148,13 @@ export class BusinessesService { async remove(id: string): Promise { await this.findOne(id); + const imageUrls = await this.images.listUrlsForBusiness(id); + await this.dataSource.transaction(async (manager) => { - await deleteBusinessCascade(manager, id, async (businessId) => { - await this.images.removeAllForBusiness(businessId); - }); + await deleteBusinessCascade(manager, id); }); + + this.images.deleteFiles(imageUrls); } async getAvailability(businessId: string) {