-
Notifications
You must be signed in to change notification settings - Fork 18
/
card.ts
87 lines (74 loc) · 2.42 KB
/
card.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
import { Bill, CardTransaction } from "./models";
import { AuthType, Context } from "./context";
import { RequiresAuth } from "./utils/decorators";
export class Card {
public get context(): Context {
return this._context;
}
public constructor(private _context: Context) {}
@RequiresAuth(AuthType.CERT, AuthType.WEB)
public getFeed(): Promise<CardTransaction[]> {
return this._context.http
.request("get", "events")
.then((data) => data.events);
}
@RequiresAuth(AuthType.CERT, AuthType.WEB)
public getTransactions(): Promise<CardTransaction[]> {
return this.getFeed().then((feed) =>
feed.filter((statement) => statement.category === "transaction")
);
}
@RequiresAuth(AuthType.CERT, AuthType.WEB)
public getPayments(): Promise<any[]> {
return this.getFeed().then((feed) =>
feed.filter((statement) => statement.category === "payment")
);
}
@RequiresAuth(AuthType.CERT, AuthType.WEB)
public async getBills(options: {
getFutureBillsDetails?: boolean;
billsAfterDueDate?: Date;
}): Promise<Bill[]> {
options = { getFutureBillsDetails: false, ...options };
const data = await this._context.http.request("get", "bills_summary");
const futureBillsUrl = data._links?.future?.href;
let bills = data.bills;
if (options.getFutureBillsDetails && futureBillsUrl) {
const dataFuture = await this._context.http.request(
"get",
futureBillsUrl
);
const closedAndOpenedBills = data.bills.filter(
(bill: Bill) => bill.state !== "future"
);
bills = dataFuture.bills.concat(closedAndOpenedBills);
}
if (options.billsAfterDueDate) {
bills = bills.filter(
(bill: Bill) =>
this.parseDate(bill.summary.due_date) >=
(options.billsAfterDueDate as Date)
);
}
return await Promise.all(
bills.map((bill: Bill) => this.getBillDetails(bill))
);
}
@RequiresAuth(AuthType.CERT, AuthType.WEB)
public async getBillDetails(bill: Bill): Promise<Bill> {
const url: string = bill?._links?.self?.href ?? "";
if (!url) {
return bill;
}
const response: any = await this._context.http.request("get", url);
return response.bill;
}
private parseDate(dateStr: string): Date {
const dateParts = dateStr.split("-");
return new Date(
parseInt(dateParts[0], 10),
parseInt(dateParts[1], 10),
parseInt(dateParts[2], 10)
);
}
}