From 6d216ba0d71d4d4daa4907e9bf495cd56411d1bf Mon Sep 17 00:00:00 2001 From: morteza-mortezai Date: Tue, 6 Jan 2026 09:03:04 +0330 Subject: [PATCH] delete --- .../controllers/restaurants.controller.ts | 9 ++++ .../providers/restaurants.service.ts | 46 ++++++++++++++++++- 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/src/modules/restaurants/controllers/restaurants.controller.ts b/src/modules/restaurants/controllers/restaurants.controller.ts index f2c7979..12d9fa6 100644 --- a/src/modules/restaurants/controllers/restaurants.controller.ts +++ b/src/modules/restaurants/controllers/restaurants.controller.ts @@ -122,4 +122,13 @@ export class RestaurantsController { return this.restaurantsService.upgradeSubscription(subscriptionId, upgradeSubscriptionDto); } + @UseGuards(SuperAdminAuthGuard) + @ApiBearerAuth() + @ApiOperation({ summary: 'Hard delete a restaurant and all related data' }) + @ApiParam({ name: 'id', required: true, description: 'Restaurant ID' }) + @Delete('super-admin/restaurants/:id/hard-delete') + hardDeleteRestaurant(@Param('id') id: string) { + return this.restaurantsService.hardDeleteRestaurant(id); + } + } diff --git a/src/modules/restaurants/providers/restaurants.service.ts b/src/modules/restaurants/providers/restaurants.service.ts index 66c838d..4c3c888 100644 --- a/src/modules/restaurants/providers/restaurants.service.ts +++ b/src/modules/restaurants/providers/restaurants.service.ts @@ -15,6 +15,12 @@ import { Role } from '../../roles/entities/role.entity'; import { normalizePhone } from 'src/modules/utils/phone.util'; import { NotificationPreference } from '../../notifications/entities/notification-preference.entity'; import { notificationPreferencesData } from '../../../seeders/data/notification-preferences.data'; +import { Order } from '../../orders/entities/order.entity'; +import { Coupon } from '../../coupons/entities/coupon.entity'; +import { Category } from '../../foods/entities/category.entity'; +import { Food } from '../../foods/entities/food.entity'; +import { Delivery } from '../../delivery/entities/delivery.entity'; +import { Schedule } from '../entities/schedule.entity'; @Injectable() export class RestaurantsService { @@ -136,7 +142,7 @@ export class RestaurantsService { } async update(id: string, dto: UpdateRestaurantDto): Promise { - + const restaurant = await this.restRepository.findOne({ id: id }); if (!restaurant) { @@ -179,4 +185,42 @@ export class RestaurantsService { return restaurant; } + async hardDeleteRestaurant(id: string): Promise { + return await this.em.transactional(async (em) => { + // Find the restaurant first + const restaurant = await em.findOne(Restaurant, { id }); + + if (!restaurant) { + throw new NotFoundException(RestMessage.NOT_FOUND); + } + + // Delete orders first (will cascade to order items and payments due to cascade settings) + await em.nativeDelete(Order, { restaurant: id }); + + // Delete coupons + await em.nativeDelete(Coupon, { restaurant: id }); + + // Delete foods (will cascade to reviews, favorites, and inventory due to cascade settings) + await em.nativeDelete(Food, { restaurant: id }); + + // Delete categories (foods should be deleted by cascade, but delete explicitly to be safe) + await em.nativeDelete(Category, { restaurant: id }); + + // Delete deliveries + await em.nativeDelete(Delivery, { restaurant: id }); + + // Delete schedules + await em.nativeDelete(Schedule, { restId: id }); + + // Delete notification preferences + await em.nativeDelete(NotificationPreference, { restaurant: id }); + + // Delete admin roles for this restaurant + await em.nativeDelete(AdminRole, { restaurant: id }); + + // Finally, delete the restaurant itself + await em.nativeDelete(Restaurant, { id }); + }); + } + }