-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
99 lines (78 loc) · 2.79 KB
/
main.js
File metadata and controls
99 lines (78 loc) · 2.79 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
"use strict";
const fetch = require("node-fetch");
class MyEdenredCardService {
constructor() {
this._session = null;
}
async login(username, password) {
const response = await fetch(
"https://www.myedenred.pt/edenred-customer/api/authenticate/default",
{
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
userId: username,
password: password,
}),
});
const responseData = await response.json();
if ('internalCode' in responseData) {
throw `failed to login: ${responseData.internalCode} ${responseData.message.join(" ")}`;
}
this._session = {
token: responseData.data.token,
customer: responseData.data.customer,
};
}
async getCards() {
const response = await fetch(
"https://www.myedenred.pt/edenred-customer/api/protected/card/list",
{
headers: {
"Authorization": this._session.token,
},
});
const responseData = await response.json();
// TODO validate the responseData.
return responseData.data;
}
async getTransactions(cardId) {
const response = await fetch(
`https://www.myedenred.pt/edenred-customer/api/protected/card/${cardId}/accountmovement`,
{
headers: {
"Authorization": this._session.token,
},
});
const responseData = await response.json();
// TODO validate the responseData.
return {
account: responseData.data.account,
transactions: responseData.data.movementList,
};
}
}
async function main(username, password) {
const service = new MyEdenredCardService();
await service.login(username, password);
const cards = await service.getCards();
for (const card of cards) {
const transactions = await service.getTransactions(card.id);
console.log(
transactions.account.cardNumber,
transactions.account.cardHolderFirstName,
`(${transactions.account.cardHolderLastName})`,
"balance",
transactions.account.availableBalance);
for (const transaction of transactions.transactions) {
console.log(
transactions.account.cardNumber,
transaction.transactionDate,
transaction.amount.toFixed(2).toString().padStart(8, ' '),
transaction.transactionName.replace("Compra: ", ""));
}
}
}
main(process.argv[2], process.argv[3]).catch(console.error);