-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtransaction-service.ts
879 lines (777 loc) · 32.4 KB
/
transaction-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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
/**
* 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 transaction-service.
*
* @module transactions
*/
import { Brackets, In, IsNull, SelectQueryBuilder } from 'typeorm';
import dinero from 'dinero.js';
import { RequestWithToken } from '../middleware/token-middleware';
import {
BaseTransactionResponse,
PaginatedBaseTransactionResponse,
SubTransactionResponse,
SubTransactionRowResponse,
TransactionResponse,
} from '../controller/response/transaction-response';
import Transaction from '../entity/transactions/transaction';
import SubTransaction from '../entity/transactions/sub-transaction';
import DineroTransformer from '../entity/transformer/dinero-transformer';
import {
parseContainerToBaseResponse,
parsePOSToBasePOS,
parseProductToBaseResponse,
parseUserToBaseResponse,
parseVatGroupToResponse,
} from '../helpers/revision-to-response';
import QueryFilter, { FilterMapping } from '../helpers/query-filter';
import {
SubTransactionRequest,
SubTransactionRowRequest,
TransactionRequest,
} from '../controller/request/transaction-request';
import User, { TermsOfServiceStatus, UserType } from '../entity/user/user';
import ContainerRevision from '../entity/container/container-revision';
import SubTransactionRow from '../entity/transactions/sub-transaction-row';
import ProductRevision from '../entity/product/product-revision';
import PointOfSaleRevision from '../entity/point-of-sale/point-of-sale-revision';
import { DineroObjectRequest } from '../controller/request/dinero-request';
import { DineroObjectResponse } from '../controller/response/dinero-response';
import BalanceService from './balance-service';
import { asBoolean, asDate, asNumber } from '../helpers/validators';
import { PaginationParameters } from '../helpers/pagination';
import { toMySQLString, utcToDate } from '../helpers/timestamps';
import {
TransactionReport,
TransactionReportCategoryEntryResponse,
TransactionReportData,
TransactionReportDataResponse,
TransactionReportEntryResponse,
TransactionReportResponse,
TransactionReportVatEntryResponse,
} from '../controller/response/transaction-report-response';
import {
collectProductsByCategory,
collectProductsByRevision,
collectProductsByVat,
reduceMapToCategoryEntries,
reduceMapToReportEntries,
reduceMapToVatEntries,
} from '../helpers/transaction-mapper';
import ProductCategoryService from './product-category-service';
import WithManager from '../database/with-manager';
export interface TransactionFilterParameters {
transactionId?: number | number[],
fromId?: number,
createdById?: number,
toId?: number,
exclusiveToId?: boolean,
pointOfSaleId?: number,
pointOfSaleRevision?: number,
containerId?: number,
containerRevision?: number,
productId?: number,
productRevision?: number,
fromDate?: Date,
tillDate?: Date,
invoiceId?: number,
}
export function parseGetTransactionsFilters(req: RequestWithToken): TransactionFilterParameters {
if ((req.query.pointOfSaleRevision && !req.query.pointOfSaleId)
|| (req.query.containerRevision && !req.query.containerId)
|| (req.query.productRevision && !req.query.productId)) {
throw new Error('Cannot filter on a revision, when there is no id given');
}
const filters: TransactionFilterParameters = {
// TODO Make this work on arrays
transactionId: asNumber(req.query.transactionId),
fromId: asNumber(req.query.fromId),
createdById: asNumber(req.query.createdById),
toId: asNumber(req.query.toId),
exclusiveToId: req.query.exclusiveToId ? asBoolean(req.query.exclusiveToId) : true,
pointOfSaleId: asNumber(req.query.pointOfSaleId),
pointOfSaleRevision: asNumber(req.query.pointOfSaleRevision),
containerId: asNumber(req.query.containerId),
containerRevision: asNumber(req.query.containerRevision),
productId: asNumber(req.query.productId),
productRevision: asNumber(req.query.productRevision),
fromDate: asDate(req.query.fromDate),
tillDate: asDate(req.query.tillDate),
invoiceId: asNumber(req.query.invoiceId),
};
return filters;
}
export default class TransactionService extends WithManager {
/**
* Gets total cost of a transaction with values stored in the database
* @returns {DineroObject.model} - the total cost of a transaction
* @param rows
*/
public async getTotalCost(rows: SubTransactionRowRequest[]): Promise<Dinero.Dinero> {
// get costs of individual rows
const rowCosts = await Promise.all(rows.map(async (row) => {
const rowCost = await this.manager.findOne(ProductRevision, {
where: {
revision: row.product.revision,
product: { id: row.product.id },
},
}).then((product) => product.priceInclVat.multiply(row.amount));
return rowCost;
}));
// sum the costs
return rowCosts.reduce((total, current) => total.add(current), dinero({ amount: 0 }));
}
/**
* Verifies whether a user has a sufficient balance to complete the transaction
* @param {TransactionRequest.model} req - the transaction request to verify
* @returns {boolean} - whether user's balance is ok or not
*/
public async verifyBalance(req: TransactionRequest): Promise<boolean> {
const rows: SubTransactionRowRequest[] = [];
req.subTransactions.forEach((sub) => sub.subTransactionRows.forEach((row) => rows.push(row)));
// check whether from user has sufficient balance
const totalCost = await this.getTotalCost(rows);
// get user balance and compare
const userBalance = dinero((await new BalanceService().getBalance(req.from)).amount);
// return whether user balance is sufficient to complete the transaction
return userBalance.greaterThanOrEqual(totalCost);
}
/**
* Tests whether a dinero request equals a dinero object
* @param {DineroObjectRequest.model} req - dinero request
* @param {DineroObject.model} din - dinero object
* @returns {boolean} - equality of the parameters
*/
public static dineroEq(req: DineroObjectRequest, din: Dinero.Dinero): boolean {
const price = dinero(req);
return price.equalsTo(din);
}
/**
* Verifies whether a sub transaction row within a sub transaction is valid
* @param {SubTransactionRowRequest.model} req - the sub transaction row request to verify
* @param container
* @returns {boolean} - whether sub transaction row is ok or not
*/
public async verifySubTransactionRow(
req: SubTransactionRowRequest, container: ContainerRevision,
): Promise<boolean> {
// check if fields provided in subtransactionrow
if (!req.product || !req.totalPriceInclVat
|| !req.amount || req.amount <= 0 || !Number.isInteger(req.amount)) {
return false;
}
// check if product is in the container
if (!container.products.some((product) => product.product
&& product.product.deletedAt == null
&& product.product.id === req.product.id
&& product.revision === req.product.revision)) {
return false;
}
// check if product exists
const product = await this.manager.findOne(ProductRevision, {
where: {
revision: req.product.revision,
product: { id: req.product.id, deletedAt: IsNull() },
},
relations: ['product'],
});
if (!product) {
return false;
}
// check whether the request price corresponds to the database price
const cost = await this.getTotalCost([req]);
return TransactionService.dineroEq(req.totalPriceInclVat, cost);
}
/**
* Verifies whether a sub transaction within a transaction is valid
* @param {SubTransactionRequest.model} req - the sub transaction request to verify
* @param {PointOfSaleRevision.model} pointOfSale - the point of sale in the request
* @param isUpdate
* @returns {boolean} - whether sub transaction is ok or not
*/
public async verifySubTransaction(
req: SubTransactionRequest, pointOfSale: PointOfSaleRevision, isUpdate?: boolean,
): Promise<boolean> {
// check if fields provided in the transaction
if (!req.to || !req.container || !req.totalPriceInclVat
|| !req.subTransactionRows || req.subTransactionRows.length === 0) {
return false;
}
// check if container is in the point of sale
if (!pointOfSale.containers.some((container) => container.container && container.container.id === req.container.id
&& container.revision === req.container.revision)) {
return false;
}
// check if to user exists, check if they are active in database if the call is not an update
const user = await this.manager.findOne(User, { where: { id: req.to } });
if (!user || (!isUpdate && !user.active)) {
return false;
}
// check whether the request price corresponds to the database price
const rows: SubTransactionRowRequest[] = [];
req.subTransactionRows.forEach((row) => rows.push(row));
const cost = await this.getTotalCost(rows);
if (!TransactionService.dineroEq(req.totalPriceInclVat, cost)) {
return false;
}
// check if container exists in database and get products for subtransactionrow check
const container = await this.manager.findOne(ContainerRevision, {
where: {
revision: req.container.revision,
container: { id: req.container.id, deletedAt: IsNull() },
},
relations: ['container', 'products'],
});
if (!container) {
return false;
}
// verify subtransaction rows
const verification = await Promise.all(req.subTransactionRows.map(
async (row) => this.verifySubTransactionRow(row, container),
));
return !verification.includes(false);
}
/**
* Verifies whether a transaction is valid
* @param {TransactionRequest.model} req - the transaction request to verify
* @param isUpdate
* @returns {boolean} - whether transaction is ok or not
*/
public async verifyTransaction(req: TransactionRequest, isUpdate?: boolean):
Promise<boolean> {
// check fields provided in the transaction
if (!req.from || !req.createdBy
|| !req.subTransactions || req.subTransactions.length === 0
|| !req.pointOfSale || !req.totalPriceInclVat) {
return false;
}
// check existence of users and whether they are active
const ids: number[] = [req.from];
if (req.createdBy !== req.from) {
ids.push(req.createdBy);
}
// don't check active users if verification is done on an update
const users = await this.manager.find(User, { where: { id: In(ids) } });
if (users.length !== ids.length
|| (!isUpdate && !users.every((user) => user.active && user.acceptedToS !== TermsOfServiceStatus.NOT_ACCEPTED))) {
return false;
}
const fromUser = await this.manager.findOne(User, { where: { id: req.from } });
if (fromUser.type === UserType.ORGAN) {
return false;
}
// check whether the request price corresponds to the database price
const rows: SubTransactionRowRequest[] = [];
req.subTransactions.forEach((sub) => sub.subTransactionRows.forEach((row) => rows.push(row)));
const cost = await this.getTotalCost(rows);
if (!TransactionService.dineroEq(req.totalPriceInclVat, cost)) {
return false;
}
// check if point of sale exists in database and get containers for subtransaction check
const pointOfSale = await this.manager.findOne(PointOfSaleRevision, {
where: {
revision: req.pointOfSale.revision,
pointOfSale: { id: req.pointOfSale.id, deletedAt: IsNull() },
},
relations: ['pointOfSale', 'containers'],
});
if (!pointOfSale) {
return false;
}
// verify subtransactions
const verification = await Promise.all(req.subTransactions.map(
async (sub) => this.verifySubTransaction(sub, pointOfSale, isUpdate),
));
return !verification.includes(false);
}
/**
* Creates a transaction from a transaction request
* @param {TransactionRequest.model} req - the transaction request to cast
* @param update
* @returns {Transaction.model} - the transaction
*/
public async asTransaction(req: TransactionRequest, update?: Transaction):
Promise<Transaction | undefined> {
if (!req) {
return undefined;
}
// init transaction
const transaction = ((update) ? Object.assign(new Transaction(), {
...update,
version: update.version + 1,
updatedAt: new Date(),
}) : Object.assign(new Transaction(), {})) as Transaction;
// get users
transaction.from = await this.manager.findOne(User, { where: { id: req.from } });
transaction.createdBy = await this.manager.findOne(User, { where: { id: req.createdBy } });
// set subtransactions
transaction.subTransactions = await Promise.all(req.subTransactions.map(
async (subTransaction) => this.asSubTransaction(subTransaction),
));
// get point of sale revision
transaction.pointOfSale = await this.manager.findOne(PointOfSaleRevision, {
where: {
revision: req.pointOfSale.revision,
pointOfSale: { id: req.pointOfSale.id },
},
relations: ['pointOfSale'],
});
return transaction;
}
/**
* Creates a transaction response from a transaction
* @returns {TransactionResponse.model} - the transaction response
* @param transaction
*/
public async asTransactionResponse(transaction: Transaction):
Promise<TransactionResponse | undefined> {
if (!transaction) {
return undefined;
}
// get sub transaction rows to calculate total cost
const rows: SubTransactionRowRequest[] = [];
transaction.subTransactions.forEach(
(sub) => sub.subTransactionRows.forEach((row) => rows.push(
{
product: {
id: row.product.product.id,
revision: row.product.revision,
},
amount: row.amount,
totalPriceInclVat: undefined,
} as SubTransactionRowRequest,
)),
);
const cost = await this.getTotalCost(rows);
return {
id: transaction.id,
createdAt: transaction.createdAt.toISOString(),
updatedAt: transaction.updatedAt.toISOString(),
from: parseUserToBaseResponse(transaction.from, false),
createdBy: parseUserToBaseResponse(transaction.createdBy, false),
subTransactions: await Promise.all(transaction.subTransactions.map(
async (subTransaction) => this.asSubTransactionResponse(subTransaction),
)),
pointOfSale: parsePOSToBasePOS(transaction.pointOfSale, false),
totalPriceInclVat: { ...cost.toObject() } as DineroObjectResponse,
} as TransactionResponse;
}
/**
* Creates a sub transaction from a sub transaction request
* @param {SubTransactionRequest.model} req - the sub transaction request to cast
* @returns {SubTransaction.model} - the sub transaction
*/
public async asSubTransaction(req: SubTransactionRequest):
Promise<SubTransaction | undefined> {
if (!req) {
return undefined;
}
// the subtransaction
const subTransaction = {} as SubTransaction;
// get user
subTransaction.to = await this.manager.findOne(User, { where: { id: req.to } });
// get container revision
subTransaction.container = await this.manager.findOne(ContainerRevision, {
where: {
revision: req.container.revision,
container: { id: req.container.id },
},
});
// sub transaction rows
subTransaction.subTransactionRows = await Promise.all(req.subTransactionRows.map(
async (row) => this.asSubTransactionRow(row, subTransaction),
));
return subTransaction;
}
/**
* Creates a sub transaction response from a sub transaction
* @returns {SubTransactionResponse.model} - the sub transaction response
* @param subTransaction
*/
public async asSubTransactionResponse(subTransaction: SubTransaction):
Promise<SubTransactionResponse | undefined> {
if (!subTransaction) {
return undefined;
}
// get sub transaction rows to calculate total cost
const rows: SubTransactionRowRequest[] = [];
subTransaction.subTransactionRows.forEach((row) => rows.push(
{
product: {
id: row.product.product.id,
revision: row.product.revision,
},
amount: row.amount,
totalPriceInclVat: undefined,
} as SubTransactionRowRequest,
));
const cost = await this.getTotalCost(rows);
return {
id: subTransaction.id,
to: parseUserToBaseResponse(subTransaction.to, false),
container: parseContainerToBaseResponse(subTransaction.container, false),
subTransactionRows: subTransaction.subTransactionRows.map((row) => ({
id: row.id,
product: parseProductToBaseResponse(row.product, false),
amount: row.amount,
totalPriceInclVat: row.product.priceInclVat.multiply(row.amount).toObject(),
} as SubTransactionRowResponse)),
totalPriceInclVat: { ...cost.toObject() } as DineroObjectResponse,
} as SubTransactionResponse;
}
/**
* Creates a sub transaction row from a sub transaction row request
* @param {SubTransactionRowRequest.model} req - the sub transaction row request to cast
* @param {SubTransaction.model} subTransaction - the sub transaction to connect
* @returns {SubTransactionRow.model} - the sub transaction row
*/
public async asSubTransactionRow(
req: SubTransactionRowRequest, subTransaction: SubTransaction,
): Promise<SubTransactionRow | undefined> {
if (!req) {
return undefined;
}
const product = await this.manager.findOne(ProductRevision, {
where: {
revision: req.product.revision,
product: { id: req.product.id },
},
relations: ['vat'],
});
return { product, amount: req.amount, subTransaction } as SubTransactionRow;
}
/**
* Invalidates user balance cache
* @param {TransactionResponse.model} transaction - transaction holding users to invalidate
*/
public static async invalidateBalanceCache(transaction: TransactionResponse):
Promise<void> {
// get user ids to invalidate
const userIds = [...new Set(transaction.subTransactions.map((sub) => sub.to.id))];
if (!userIds.includes(transaction.from.id)) {
userIds.push(transaction.from.id);
}
await new BalanceService().clearBalanceCache(userIds);
}
private buildGetTransactionsQuery(
params: TransactionFilterParameters = {}, user?: User,
): SelectQueryBuilder<Transaction> {
// Extract fromDate and tillDate, as they cannot be directly passed to QueryFilter.
const { fromDate, tillDate, ...p } = params;
function applySubTransactionFilters(query: SelectQueryBuilder<any>): SelectQueryBuilder<any> {
const mapping: FilterMapping = {
transactionId: 'transaction.id',
toId: 'subTransaction.toId',
pointOfSaleId: 'transaction.pointOfSalePointOfSaleId',
pointOfSaleRevision: 'transaction.pointOfSaleRevision',
containerId: 'subTransaction.containerContainerId',
containerRevision: 'subTransaction.containerRevision',
productId: 'subTransactionRow.productProductId',
invoiceId: 'subTransactionRow.invoice',
productRevision: 'subTransactionRow.productRevision',
};
return QueryFilter.applyFilter(query, mapping, p);
}
const query = this.manager.createQueryBuilder(Transaction, 'transaction')
.addSelect((qb) => {
const subquery = qb.subQuery()
.select('sum(subTransactionRow.amount * product.priceInclVat) as value')
.from(SubTransaction, 'subTransaction')
.leftJoin('subTransaction.subTransactionRows', 'subTransactionRow')
.leftJoin('subTransactionRow.product', 'product')
.where('subTransaction.transactionId = transaction.id');
return applySubTransactionFilters(subquery);
}, 'value')
.leftJoinAndSelect('transaction.from', 'from')
.leftJoinAndSelect('transaction.createdBy', 'createdBy')
.leftJoinAndSelect('transaction.pointOfSale', 'pointOfSaleRev')
.withDeleted()
.leftJoinAndSelect('pointOfSaleRev.pointOfSale', 'pointOfSale')
.withDeleted()
.leftJoin('transaction.subTransactions', 'subTransaction')
.leftJoin('subTransaction.subTransactionRows', 'subTransactionRow')
.distinct(true);
query.orderBy({ 'transaction.createdAt': 'DESC' });
if (fromDate) query.andWhere('transaction.createdAt >= :fromDate', { fromDate: toMySQLString(fromDate) });
if (tillDate) query.andWhere('transaction.createdAt < :tillDate', { tillDate: toMySQLString(tillDate) });
const mapping = {
fromId: 'transaction.fromId',
createdById: 'transaction.createdById',
};
QueryFilter.applyFilter(query, mapping, p);
if (user) {
query.andWhere(new Brackets((qb) => {
qb.where('transaction.fromId = :userId', { userId: user.id })
.orWhere('subTransaction.toId = :userId', { userId: user.id });
}));
}
return applySubTransactionFilters(query);
}
/**
* Returns all transactions requested with the filter
* @param {TransactionFilterParameters.model} params - the filter parameters
* @param {PaginationParameters} pagination
* @param {User.model} user - A user that is involved in all transactions
* @returns {BaseTransactionResponse[]} - the transactions without sub transactions
*/
public async getTransactions(
params: TransactionFilterParameters, pagination: PaginationParameters = {}, user?: User,
): Promise<PaginatedBaseTransactionResponse> {
const { take, skip } = pagination;
const builder = this.buildGetTransactionsQuery(params, user);
const results = await Promise.all([
builder.limit(take).offset(skip).getRawMany(),
builder.getCount(),
]);
const records = results[0].map((o) => {
const value = DineroTransformer.Instance.from(o.value || 0);
const v: BaseTransactionResponse = {
id: o.transaction_id,
createdAt: utcToDate(o.transaction_createdAt).toISOString(),
updatedAt: utcToDate(o.transaction_updatedAt).toISOString(),
from: {
id: o.from_id,
createdAt: utcToDate(o.from_createdAt).toISOString(),
updatedAt: utcToDate(o.from_updatedAt).toISOString(),
firstName: o.from_firstName,
lastName: o.from_lastName,
},
createdBy: o.createdBy_id ? {
id: o.createdBy_id,
createdAt: utcToDate(o.createdBy_createdAt).toISOString(),
updatedAt: utcToDate(o.createdBy_updatedAt).toISOString(),
firstName: o.createdBy_firstName,
lastName: o.createdBy_lastName,
} : undefined,
pointOfSale: {
id: o.pointOfSale_id,
createdAt: utcToDate(o.pointOfSale_createdAt).toISOString(),
updatedAt: utcToDate(o.pointOfSaleRev_updatedAt).toISOString(),
name: o.pointOfSaleRev_name,
},
value: value.toObject(),
};
return v;
});
return {
_pagination: {
take, skip, count: results[1],
},
records,
};
}
/**
* Saves a transaction to the database, the transaction request should be verified beforehand
* @param {TransactionRequest.model} req - the transaction request to save
* @returns {Transaction.model} - the saved transaction
*/
public async createTransaction(req: TransactionRequest):
Promise<TransactionResponse | undefined> {
const transaction = await this.asTransaction(req);
await transaction.save();
// save the transaction and invalidate user balance cache
const savedTransaction = await this.asTransactionResponse(transaction);
await TransactionService.invalidateBalanceCache(savedTransaction);
// save transaction and return response
return savedTransaction;
}
/**
* Gets a single transaction from the database by id
* @param {integer} id - the id of the requested transaction
* @returns {Transaction.model} - the requested transaction transaction
*/
public async getSingleTransaction(id: number): Promise<TransactionResponse | undefined> {
const transaction = await this.manager.findOne(Transaction, {
where: { id },
relations: [
'from', 'createdBy', 'subTransactions', 'subTransactions.to', 'subTransactions.subTransactionRows',
// We query a lot here, but we will parse this later to a very simple BaseResponse
'pointOfSale', 'pointOfSale.pointOfSale',
'subTransactions.container', 'subTransactions.container.container',
'subTransactions.subTransactionRows.product', 'subTransactions.subTransactionRows.product.product',
'subTransactions.subTransactionRows.product.vat',
],
withDeleted: true,
});
return this.asTransactionResponse(transaction);
}
/**
* Updates a transaction and its user relations in the database
* @param {string} id - requested transaction id
* @param {TransactionRequest.model} req - new transaction request
* @returns {TransactionResponse.model} updated transaction
* @returns {undefined} undefined when transaction not found
*/
public async updateTransaction(id: number, req: TransactionRequest):
Promise<TransactionResponse | undefined> {
const transaction = await this.asTransaction(req, await this.manager.findOne(Transaction, { where: { id } }));
// delete old transaction
await this.deleteTransaction(id);
// save updated transaction with same id
await this.manager.save(Transaction, transaction);
// invalidate updated transaction user balance cache
const updatedTransaction = await this.getSingleTransaction(id);
await TransactionService.invalidateBalanceCache(updatedTransaction);
// return updatedTransaction;
return updatedTransaction;
}
/**
* Deletes a transaction
* @param {number} id - the id of the requested transaction
* @returns {TransactionResponse.model} - the deleted transaction
*/
public async deleteTransaction(id: number):
Promise<TransactionResponse | undefined> {
// get the transaction we should delete
const transaction = await this.getSingleTransaction(id);
await this.manager.delete(Transaction, id);
// invalidate user balance cache
await TransactionService.invalidateBalanceCache(transaction);
// return deleted transaction
return transaction;
}
/**
* Converts a transactionReport to a TransactionReportResponse
* @param transactionReport
* @private
*/
private static transactionReportToResponse(transactionReport: TransactionReport): TransactionReportResponse {
const categories: TransactionReportCategoryEntryResponse[] = [];
transactionReport.data.categories.forEach((entry) => {
const category: TransactionReportCategoryEntryResponse = {
category: ProductCategoryService.asProductCategoryResponse(entry.category),
totalExclVat: dinero({ amount : Math.round(entry.totalExclVat) }).toObject(),
totalInclVat: entry.totalInclVat.toObject() as DineroObjectResponse,
};
categories.push(category);
});
let totalExclVat = 0;
let totalInclVat = 0;
const entries: TransactionReportEntryResponse[] = [];
transactionReport.data.entries.forEach((productEntry) => {
const count = productEntry.count;
const amountInclVat = productEntry.product.priceInclVat.getAmount() * count;
const amountExclVat = amountInclVat / (1 + (productEntry.product.vat.percentage / 100));
totalInclVat += amountInclVat;
totalExclVat += amountExclVat;
const entry: TransactionReportEntryResponse = {
count,
product: parseProductToBaseResponse(productEntry.product, false),
totalExclVat: dinero({ amount: Math.round(amountExclVat) }).toObject(),
totalInclVat: dinero({ amount: amountInclVat }).toObject(),
};
entries.push(entry);
});
const vat: TransactionReportVatEntryResponse[] = [];
transactionReport.data.vat.forEach((vatEntry) => {
const entry: TransactionReportVatEntryResponse = {
totalExclVat: dinero({ amount: Math.round(vatEntry.totalExclVat) }).toObject(),
totalInclVat: vatEntry.totalInclVat.toObject(),
vat: parseVatGroupToResponse(vatEntry.vat),
};
vat.push(entry);
});
const data: TransactionReportDataResponse = {
categories,
entries,
vat,
};
return {
data,
parameters: transactionReport.parameters,
totalExclVat: dinero({ amount: Math.round(totalExclVat) }).toObject(),
totalInclVat: dinero({ amount: totalInclVat }).toObject(),
};
}
/**
* Generates a transaction report object from the given transaction filter parameters
* @param parameters - Parameters describing what should be included in the report
*/
public async getTransactionReport(parameters: TransactionFilterParameters): Promise<TransactionReport> {
let baseTransactions = (await this.getTransactions(parameters)).records;
const transactionReportData = await this.getTransactionReportData(baseTransactions, parameters.exclusiveToId ? parameters.toId : undefined);
return {
data: transactionReportData,
parameters,
};
}
/**
* Creates a transaction report response from the given parameters
* @param parameters
*/
public async getTransactionReportResponse(parameters: TransactionFilterParameters): Promise<TransactionReportResponse> {
const transactionReport = await this.getTransactionReport(parameters);
return TransactionService.transactionReportToResponse(transactionReport);
}
public async getTransactionsFromBaseTransactions(baseTransactions: BaseTransactionResponse[], dropInvoiced = true): Promise<Transaction[]> {
const ids = baseTransactions.map((t) => t.id);
let transactions = await this.manager.find(Transaction, {
where: {
id: In(ids),
},
relations: [
'subTransactions',
'from',
'subTransactions.to',
'subTransactions.subTransactionRows',
'subTransactions.subTransactionRows.product',
'subTransactions.subTransactionRows.product.category',
'subTransactions.subTransactionRows.product.product',
'subTransactions.subTransactionRows.product.vat',
'subTransactions.subTransactionRows.invoice',
],
});
// Don't consider transactions from invoice accounts
if (dropInvoiced) {
const invoiceUsers = new Set((await this.manager.find(User, { where: { type: In([UserType.INVOICE]) } })).map((u) => u.id));
transactions = transactions.filter((t) => !invoiceUsers.has(t.from.id));
}
return transactions;
}
/**
* Creates TransactionReportData for the given baseTransactions
* @param baseTransactions - Transactions to parse
* @param exclusiveToId - If not undefined it will drop all Sub transactions with a toId different from the param.
* @param dropInvoiced - If invoiced SubTransactionRows should be ignored, defaults to true
*/
public async getTransactionReportData(baseTransactions: BaseTransactionResponse[], exclusiveToId: number | undefined, dropInvoiced = true): Promise<TransactionReportData> {
const transactions = await this.getTransactionsFromBaseTransactions(baseTransactions, dropInvoiced);
const productEntryMap = new Map<string, SubTransactionRow[]>();
const vatEntryMap = new Map<number, SubTransactionRow[]>();
const categoryEntryMap = new Map<number, SubTransactionRow[]>();
let subTransactions = transactions.reduce<SubTransaction[]>((acc, cur) => acc.concat(cur.subTransactions), []);
if (exclusiveToId) {
subTransactions = subTransactions.filter((st) => st.to.id === exclusiveToId);
}
const subTransactionRows = subTransactions.reduce<SubTransactionRow[]>((acc, cur) => acc.concat(cur.subTransactionRows), []);
subTransactionRows.forEach((tSubRow) => {
if (dropInvoiced && tSubRow.invoice) return;
collectProductsByRevision(productEntryMap, tSubRow);
collectProductsByCategory(categoryEntryMap, tSubRow);
collectProductsByVat(vatEntryMap, tSubRow);
});
return {
categories: reduceMapToCategoryEntries(categoryEntryMap),
entries: reduceMapToReportEntries(productEntryMap),
vat: reduceMapToVatEntries(vatEntryMap),
};
}
}