-
Notifications
You must be signed in to change notification settings - Fork 3
/
revision-to-response.ts
187 lines (175 loc) · 6.12 KB
/
revision-to-response.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/**
* 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 revision-to-response.
*
* @module helpers
*/
import ProductRevision from '../entity/product/product-revision';
import { BaseProductResponse } from '../controller/response/product-response';
import { BaseContainerResponse } from '../controller/response/container-response';
import ContainerRevision from '../entity/container/container-revision';
import { BasePointOfSaleResponse } from '../controller/response/point-of-sale-response';
import PointOfSaleRevision from '../entity/point-of-sale/point-of-sale-revision';
import User, { TermsOfServiceStatus, UserType } from '../entity/user/user';
import { BaseUserResponse, InvoiceUserResponse, UserResponse } from '../controller/response/user-response';
import VatGroup from '../entity/vat-group';
import { BaseVatGroupResponse } from '../controller/response/vat-group-response';
import BaseFile from '../entity/file/base-file';
import { SimpleFileResponse } from '../controller/response/simple-file-response';
import InvoiceUser from '../entity/user/invoice-user';
export function parseProductToBaseResponse(
product: ProductRevision, timestamps: boolean,
): BaseProductResponse {
return {
id: product.product.id,
name: product.name,
revision: product.revision,
priceInclVat: product.priceInclVat.toObject(),
createdAt: timestamps ? product.createdAt.toISOString() : undefined,
updatedAt: timestamps ? product.updatedAt.toISOString() : undefined,
vat: {
id: product.vat.id,
percentage: product.vat.percentage,
hidden: product.vat.hidden,
},
} as BaseProductResponse;
}
export function parseContainerToBaseResponse(
container: ContainerRevision, timestamps: boolean,
): BaseContainerResponse {
return {
id: container.container.id,
name: container.name,
revision: container.revision,
createdAt: timestamps ? container.createdAt.toISOString() : undefined,
updatedAt: timestamps ? container.updatedAt.toISOString() : undefined,
} as BaseContainerResponse;
}
export function parsePOSToBasePOS(
pos: PointOfSaleRevision, timestamps: boolean,
): BasePointOfSaleResponse {
return {
id: pos.pointOfSale.id,
name: pos.name,
createdAt: timestamps ? pos.createdAt.toISOString() : undefined,
updatedAt: timestamps ? pos.updatedAt.toISOString() : undefined,
} as BasePointOfSaleResponse;
}
/**
* Parses a raw user DB object to BaseUserResponse
* @param user - User to parse
* @param timestamps - Boolean if createdAt and UpdatedAt should be included
*/
export function parseUserToBaseResponse(user: User, timestamps: boolean): BaseUserResponse {
if (!user) return undefined;
return {
id: user.id,
firstName: user.firstName,
lastName: user.lastName,
nickname: user.nickname,
createdAt: timestamps ? user.createdAt.toISOString() : undefined,
updatedAt: timestamps ? user.updatedAt.toISOString() : undefined,
} as BaseUserResponse;
}
export function parseVatGroupToResponse(vatGroup: VatGroup): BaseVatGroupResponse {
return {
id: vatGroup.id,
percentage: vatGroup.percentage,
hidden: vatGroup.hidden,
};
}
/**
* Parses a User DB entity to a UserResponse
* @param user - User to parse
* @param timestamps - Boolean if createdAt and UpdatedAt should be included
*/
export function parseUserToResponse(user: User, timestamps = false): UserResponse {
if (!user) return undefined;
return {
...parseUserToBaseResponse(user, timestamps),
active: user.active,
deleted: user.deleted,
type: UserType[user.type],
acceptedToS: user.acceptedToS,
email: user.type === UserType.LOCAL_USER ? user.email : undefined,
extensiveDataProcessing: user.extensiveDataProcessing,
ofAge: user.ofAge,
canGoIntoDebt: user.canGoIntoDebt,
};
}
export interface RawUser {
createdAt: string,
updatedAt: string,
version: number,
id: number,
firstName: string,
lastName: string,
nickname: string,
active: number,
ofAge: number,
email: string,
deleted: number,
type: string,
acceptedToS: TermsOfServiceStatus,
extensiveDataProcessing: number,
canGoIntoDebt: number,
}
/**
* Parses a raw User Entity to a UserResponse
* @param user
* @param timestamps
*/
export function parseRawUserToResponse(user: RawUser, timestamps = false): UserResponse {
return {
id: user.id,
firstName: user.firstName,
lastName: user.lastName,
nickname: user.nickname,
createdAt: timestamps ? user.createdAt : undefined,
updatedAt: timestamps ? user.updatedAt : undefined,
active: user.active === 1,
deleted: user.deleted === 1,
type: user.type,
email: user.type === UserType.LOCAL_USER ? user.email : undefined,
acceptedToS: user.acceptedToS,
extensiveDataProcessing: user.extensiveDataProcessing === 1,
ofAge: user.ofAge === 1,
canGoIntoDebt: user.canGoIntoDebt === 1,
};
}
export function parseFileToResponse<T extends BaseFile>(file: T): SimpleFileResponse {
return {
createdBy: parseUserToResponse(file.createdBy, false),
downloadName: file.downloadName,
id: file.id,
location: file.location,
};
}
export function parseInvoiceUserToResponse(invoiceUser: InvoiceUser): InvoiceUserResponse {
return {
automatic: invoiceUser.automatic,
city: invoiceUser.city,
country: invoiceUser.country,
postalCode: invoiceUser.postalCode,
street: invoiceUser.street,
user: parseUserToBaseResponse(invoiceUser.user, false),
};
}