-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
164 lines (151 loc) · 4.48 KB
/
app.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
const fetch = require('node-fetch')
exports.tbank_salary_transfer = async (event, context) => {
const eventRecord = event.Records[0]
const eventName = eventRecord.eventName
const dynamodbRecord = eventRecord.dynamodb
console.log('event', eventRecord)
// If userIdentity prop exists in event.Records[0], means it's a System Delete (based off TTL attribute)
if (eventName === 'REMOVE') {
// if ('userIdentity' in eventRecord && eventName === 'REMOVE') {
// We immediately process it
const email = dynamodbRecord.OldImage.email.S
const taskName = dynamodbRecord.OldImage.task_name.S
const eventId = eventRecord.eventID
console.log('Currently running the task', taskName, 'for user', email)
if (taskName === 'tbank.salary.transfer') {
const apiKey = 'randomKey'
const payload = dynamodbRecord.OldImage
const data = JSON.stringify({
apiKey,
eventId,
payload
})
console.log(data)
const resp = await post(
// 'https://jiajian-0f4885db.localhost.run/integrations/tbank/recipe_salary_transfer',
'https://api.ourfin.tech/integrations/tbank/recipe_salary_transfer',
{
'Content-Type': 'application/json'
},
data
)
console.log(resp)
}
if (taskName === 'smartfin.aggregated_email') {
const apiKey = 'randomKey'
const payload = dynamodbRecord.OldImage
const data = JSON.stringify({
apiKey,
eventId,
payload
})
console.log(data)
const resp = await post(
// 'https://jiajian-0f4885db.localhost.run/integrations/smartfin/aggregated_email',
'https://api.ourfin.tech/integrations/smartfin/aggregated_email',
{
'Content-Type': 'application/json'
},
data
)
console.log(resp)
}
console.log('Done running the task', taskName, 'for user', email)
} else {
console.log(
'Event is',
eventName,
'and not a system delete, not running the task.'
)
}
const response = {
statusCode: 200,
body: JSON.stringify(
'{"status": 200, "message": "Lambda task has been triggered."}'
)
}
return response
}
exports.smartfin_aggregated_email = async (event, context) => {
const eventRecord = event.Records[0]
const eventName = eventRecord.eventName
const dynamodbRecord = eventRecord.dynamodb
console.log('event', eventRecord)
// If userIdentity prop exists in event.Records[0], means it's a System Delete (based off TTL attribute)
if (eventName === 'REMOVE') {
// if ('userIdentity' in eventRecord && eventName === 'REMOVE') {
// We immediately process it
const email = dynamodbRecord.OldImage.email.S
const taskName = dynamodbRecord.OldImage.task_name.S
const eventId = eventRecord.eventID
console.log('Currently running the task', taskName, 'for user', email)
if (taskName === 'smartfin.aggregated_email') {
const apiKey = 'randomKey'
const payload = dynamodbRecord.OldImage
const data = JSON.stringify({
apiKey,
eventId,
payload
})
console.log(data)
const resp = await post(
'https://api.ourfin.tech/integrations/smartfin/aggregated_email',
{
'Content-Type': 'application/json'
},
data
)
console.log(resp)
}
console.log('Done running the task', taskName, 'for user', email)
} else {
console.log(
'Event is',
eventName,
'and not a system delete, not running the task.'
)
}
const response = {
statusCode: 200,
body: JSON.stringify(
'{"status": 200, "message": "Lambda task has been triggered."}'
)
}
return response
}
const get = async (requestUrl) => {
return new Promise((resolve, reject) => {
try {
fetch(requestUrl, {
method: 'GET',
headers: {
Accept: 'application/json'
}
})
.then((response) => response.json())
.then((json) => resolve(json))
} catch (error) {
reject(error)
}
})
}
const post = async (requestUrl, headers = {}, body = '') => {
return new Promise((resolve, reject) => {
console.log(headers)
try {
fetch(requestUrl, {
method: 'POST',
headers: {
Accept: 'application/json',
...headers
},
body
})
.then((response) => response.json())
.then((json) => resolve(json))
} catch (error) {
console.log('post > error occured')
reject(error)
}
})
}