Add product retrieval by department categories and typecasting fixes
- Added methods in `department_categories_o`, `departments_o`, and `categories_o` to retrieve products associated with department categories. - Updated `productsRoute` and `superuserDepartmentRoute` to fetch department-specific products. - Fixed typecasting in product price assignments for improved data consistency.
This commit is contained in:
@@ -97,4 +97,22 @@ class categories_o extends db
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return products_o[] An array of products in this category
|
||||
* @throws Exception If the object is not selected, it throws an exception
|
||||
*/
|
||||
public function getProducts(): array
|
||||
{
|
||||
self::requireSelected();
|
||||
$products_data = (new products_o())->getFieldsWhere(['category' => $this->id], ['id']);
|
||||
/** @var products_o[] $products */
|
||||
$products = [];
|
||||
foreach ($products_data as $product_data) {
|
||||
$product = new products_o();
|
||||
$product->select((int)$product_data['id']);
|
||||
$products[] = $product;
|
||||
}
|
||||
return $products;
|
||||
}
|
||||
}
|
||||
@@ -108,4 +108,36 @@ class department_categories_o extends db
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
* @returns department_categories_o[] Array of department category objects for a department
|
||||
*/
|
||||
public function getDepartmentCategoryObjects(int $department): array
|
||||
{
|
||||
$department_categories = self::getFieldsWhere(
|
||||
[
|
||||
'department_id' => $department,
|
||||
'deleted_at' => null
|
||||
],
|
||||
['id']
|
||||
);
|
||||
/** @var department_categories_o[] $objects */
|
||||
$objects = [];
|
||||
foreach ( $department_categories as $department_category ) {
|
||||
$objects[] = (new department_categories_o())->select((int)$department_category['id']);
|
||||
}
|
||||
return $objects;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the category associated with this department category
|
||||
* @return categories_o The category object
|
||||
* @throws Exception If the category does not exist
|
||||
*/
|
||||
public function getCategory(): categories_o
|
||||
{
|
||||
self::requireSelected();
|
||||
return (new categories_o())->select($this->category_id->value());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -380,4 +380,29 @@ class departments_o extends db
|
||||
$this->getObjectProperties();
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
* @return products_o[] An array of all products in the department's categories
|
||||
*/
|
||||
public function getAllProductInDepartmentCategories(): array
|
||||
{
|
||||
self::requireSelected();
|
||||
// Get all the categories associated
|
||||
$department_categories = (new department_categories_o())->getDepartmentCategoryObjects($this->id);
|
||||
$categories = [];
|
||||
/** @var products_o[] $products */
|
||||
$products = [];
|
||||
foreach ( $department_categories as $department_category ) {
|
||||
$categories[] = $department_category->getCategory();
|
||||
}
|
||||
foreach ( $categories as $category ) {
|
||||
$cat_products = $category->getProducts();
|
||||
foreach ($cat_products as $cat_product) {
|
||||
$products[] = $cat_product;
|
||||
}
|
||||
}
|
||||
// Remove duplicate products
|
||||
return array_unique($products, SORT_REGULAR);
|
||||
}
|
||||
}
|
||||
@@ -230,7 +230,7 @@ class products_o extends db
|
||||
$prices = $db->fetch_all($result);
|
||||
foreach ( $products as $key => $product ) {
|
||||
foreach ( $prices as $price ) {
|
||||
if ($product['id'] === $price['product_id']) {
|
||||
if ((int)$product['id'] === (int)$price['product_id']) {
|
||||
$products[$key]['price'] = $price['price'];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,9 @@
|
||||
namespace routes;
|
||||
|
||||
use classes\authentication;
|
||||
use objects\categories_o;
|
||||
use objects\department_categories_o;
|
||||
use objects\departments_o;
|
||||
use objects\logs_o;
|
||||
use objects\product_options_o;
|
||||
use objects\products_o;
|
||||
@@ -240,12 +243,14 @@ class productsRoute
|
||||
(new logs_o())->add('products', 'global', 1, $user->id, 'LIST_PRODUCTS', 'Successfully listed products');
|
||||
// Check if the department_id is set
|
||||
if (isset($data['department_id'])) {
|
||||
// Get all product ids contained in a category attached to the department
|
||||
$departmentSpecificProducts = (new departments_o())->select((int)$data['department_id'])->getAllProductInDepartmentCategories();
|
||||
// Return the list of products
|
||||
$response->success(
|
||||
(new products_o())->applyDepartmentPricing((array)(new products_o())->listObjectsWithPaginationIfSet(
|
||||
function ($product) {
|
||||
return parseProduct($product);
|
||||
}
|
||||
},
|
||||
), (int)$data['department_id'])
|
||||
);
|
||||
}
|
||||
|
||||
@@ -135,8 +135,16 @@ class superuserDepartmentRoute
|
||||
(new logs_o())->add('departments', 'global', 1, $user->id, 'SUPERUSER_FETCH_DEPARTMENT_PRICES', 'Successfully fetched department prices');
|
||||
// Return the department
|
||||
$response->success(
|
||||
array_map(
|
||||
function ($price_row) {
|
||||
$price_row['id'] = (int)$price_row['id'];
|
||||
$price_row['department_id'] = (int)$price_row['department_id'];
|
||||
$price_row['product_id'] = (int)$price_row['product_id'];
|
||||
$price_row['price'] = (int)$price_row['price'];
|
||||
return $price_row;
|
||||
},
|
||||
(new departments_o())->getDepartmentProductPrices((int)$this->fromRequest('department_id'))
|
||||
);
|
||||
));
|
||||
} else {
|
||||
// Log the incident
|
||||
(new logs_o())->add('departments', 'global', 1, 0, 'SUPERUSER_FETCH_DEPARTMENT_PRICES', 'No user found, or invalid session');
|
||||
|
||||
Reference in New Issue
Block a user