-
Notifications
You must be signed in to change notification settings - Fork 3
/
product-request-spec.ts
96 lines (86 loc) · 3.46 KB
/
product-request-spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
* SudoSOS back-end API service.
* Copyright (C) 2024 Study association GEWIS
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* @license
*/
/**
* This is the module page of the product-request-spec.
*
* @module internal/spec/product-request-spec
*/
import CreateProductParams, { BaseProductParams } from '../product-request';
import {
Specification,
toFail,
toPass,
validateSpecification,
ValidationError,
} from '../../../helpers/specification-validation';
import ProductCategory from '../../../entity/product/product-category';
import stringSpec from './string-spec';
import { INVALID_PRODUCT_PRICE } from './validation-errors';
import VatGroup from '../../../entity/vat-group';
import { ownerIsOrgan } from './general-validators';
import { DineroObjectRequest } from '../dinero-request';
const validAlcohol = (alcoholPercentage: number) => {
if (alcoholPercentage < 0) {
return toFail(new ValidationError('Alcohol percentage must be non-negative'));
}
return toPass(alcoholPercentage);
};
const validCategory = async (categoryId: number) => {
const category = await ProductCategory.findOne({ where: { id: categoryId } });
if (!category) {
return toFail(new ValidationError(`${categoryId} is an invalid product category.`));
}
return toPass(categoryId);
};
const validVatGroup = async (vat: number) => {
const vatGroup = await VatGroup.find({ where: { id: vat } });
if (!vatGroup || vatGroup.length === 0 || vatGroup[0].deleted) {
return toFail(new ValidationError(`${vat} is an invalid VAT group.`));
}
return toPass(vat);
};
const validPrice = (priceInclVat: DineroObjectRequest) => {
if (priceInclVat.amount < 0) {
return toFail(INVALID_PRODUCT_PRICE());
}
return toPass(priceInclVat);
};
const baseProductRequestSpec: <T extends BaseProductParams>()
=> Specification<T, ValidationError> = () => [
[stringSpec(), 'name', new ValidationError('Name:')],
[[validPrice], 'priceInclVat', new ValidationError('priceInclVat:')],
[[validCategory], 'category', new ValidationError('category:')],
[[validVatGroup], 'vat', new ValidationError('vat:')],
[[validAlcohol], 'alcoholPercentage', new ValidationError('alcoholPercentage:')],
];
const createProductRequestSpec: Specification<CreateProductParams, ValidationError> = [
...baseProductRequestSpec<CreateProductParams>(),
[[ownerIsOrgan], 'ownerId', new ValidationError('ownerId:')],
];
export async function verifyProductRequest(productRequest: BaseProductParams) {
return Promise.resolve(await validateSpecification<BaseProductParams, ValidationError>(
productRequest, baseProductRequestSpec<BaseProductParams>(),
));
}
export async function verifyCreateProductRequest(productRequest: CreateProductParams) {
return Promise.resolve(await validateSpecification<CreateProductParams, ValidationError>(
productRequest, createProductRequestSpec,
));
}