-
Notifications
You must be signed in to change notification settings - Fork 3
/
point-of-sale-request-spec.ts
88 lines (81 loc) · 3.19 KB
/
point-of-sale-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
/**
* 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 point-of-sale-request-spec.
*
* @module internal/spec/point-of-sale-request-spec
*/
import {
createArrayRule,
Specification, toFail,
toPass, validateSpecification,
ValidationError,
} from '../../../helpers/specification-validation';
import Container from '../../../entity/container/container';
import { BasePointOfSaleParams, CreatePointOfSaleParams, UpdatePointOfSaleParams } from '../point-of-sale-request';
import { ContainerParams } from '../container-request';
import stringSpec from './string-spec';
import {
INVALID_CONTAINER_ID,
} from './validation-errors';
import { rolesCannotBeSystemDefault, rolesMustExist, userMustExist } from './general-validators';
import { verifyContainerRequest } from './container-request-spec';
/**
* Tests if the given param is either a valid container ID or ContainerRequest
* @param p
*/
async function validContainerRequestOrId(p: number | ContainerParams) {
if (typeof p === 'number') {
const product = await Container.findOne({ where: { id: p } });
if (!product) return toFail(INVALID_CONTAINER_ID(p));
return toPass(p);
}
return Promise.resolve(await verifyContainerRequest(p));
}
/**
* Specification of a basePointOfSale
* Again we use a function since otherwise it tends to resuse internal ValidationErrors.
*/
const basePointOfSaleRequestSpec:<T extends BasePointOfSaleParams>() =>
Specification<T, ValidationError> = () => [
[stringSpec(), 'name', new ValidationError('Name:')],
[[createArrayRule([validContainerRequestOrId])], 'containers', new ValidationError('Containers:')],
[[rolesMustExist, rolesCannotBeSystemDefault], 'cashierRoleIds', new ValidationError('cashierRoleIds:')],
];
/**
* Specification of a createPointOfSaleRequest
*/
const createPointOfSaleRequestSpec
: () => Specification<CreatePointOfSaleParams, ValidationError> = () => [
...(basePointOfSaleRequestSpec<CreatePointOfSaleParams>()),
[[userMustExist], 'ownerId', new ValidationError('ownerId:')],
];
export async function verifyCreatePointOfSaleRequest(createPointOfSaleRequest:
CreatePointOfSaleParams) {
return Promise.resolve(await validateSpecification(
createPointOfSaleRequest, createPointOfSaleRequestSpec(),
));
}
export async function verifyUpdatePointOfSaleRequest(updatePointOfSaleRequest:
UpdatePointOfSaleParams) {
return Promise.resolve(await validateSpecification(
updatePointOfSaleRequest, basePointOfSaleRequestSpec(),
));
}