44 lines
1.8 KiB
TypeScript
44 lines
1.8 KiB
TypeScript
import { NotFoundException } from '@nestjs/common';
|
|
import { EntityManager } from 'typeorm';
|
|
import { AttributeDefinitionEntity } from '../../entities/attribute-definition.entity';
|
|
import { BookingEntity } from '../../entities/booking.entity';
|
|
import { BusinessAttributeValueEntity } from '../../entities/business-attribute-value.entity';
|
|
import { BusinessCategoryEntity } from '../../entities/business-category.entity';
|
|
import { BusinessServiceEntity } from '../../entities/business-service.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. */
|
|
export async function deleteBusinessCascade(
|
|
manager: EntityManager,
|
|
businessId: string,
|
|
): Promise<void> {
|
|
await manager.delete(BookingEntity, { businessId });
|
|
await manager.delete(BusinessServiceEntity, { businessId });
|
|
await manager.delete(BusinessAttributeValueEntity, { businessId });
|
|
|
|
const result = await manager.delete(BusinessEntity, businessId);
|
|
if (!result.affected) {
|
|
throw new NotFoundException('Business not found');
|
|
}
|
|
}
|
|
|
|
/** Remove a category after all its businesses and catalog rows are cleared. */
|
|
export async function deleteCategoryCascade(
|
|
manager: EntityManager,
|
|
categoryId: string,
|
|
businessIds: string[],
|
|
): Promise<void> {
|
|
for (const businessId of businessIds) {
|
|
await deleteBusinessCascade(manager, businessId);
|
|
}
|
|
|
|
await manager.delete(ServiceTemplateEntity, { categoryId });
|
|
await manager.delete(AttributeDefinitionEntity, { categoryId });
|
|
|
|
const result = await manager.delete(BusinessCategoryEntity, categoryId);
|
|
if (!result.affected) {
|
|
throw new NotFoundException('Business category not found');
|
|
}
|
|
}
|