-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathTransactionDisplayValues.class.ts
More file actions
115 lines (90 loc) · 3.78 KB
/
TransactionDisplayValues.class.ts
File metadata and controls
115 lines (90 loc) · 3.78 KB
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
import { Transaction } from '@models/transaction/Transaction.class'
import { penceToPoundsWithCurrency } from '@utils/currency-formatter'
import { getFriendlyStatus, Status } from '@models/transaction/types/status'
import changeCase from 'change-case'
import { ReasonFriendlyNames } from '@models/transaction/types/reason'
import { ResourceType } from '@models/transaction/types/resource-type'
import { DATE_TIME, dateTimeWithOffset, ZONED_DATE_TIME } from '@models/constants/time-formats'
export class TransactionDisplayValues {
private readonly transaction: Transaction
private readonly isRefund: boolean
private readonly isDispute: boolean
constructor(transaction: Transaction) {
this.transaction = transaction
this.isRefund = transaction.transactionType === ResourceType.REFUND
this.isDispute = transaction.transactionType === ResourceType.DISPUTE
}
get amount(): string {
return penceToPoundsWithCurrency(this.transaction.amount)
}
get netAmount(): string {
if (this.isRefund && this.transaction.state.status === Status.SUCCESS) {
return penceToPoundsWithCurrency(-this.transaction.amount)
}
if (this.isDispute && this.transaction.state.status === Status.LOST) {
return penceToPoundsWithCurrency(-this.transaction.netAmount!)
}
return this.transaction.netAmount ? penceToPoundsWithCurrency(this.transaction.netAmount) : ''
}
get totalAmount(): string {
return this.transaction.totalAmount ? penceToPoundsWithCurrency(this.transaction.totalAmount) : ''
}
get refundedAmount(): string {
return this.transaction.refundSummary
? penceToPoundsWithCurrency(this.transaction.refundSummary.amountRefunded)
: ''
}
get signedAmount(): string {
if (this.isDispute && this.transaction.state.status === Status.WON) {
return penceToPoundsWithCurrency(this.transaction.amount)
}
if (this.isRefund || this.isDispute) {
return penceToPoundsWithCurrency(-this.transaction.amount)
}
return penceToPoundsWithCurrency(this.transaction.amount)
}
get fee(): string {
return this.transaction.fee ? penceToPoundsWithCurrency(this.transaction.fee) : ''
}
get corporateCardSurcharge(): string {
return this.transaction.corporateCardSurcharge
? penceToPoundsWithCurrency(this.transaction.corporateCardSurcharge)
: ''
}
get cardNumber(): string {
return this.transaction.cardDetails ? `•••• ${this.transaction.cardDetails.lastDigitsCardNumber}` : ''
}
get createdDate(): string {
return this.transaction.createdDate.toFormat(DATE_TIME)
}
get createdDateWithOffset(): string {
return dateTimeWithOffset(this.transaction.createdDate)
}
get zonedCreatedDate(): string {
return this.transaction.createdDate.toFormat(ZONED_DATE_TIME)
}
get email(): string {
return (this.isRefund ? this.transaction.paymentDetails!.email : this.transaction.email) ?? ''
}
get paymentProvider(): string {
return changeCase.upperCaseFirst(this.transaction.paymentProvider)
}
// reference for the parent payment if refund or dispute
get paymentReference(): string {
return this.isRefund || this.isDispute ? this.transaction.paymentDetails!.reference : this.transaction.reference
}
get paymentType(): string {
return this.transaction.walletType ? changeCase.titleCase(this.transaction.walletType) : 'Card'
}
get status(): string | undefined {
return getFriendlyStatus(this.transaction.transactionType, this.transaction.state.status)
}
get evidenceDueDate(): string {
return this.transaction.evidenceDueDate ? dateTimeWithOffset(this.transaction.evidenceDueDate) : ''
}
get disputeReason(): string {
return this.transaction.reason !== undefined
? (ReasonFriendlyNames[this.transaction.reason] ?? ReasonFriendlyNames.OTHER)
: ''
}
}