-
Notifications
You must be signed in to change notification settings - Fork 3
/
point-of-sale-service.ts
310 lines (278 loc) · 9.77 KB
/
point-of-sale-service.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/**
* 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-service.
*
* @module catalogue/point-of-sale
*/
import { FindManyOptions, FindOptionsRelations, FindOptionsWhere, In, IsNull, Raw } from 'typeorm';
import {
BasePointOfSaleResponse,
PaginatedPointOfSaleResponse,
PointOfSaleResponse,
PointOfSaleWithContainersResponse,
} from '../controller/response/point-of-sale-response';
import PointOfSale from '../entity/point-of-sale/point-of-sale';
import PointOfSaleRevision from '../entity/point-of-sale/point-of-sale-revision';
import QueryFilter, { FilterMapping } from '../helpers/query-filter';
import User, { TermsOfServiceStatus, UserType } from '../entity/user/user';
import Container from '../entity/container/container';
import ContainerRevision from '../entity/container/container-revision';
// eslint-disable-next-line import/no-cycle
import ContainerService from './container-service';
import { PaginationParameters } from '../helpers/pagination';
import { CreatePointOfSaleParams, UpdatePointOfSaleParams } from '../controller/request/point-of-sale-request';
import AuthenticationService from './authentication-service';
import { ContainerWithProductsResponse } from '../controller/response/container-response';
import Role from '../entity/rbac/role';
import RBACService from './rbac-service';
/**
* Define point of sale filtering parameters used to filter query results.
*/
export interface PointOfSaleParameters {
/**
* Filter based on point of sale id.
*/
pointOfSaleId?: number;
/**
* Filter based on point of sale revision.
*/
pointOfSaleRevision?: number;
/**
* Filter based on point of sale owner.
*/
ownerId?: number;
/**
* If containers should be added to the response
*/
returnContainers?: boolean
/**
* If products should be added to the response
*/
returnProducts?: boolean
/**
* Whether to select public points of sale.
*/
public?: boolean;
}
export default class PointOfSaleService {
public static revisionToBaseResponse(revision: PointOfSaleRevision): BasePointOfSaleResponse {
return {
id: revision.pointOfSaleId,
name: revision.name,
};
}
/**
* Transforms a point of sale revision into a response.
* @param revision
* @private
*/
public static revisionToResponse(revision: PointOfSaleRevision): PointOfSaleResponse | PointOfSaleWithContainersResponse {
const response: PointOfSaleResponse = {
...PointOfSaleService.revisionToBaseResponse(revision),
createdAt: revision.pointOfSale.createdAt.toISOString(),
updatedAt: revision.pointOfSale.updatedAt.toISOString(),
revision: revision.revision,
useAuthentication: revision.useAuthentication,
owner: {
id: revision.pointOfSale.owner.id,
firstName: revision.pointOfSale.owner.firstName,
lastName: revision.pointOfSale.owner.lastName,
},
cashierRoles: revision.pointOfSale.cashierRoles.map((r) => RBACService.asRoleResponse(r)),
};
if (revision.containers) {
return {
...response,
containers: revision.containers.map((c) => ContainerService.revisionToResponse(c)) as ContainerWithProductsResponse[],
};
}
return response;
}
/**
* Raw sql to deal with current revision.
* @param revision
*/
public static revisionSubQuery(revision?: number): string {
if (revision) return `${revision}`;
return PointOfSale
.getRepository()
.createQueryBuilder('pos')
.select('pos.currentRevision')
.where('pos.id = PointOfSaleRevision.pointOfSaleId').getSql();
}
/**
* Query to return current point of sales.
* @param filters - Parameters to query the point of sales with.
* @param pagination
* @param user
*/
public static async getPointsOfSale(
filters: PointOfSaleParameters = {}, pagination: PaginationParameters = {}, user?: User,
): Promise<PaginatedPointOfSaleResponse> {
const { take, skip } = pagination;
const [data, count] = await PointOfSaleRevision.findAndCount({ ...(await this.getOptions(filters, user)), take, skip });
const records = data.map((revision: PointOfSaleRevision) => this.revisionToResponse(revision));
return {
_pagination: {
take, skip, count,
},
records,
};
}
/**
* Updates a PointOfSale
* @param update - The update to apply
*/
public static async updatePointOfSale(update: UpdatePointOfSaleParams): Promise<PointOfSaleWithContainersResponse> {
const base = await PointOfSale.findOne({ where: { id: update.id } });
const containers = await Container.findByIds(update.containers);
const containerIds: { revision: number, container: { id: number } }[] = (
containers.map((container) => (
({ revision: container.currentRevision, container: { id: container.id } }))));
const containerRevisions: ContainerRevision[] = await ContainerRevision.findByIds(containerIds);
const pointOfSaleRevision: PointOfSaleRevision = Object.assign(new PointOfSaleRevision(), {
pointOfSale: base,
containers: containerRevisions,
name: update.name,
// Increment revision.
revision: base.currentRevision ? base.currentRevision + 1 : 1,
useAuthentication: update.useAuthentication,
});
// First save revision.
await PointOfSaleRevision.save(pointOfSaleRevision);
// Set roles
base.cashierRoles = await Role.find({ where: { id: In(update.cashierRoleIds ?? []) } });
// Increment current revision.
// eslint-disable-next-line no-param-reassign
base.currentRevision = base.currentRevision ? base.currentRevision + 1 : 1;
await PointOfSale.save(base);
const options = await this.getOptions({ pointOfSaleId: base.id, returnContainers: true, returnProducts: true });
return (this.revisionToResponse(await PointOfSaleRevision.findOne({ ...options }))) as PointOfSaleWithContainersResponse;
}
/**
* Creates a new PointOfSale
*
* @param posRequest - The POS to be created.
*/
public static async createPointOfSale(posRequest: CreatePointOfSaleParams) {
const owner = await User.findOne({ where: { id: posRequest.ownerId } });
if (!owner) return undefined;
const posUser = await User.save({
firstName: 'Point of Sale',
type: UserType.POINT_OF_SALE,
active: true,
acceptedToS: TermsOfServiceStatus.NOT_REQUIRED,
});
const base = Object.assign(new PointOfSale(), {
owner,
user: posUser,
});
// Save the base and update.
await base.save();
const update: UpdatePointOfSaleParams = {
...posRequest,
id: base.id,
};
return this.updatePointOfSale(update);
}
/**
* (Soft) delete a point of sale
* @param pointOfSaleId
*/
public static async deletePointOfSale(pointOfSaleId: number): Promise<void> {
const pointOfSale = await PointOfSale.findOne({ where: { id: pointOfSaleId } });
if (pointOfSale == null) {
throw new Error('Point of sale not found');
}
await PointOfSale.softRemove(pointOfSale);
}
/**
* Test to see if the user can view a specified Point of Sale
* @param userId - The User to test
* @param pointOfSale - The Point of Sale to view
*/
public static async canViewPointOfSale(userId: number, pointOfSale: PointOfSale)
: Promise<boolean> {
if (!pointOfSale) return false;
return pointOfSale.owner.id === userId;
}
public static async getOptions(params: PointOfSaleParameters, user?: User): Promise<FindManyOptions<PointOfSaleRevision>> {
const filterMapping: FilterMapping = {
pointOfSaleId: 'pointOfSaleId',
};
const relations: FindOptionsRelations<PointOfSaleRevision> = {
pointOfSale: {
owner: true,
cashierRoles: true,
},
};
if (params.returnContainers) {
relations.containers = {
container: {
owner: true,
},
};
if (params.returnProducts) {
relations.containers.products = {
product: {
image: true,
owner: true,
},
category: true,
vat: true,
};
}
}
let revisionFilter: any = {};
revisionFilter.revision = Raw(alias => `${alias} = (${this.revisionSubQuery(params.pointOfSaleRevision)})`);
let owner: FindOptionsWhere<User> = {};
if (user) {
const organIds = (await new AuthenticationService().getMemberAuthenticators(user)).map((u) => u.id);
owner = { id: In(organIds) };
} else if (params.ownerId) {
owner = { id: params.ownerId };
}
let where: FindOptionsWhere<PointOfSaleRevision> = {
...QueryFilter.createFilterWhereClause(filterMapping, params),
...revisionFilter,
pointOfSale: {
deletedAt: IsNull(),
owner,
},
containers: {
container: {
deletedAt: IsNull(),
},
products: {
product: {
deletedAt: IsNull(),
},
},
},
};
const options: FindManyOptions<PointOfSaleRevision> = {
where,
order: { createdAt: 'ASC' },
withDeleted: true,
};
return { ...options, relations };
}
}