2022-09-07 17:28:41 +02:00
|
|
|
import { ProductCategory } from '../models/product-category';
|
2022-09-08 17:51:48 +02:00
|
|
|
import { StockMovementReason } from '../models/product';
|
2022-09-07 17:28:41 +02:00
|
|
|
|
|
|
|
export default class ProductLib {
|
|
|
|
/**
|
|
|
|
* Map product categories by position
|
|
|
|
* @param categories unsorted categories, as returned by the API
|
|
|
|
*/
|
2022-09-08 17:51:48 +02:00
|
|
|
static sortCategories = (categories: Array<ProductCategory>): Array<ProductCategory> => {
|
2022-09-07 17:28:41 +02:00
|
|
|
const sortedCategories = categories
|
|
|
|
.filter(c => !c.parent_id)
|
|
|
|
.sort((a, b) => a.position - b.position);
|
|
|
|
const childrenCategories = categories
|
|
|
|
.filter(c => typeof c.parent_id === 'number')
|
|
|
|
.sort((a, b) => b.position - a.position);
|
|
|
|
childrenCategories.forEach(c => {
|
|
|
|
const parentIndex = sortedCategories.findIndex(i => i.id === c.parent_id);
|
|
|
|
sortedCategories.splice(parentIndex + 1, 0, c);
|
|
|
|
});
|
|
|
|
return sortedCategories;
|
|
|
|
};
|
2022-09-08 17:51:48 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the translation key associated with the given reason
|
|
|
|
*/
|
|
|
|
static stockMovementReasonTrKey = (reason: StockMovementReason): string => {
|
|
|
|
return `app.admin.store.stock_movement_reason.${reason}`;
|
|
|
|
};
|
2022-09-07 17:28:41 +02:00
|
|
|
}
|