-
Notifications
You must be signed in to change notification settings - Fork 3
/
container-service.ts
364 lines (322 loc) · 12 KB
/
container-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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/**
* 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/>.
*/
import { FindManyOptions, FindOptionsRelations, FindOptionsWhere, In, IsNull, Raw } from 'typeorm';
import {
BaseContainerResponse,
ContainerResponse,
ContainerWithProductsResponse,
PaginatedContainerResponse,
PaginatedContainerWithProductResponse,
} from '../controller/response/container-response';
import Container from '../entity/container/container';
import ContainerRevision from '../entity/container/container-revision';
import QueryFilter, { FilterMapping } from '../helpers/query-filter';
import ProductRevision from '../entity/product/product-revision';
import { PaginationParameters } from '../helpers/pagination';
import { CreateContainerParams, UpdateContainerParams } from '../controller/request/container-request';
import User from '../entity/user/user';
import { UpdatePointOfSaleParams } from '../controller/request/point-of-sale-request';
// eslint-disable-next-line import/no-cycle
import PointOfSaleService from './point-of-sale-service';
// eslint-disable-next-line import/no-cycle
import ProductService from './product-service';
import AuthenticationService from './authentication-service';
import PointOfSaleRevision from '../entity/point-of-sale/point-of-sale-revision';
interface ContainerVisibility {
own: boolean;
public: boolean;
}
/**
* Define container filtering parameters used to filter query results.
*/
export interface ContainerFilterParameters {
/**
* Filter based on pointOfSale id.
*/
posId?: number;
/**
* Filter based on pointOfSale revision.
*/
posRevision?: number;
/**
* Whether to select public containers.
*/
public?: boolean;
/**
* Filter based on container id.
*/
containerId?: number;
/**
* Filter based on container revision.
*/
containerRevision?: number;
/**
* Filter based on container owner.
*/
ownerId?: number;
/**
* Filter on containers related to the given user.
*/
userId?: number;
returnProducts?: boolean;
productId?: number;
returnPointsOfSale?: boolean;
}
export default class ContainerService {
public static revisionToBaseResponse(revision: ContainerRevision): BaseContainerResponse {
return {
id: revision.containerId,
name: revision.name,
};
}
public static revisionToResponse(revision: ContainerRevision): ContainerResponse | ContainerWithProductsResponse {
const response: ContainerResponse = {
id: revision.containerId,
revision: revision.revision,
name: revision.name,
createdAt: revision.createdAt.toISOString(),
updatedAt: revision.updatedAt.toISOString(),
public: revision.container.public,
owner: {
id: revision.container.owner.id,
firstName: revision.container.owner.firstName,
lastName: revision.container.owner.lastName,
},
};
if (revision.products) {
return {
...response,
products: revision.products.map((p) => ProductService.revisionToResponse(p)),
};
}
return response;
}
public static revisionSubQuery(revision?: number): string {
if (revision) return `${revision}`;
return Container
.getRepository()
.createQueryBuilder('container')
.select('container.currentRevision')
.where('`container`.`id` = `ContainerRevision`.`containerId`').getSql();
}
/**
* Query for getting all containers.
* @param filters
* @param pagination
* @param user
*/
public static async getContainers(
filters: ContainerFilterParameters = {}, pagination: PaginationParameters = {}, user?: User,
): Promise<PaginatedContainerResponse | PaginatedContainerWithProductResponse> {
const { take, skip } = pagination;
const options = await this.getOptions(filters, user);
const [data, count] = await ContainerRevision.findAndCount({ ...options, take, skip });
const records = data.map((revision) => this.revisionToResponse(revision));
return {
_pagination: {
take, skip, count,
},
records,
};
}
public static async getSingleContainer(filters: ContainerFilterParameters = {}): Promise<ContainerWithProductsResponse> {
const options = await this.getOptions(filters);
const container = await ContainerRevision.findOne({ ...options });
return this.revisionToResponse(container) as ContainerWithProductsResponse;
}
/**
* Creates a new container.
* @param container - The params that describe the container to be created.
*/
public static async createContainer(container: CreateContainerParams)
: Promise<ContainerWithProductsResponse> {
const base = Object.assign(new Container(), {
public: container.public,
owner: container.ownerId,
});
// Save the base.
await base.save();
const update: UpdateContainerParams = {
...container,
id: base.id,
};
return this.updateContainer(update);
}
/**
* Updates a container by directly creating a revision.
* @param update - The container update
*/
public static async updateContainer(update: UpdateContainerParams): Promise<ContainerWithProductsResponse> {
const base = await Container.findOne({ where: { id: update.id } });
if (base == null) throw new Error('Container not found.');
// Get the latest products
const opt = await ProductService.getOptions({});
const where = { ...opt.where, product: { id: In(update.products) } };
const productRevisions = await ProductRevision.find({ ...opt, where });
// Set base container and apply new revision.
const containerRevision: ContainerRevision = Object.assign(new ContainerRevision(), {
container: base,
products: productRevisions,
name: update.name,
// Increment revision.
revision: base.currentRevision ? base.currentRevision + 1 : 1,
});
// First save revision.
await ContainerRevision.save(containerRevision);
// Increment current revision.
// eslint-disable-next-line no-param-reassign
base.currentRevision = base.currentRevision ? base.currentRevision + 1 : 1;
// eslint-disable-next-line no-param-reassign
base.public = update.public;
await base.save();
await this.propagateContainerUpdate(base.id);
const options = await this.getOptions({ containerId: base.id, returnProducts: true });
return (this.revisionToResponse(await ContainerRevision.findOne({ ...options }))) as ContainerWithProductsResponse;
}
/**
* (Soft) delete a container
* @param containerId
*/
public static async deleteContainer(containerId: number): Promise<void> {
let options = await this.getOptions({ containerId: containerId, returnPointsOfSale: true });
const containerRevision = await ContainerRevision.findOne(options);
if (containerRevision == null) {
throw new Error('Container not found');
}
// Propagate deletion to points of sale: explicitly remove this (just deleted) container
const { pointsOfSale } = containerRevision;
pointsOfSale.forEach((p) => p.containers = p.containers.filter((c) => c.containerId !== containerId));
await this.executePropagation(pointsOfSale);
await Container.softRemove(containerRevision.container);
}
/**
* Given a set of points of sale, update those point of sale
* (for example when one of their containers are updated/deleted)
* @private
*/
private static async executePropagation(pointsOfSale: PointOfSaleRevision[]) {
// Deleted points of sale might still be given, so we filter these manually to prevent unnecessary updates
const pos = pointsOfSale.filter((p) => p.pointOfSale && p.pointOfSale.deletedAt == null);
for (const p of pos) {
const update: UpdatePointOfSaleParams = {
containers: p.containers.filter((c) => c.container.deletedAt == null).map((c: ContainerRevision) => c.container.id),
useAuthentication: p.useAuthentication,
name: p.name,
id: p.pointOfSale.id,
};
await PointOfSaleService.updatePointOfSale(update);
}
}
/**
* Propagates the container update to all point of sales.
*
* All POS that contain the previous version of this container
* will be revised to include the new revision.
*
* @param containerId - The container to propagate
*/
public static async propagateContainerUpdate(containerId: number) {
let options = await this.getOptions({ containerId: containerId, returnProducts: true, returnPointsOfSale: true });
// Get previous revision of container.
(options.where as FindOptionsWhere<ContainerRevision>).revision = Raw(alias => `${alias} = (${this.revisionSubQuery()}) - 1`);
const containerRevision = await ContainerRevision.findOne(options);
// Container is new, no need to propagate.
if (!containerRevision) return;
// Only update POS that contain previous container but are current version themselves.
const pos = containerRevision.pointsOfSale
.filter((p) => p.pointOfSale.deletedAt == null && p.revision === p.pointOfSale.currentRevision)
.filter((p, index, self) => (
index === self.findIndex((p2) => p.pointOfSale.id === p2.pointOfSale.id)));
return this.executePropagation(pos);
}
/**
* Test to see if the user can view a specified container
* @param userId - The User to test
* @param container - The container to view
*/
public static async canViewContainer(userId: number, container: Container)
: Promise<ContainerVisibility> {
const result: ContainerVisibility = { own: false, public: false };
if (!container) return result;
if (container.owner.id === userId) result.own = true;
if (container.public) result.public = true;
return result;
}
/**
* Returns the options for the query
* @param params
* @param user
*/
public static async getOptions(params: ContainerFilterParameters, user?: User): Promise<FindManyOptions<ContainerRevision>> {
const filterMapping: FilterMapping = {
containerId: 'containerId',
};
const relations: FindOptionsRelations<ContainerRevision> = {
container: {
owner: true,
},
};
if (params.returnPointsOfSale) relations.pointsOfSale = {
pointOfSale: true,
containers: true,
};
if (params.returnProducts) relations.products = {
product: {
image: true,
owner: true,
},
vat: true,
category: true,
};
let owner: FindOptionsWhere<User> = {};
if (user) {
const organIds = (await AuthenticationService.getMemberAuthenticators(user)).map((u) => u.id);
owner = { id: In(organIds) };
} else if (params.ownerId) {
owner = { id: params.ownerId };
}
let revisionFilter: any = {};
// Do not filter on revision if we are getting a specific POS
if (!params.posId && !params.posRevision) {
revisionFilter.revision = Raw(alias => `${alias} = (${this.revisionSubQuery(params.containerRevision)})`);
}
let where: FindOptionsWhere<ContainerRevision> = {
...QueryFilter.createFilterWhereClause(filterMapping, params),
...revisionFilter,
pointsOfSale: {
pointOfSaleId: params.posId,
revision: params.posRevision,
},
container: {
deletedAt: IsNull(),
owner,
},
products: {
product: {
deletedAt: IsNull(),
},
},
};
const options: FindManyOptions<ContainerRevision> = {
where,
order: { createdAt: 'ASC' },
withDeleted: true,
};
return { ...options, relations };
}
}