86 lines
3.7 KiB
PHP
86 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace routes;
|
|
|
|
use classes\authentication;
|
|
use objects\bookings_o;
|
|
use objects\logs_o;
|
|
use traits\route_t;
|
|
|
|
class bookingsRoute
|
|
{
|
|
use route_t;
|
|
|
|
public function run(): void
|
|
{
|
|
$this->get('/bookings', function () {
|
|
// Require the user to be logged in
|
|
global $response;
|
|
$this->requirePermission('list_bookings');
|
|
// Get the user object
|
|
$user = (new authentication())->get_user();
|
|
// Check if the request was successful
|
|
if ($user) {
|
|
// Log the incident
|
|
(new logs_o())->add('bookings', 'global', 1, $user->id, 'LIST_BOOKINGS', 'Successfully listed bookings');
|
|
// Return the list of departments
|
|
$response->success(
|
|
(new bookings_o())->listObjectsWithPaginationIfSet()
|
|
);
|
|
} else {
|
|
// Log the incident
|
|
(new logs_o())->add('bookings', 'global', 1, 0, 'LIST_BOOKINGS', 'No user found, or invalid session');
|
|
// Return an error
|
|
$response->error('Invalid session', 400);
|
|
}
|
|
});
|
|
// Synchronize booking from the external system
|
|
$this->post('/admin/bookings/sync', function () {
|
|
// Require the user to be logged in
|
|
global $response;
|
|
if ($this->fromRequest('auth_key') !== 'earm8BX4MFTgS6JCNQdqW5EzHUutv2Vx')
|
|
$this->requirePermission('sync_bookings');
|
|
// Check if the request was successful
|
|
$booking = [
|
|
'id' => $this->fromRequest('id'),
|
|
'customer_number' => $this->fromRequest('customer_number'),
|
|
'wash_type' => $this->fromRequest('wash_type'),
|
|
'contact_email' => $this->fromRequest('contact_email'),
|
|
'reference_number' => $this->fromRequest('reference_number'),
|
|
'regNrTraekker' => $this->fromRequest('regNrTraekker'),
|
|
'regNrTrailer' => $this->fromRequest('regNrTrailer'),
|
|
'washCertificateEmail' => $this->fromRequest('washCertificateEmail'),
|
|
'date' => $this->fromRequest('date'),
|
|
'department' => $this->fromRequest('department'),
|
|
'pickup_bool' => $this->fromRequest('pickup_bool'),
|
|
'notes' => $this->fromRequest('notes'),
|
|
'washCertificateStatus' => $this->fromRequest('washCertificateStatus'),
|
|
'washCertificateUrl' => $this->fromRequest('washCertificateUrl'),
|
|
'status' => $this->fromRequest('status'),
|
|
];
|
|
// Log the incident
|
|
(new logs_o())->add('bookings', 'global', 1, 0, 'SYNC_BOOKINGS', 'Successfully synced bookings');
|
|
// Add the booking, if it doesn't exist, update it if it does
|
|
(new bookings_o())->addOrUpdate(
|
|
(int)$booking['id'],
|
|
(int)$booking['customer_number'],
|
|
(string)$booking['wash_type'],
|
|
(string)$booking['contact_email'],
|
|
(string)$booking['reference_number'],
|
|
(string)$booking['regNrTraekker'],
|
|
(string)$booking['regNrTrailer'],
|
|
(string)$booking['washCertificateEmail'],
|
|
(string)$booking['date'],
|
|
(string)$booking['department'],
|
|
(bool)$booking['pickup_bool'],
|
|
(string)$booking['notes'],
|
|
(string)$booking['washCertificateStatus'],
|
|
(string)$booking['washCertificateUrl'],
|
|
(string)$booking['status']
|
|
);
|
|
$response->success(
|
|
['message' => 'Successfully synced booking']
|
|
);
|
|
});
|
|
}
|
|
} |