-
Notifications
You must be signed in to change notification settings - Fork 0
/
transaction.manager.ts
64 lines (54 loc) · 2.42 KB
/
transaction.manager.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
import { Posnet } from '../../posnet';
import { TransactionEndCommand, TransactionEndPayload } from '../classes/transaction/transaction-end.command';
import { TransactionInitCommand } from '../classes/transaction/transaction-init.command';
import { TransactionLineCommand, TransactionLinePayload } from '../classes/transaction/transaction-line.command';
import { TransactionPaymentCommand, TransactionPaymentPayload } from '../classes/transaction/transaction-payment.command';
import { TransactionVatBuyerCommand } from '../classes/transaction/vat/transaction-vat-buyer.command';
import { TransactionVatInitCommand, TransactionVatInitPayload } from '../classes/transaction/vat/transaction-vat-init.command';
export enum TransactionInvoiceType {
VAT = 'VAT',
SIMPLIFIED = 'SIMPLIFIED',
}
export interface Transaction {
products: TransactionLinePayload[];
payments: TransactionPaymentPayload[];
buyer?: TransactionVatInitPayload;
end: TransactionEndPayload;
invoiceType?: TransactionInvoiceType;
}
export class TransactionManager {
constructor(private posnet: Posnet) {}
validate(transaction: Transaction) {
return true;
}
async execute(transaction: Transaction) {
this.validate(transaction);
if (transaction.buyer && transaction.invoiceType === TransactionInvoiceType.VAT) {
await this.posnet.execute(new TransactionVatInitCommand(transaction.buyer));
} else {
await this.posnet.execute(new TransactionInitCommand({
blockMode: true,
}));
}
for (const product of transaction.products) {
await this.posnet.execute(new TransactionLineCommand(product));
}
if (!transaction.end.valuePaymentForms && transaction.payments.length > 0 && !transaction.buyer) {
transaction.end.valuePaymentForms = transaction.payments.reduce((acc, payment) => acc + payment.value, 0);
} else if (transaction.buyer) {
delete transaction.end.valuePaymentForms;
}
// Payment info for B2B invoice is on the buyer info
if (!transaction.buyer) {
for (const payment of transaction.payments) {
await this.posnet.execute(new TransactionPaymentCommand(payment));
}
}
if (transaction.buyer && transaction.invoiceType !== TransactionInvoiceType.VAT) {
await this.posnet.execute(new TransactionVatBuyerCommand({
nipNumber: transaction.buyer.nipNumber,
}));
}
await this.posnet.execute(new TransactionEndCommand(transaction.end));
}
}