From 9536b679982002162f6a284a4430bb29d220911f Mon Sep 17 00:00:00 2001 From: Jepp9350 <2jepp9350@gmail.com> Date: Wed, 19 Feb 2025 17:54:53 +0100 Subject: [PATCH] Refactor unfulfilled bookings notification logic Introduce a function to aggregate unfulfilled bookings by department. Replace individual Slack notifications with a single summary per department, improving efficiency and reducing redundant calls. --- services/nginx/app/objects/bookings_o.php | 37 +++++++++++++++++------ 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/services/nginx/app/objects/bookings_o.php b/services/nginx/app/objects/bookings_o.php index 0bdd4547..87119869 100644 --- a/services/nginx/app/objects/bookings_o.php +++ b/services/nginx/app/objects/bookings_o.php @@ -269,6 +269,18 @@ class bookings_o extends db public function checkUnfulfilledBookings(): void { + $unfulfilled_bookings = []; + function addUnfulfilledBooking(int $department, array $arr): array + { + // Check if the department has a count in the unfulfilled bookings array + if (!isset($arr[$department])) { + $arr[$department] = 0; + } + // Add the unfulfilled booking to the department count + $arr[$department]++; + return $arr; + } + global $db; // Get all the unfulfilled bookings $bookings = $this->listObjectsWithPagination(1, 100000, null, ['status' => 'pending']); @@ -278,19 +290,26 @@ class bookings_o extends db echo "Booking with ID $booking[id] has been cancelled\n"; continue; } + // Check if the booking is scheduled for the future + if (strtotime($booking['date']) > time()) { + continue; + } // Check if the booking has been fulfilled $fulfilled = $this->checkBookingFulfilled($booking['id']); if (!$fulfilled) { + $unfulfilled_bookings = addUnfulfilledBooking($booking['department'], $unfulfilled_bookings); echo "Booking with ID $booking[id] has not been fulfilled\n"; - // Send a department webhook if the booking has not been fulfilled - $slack = new slack(); - try { - $slack->send_department_booking_notification($booking['department'], $slack->format_unfulfilled_booking($booking['id'], $booking['customer_number'], $booking['wash_type'], $booking['contact_email'], $booking['reference_number'], $booking['regNrTraekker'], $booking['regNrTrailer'], $booking['washCertificateEmail'], $booking['date'], $booking['department'], $booking['pickup_bool'], $booking['notes'], $booking['washCertificateStatus'], $booking['washCertificateUrl'], $booking['status'])); - } catch (\Exception $e) { - // Log the error - $logs = new logs_o(); - $logs->add('slack', 0, 3, 0, 'SEND_DEPARTMENT_BOOKING_NOTIFICATION', $e->getMessage()); - } + } + } + // Send a department webhook if the booking has not been fulfilled + foreach ( $unfulfilled_bookings as $department => $count ) { + $slack = new slack(); + try { + $slack->send_department_booking_notification($department, 'There are ' . $count . ' unfulfilled bookings'); + } catch (\Exception $e) { + // Log the error + $logs = new logs_o(); + $logs->add('slack', 0, 3, 0, 'SEND_DEPARTMENT_UNFULFILLED_BOOKINGS_NOTIFICATION', $e->getMessage()); } } }