Files
api/services/nginx/app/routes/departmentDailyReportsRoute.php
T
Jepp9350 d6ed51f9ba Add endpoint to fetch the latest department daily report
Introduced a new method in `department_daily_reports_o` to select the latest daily report for a department. Added a corresponding API route to fetch the latest report details for a given department ID, with appropriate validations and access control measures. Adjusted parameter handling logic to enhance request validation.
2025-03-04 15:19:40 +01:00

498 lines
23 KiB
PHP

<?php
namespace routes;
use classes\authentication;
use objects\department_daily_reports_o;
use objects\logs_o;
use traits\route_t;
class departmentDailyReportsRoute
{
use route_t;
public function run(): void
{
$this->get('/departments/daily-reports', function () {
// Require the user to be logged in
global $response;
$this->requirePermission('list_department_daily_reports');
// Get the user object
$user = (new authentication())->get_user();
// Check if the request was successful
if ($user) {
// Log the incident
(new logs_o())->add('departments', 'global', 1, $user->id, 'LIST_DEPARTMENT_DAILY_REPORTS', 'Successfully listed departments daily reports');
$department_daily_reports_o = new department_daily_reports_o();
// Return the list of departments
$response->success(
$department_daily_reports_o
->setSearchableFields([
// The fields that can be searched. This would otherwise make it possible to get secret information from the database, simply by searching for it and getting the result count back
'id',
'water_usage',
'notes',
'filled_by',
'created_at'
])
->listObjectsWithPaginationIfSet(
function ($department_report) use ($user) {
return [
'id' => (int)$department_report['id'],
'department_id' => (int)$department_report['department_id'],
'water_usage' => (int)$department_report['water_usage'],
'notes' => (string)$department_report['notes'],
'filled_by' => (int)$department_report['filled_by'],
'created_at' => (string)$department_report['created_at'],
];
},
// This makes sure that the user can only see reports from the departments they explicitly have access to
$department_daily_reports_o->forceRestrictFilters(
[
// This makes sure that the user can only see orders from the departments they explicitly have access to
'department_id' => $user->getGroup()->getDepartments(),
]
)
)
);
} else {
// Log the incident
(new logs_o())->add('departments', 'global', 1, 0, 'LIST_DEPARTMENT_DAILY_REPORTS', 'No user found, or invalid session');
// Return an error
$response->error('Invalid session', 400);
}
},
[
'list_department_daily_reports' => 'List the department daily reports, provided the user has access to the department',
'department_access_:id' => 'Access the department'
]
);
$this->get('/departments/daily-reports/latest', function () {
// Require the user to be logged in
global $response;
$this->requirePermission('list_department_daily_reports');
// Get the user object
$user = (new authentication())->get_user();
// Check if the request was successful
if ($user) {
// Check if the required fields are set
self::requireParameters(
[
'id'
]
);
// Validate the department_id
self::requireType(
(int)self::getParameter('id'),
self::type_int()
);
// Require the department_id to be above 0
self::requireMinValue(
(int)self::getParameter('id'),
1
);
// Determine if the user has access to the department
self::requireDepartmentAccess((int)self::getParameter('id'));
// Log the incident
(new logs_o())->add('departments', 'global', 1, $user->id, 'LIST_DEPARTMENT_DAILY_REPORTS', 'Successfully listed departments daily reports');
// Return the list of departments
$department_daily_reports_o = new department_daily_reports_o();
try {
$department_daily_report_latest = $department_daily_reports_o->selectLastDepartmentDailyReport((int)self::getParameter('id'));
} catch (\Exception $e) {
$response->error('No department daily report found', 404);
}
$response->success(
[
'id' => (int)$department_daily_report_latest->id,
'department_id' => (int)$department_daily_report_latest->department_id->value(),
'water_usage' => (int)$department_daily_report_latest->water_usage->value(),
'water_usage_morning' => (int)$department_daily_report_latest->water_usage_morning->value(),
'notes' => (string)$department_daily_report_latest->notes->value(),
'filled_by' => (int)$department_daily_report_latest->filled_by->value(),
'created_at' => (string)$department_daily_report_latest->created_at->value(),
]
);
} else {
// Log the incident
(new logs_o())->add('departments', 'global', 1, 0, 'LIST_DEPARTMENT_DAILY_REPORTS', 'No user found, or invalid session');
// Return an error
$response->error('Invalid session', 400);
}
},
[
'list_department_daily_reports' => 'List the department daily reports, provided the user has access to the department',
'department_access_:id' => 'Access the department'
]
);
$this->post('/departments/daily-reports', function () {
// Require the user to be logged in
global $response;
$this->requirePermission('create_department_daily_reports');
// Get the user object
$user = (new authentication())->get_user();
// Check if the request was successful
if ($user) {
// Check if the required fields are set
self::requireParameters(
[
'department_id',
'water_usage',
'water_usage_morning',
'notes'
]
);
// Validate the department_id
self::requireType(
(int)self::getParameter('department_id'),
self::type_int()
);
// Require the department_id to be above 0
self::requireMinValue(
(int)self::getParameter('department_id'),
1
);
// Validate the water_usage
self::requireType(
(int)self::getParameter('water_usage'),
self::type_int()
);
// Require the water_usage to be at least 0
self::requireMinValue(
(int)self::getParameter('water_usage'),
0
);
// Validate the water_usage_morning
self::requireType(
(int)self::getParameter('water_usage_morning'),
self::type_int()
);
// Require the water_usage_morning to be at least 0
self::requireMinValue(
(int)self::getParameter('water_usage_morning'),
0
);
// Validate the notes
self::requireType(
(string)self::getParameter('notes'),
self::type_string()
);
// Require the notes to be at least 0 characters long
self::requireMinLength(
'notes',
0
);
// Require the notes to be at most 4000 characters long
self::requireMaxLength(
'notes',
4000
);
// Determine if the user has access to the department
self::requireDepartmentAccess((int)self::getParameter('department_id'));
// Log the incident
(new logs_o())->add('departments', 'global', 1, $user->id, 'CREATE_DEPARTMENT_DAILY_REPORTS', 'Successfully created department daily report');
// Return the list of departments
$response->success(
(new department_daily_reports_o())
->add(
(int)self::getParameter('department_id'),
(int)self::getParameter('water_usage'),
(int)self::getParameter('water_usage_morning'),
(string)self::getParameter('notes'),
$user->id
)->asArray()
);
} else {
// Log the incident
(new logs_o())->add('departments', 'global', 1, 0, 'CREATE_DEPARTMENT_DAILY_REPORTS', 'No user found, or invalid session');
// Return an error
$response->error('Invalid session', 400);
}
},
[
'create_department_daily_reports' => 'Create a new department daily report',
'department_access_:id' => 'Access the department'
]
);
$this->put('/departments/daily-reports', function () {
// Require the user to be logged in
global $response;
$this->requirePermission('update_department_daily_reports');
// Get the user object
$user = (new authentication())->get_user();
// Check if the request was successful
if ($user) {
// Check if the required fields are set
self::requireParameters(
[
'id',
]
);
// Validate the id
self::requireType(
(int)self::getParameter('id'),
self::type_int()
);
// Require the department_id to be above 0
self::requireMinValue(
(int)self::getParameter('id'),
1
);
// Check which fields are set
if (self::isParametersSet(['water_usage'])) {
// Validate the water_usage
self::requireType(
(int)self::getParameter('water_usage'),
self::type_int()
);
// Require the water_usage to be at least 0
self::requireMinValue(
(int)self::getParameter('water_usage'),
0
);
}
if (self::isParametersSet(['water_usage_morning'])) {
// Validate the water_usage_morning
self::requireType(
(int)self::getParameter('water_usage_morning'),
self::type_int()
);
// Require the water_usage_morning to be at least 0
self::requireMinValue(
(int)self::getParameter('water_usage_morning'),
0
);
}
if (self::isParametersSet(['notes'])) {
// Validate the notes
self::requireType(
(string)self::getParameter('notes'),
self::type_string()
);
// Require the notes to be at least 0 characters long
self::requireMinLength(
(string)self::getParameter('notes'),
0
);
// Require the notes to be at most 4000 characters long
self::requireMaxLength(
(string)self::getParameter('notes'),
4000
);
}
// Check if the report exists
// Update the department daily report
$department_daily_reports_o = new department_daily_reports_o();
$department_daily_report = $department_daily_reports_o->select((int)self::getParameter('id'));
// Determine if the user has access to the department
self::requireDepartmentAccess((int)$department_daily_report->department_id->value());
// Check the parameters that are set
if (self::isParametersSet(['water_usage'])) {
$department_daily_report->water_usage->set((int)self::getParameter('water_usage'));
}
if (self::isParametersSet(['water_usage_morning'])) {
$department_daily_report->water_usage_morning->set((int)self::getParameter('water_usage_morning'));
}
if (self::isParametersSet(['notes'])) {
$department_daily_report->notes->set((string)self::getParameter('notes'));
}
// Log the incident
(new logs_o())->add('departments', 'global', 1, $user->id, 'UPDATE_DEPARTMENT_DAILY_REPORTS', 'Successfully updated department daily report');
// Return the department daily report
$response->success($department_daily_report->asArray());
} else {
// Log the incident
(new logs_o())->add('departments', 'global', 1, 0, 'UPDATE_DEPARTMENT_DAILY_REPORTS', 'No user found, or invalid session');
// Return an error
$response->error('Invalid session', 400);
}
},
[
'update_department_daily_reports' => 'Update a department daily report',
'department_access_:id' => 'Access the department'
]
);
$this->get('/departments/daily-reports/product-count', function () {
// Require the user to be logged in
global $response;
$this->requirePermission('list_department_daily_reports');
// Get the user object
$user = (new authentication())->get_user();
// Check if the request was successful
if ($user) {
// Check if the required fields are set
self::requireParameters(
[
'date',
'department_id',
'product_id'
]
);
// Validate the date
self::requireType(
(string)self::getParameter('date'),
self::type_string()
);
// Require the date to be at least 0 characters long
self::requireMinLength(
'date',
0
);
// Require the date to be at most 10 characters long
self::requireMaxLength(
'date',
10
);
// Validate the date format (YYYY-MM-DD)
self::requireDateFormat(
(string)self::getParameter('date'),
'Y-m-d'
);
// Validate the department_id
self::requireType(
(int)self::getParameter('department_id'),
self::type_int()
);
// Require the department_id to be above 0
self::requireMinValue(
(int)self::getParameter('department_id'),
1
);
// Validate the product_id
self::requireType(
(int)self::getParameter('product_id'),
self::type_int()
);
// Require the product_id to be above 0
self::requireMinValue(
(int)self::getParameter('product_id'),
1
);
// Determine if the user has access to the department
self::requireDepartmentAccess((int)self::getParameter('department_id'));
// Log the incident
(new logs_o())->add('departments', 'global', 1, $user->id, 'LIST_DEPARTMENT_DAILY_REPORTS_PRODUCTS', 'Successfully listed departments daily reports products');
// Return the list of products sold on the selected date
$response->success(
[
'quantity' =>
(new department_daily_reports_o())
->getProductsSoldOnDate(
(string)self::getParameter('date'),
(int)self::getParameter('department_id'),
(int)self::getParameter('product_id')
),
'date' => (string)self::getParameter('date'),
'department_id' => (int)self::getParameter('department_id'),
'product_id' => (int)self::getParameter('product_id'),
// This is the amount of products sold on the selected date, that this product could have been sold as a part of. (Addons)
'out_of' => (int)(new department_daily_reports_o())->getProductsSoldWithAddonApplicable(
(string)self::getParameter('date'),
(int)self::getParameter('department_id'),
(int)self::getParameter('product_id')
)
]
);
} else {
// Log the incident
(new logs_o())->add('departments', 'global', 1, 0, 'LIST_DEPARTMENT_DAILY_REPORTS_PRODUCTS', 'No user found, or invalid session');
// Return an error
$response->error('Invalid session', 400);
}
},
[
'list_department_daily_reports' => 'List the department daily reports, provided the user has access to the department',
'department_access_:id' => 'Access the department'
]
);
$this->get('/departments/daily-reports/transaction-count', function () {
// Require the user to be logged in
global $response;
$this->requirePermission('list_department_daily_reports');
// Get the user object
$user = (new authentication())->get_user();
// Check if the request was successful
if ($user) {
// Check if the required fields are set
self::requireParameters(
[
'date',
'department_id'
]
);
// Validate the date
self::requireType(
(string)self::getParameter('date'),
self::type_string()
);
// Require the date to be at least 0 characters long
self::requireMinLength(
'date',
0
);
// Require the date to be at most 10 characters long
self::requireMaxLength(
'date',
10
);
// Validate the date format (YYYY-MM-DD)
self::requireDateFormat(
(string)self::getParameter('date'),
'Y-m-d'
);
// Validate the department_id
self::requireType(
(int)self::getParameter('department_id'),
self::type_int()
);
// Require the department_id to be above 0
self::requireMinValue(
(int)self::getParameter('department_id'),
1
);
// Determine if the user has access to the department
self::requireDepartmentAccess((int)self::getParameter('department_id'));
// Log the incident
(new logs_o())->add('departments', 'global', 1, $user->id, 'LIST_DEPARTMENT_DAILY_REPORTS_PRODUCTS', 'Successfully listed departments daily reports products');
// Return the list of products sold on the selected date
$response->success(
[
'quantity' =>
(new department_daily_reports_o())
->getTransactionsOnDateCount(
(string)self::getParameter('date'),
(int)self::getParameter('department_id')
),
'products' =>
(new department_daily_reports_o())
->getTransactionProductsOnDateCount(
(string)self::getParameter('date'),
(int)self::getParameter('department_id')
),
'date' => (string)self::getParameter('date'),
'department_id' => (int)self::getParameter('department_id')
]
);
} else {
// Log the incident
(new logs_o())->add('departments', 'global', 1, 0, 'LIST_DEPARTMENT_DAILY_REPORTS_PRODUCTS', 'No user found, or invalid session');
// Return an error
$response->error('Invalid session', 400);
}
},
[
'list_department_daily_reports' => 'List the department daily reports, provided the user has access to the department',
'department_access_:id' => 'Access the department'
]
);
}
}