-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
189 lines (175 loc) · 4.76 KB
/
index.js
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
const express = require('express');
const slashes = require('connect-slashes');
const cors = require('cors');
const { default: axios } = require('axios');
if(process.env.NODE_ENV !== "production") {
require('dotenv').config();
}
const PORT = process.env.PORT || 5000;
// Setting up the server
const app = express();
app.use(cors())
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.use(slashes(false));
app.listen(PORT, () => console.log(`Althsis server is up and running on port ${PORT}`));
const CLIENT_ID = process.env.CLIENT_ID;
const CLIENT_SECRET = process.env.CLIENT_SECRET;
app.post('/api/createConsent/:phone', (req, res) => {
const phone = req.params.phone;
const clientURL = req.body.clientURL || "https://althsis.netlify.app";
if(phone.length != 10){
return res.json({err: "invalid phone number"});
}
console.log("Setu request:", { phone, CLIENT_ID, CLIENT_SECRET, clientURL });
const CONSENT_DEADLINE = 2;
let expiryDate = new Date(Date.now() + ( 3600 * 1000 * 24 * CONSENT_DEADLINE));
axios.post(
'https://fiu-uat.setu.co/consents',
{
"Detail": {
"consentStart": `${(new Date()).toISOString()}`,
"consentExpiry": expiryDate.toISOString(),
"Customer": {
"id": `${phone}@onemoney`
},
"FIDataRange": {
"from": "2021-01-01T00:00:00Z",
"to": "2022-01-01T00:00:00Z"
},
"consentMode": "STORE",
"consentTypes": [
"TRANSACTIONS",
"PROFILE",
"SUMMARY"
],
"fetchType": "PERIODIC",
"Frequency": {
"value": 30,
"unit": "MONTH"
},
"DataFilter": [
{
"type": "TRANSACTIONAMOUNT",
"value": "5000",
"operator": ">="
}
],
"DataLife": {
"value": 1,
"unit": "MONTH"
},
"DataConsumer": {
"id": "setu-fiu-id"
},
"Purpose": {
"Category": {
"type": "string"
},
"code": "101",
"text": "Loan underwriting",
"refUri": "https://api.rebit.org.in/aa/purpose/101.xml"
},
"fiTypes": [
"DEPOSIT"
]
},
"redirectUrl": `${clientURL}/dash?fromSetu=true`,
},
{
headers: {
"x-client-id": CLIENT_ID,
"x-client-secret": CLIENT_SECRET
}
}
)
.then(setuResponse => {
// console.log("Setu response:", setuResponse);
res.json(setuResponse.data)
})
.catch(err => {
// console.log("Setu error:", err);
res.status(500).json({error: "Some issue accessing SETU APIs !!"})
});
});
app.get('/api/getConsent/:consentID', (req, res) => {
const consentID = req.params.consentID;
if(!consentID){
return res.json({err: "invalid consent ID"});
}
axios.get(
`https://fiu-uat.setu.co/consents/${consentID}`,
{
headers: {
"x-client-id": CLIENT_ID,
"x-client-secret": CLIENT_SECRET
}
}
)
.then(setuResponse => {
res.json(setuResponse.data)
})
.catch(err => {
res.json({error: err})
});
});
app.post('/api/createDataSession/:consentID', (req, res) => {
const consentID = req.params.consentID;
if(!consentID){
return res.json({err: "invalid consent ID"});
}
axios.post(
`https://fiu-uat.setu.co/sessions`,
{
"consentId": consentID,
"DataRange": {
"from": "2021-01-01T00:00:00Z",
"to": "2022-01-01T00:00:00Z"
},
"format": "json"
},
{
headers: {
"x-client-id": CLIENT_ID,
"x-client-secret": CLIENT_SECRET
}
}
)
.then(setuResponse => {
res.json(setuResponse.data)
})
.catch(err => {
res.json({error: err})
});
});
app.get('/api/getData/:dataSessionID', (req, res) => {
const dataSessionID = req.params.dataSessionID;
console.log("Get data: ", { dataSessionID, CLIENT_ID, CLIENT_SECRET });
if(!dataSessionID){
return res.json({err: "invalid consent ID"});
}
axios.get(
`https://fiu-uat.setu.co/sessions/${dataSessionID}`,
{
headers: {
"x-client-id": CLIENT_ID,
"x-client-secret": CLIENT_SECRET
}
}
)
.then(setuResponse => {
console.log("setu response", setuResponse.data);
res.json(setuResponse.data)
})
.catch(err => {
console.log("setu getData err", err);
res.status(500).json({error: "Some error fetching data"});
});
});
app.get('/responses/ping', (req, res)=>{
res.status(200).send('-- ok --');
});
// 404 pages for development
app.get('*', (req, res)=>{
res.status(404).send("API not found :( <br> ¯\\_(ツ)_/¯");
});