Add order attachments module
- Implemented CRUD operations for order attachments, including adding, listing, downloading, and deleting. - Updated `db_object_t` and `attachments` to enhance object-attachment interactions with new methods for formatting, creating, and managing attachments. - Added new routes (`/orders/attachments` and `/attachments/upload`) for attachment-related functionality. - Adjusted `file_server.php` to handle temp file downloads and attachment storage. - Improved typing and error handling across attachment helper methods and classes.
This commit is contained in:
@@ -2,6 +2,9 @@
|
||||
|
||||
namespace routes;
|
||||
|
||||
use attachments\helpers\attachment_content;
|
||||
use classes\attachment_store;
|
||||
use classes\attachments;
|
||||
use classes\authentication;
|
||||
use classes\response;
|
||||
use classes\stripe;
|
||||
@@ -314,6 +317,198 @@ class ordersRoute
|
||||
]
|
||||
);
|
||||
|
||||
$this->get('/orders/attachments/download', function () {
|
||||
// Require the user to be logged in
|
||||
global $response;
|
||||
$this->requirePermission('download_order_attachments');
|
||||
// Get the user object
|
||||
$user = (new authentication())->get_user();
|
||||
// Check if the request was successful
|
||||
if ($user) {
|
||||
// Get the order ID and attachment ID from the request
|
||||
self::requireParameters([
|
||||
'order_id',
|
||||
'attachment_id'
|
||||
]);
|
||||
$order_id = self::getParameter('order_id');
|
||||
$attachment_id = self::getParameter('attachment_id');
|
||||
if (!is_numeric($order_id) || (int)$order_id < 1) {
|
||||
$response->error('Invalid order ID', 400);
|
||||
}
|
||||
if (!is_numeric($attachment_id) || (int)$attachment_id < 1) {
|
||||
$response->error('Invalid attachment ID', 400);
|
||||
}
|
||||
// Get the current order
|
||||
$order = (new orders_o())->getOrderById((int)$order_id);
|
||||
// Check if the order exists
|
||||
if (!$order->exists()) {
|
||||
$response->error('Order not found', 400);
|
||||
}
|
||||
// Get the attachment
|
||||
$attachment = $order->getAttachment((int)$attachment_id);
|
||||
if (!$attachment->exists()) {
|
||||
$response->error('Attachment not found', 400);
|
||||
}
|
||||
// Create a download link
|
||||
$attachment_store = new attachment_store();
|
||||
$attachments = new attachments();
|
||||
$attachment_formatted = $attachments->format($attachment);
|
||||
$download_link = $attachment_store->generateDirectDownloadUrl(
|
||||
$attachment_formatted->content->document
|
||||
);
|
||||
// Log the incident
|
||||
(new logs_o())->add('orders', $order->department_id->value(), 1, $user->id, 'DOWNLOAD_ORDER_ATTACHMENT', 'Successfully downloaded an attachment for an order (Order ID: ' . $order_id . ', Attachment ID: ' . $attachment_id . ')');
|
||||
// Return the download link
|
||||
$response->success(['download_link' => $download_link]);
|
||||
|
||||
} else {
|
||||
// Log the incident
|
||||
(new logs_o())->add('orders', 'global', 1, 0, 'DOWNLOAD_ORDER_ATTACHMENT', 'No user found, or invalid session');
|
||||
// Return an error
|
||||
$response->error('Invalid session', 400);
|
||||
}
|
||||
},
|
||||
[
|
||||
'download_order_attachments' => 'Download attachments for an order'
|
||||
]
|
||||
);
|
||||
|
||||
$this->get('/orders/attachments', function () {
|
||||
// Require the user to be logged in
|
||||
global $response;
|
||||
$this->requirePermission('list_order_attachments');
|
||||
// Get the user object
|
||||
$user = (new authentication())->get_user();
|
||||
// Check if the request was successful
|
||||
if ($user) {
|
||||
// Get the order ID from the request
|
||||
self::requireParameters([
|
||||
'id'
|
||||
]);
|
||||
$order_id = self::getParameter('id');
|
||||
if (!is_numeric($order_id) || (int)$order_id < 1) {
|
||||
$response->error('Invalid order ID', 400);
|
||||
}
|
||||
// Get the current order
|
||||
$order = (new orders_o())->getOrderById((int)$order_id);
|
||||
// Check if the order exists
|
||||
if (!$order->exists()) {
|
||||
$response->error('Order not found', 400);
|
||||
}
|
||||
// Get the attachments
|
||||
$attachments = $order->listAttachments();
|
||||
// Log the incident
|
||||
(new logs_o())->add('orders', $order->department_id->value(), 1, $user->id, 'LIST_ORDER_ATTACHMENTS', 'Successfully listed attachments for an order (ID: ' . $order_id . ')');
|
||||
// Return the attachments
|
||||
$response->success($attachments);
|
||||
} else {
|
||||
// Log the incident
|
||||
(new logs_o())->add('orders', 'global', 1, 0, 'LIST_ORDER_ATTACHMENTS', 'No user found, or invalid session');
|
||||
// Return an error
|
||||
$response->error('Invalid session', 400);
|
||||
}
|
||||
},
|
||||
[
|
||||
'list_order_attachments' => 'List attachments for an order'
|
||||
]
|
||||
);
|
||||
|
||||
$this->post('/orders/attachments/upload', function () {
|
||||
// Require the user to be logged in
|
||||
global $response;
|
||||
$this->requirePermission('add_order_attachments');
|
||||
// Get the user object
|
||||
$user = (new authentication())->get_user();
|
||||
// Check if the request was successful
|
||||
if ($user) {
|
||||
// Check if the order ID and file are set
|
||||
$this->requireParameters(['order_id', 'base64_file', 'file_name']);
|
||||
$order_id = (int)$this->getParameter('order_id');
|
||||
if (!is_numeric($order_id) || (int)$order_id < 1) {
|
||||
$response->error('Invalid order ID', 400);
|
||||
}
|
||||
// Get the current order
|
||||
$order = (new orders_o())->getOrderById((int)$order_id);
|
||||
// Check if the order exists
|
||||
if (!$order->exists()) {
|
||||
$response->error('Order not found', 400);
|
||||
}
|
||||
// Get the base64 file
|
||||
$base64_file = (string)$this->getParameter('base64_file');
|
||||
$attachment_store = new attachment_store();
|
||||
// Determine the file extension from the file name
|
||||
$file_name = (string)$this->getParameter('file_name');
|
||||
$extension = pathinfo($file_name, PATHINFO_EXTENSION);
|
||||
$object_name = $attachment_store->storeTempFileFromBase64(
|
||||
$base64_file,
|
||||
$extension
|
||||
);
|
||||
if ($object_name === false) {
|
||||
$response->error('Failed to store the attachment file.', 500);
|
||||
}
|
||||
$object_attachment = $order->addAttachment((new attachment_content())->setDocument($object_name)->setOther((string)self::getParameter('file_name')));
|
||||
// Log the incident
|
||||
(new logs_o())->add('orders', $order->department_id->value(), 1, $user->id, 'ADD_ORDER_ATTACHMENT', 'Successfully added an attachment for an order (Order ID: ' . $order_id . ')');
|
||||
// Return a success message
|
||||
$attachments = new attachments();
|
||||
$response->success($attachments->format($attachments->get($object_attachment->id)));
|
||||
} else {
|
||||
// Log the incident
|
||||
(new logs_o())->add('orders', 'global', 1, 0, 'ADD_ORDER_ATTACHMENT', 'No user found, or invalid session');
|
||||
// Return an error
|
||||
$response->error('Invalid session', 400);
|
||||
}
|
||||
},
|
||||
[
|
||||
'add_order_attachments' => 'Add attachments for an order'
|
||||
]
|
||||
);
|
||||
|
||||
$this->delete('/orders/attachments', function () {
|
||||
// Require the user to be logged in
|
||||
global $response;
|
||||
$this->requirePermission('delete_order_attachments');
|
||||
// Get the user object
|
||||
$user = (new authentication())->get_user();
|
||||
// Check if the request was successful
|
||||
if ($user) {
|
||||
// Get the order ID and attachment ID from the request
|
||||
self::requireParameters([
|
||||
'order_id',
|
||||
'attachment_id'
|
||||
]);
|
||||
$order_id = self::getParameter('order_id');
|
||||
$attachment_id = self::getParameter('attachment_id');
|
||||
if (!is_numeric($order_id) || (int)$order_id < 1) {
|
||||
$response->error('Invalid order ID', 400);
|
||||
}
|
||||
if (!is_numeric($attachment_id) || (int)$attachment_id < 1) {
|
||||
$response->error('Invalid attachment ID', 400);
|
||||
}
|
||||
// Get the current order
|
||||
$order = (new orders_o())->getOrderById((int)$order_id);
|
||||
// Check if the order exists
|
||||
if (!$order->exists()) {
|
||||
$response->error('Order not found', 400);
|
||||
}
|
||||
// Delete the attachment
|
||||
$order->removeAttachment((int)$attachment_id);
|
||||
// Log the incident
|
||||
(new logs_o())->add('orders', $order->department_id->value(), 1, $user->id, 'DELETE_ORDER_ATTACHMENT', 'Successfully deleted an attachment for an order (Order ID: ' . $order_id . ', Attachment ID: ' . $attachment_id . ')');
|
||||
// Return a success message
|
||||
$response->success(['message' => 'Attachment deleted successfully']);
|
||||
} else {
|
||||
// Log the incident
|
||||
(new logs_o())->add('orders', 'global', 1, 0, 'DELETE_ORDER_ATTACHMENT', 'No user found, or invalid session');
|
||||
// Return an error
|
||||
$response->error('Invalid session', 400);
|
||||
}
|
||||
},
|
||||
[
|
||||
'delete_order_attachments' => 'Delete attachments for an order'
|
||||
]
|
||||
);
|
||||
|
||||
$this->post('/orders/mark_as_completed', function () {
|
||||
// Require the user to be logged in
|
||||
global $response;
|
||||
|
||||
Reference in New Issue
Block a user