Refactor xlvask_potential_order_matches_o object, update related method signatures, and introduce route for retrieving potential order matches with permission checks and pagination.

This commit is contained in:
Jepp9350
2025-06-16 10:47:53 +02:00
parent d72c534b2f
commit 3987cf5de9
4 changed files with 116 additions and 21 deletions
@@ -262,11 +262,13 @@ class xlvask_tasks
$log->WashId,
)) {
// There's no match in the database, so we will add it.
//echo '# Adding potential order match for wash: ' . $log->WashId . ' - Order: ' . $log->getPotentialOrder()->id . ' - Customer: ' . $customer->customerId . ' - Customer Number: ' . (int)$customer->getUser()->customer_number->value() . ' - Department: ' . $log->getDepartment()->id . PHP_EOL;
$xlvask_potential_order_matches_o->add(
$log->WashId,
$log->getPotentialOrder()->id,
$customer->customerId,
$customer->externId, // Customer number
(string)$log->WashId,
(int)$log->getPotentialOrder()->id,
(string)$customer->customerId,
(int)$customer->getUser()->customer_number->value(),
(int)$log->getDepartment()->id,
);
}
//echo '# This wash might be associated with an order: ' . $log->getPotentialOrder()->id . PHP_EOL;
@@ -13,8 +13,8 @@ class xlvask_potential_order_matches_o extends db
use db_object_t;
public object_property $wash_id; // The wash id (In XL Vask - UUID)
public object_property $order; // The order id that this potential order match is for (In XL Vask - UUID)
public object_property $customerId; // The customer id (In XL Vask - UUID)
public object_property $order_id; // The order id that this potential order match is for (In XL Vask - UUID)
public object_property $customer_id; // The customer id (In XL Vask - UUID)
public object_property $customer_number; // The customer number
public object_property $ignore_duplicate; // Whether to ignore duplicate matches (0 = No, 1 = Yes)
public object_property $department; // The department id
@@ -38,8 +38,8 @@ class xlvask_potential_order_matches_o extends db
return [
'id' => (int)$this->id,
'wash_id' => (string)$this->wash_id->value(),
'order' => (int)$this->order->value(),
'customer_id' => (int)$this->customerId->value(),
'order_id' => (int)$this->order_id->value(),
'customer_id' => (int)$this->customer_id->value(),
'customer_number' => $this->customer_number->value(),
'ignore_duplicate' => (int)$this->ignore_duplicate->value(),
'department' => (int)$this->department->value(),
@@ -52,25 +52,32 @@ class xlvask_potential_order_matches_o extends db
* Add a new object to the database
* @param string $washId The wash id (In XL Vask - UUID)
* @param int $order The order id that this potential order match is for (In XL Vask - UUID)
* @param string $customerId The customer id (In XL Vask - UUID)
* @param string $customer_id The customer id (In XL Vask - UUID)
* @param int $customerNumber The customer number
* @param int $department The department id
* @return void
* @throws Exception If the object was not created successfully
*/
public function add(
string $washId,
int $order,
string $customerId,
string $customer_id,
int $customerNumber,
int $department,
): void
{
$tmp_id = self::add_object([
'wash_id' => $washId,
'order' => (int)$order,
'customer_id' => $customerId,
'customer_number' => (int)$customerNumber,
'department' => (int)(new orders_o())->select((int)$order)->department_id->value(),
]);
echo 'Wash ID: ' . $washId . ', Order: ' . $order . ', Customer ID: ' . $customer_id . ', Customer Number: ' . $customerNumber . ', Department: ' . $department . "\n";
$tmp_id = self::add_object(
[
'wash_id' => $washId,
'order_id' => $order,
'customer_id' => $customer_id,
'customer_number' => $customerNumber,
'ignore_duplicate' => 0, // Default to not ignoring duplicates
'department' => $department,
]
);
echo 'Temporary ID: ' . $tmp_id . "\n";
$this->id = $tmp_id;
self::getObjectProperties();
self::objectChanged();
@@ -80,8 +87,8 @@ class xlvask_potential_order_matches_o extends db
public function getObjectProperties(): void
{
$this->wash_id = new object_property($this->table, $this->id, 'wash_id', 'string', true);
$this->order = new object_property($this->table, $this->id, 'order', 'int', true);
$this->customerId = new object_property($this->table, $this->id, 'customer_id', 'string', true);
$this->order_id = new object_property($this->table, $this->id, 'order_id', 'int', true);
$this->customer_id = new object_property($this->table, $this->id, 'customer_id', 'string', true);
$this->customer_number = new object_property($this->table, $this->id, 'customer_number', 'int', true);
$this->ignore_duplicate = new object_property($this->table, $this->id, 'ignore_duplicate', 'int', true);
$this->department = new object_property($this->table, $this->id, 'department', 'int', true);
@@ -108,8 +115,8 @@ class xlvask_potential_order_matches_o extends db
$slack->send_message('
A new potential order match has been created:
- Wash ID: ' . $this->wash_id->value() . '
- Order ID: ' . $this->order->value() . '
- Customer ID: ' . $this->customerId->value() . '
- Order ID: ' . $this->order_id->value() . '
- Customer ID: ' . $this->customer_id->value() . '
- Customer Number: ' . $this->customer_number->value() . '
- Department: ' . $this->department->value(),
'xlvask_potential_order_matches');
@@ -0,0 +1,84 @@
<?php
namespace routes;
use classes\authentication;
use objects\logs_o;
use traits\route_t;
class potentialOrderMatchesRoute
{
use route_t;
public function run(): void
{
$this->get('/orders/sync/potential-matches', function () {
// Require the user to be logged in
global $response;
self::requirePermission('list_potential_order_matches');
// Check if the user has permission to list all potential order matches
if (self::hasPermission('list_all_potential_order_matches')) {
$this->requirePermission('list_all_potential_order_matches');
} else {
$this->requirePermission('list_own_potential_order_matches');
}
// Get the user object
$user = (new authentication())->get_user();
// Check if the request was successful
if ($user) {
// Log the incident
(new logs_o())->add('potential_order_matches', 'global', 1, $user->id, 'LIST_OWN_POTENTIAL_ORDER_MATCHES', 'User accessed the list of potential order matches');
$potentialOrderMatches = new \objects\xlvask_potential_order_matches_o();
// Return the list of potential order matches
$response->success(
$potentialOrderMatches
->setSearchableFields([
'id',
'wash_id',
'order_id',
'customer_id',
'customer_number',
'ignore_duplicate',
'department',
'created_at',
'updated_at',
'deleted_at',
])
->listObjectsWithPaginationIfSet(
function ($match) use ($potentialOrderMatches, $user) {
return [
'id' => (int)$match['id'],
'wash_id' => (string)$match['wash_id'],
'order_id' => (int)$match['order_id'],
'customer_id' => (string)$match['customer_id'],
'customer_number' => (int)$match['customer_number'],
'ignore_duplicate' => (int)$match['ignore_duplicate'],
'department' => (int)$match['department'],
'created_at' => (string)$match['created_at'],
'updated_at' => (string)$match['updated_at'],
'deleted_at' => $match['deleted_at'] ? (string)$match['deleted_at'] : null,
];
},
$potentialOrderMatches->forceRestrictFilters(
[
// This makes sure that the user can only see department matches that belong to their departments
'department' => $user->getGroup()->getDepartments()
]
)
)
);
} else {
// Log the incident
(new logs_o())->add('potential_order_matches', 'global', 1, 0, 'LIST_OWN_POTENTIAL_ORDER_MATCHES', 'No user found, or invalid session');
// Return an error
$response->error('Invalid session', 400);
}
},
[
'list_potential_order_matches' => 'List potential order matches, provided the user has either list_all_potential_order_matches, or list_own_potential_order_matches permission',
'list_own_potential_order_matches' => 'List all potential order matches for the logged in user',
'list_all_potential_order_matches' => 'List all potential order matches for all users (superuser only)',
]
);
}
}
@@ -951,8 +951,10 @@ trait db_object_t
}
// Prepare the SQL query to insert the data
$columns = implode(', ', array_keys($data));
// Escape the column names to prevent SQL injection and reserved keyword issues
$values = implode("', '", array_values($data));
$sql = "INSERT INTO $this->table ($columns) VALUES ('$values')";
//echo "SQL: $sql\n"; // Debugging line, can be removed in production
$db->query($sql);
return $db->insert_id();
} catch (Exception $e) {