← All changes
|
app/Hooks/Handlers/CleanupHandlers/CalenderCleaner.php
+23
-1
2.2.5
→
2.5.0
View file →
| @@ -2,8 +2,9 @@ | ||
| 2 | 2 | |
| 3 | 3 | namespace FluentBooking\App\Hooks\Handlers\CleanupHandlers; |
| 4 | 4 | |
| 5 | 5 | use FluentBooking\App\Models\Booking; |
| 6 | +use FluentBooking\App\Models\BookingHost; | |
| 6 | 7 | use FluentBooking\App\Models\CalendarSlot; |
| 7 | 8 | use FluentBooking\App\Models\Availability; |
| 8 | 9 | use FluentBooking\App\Models\Meta; |
| 9 | 10 | use FluentBooking\Framework\Support\Arr; |
| @@ -61,9 +62,30 @@ | ||
| 61 | 62 | Booking::query() |
| 62 | 63 | ->where('event_id', $event->id) |
| 63 | 64 | ->whereHas('hosts', function ($query) use ($calendarUserId){ |
| 64 | 65 | $query->where('user_id', $calendarUserId); |
| 65 | - })->delete(); | |
| 66 | + }) | |
| 67 | + ->withCount('hosts') | |
| 68 | + ->chunkById(100, function ($bookings) use ($calendarUserId) { | |
| 69 | + $sharedBookingIds = []; | |
| 70 | + | |
| 71 | + foreach ($bookings as $booking) { | |
| 72 | + // A collective booking still happens with its other hosts. | |
| 73 | + if ($booking->hosts_count > 1) { | |
| 74 | + $sharedBookingIds[] = $booking->id; | |
| 75 | + continue; | |
| 76 | + } | |
| 77 | + | |
| 78 | + $bookingId = $booking->id; | |
| 79 | + do_action('fluent_booking/before_delete_booking', $booking); | |
| 80 | + $booking->delete(); | |
| 81 | + do_action('fluent_booking/after_delete_booking', $bookingId); | |
| 82 | + } | |
| 83 | + | |
| 84 | + if ($sharedBookingIds) { | |
| 85 | + BookingHost::query()->where('user_id', $calendarUserId)->whereIn('booking_id', $sharedBookingIds)->delete(); | |
| 86 | + } | |
| 87 | + }); | |
| 66 | 88 | } |
| 67 | 89 | } |
| 68 | 90 | } |
| 69 | 91 | |