This commit is contained in:
2026-01-06 09:03:04 +03:30
parent 69921e1bf9
commit 6d216ba0d7
2 changed files with 54 additions and 1 deletions
@@ -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);
}
}
@@ -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<Restaurant> {
const restaurant = await this.restRepository.findOne({ id: id });
if (!restaurant) {
@@ -179,4 +185,42 @@ export class RestaurantsService {
return restaurant;
}
async hardDeleteRestaurant(id: string): Promise<void> {
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 });
});
}
}