diff --git a/objects/bookings_o.php b/objects/bookings_o.php new file mode 100644 index 00000000..93436c7a --- /dev/null +++ b/objects/bookings_o.php @@ -0,0 +1,95 @@ +setTable('bookings'); + } + + public function add(int $customer_number, string $wash_type, string $contact_email, string $reference_number, string $regNrTraekker, string $regNrTrailer, string $washCertificateEmail, string $date, string $department, int $pickup_bool, string $notes, string $washCertificateStatus, string $washCertificateUrl, string $status): void + { + global $db; + // Avoid SQL injection + $wash_type = $db->escape_string($wash_type); + $contact_email = $db->escape_string($contact_email); + $reference_number = $db->escape_string($reference_number); + $regNrTraekker = $db->escape_string($regNrTraekker); + $regNrTrailer = $db->escape_string($regNrTrailer); + $washCertificateEmail = $db->escape_string($washCertificateEmail); + $date = $db->escape_string($date); + $department = $db->escape_string($department); + $notes = $db->escape_string($notes); + $washCertificateStatus = $db->escape_string($washCertificateStatus); + $washCertificateUrl = $db->escape_string($washCertificateUrl); + $status = $db->escape_string($status); + // Create a new record in the database ( Replace the code, if an entry already exists ) + $sql = "INSERT INTO $this->table (customer_number, wash_type, contact_email, reference_number, regNrTraekker, regNrTrailer, washCertificateEmail, date, department, pickup_bool, notes, washCertificateStatus, washCertificateUrl, status) VALUES ($customer_number, '$wash_type', '$contact_email', '$reference_number', '$regNrTraekker', '$regNrTrailer', '$washCertificateEmail', '$date', '$department', $pickup_bool, '$notes', '$washCertificateStatus', '$washCertificateUrl', '$status')"; + $db->query($sql); + + // Get the id of the new record + $this->id = $db->insert_id(); + } + + public function addOrUpdate($id, $customer_number, $wash_type, $contact_email, $reference_number, $regNrTraekker, $regNrTrailer, $washCertificateEmail, $date, $department, $pickup_bool, $notes, $washCertificateStatus, $washCertificateUrl, $status): void + { + global $db; + // Avoid SQL injection + $wash_type = $db->escape_string($wash_type); + $contact_email = $db->escape_string($contact_email); + $reference_number = $db->escape_string($reference_number); + $regNrTraekker = $db->escape_string($regNrTraekker); + $regNrTrailer = $db->escape_string($regNrTrailer); + $washCertificateEmail = $db->escape_string($washCertificateEmail); + $date = $db->escape_string($date); + $department = $db->escape_string($department); + $notes = $db->escape_string($notes); + $washCertificateStatus = $db->escape_string($washCertificateStatus); + $washCertificateUrl = $db->escape_string($washCertificateUrl); + $status = $db->escape_string($status); + // Create a new record in the database ( Replace the code, if an entry already exists ) + $sql = "INSERT INTO $this->table (id, customer_number, wash_type, contact_email, reference_number, regNrTraekker, regNrTrailer, washCertificateEmail, date, department, pickup_bool, notes, washCertificateStatus, washCertificateUrl, status) VALUES ($id, $customer_number, '$wash_type', '$contact_email', '$reference_number', '$regNrTraekker', '$regNrTrailer', '$washCertificateEmail', '$date', '$department', $pickup_bool, '$notes', '$washCertificateStatus', '$washCertificateUrl', '$status') ON DUPLICATE KEY UPDATE customer_number = $customer_number, wash_type = '$wash_type', contact_email = '$contact_email', reference_number = '$reference_number', regNrTraekker = '$regNrTraekker', regNrTrailer = '$regNrTrailer', washCertificateEmail = '$washCertificateEmail', date = '$date', department = '$department', pickup_bool = $pickup_bool, notes = '$notes', washCertificateStatus = '$washCertificateStatus', washCertificateUrl = '$washCertificateUrl', status = '$status'"; + $db->query($sql); + } + + public function getObjectProperties(): void + { + $this->customer_number = new object_property($this->table, $this->id, 'customer_number', 'int', true); + $this->wash_type = new object_property($this->table, $this->id, 'wash_type', 'string', true); + $this->contact_email = new object_property($this->table, $this->id, 'contact_email', 'string', true); + $this->reference_number = new object_property($this->table, $this->id, 'reference_number', 'string', true); + $this->regNrTraekker = new object_property($this->table, $this->id, 'regNrTraekker', 'string', true); + $this->regNrTrailer = new object_property($this->table, $this->id, 'regNrTrailer', 'string', true); + $this->washCertificateEmail = new object_property($this->table, $this->id, 'washCertificateEmail', 'string', true); + $this->date = new object_property($this->table, $this->id, 'date', 'string', true); + $this->department = new object_property($this->table, $this->id, 'department', 'string', true); + $this->pickup_bool = new object_property($this->table, $this->id, 'pickup_bool', 'int', true); + $this->notes = new object_property($this->table, $this->id, 'notes', 'string', true); + $this->washCertificateStatus = new object_property($this->table, $this->id, 'washCertificateStatus', 'string', true); + $this->washCertificateUrl = new object_property($this->table, $this->id, 'washCertificateUrl', 'string', true); + $this->status = new object_property($this->table, $this->id, 'status', 'string', true); + } +} \ No newline at end of file diff --git a/routes/bookingsRoute.php b/routes/bookingsRoute.php new file mode 100644 index 00000000..5adaf7a5 --- /dev/null +++ b/routes/bookingsRoute.php @@ -0,0 +1,86 @@ +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'] + ); + }); + } +} \ No newline at end of file