Add endpoint for unfulfilled department bookings count

Introduced a new method to fetch unfulfilled bookings count by department in `bookings_o`. Added a corresponding route in `bookingsRoute` to expose this functionality via an API endpoint. Validations and logging were also implemented to ensure proper usage and tracking.
This commit is contained in:
Jepp9350
2025-01-10 13:01:50 +01:00
parent 6bcf310bf3
commit d7174ce29e
3 changed files with 52 additions and 0 deletions
+9
View File
@@ -125,4 +125,13 @@ class bookings_o extends db
$response->paginate($page, $limit, $total);
return $array;
}
public function getDepartmentBookingsUnfulfilledCount(int $department_id): int
{
global $db;
$sql = "SELECT COUNT(*) as count FROM $this->table WHERE department = $department_id AND status = 'pending'";
$result = $db->query($sql);
$row = $db->fetch_assoc($result);
return $row['count'];
}
}
+7
View File
@@ -126,4 +126,11 @@ class departments_o extends db
$result = $db->query($sql);
return $db->fetch_all($result);
}
public function selectId(int $department_id): self
{
$this->id = $department_id;
$this->getObjectProperties();
return $this;
}
}
+36
View File
@@ -5,6 +5,7 @@ namespace routes;
use classes\authentication;
use classes\response;
use objects\bookings_o;
use objects\departments_o;
use objects\logs_o;
use traits\route_t;
@@ -112,5 +113,40 @@ class bookingsRoute
['message' => 'Successfully synced booking']
);
});
// Get a departments unfulfilled bookings (count) for the day
$this->get('/admin/bookings/department/count', function () {
// Require the user to be logged in
global $response;
$this->requirePermission('list_department_bookings_count');
// Get the user object
$user = (new authentication())->get_user();
// Check if the request was successful
if ($user) {
// Check if the department_id is set
if ($this->fromRequest('department_id') === null) {
$response->error('Department ID is required', 400);
}
// Check if the department id is a valid number
if (!is_numeric($this->fromRequest('department_id'))) {
$response->error('Department ID must be a number', 400);
}
// Check if the department exists
if (!(new departments_o())->selectId((int)$this->fromRequest('department_id'))->exists()) {
$response->error('Department not found', 404);
}
// Log the incident
(new logs_o())->add('bookings', 'global', 1, $user->id, 'LIST_DEPARTMENT_BOOKINGS_COUNT', 'Successfully listed department bookings');
// Return the list of departments
$response->success(
(new bookings_o())->getDepartmentBookingsUnfulfilledCount((int)$this->fromRequest('department_id'))
);
} else {
// Log the incident
(new logs_o())->add('bookings', 'global', 1, 0, 'LIST_DEPARTMENT_BOOKINGS_COUNT', 'No user found, or invalid session');
// Return an error
$response->error('Invalid session', 400);
}
});
}
}