← All changes
|
app/Hooks/Handlers/CleanupHandlers/CalenderCleaner.php
+28
-4
1.5.21
→
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; |
| @@ -29,11 +30,12 @@ | ||
| 29 | 30 | ->where('calendar_id', $calendar->id)->get(); |
| 30 | 31 | |
| 31 | 32 | if ($calendarEvents->count()) { |
| 32 | 33 | foreach ($calendarEvents as $event) { |
| 34 | + $eventId = $event->id; | |
| 33 | 35 | do_action('fluent_booking/before_delete_calendar_event', $event, $calendar); |
| 34 | 36 | $event->delete(); |
| 35 | - do_action('fluent_booking/after_delete_calendar_event', $event, $calendar); | |
| 37 | + do_action('fluent_booking/after_delete_calendar_event', $eventId, $calendar); | |
| 36 | 38 | } |
| 37 | 39 | } |
| 38 | 40 | |
| 39 | 41 | $teamEvents = CalendarSlot::query() |
| @@ -60,9 +62,30 @@ | ||
| 60 | 62 | Booking::query() |
| 61 | 63 | ->where('event_id', $event->id) |
| 62 | 64 | ->whereHas('hosts', function ($query) use ($calendarUserId){ |
| 63 | 65 | $query->where('user_id', $calendarUserId); |
| 64 | - })->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 | + }); | |
| 65 | 88 | } |
| 66 | 89 | } |
| 67 | 90 | } |
| 68 | 91 | |
| @@ -77,13 +100,14 @@ | ||
| 77 | 100 | $eventAssociated = CalendarSlot::where('availability_id', $schedule->id) |
| 78 | 101 | ->where('user_id', '!=', $schedule->object_id) |
| 79 | 102 | ->count(); |
| 80 | 103 | |
| 104 | + $scheduleId = $schedule->id; | |
| 81 | 105 | if ($eventAssociated == 0) { |
| 82 | - do_action('fluent_booking/before_delete_availability_schedule', $schedule, $calendar); | |
| 106 | + do_action('fluent_booking/before_delete_availability_schedule', $schedule); | |
| 83 | 107 | $schedule->delete(); |
| 84 | - do_action('fluent_booking/after_delete_availability_schedule', $schedule, $calendar); | |
| 108 | + do_action('fluent_booking/after_delete_availability_schedule', $scheduleId); | |
| 85 | 109 | } |
| 86 | 110 | } |
| 87 | 111 | } |
| 88 | 112 | } |
| 89 | 113 | } |