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.
This commit is contained in:
Jepp9350
2025-02-19 17:54:53 +01:00
parent 53b61654bf
commit 9536b67998
+28 -9
View File
@@ -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());
}
}
}