53 lines
2.0 KiB
PHP
53 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace routes;
|
|
|
|
use classes\authentication;
|
|
use customers\economicCustomers;
|
|
use objects\logs_o;
|
|
use objects\users_o;
|
|
use traits\route_t;
|
|
|
|
class customerSearchRoute
|
|
{
|
|
use route_t;
|
|
|
|
public function run(): void
|
|
{
|
|
$this->post('/customers/search', function () {
|
|
// Require the user to be logged in
|
|
global $response;
|
|
$this->requirePermission('search_customers');
|
|
// Get the user object
|
|
$user = (new authentication())->get_user();
|
|
// Check if the request was successful
|
|
if ($user) {
|
|
// Get the post data
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
|
// Check if the pagination, search and sort parameters are set
|
|
if (!isset($data['search'])) {
|
|
$response->error('Missing required body parameter search', 400);
|
|
}
|
|
if (!isset($data['filter'])) {
|
|
$response->error('Missing required body parameter filter', 400);
|
|
}
|
|
// Check if the search parameter is valid
|
|
$allowedSearchFilters = (new economicCustomers())->allowed_search_filters_customers();
|
|
if (!in_array($data['filter'], $allowedSearchFilters)) {
|
|
$response->error('Invalid search parameter', 400);
|
|
}
|
|
(new logs_o())->add('customers', 'global', 1, $user->id, 'SEARCH_CUSTOMERS', 'Successfully retrieved customers meeting search criteria');
|
|
// Return the list of users
|
|
$response->success(
|
|
(array)(new economicCustomers())->searchCustomers((string)$data['search'], (string)$data['filter'])
|
|
);
|
|
} else {
|
|
// Log the incident
|
|
(new logs_o())->add('customers', 'global', 1, 0, 'SEARCH_CUSTOMERS', 'No user found, or invalid session');
|
|
// Return an error
|
|
$response->error('Invalid session', 400);
|
|
}
|
|
});
|
|
|
|
}
|
|
} |