mirror of
https://github.com/itflow-org/itflow
synced 2026-11-11 07:01:16 +00:00
Products API - Add update endpoint
This commit is contained in:
50
api/v1/products/product_model.php
Normal file
50
api/v1/products/product_model.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
// Map product API field names to the input keys used by productSave().
|
||||
$field_names = [
|
||||
'product_name' => 'name',
|
||||
'product_type' => 'type',
|
||||
'product_category_id' => 'category',
|
||||
'product_price' => 'price',
|
||||
'product_cost' => 'cost',
|
||||
'product_tax_id' => 'tax',
|
||||
'product_frequency' => 'frequency',
|
||||
'product_seat_source' => 'seat_source',
|
||||
'product_code' => 'code',
|
||||
'product_vendor_id' => 'vendor',
|
||||
'product_vendor_part' => 'vendor_part',
|
||||
'product_reorder_level' => 'reorder_level',
|
||||
'product_reorder_qty' => 'reorder_qty',
|
||||
'product_location' => 'location',
|
||||
'product_asset_type' => 'asset_type',
|
||||
'product_make' => 'make',
|
||||
'product_model' => 'model',
|
||||
'product_warranty_months' => 'warranty_months',
|
||||
'product_description' => 'description',
|
||||
'product_notes' => 'notes',
|
||||
'product_track_stock' => 'track_stock',
|
||||
];
|
||||
|
||||
$product_input = [];
|
||||
$product_model_error = '';
|
||||
|
||||
foreach ($field_names as $api_field => $key) {
|
||||
if (!array_key_exists($api_field, $_POST)) {
|
||||
continue;
|
||||
}
|
||||
if (!is_scalar($_POST[$api_field]) && $_POST[$api_field] !== null) {
|
||||
$product_model_error = "The $api_field field must be a scalar value.";
|
||||
break;
|
||||
}
|
||||
|
||||
$value = (string) ($_POST[$api_field] ?? '');
|
||||
if ($key === 'track_stock') {
|
||||
$value = in_array(strtolower(trim($value)), ['1', 'true', 'yes', 'on', 'tracked'], true);
|
||||
}
|
||||
$product_input[$key] = $value;
|
||||
}
|
||||
|
||||
// The product form posts the stock switch with the type, including when it is off.
|
||||
if (array_key_exists('type', $product_input) && !array_key_exists('track_stock', $product_input)) {
|
||||
$product_input['track_stock'] = false;
|
||||
}
|
||||
60
api/v1/products/update.php
Normal file
60
api/v1/products/update.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
require_once '../validate_api_key.php';
|
||||
|
||||
require_once '../require_post_method.php';
|
||||
|
||||
// Parse ID
|
||||
$product_id = intval($_POST['product_id'] ?? 0);
|
||||
|
||||
// Default
|
||||
$update_count = false;
|
||||
|
||||
if (!empty($product_id)) {
|
||||
|
||||
$product_row = productRow($product_id);
|
||||
|
||||
// Variable assignment from POST - only supplied fields are changed
|
||||
require_once 'product_model.php';
|
||||
|
||||
if ($product_row) {
|
||||
|
||||
if ($product_model_error !== '') {
|
||||
http_response_code(400);
|
||||
$return_arr['success'] = "False";
|
||||
$return_arr['message'] = $product_model_error;
|
||||
echo json_encode($return_arr);
|
||||
exit();
|
||||
}
|
||||
|
||||
if (!$product_input) {
|
||||
http_response_code(400);
|
||||
$return_arr['success'] = "False";
|
||||
$return_arr['message'] = 'Provide at least one product field to update.';
|
||||
echo json_encode($return_arr);
|
||||
exit();
|
||||
}
|
||||
|
||||
if (!productSave($product_input, $product_id, $error)) {
|
||||
http_response_code(400);
|
||||
$return_arr['success'] = "False";
|
||||
$return_arr['message'] = $error;
|
||||
echo json_encode($return_arr);
|
||||
exit();
|
||||
}
|
||||
|
||||
// Check update & get the updated product
|
||||
if (!mysqli_errno($mysqli)) {
|
||||
$product_row = productRow($product_id);
|
||||
$product_name = escapeSql($product_row['product_name']);
|
||||
$update_count = 1;
|
||||
|
||||
// Logging
|
||||
logAudit("Product", "Edit", "$product_name via API ($api_key_name)", 0, $product_id);
|
||||
logAudit("API", "Success", "Edited product $product_name via API ($api_key_name)", 0, $product_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Output
|
||||
require_once '../update_output.php';
|
||||
Reference in New Issue
Block a user