Add customer time bookings routes for public access to opening hours, types, and entries
This commit is contained in:
@@ -197,11 +197,11 @@ class department_time_bookings_entries_o extends db
|
||||
: self::generateUpdateBookingMessage(true)),
|
||||
true,
|
||||
);
|
||||
print_r($department->notificationSmsPhoneNumbers());
|
||||
//print_r($department->notificationSmsPhoneNumbers());
|
||||
// Get the phone numbers of the customer
|
||||
$customer_phone_numbers = [];
|
||||
$customer_phone_numbers[] = '' . $this->phone_country_code->value() . $this->phone->value();
|
||||
print_r($customer_phone_numbers);
|
||||
//print_r($customer_phone_numbers);
|
||||
// Send the message to the customer
|
||||
$gateway->send(
|
||||
[...$customer_phone_numbers],
|
||||
@@ -255,7 +255,7 @@ class department_time_bookings_entries_o extends db
|
||||
self::requireSelected();
|
||||
$message .= self::addNewline('Afdeling: ' . $department->name->value());
|
||||
$message .= self::addNewline('Tidspunkt: ' . (string)$this->start->value());
|
||||
$message .= self::addNewline('Type: ' . (string)$type->name->value());
|
||||
$message .= self::addNewline('Type: ' . (string)$type->getName());
|
||||
$registration_number = $this->reg->value();
|
||||
if (!empty($registration_number)) {
|
||||
$message .= self::addNewline('Registreringsnummer: ' . $registration_number);
|
||||
|
||||
@@ -86,4 +86,45 @@ class department_time_bookings_types_o extends db
|
||||
{
|
||||
//TODO: Add cache invalidation
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the duration of a booking type by its ID
|
||||
* @param int $id The ID of the booking type
|
||||
* @return int The duration of the booking type in minutes
|
||||
* @throws Exception If the object could not be selected
|
||||
*/
|
||||
public function getDurationById(int $id): int
|
||||
{
|
||||
self::select((int)$id);
|
||||
return (int)$this->duration->value();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getName(): string
|
||||
{
|
||||
self::requireSelected();
|
||||
// Check if the type has a name set
|
||||
$type_name = $this->name->value();
|
||||
if (!empty($type_name)) {
|
||||
return $type_name;
|
||||
}
|
||||
// If the type does not have a name set, return the product name
|
||||
return self::getProduct()->name->value();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getProduct(): \objects\products_o
|
||||
{
|
||||
self::requireSelected();
|
||||
$product = new \objects\products_o();
|
||||
$product->select((int)$this->product->value());
|
||||
if (!$product->exists()) {
|
||||
throw new Exception('Product not found');
|
||||
}
|
||||
return $product;
|
||||
}
|
||||
}
|
||||
@@ -139,6 +139,13 @@ class department_variables_o extends db
|
||||
if (!$variable) {
|
||||
return null;
|
||||
}
|
||||
// Check if the variable is 'false' or 'true'
|
||||
if ($variable[0]['value'] === 'false') {
|
||||
return false;
|
||||
}
|
||||
if ($variable[0]['value'] === 'true') {
|
||||
return true;
|
||||
}
|
||||
return $variable[0]['value'];
|
||||
}
|
||||
|
||||
|
||||
@@ -346,4 +346,13 @@ class departments_o extends db
|
||||
self::requireSelected();
|
||||
return (new department_notification_sms_o())->getDepartmentActivePhoneNumbers($this->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function isModuleTimeBookingsEnabled(): bool
|
||||
{
|
||||
self::requireSelected();
|
||||
return (bool)$this->variables->getVariable('bookingsystem_time_based_enabled');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,176 @@
|
||||
<?php
|
||||
|
||||
namespace routes;
|
||||
|
||||
use classes\response;
|
||||
use classes\router;
|
||||
use objects\department_time_bookings_entries_o;
|
||||
use objects\department_time_bookings_opening_hours_o;
|
||||
use objects\department_time_bookings_types_o;
|
||||
use traits\route_t;
|
||||
|
||||
class customerTimeBookingsRoute
|
||||
{
|
||||
use route_t;
|
||||
|
||||
public function run(): void
|
||||
{
|
||||
global /** @var response $response */
|
||||
/** @var router $router */
|
||||
$router, $response;
|
||||
|
||||
|
||||
/** Guest Time Bookings -> Opening Hours -> GET */
|
||||
$this->get('/department/timebookings/opening-hours/public', function () {
|
||||
global $response;
|
||||
if (!self::isParametersSet(['id'])) {
|
||||
$response->error('Missing id parameter', 400);
|
||||
}
|
||||
self::requireType((int)self::getParameter('id'), self::type_int());
|
||||
self::requireMinValue((int)self::getParameter('id'), 1);
|
||||
self::requireSameLength(self::getParameter('id'), (int)self::getParameter('id'));
|
||||
// Check if the department exists
|
||||
$department = new \objects\departments_o();
|
||||
$department->select((int)self::getParameter('id'));
|
||||
if (!$department->exists()) {
|
||||
$response->error('Department not found', 404);
|
||||
}
|
||||
// Check if the department has time bookings enabled
|
||||
$variable = $department->isModuleTimeBookingsEnabled();
|
||||
if (!$variable) {
|
||||
$response->error('Department time bookings are not enabled', 404);
|
||||
}
|
||||
$department_time_bookings_opening_hours = new department_time_bookings_opening_hours_o();
|
||||
$department_time_bookings_opening_hours->selectByDepartment(
|
||||
(int)self::getParameter('id')
|
||||
);
|
||||
if (!$department_time_bookings_opening_hours->exists()) {
|
||||
$response->error('Department time bookings opening hours not found', 404);
|
||||
}
|
||||
$result = $department_time_bookings_opening_hours->asArray();
|
||||
// Restrict the result to only the opening hours
|
||||
$result = array_filter($result, function ($key) {
|
||||
return in_array($key, [
|
||||
'monday_start',
|
||||
'monday_end',
|
||||
'tuesday_start',
|
||||
'tuesday_end',
|
||||
'wednesday_start',
|
||||
'wednesday_end',
|
||||
'thursday_start',
|
||||
'thursday_end',
|
||||
'friday_start',
|
||||
'friday_end',
|
||||
'saturday_start',
|
||||
'saturday_end',
|
||||
'sunday_start',
|
||||
'sunday_end'
|
||||
]);
|
||||
}, ARRAY_FILTER_USE_KEY);
|
||||
$response->success($result);
|
||||
},
|
||||
[
|
||||
// No permissions required for this endpoint, as it is for guests
|
||||
]
|
||||
);
|
||||
|
||||
/** Guest Time Bookings -> Types -> GET */
|
||||
$this->get('/department/timebookings/types/public', function () {
|
||||
global $response;
|
||||
if (!self::isParametersSet(['id'])) {
|
||||
$response->error('Missing id parameter', 400);
|
||||
}
|
||||
self::requireType((int)self::getParameter('id'), self::type_int());
|
||||
self::requireMinValue((int)self::getParameter('id'), 1);
|
||||
self::requireSameLength(self::getParameter('id'), (int)self::getParameter('id'));
|
||||
// Check if the department exists
|
||||
$department = new \objects\departments_o();
|
||||
$department->select((int)self::getParameter('id'));
|
||||
if (!$department->exists()) {
|
||||
$response->error('Department not found', 404);
|
||||
}
|
||||
// Check if the department has time bookings enabled
|
||||
$variable = $department->isModuleTimeBookingsEnabled();
|
||||
if (!$variable) {
|
||||
$response->error('Department time bookings are not enabled', 404);
|
||||
}
|
||||
$department_time_bookings_types = new department_time_bookings_types_o();
|
||||
$booking_types_array = $department_time_bookings_types->getFieldsWhere(
|
||||
[
|
||||
'department' => (int)self::getParameter('id'),
|
||||
],
|
||||
['id', 'department', 'product', 'name', 'description', 'duration']
|
||||
);
|
||||
function formatBookingType($booking_type): array
|
||||
{
|
||||
return [
|
||||
'id' => (int)$booking_type['id'],
|
||||
'department' => (int)$booking_type['department'],
|
||||
'product' => (int)$booking_type['product'],
|
||||
'name' => (string)$booking_type['name'],
|
||||
'description' => (string)$booking_type['description'],
|
||||
'duration' => (int)$booking_type['duration'],
|
||||
];
|
||||
}
|
||||
|
||||
$result = array_map('formatBookingType', $booking_types_array);
|
||||
if (empty($result)) {
|
||||
$response->error('No department time bookings types found', 404);
|
||||
}
|
||||
$response->success($result);
|
||||
},
|
||||
[
|
||||
// No permissions required for this endpoint, as it is for guests
|
||||
]
|
||||
);
|
||||
/** Guest Time Bookings -> Entries -> GET */
|
||||
$this->get('/department/timebookings/entries/public', function () {
|
||||
/**
|
||||
* @example Usage of this endpoint:
|
||||
* GET /department/timebookings/entries/public?id=1&filters=created_at-date_from:2025-04-01,created_at-date_to:2025-05-30&order=created_at:desc
|
||||
* This will return all time booking entries for the department with ID 1, created between 2025-04-01 and 2025-05-30, ordered by created_at in descending order.
|
||||
* https://api.truckwash.dk:4433/department/timebookings/entries/public?id=1&filters=created_at-date_from:2025-04-01,created_at-date_to:2025-05-30&order=created_at:desc
|
||||
*/
|
||||
global $response;
|
||||
if (!self::isParametersSet(['id'])) {
|
||||
$response->error('Missing id parameter', 400);
|
||||
}
|
||||
self::requireType((int)self::getParameter('id'), self::type_int());
|
||||
self::requireMinValue((int)self::getParameter('id'), 1);
|
||||
self::requireSameLength(self::getParameter('id'), (int)self::getParameter('id'));
|
||||
// Check if the department exists
|
||||
$department = new \objects\departments_o();
|
||||
$department->select((int)self::getParameter('id'));
|
||||
if (!$department->exists()) {
|
||||
$response->error('Department not found', 404);
|
||||
}
|
||||
// Check if the department has time bookings enabled
|
||||
$variable = $department->isModuleTimeBookingsEnabled();
|
||||
if (!$variable) {
|
||||
$response->error('Department time bookings are not enabled', 404);
|
||||
}
|
||||
$department_time_bookings_entries = new department_time_bookings_entries_o();
|
||||
$booking_entries_array = $department_time_bookings_entries->listObjectsWithPaginationIfSet(
|
||||
function ($booking_entry): array {
|
||||
$booking_type_duration = (new department_time_bookings_types_o())->getDurationById((int)$booking_entry['type']);
|
||||
return [
|
||||
'start' => (string)$booking_entry['start'],
|
||||
'duration' => (int)$booking_type_duration, // The duration in minutes, defined by the type
|
||||
];
|
||||
},
|
||||
$department_time_bookings_entries->forceRestrictFilters(
|
||||
[
|
||||
'department' => [
|
||||
(int)$department->id
|
||||
],
|
||||
]
|
||||
)
|
||||
);
|
||||
$response->success($booking_entries_array);
|
||||
},
|
||||
[
|
||||
// No permissions required for this endpoint, as it is for guests
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -327,19 +327,71 @@ class departmentTimeBookingsRoute
|
||||
self::requirePermission('department_timebookings_entries_post');
|
||||
$user = (new authentication())->get_user();
|
||||
if ($user) {
|
||||
self::requireParameters(['department', 'type', 'start', 'end']);
|
||||
self::requireParameters(['department', 'type', 'start']);
|
||||
self::requireType((int)self::getParameter('department'), self::type_int());
|
||||
self::requireMinValue((int)self::getParameter('department'), 1);
|
||||
self::requireSameLength(self::getParameter('department'), (int)self::getParameter('department'));
|
||||
self::requireDepartmentAccess((int)self::getParameter('department'));
|
||||
|
||||
// Get the type
|
||||
self::requireType((int)self::getParameter('type'), self::type_int());
|
||||
self::requireMinValue((int)self::getParameter('type'), 1);
|
||||
self::requireSameLength(self::getParameter('type'), (int)self::getParameter('type'));
|
||||
|
||||
// Get the start time
|
||||
self::requireType((string)self::getParameter('start'), self::type_string());
|
||||
self::requireMinLength('start', 1);
|
||||
self::requireMaxLength('start', 255);
|
||||
self::requireSameLength(self::getParameter('start'), (string)self::getParameter('start'));
|
||||
self::requireDateFormat((string)self::getParameter('start'), 'Y-m-d H:i:s');
|
||||
|
||||
// Check if the department exists
|
||||
$department = new \objects\departments_o();
|
||||
$department->select((int)self::getParameter('department'));
|
||||
if (!$department->exists()) {
|
||||
(new logs_o())->add('department_time_bookings_entries', 'global', 0, $user->id, 'DEPARTMENT_TIME_BOOKINGS_ENTRIES_POST', 'Department not found');
|
||||
$response->error('Department not found', 404);
|
||||
}
|
||||
// Check if the department has time bookings enabled
|
||||
$variable = $department->isModuleTimeBookingsEnabled();
|
||||
if (!$variable) {
|
||||
(new logs_o())->add('department_time_bookings_entries', 'global', 0, $user->id, 'DEPARTMENT_TIME_BOOKINGS_ENTRIES_POST', 'Department time bookings are not enabled');
|
||||
$response->error('Department time bookings are not enabled', 404);
|
||||
}
|
||||
// Check if the type exists
|
||||
$department_time_bookings_types = new department_time_bookings_types_o();
|
||||
$department_time_bookings_types->select((int)self::getParameter('type'));
|
||||
if (!$department_time_bookings_types->exists()) {
|
||||
(new logs_o())->add('department_time_bookings_entries', 'global', 0, $user->id, 'DEPARTMENT_TIME_BOOKINGS_ENTRIES_POST', 'Department time bookings type not found');
|
||||
$response->error('Department time bookings type not found', 404);
|
||||
}
|
||||
// Check if the type belongs to the department
|
||||
if ((int)$department_time_bookings_types->department->value() !== (int)$department->id) {
|
||||
(new logs_o())->add('department_time_bookings_entries', 'global', 0, $user->id, 'DEPARTMENT_TIME_BOOKINGS_ENTRIES_POST', 'Department time bookings type does not belong to the department');
|
||||
$response->error('Department time bookings type does not belong to the department', 404);
|
||||
}
|
||||
// Calculate the end time
|
||||
$start_time = \DateTime::createFromFormat('Y-m-d H:i:s', (string)self::getParameter('start'));
|
||||
if (!$start_time) {
|
||||
(new logs_o())->add('department_time_bookings_entries', 'global', 0, $user->id, 'DEPARTMENT_TIME_BOOKINGS_ENTRIES_POST', 'Invalid start time format');
|
||||
$response->error('Invalid start time format', 400);
|
||||
}
|
||||
$duration = (int)$department_time_bookings_types->duration->value();
|
||||
if ($duration <= 0) {
|
||||
(new logs_o())->add('department_time_bookings_entries', 'global', 0, $user->id, 'DEPARTMENT_TIME_BOOKINGS_ENTRIES_POST', 'Invalid duration for department time bookings type');
|
||||
$response->error('Invalid duration for department time bookings type', 400);
|
||||
}
|
||||
$end_time = clone $start_time;
|
||||
$end_time->modify("+{$duration} minutes");
|
||||
|
||||
|
||||
// Add the department time bookings entry
|
||||
$department_time_bookings_entries = new department_time_bookings_entries_o();
|
||||
$department_time_bookings_entries->add(
|
||||
(int)self::getParameter('department'),
|
||||
(int)self::getParameter('type'),
|
||||
(string)self::getParameter('start'),
|
||||
(string)self::getParameter('end'),
|
||||
(string)$start_time->format('Y-m-d H:i:s'),
|
||||
(string)$end_time->format('Y-m-d H:i:s'),
|
||||
(int)(self::isParametersSet(['phone_country_code']) ? self::getParameter('phone_country_code') : 0),
|
||||
(int)(self::isParametersSet(['phone']) ? self::getParameter('phone') : 0),
|
||||
(self::isParametersSet(['note']) ? (string)self::getParameter('note') : null),
|
||||
|
||||
Reference in New Issue
Block a user