-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathproduct-service.ts
421 lines (375 loc) · 13.1 KB
/
product-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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/**
* 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-service.
*
* @module catalogue/products
*/
import {
FindManyOptions,
FindOptionsRelations,
FindOptionsWhere, In, IsNull,
Raw,
} from 'typeorm';
import { DineroObject } from 'dinero.js';
import {
BaseProductResponse,
PaginatedProductResponse,
ProductResponse,
} from '../controller/response/product-response';
import Product from '../entity/product/product';
import ProductRevision from '../entity/product/product-revision';
import DineroTransformer from '../entity/transformer/dinero-transformer';
import QueryFilter, { FilterMapping } from '../helpers/query-filter';
import User from '../entity/user/user';
import CreateProductParams, { UpdateProductParams } from '../controller/request/product-request';
import { PaginationParameters } from '../helpers/pagination';
import { RequestWithToken } from '../middleware/token-middleware';
import { asDate, asNumber } from '../helpers/validators';
// eslint-disable-next-line import/no-cycle
import ContainerService from './container-service';
import { UpdateContainerParams } from '../controller/request/container-request';
import AuthenticationService from './authentication-service';
import ContainerRevision from '../entity/container/container-revision';
/**
* Define product filtering parameters used to filter query results.
*/
export interface ProductFilterParameters {
/**
* Filter based on product id.
*/
productId?: number;
/**
* Filter based on product revision.
*/
productRevision?: number;
/**
* Filter based on product owner.
*/
ownerId?: number;
/**
* Filter based on the product category id.
*/
categoryId?: number;
// /**
// * Filter based on the product category name.
// */
// categoryName?: string;
/**
* Filter based on the VAT group id.
*/
vatGroupId?: number;
/**
* Filter based on created at attribute.
*/
createdAt?: Date;
/**
* Filter based on updated at attribute.
*/
updatedAt?: Date;
// /**
// * Filter based on product name.
// * TODO Maybe make this fuzzy? i.e, products like:
// */
// productName?: string;
/**
* Filter based on product price.
*/
priceInclVat?: number;
/**
* Filter based on alcohol percentage.
*/
alcoholPercentage?: number;
/**
* Filter on featured products
*/
featured?: boolean;
/**
* Filter on preferred products
*/
preferred?: boolean;
/**
* Filter on shown on narrowcasting screens
*/
priceList?: boolean;
returnContainers?: boolean;
}
// TODO Add filtering to get products query
export function parseGetProductFilters(req: RequestWithToken): ProductFilterParameters {
if (req.query.productRevision && !req.query.productId) {
throw new Error('Cannot filter on a revision, when there is no id given');
}
const filters: ProductFilterParameters = {
productId: asNumber(req.query.productId),
productRevision: asNumber(req.query.productRevision),
ownerId: asNumber(req.query.fromId),
categoryId: asNumber(req.query.categoryId),
// categoryName: asString(req.query.categoryName),
createdAt: asDate(req.query.createdAt),
updatedAt: asDate(req.query.updatedAt),
// productName: asString(req.query.productName),
priceInclVat: asNumber(req.query.priceInclVat),
alcoholPercentage: asNumber(req.query.alcoholPercentage),
featured: Boolean(req.query.featured),
preferred: Boolean(req.query.preferred),
priceList: Boolean(req.query.priceList),
};
return filters;
}
/**
* Wrapper for all Product related logic.
*/
export default class ProductService {
public static revisionToBaseResponse(revision: ProductRevision): BaseProductResponse {
return {
id: revision.productId,
name: revision.name,
priceInclVat: revision.priceInclVat.toObject(),
vat: {
id: revision.vat.id,
percentage: revision.vat.percentage,
hidden: revision.vat.hidden,
},
};
}
public static revisionToResponse(revision: ProductRevision): ProductResponse {
const priceInclVat = revision.priceInclVat.toObject();
const priceExclVat: DineroObject = {
...revision.priceInclVat.toObject(),
amount: Math.round(priceInclVat.amount / (1 + (revision.vat.percentage / 100))),
};
const image = revision.product?.image ? revision.product.image.downloadName : null;
return {
id: revision.product.id,
revision: revision.revision,
alcoholPercentage: parseFloat(String(revision.alcoholPercentage)),
featured: revision.featured,
preferred: revision.preferred,
priceList: revision.priceList,
category: {
id: revision.category.id,
name: revision.category.name,
},
createdAt: revision.product.createdAt.toISOString(),
updatedAt: revision.product.updatedAt.toISOString(),
owner: {
id: revision.product.owner.id,
firstName: revision.product.owner.firstName,
lastName: revision.product.owner.lastName,
},
image,
name: revision.name,
priceInclVat,
priceExclVat,
vat: {
id: revision.vat.id,
percentage: revision.vat.percentage,
hidden: revision.vat.hidden,
},
};
}
private static revisionSubQuery(revision?: number): string {
if (revision) return `${revision}`;
return Product
.getRepository()
.createQueryBuilder('product')
.select('product.currentRevision')
.where('product.id = ProductRevision.productId').getSql();
}
public static async getProducts(filters: ProductFilterParameters = {},
pagination: PaginationParameters = {}, user?: User): Promise<PaginatedProductResponse> {
const { take, skip } = pagination;
const options = await this.getOptions(filters, user);
const [data, count] = await ProductRevision.findAndCount({ ...options, take, skip });
const records = data.map((revision: ProductRevision) => this.revisionToResponse(revision));
return {
_pagination: {
take, skip, count,
},
records,
};
}
/**
* Creates a new product.
*
* If approve is false, then the newly created product resides in the
* Product table and has no revision, but it does have an updated product.
* To confirm the product the updated product has to be confirmed and a revision will be created.
*
* @param product - The product to be created.
*/
public static async createProduct(product: CreateProductParams)
: Promise<ProductResponse> {
const owner = await User.findOne({ where: { id: product.ownerId } });
if (!owner) return undefined;
const base = Object.assign(new Product(), {
owner,
});
// Save the product.
await base.save();
const update: UpdateProductParams = {
priceInclVat: product.priceInclVat,
category: product.category,
vat: product.vat,
alcoholPercentage: product.alcoholPercentage,
name: product.name,
id: base.id,
featured: product.featured,
preferred: product.preferred,
priceList: product.priceList,
};
let createdProduct: ProductResponse;
createdProduct = await this.updateProduct(update);
return createdProduct;
}
public static async updateProduct(update: UpdateProductParams) {
const base = await Product.findOne({ where: { id: update.id } });
const product = { ...base };
// Set base product, then the oldest settings and then the newest.
const productRevision: ProductRevision = Object.assign(new ProductRevision(), {
product,
// Apply the update.
...update,
// Increment revision.
revision: base.currentRevision ? base.currentRevision + 1 : 1,
// Fix dinero
priceInclVat: DineroTransformer.Instance.from(update.priceInclVat.amount),
});
// First save the revision.
await ProductRevision.save(productRevision);
// Increment current revision.
// eslint-disable-next-line no-param-reassign
base.currentRevision = base.currentRevision ? base.currentRevision + 1 : 1;
await base.save();
await this.propagateProductUpdate(base.id);
const options = await this.getOptions({ productId: base.id });
return (this.revisionToResponse(await ProductRevision.findOne({ ...options })));
}
/**
* Given a set of containers, update those containers
* (for example when one of their products are updated/deleted)
* @private
*/
private static async executePropagation(containers: ContainerRevision[]) {
// Deleted containers might still be given, so we filter these manually to prevent unnecessary updates
const cont = containers.filter((c) => c.container && c.container.deletedAt == null);
for (const c of cont) {
const update: UpdateContainerParams = {
products: c.products.map((p) => p.productId),
public: c.container.public,
name: c.name,
id: c.containerId,
};
await ContainerService.updateContainer(update);
}
}
/**
* Propagates the product update to all parent containers
*
* All containers that contain the previous version of this product
* will be revised to include the new revision.
*
* @param productId - The product to propagate
*/
public static async propagateProductUpdate(productId: number) {
let options = await this.getOptions({ productId, returnContainers: true });
// Get previous revision of container.
(options.where as FindOptionsWhere<ContainerRevision>).revision = Raw(alias => `${alias} = (${this.revisionSubQuery()}) - 1`);
const productRevision = await ProductRevision.findOne(options);
if (productRevision == null) return;
const containers = productRevision.containers
.filter((c) => c.container.deletedAt == null && c.revision === c.container.currentRevision)
.filter((c, index, self) => (
index === self.findIndex((c2) => c.container.id === c2.container.id)));
return this.executePropagation(containers);
}
/**
* (Soft) delete a product
* @param productId
*/
public static async deleteProduct(productId: number): Promise<void> {
const options = await this.getOptions({ productId, returnContainers: true });
const productRevision = await ProductRevision.findOne(options);
if (productRevision == null) {
throw new Error('Product not found!');
}
const { containers } = productRevision;
containers.forEach((c => c.products = c.products.filter((p) => p.productId !== productId)));
await this.executePropagation(containers);
await Product.softRemove(productRevision.product);
}
/**
* Returns a FindManyOptions based on the given parameters
* @param params - The parameters to filter on
* @param user - The user to filter on
*/
public static async getOptions(params: ProductFilterParameters, user?: User): Promise<FindManyOptions<ProductRevision>> {
const filterMapping: FilterMapping = {
productId: 'productId',
categoryId: 'category.id',
categoryName: 'category.name',
vatGroupId: 'vat.id',
createdAt: 'product.createdAt',
updatedAt: 'productrevision.updatedAt',
productName: 'productrevision.name',
priceInclVat: 'productrevision.priceInclVat',
alcoholPercentage: 'productrevision.alcoholpercentage',
featured: 'featured',
preferred: 'preferred',
priceList: 'priceList',
};
const relations: FindOptionsRelations<ProductRevision> = {
product: {
owner: true,
image: true,
},
vat: true,
category: true,
};
if (params.returnContainers) relations.containers = {
container: true,
products: true,
};
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 revisionFilter: any = {};
// Do not filter on revision if we are getting a specific POS
revisionFilter.revision = Raw(alias => `${alias} = (${this.revisionSubQuery(params.productRevision)})`);
let where: FindOptionsWhere<ProductRevision> = {
...QueryFilter.createFilterWhereClause(filterMapping, params),
...revisionFilter,
product: {
deletedAt: IsNull(),
owner,
},
};
const options: FindManyOptions<ProductRevision> = {
where,
order: { name: 'ASC' },
withDeleted: true,
};
return { ...options, relations };
}
}