| Current Path : /var/node/inatote/Inatote-Backend/categories/ |
| Current File : /var/node/inatote/Inatote-Backend/categories/category.js |
import requiredParam from '../helpers/required-param.js'
import { InvalidPropertyError } from '../helpers/errors.js'
import upperFirst from '../helpers/upper-first.js'
export default function makeCategory(categoryInfo = requiredParam('categoryInfo')) {
const validorder = validate(categoryInfo)
const normalorder = normalize(validorder)
return Object.freeze(normalorder)
function validate({
vendorID = requiredParam('vendorID'),
categoryName = requiredParam('categoryName'),
products = requiredParam('products'),
...otherInfo
} = {}) {
validateName("Category", categoryName);
return { vendorID, categoryName, products, ...otherInfo }
function validateName(label, name) {
if (name.length < 2) {
throw new InvalidPropertyError(
`A ${label} name must be at least 2 characters long.`
)
}
}
}
function normalize({ categoryName, ...otherInfo }) {
return {
...otherInfo,
categoryName: upperFirst(categoryName),
isEnabled : otherInfo.isEnabled ? otherInfo.isEnabled : false
}
}
}