-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.js
More file actions
299 lines (269 loc) · 11.2 KB
/
handler.js
File metadata and controls
299 lines (269 loc) · 11.2 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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
const { initialize, retriveMessageFromId,generateTextFromMail,getUserInfoFromCred, checkValidEmail, } = require("./utils/mail.js");
var aws = require('aws-sdk');
const { formatMonth, buildEmailSearchString,convertAmountStringToInt } = require("./utils/misc.js");
var { convert } = require('html-to-text');
var process = require('process')
var lambda = new aws.Lambda({
region: 'ap-south-1' //change to your region
});
let gmailList
module.exports.hello = async (event,context,callback) => {
console.log(event)
const {tokens, usedApps} =
event
// event
// {
// tokens: [
// {
// "access_token": "ya29.a0AX9GBdWJAbkOIYguze3aajZ5z4uhqf8x3UepEVr2idcMRPACWoRY7IvTa9cYaa3FK7vwof-dvpOwS_tF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YO2B5UpeyMvoc3idmmYtsqy6K8HJDB17UYg0-ei_baH0gGcZVckZh03itL_LRRtnT2WY9DCV0TNkMTLpnQi5EVs5o7FLNk1k0Xw29MENLFyhw1EPGNqPje7OsOOvAF8ZZaiha5PsZbUVMoGydeW4GLhVxTTnqseMjmGzsigJ57FsqM7zHkFC205aicKw6gVtuzj7AdIy6xCHsuDxWCJ2VZdPMaskRg8oi9YEPqH0ksVQUZZyF-lg",
// "refresh_token": "1//0gEueHFCZSCYvCgYIARAAGBASNwF-L9Ir3RYPuq4YJqOtmcGK0kmEqxzpnqSWMex4GckZlGbWfKoZKedICi0iuJKiiYcEDOR1WlU",
// "scope": "openid https://www.googleapis.com/auth/gmail.readonly https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email",
// "token_type": "Bearer"
// }
// ],
// usedApps: [ 'simpl','Flipkart Pay Later','Amazon Pay Later' ]
// }
//JSON.parse(event.body)
const searchPhrase = {
'Flipkart Pay Later': {
due: "Flipkart Pay Later statement for",
paid: "Your Flipkart Pay Later bill payment was successful",
},
ola: {
due: "you paid towards olamoney ",
paid: "Your Ola Money Postpaid payment is now due",
},
slice:{
due: "Card dues to be paid in full",
paid: "Slice your repayment successful",
},
simpl:{
due:'Your Simpl bill of is due',
paid:'has been received for your Simpl account on'
},
'Freecharge Pay Later':{
due:'Thank you for using Freecharge Pay Later. Your bill details are given below.',
paid:'Freecharge Pay Later Bill Payment Successful!'
},
'Amazon Pay Later':{
due: 'Auto repayment reminder: Amazon Pay Later repayment is due on',
paid: 'Amazon Pay Later repayment successful'
}
};
const fromTable = {
'simpl':{ from: 'simpl'},
'Flipkart Pay Later':{from: 'flipkart'},
'freecharge':{from: 'freecharge'},
'slice':{from: 'slice'},
'amazonPayLater':{from: 'slice'}
}
let billDetails = {}
usedApps.forEach(app=> billDetails[app] = {due:0,paid:0})
function calculate(dueArray, paidArray){
console.log(dueArray, paidArray)
let Month = ['January','February','March','April','May','June','July','August','September','October','November','December']
let currMonth
if(new Date().getMonth()) currMonth = Month[new Date().getMonth()-1]
else currMonth = Month[11]
// this currmonth is the previous month on which bill is generated
let total_dues = 0
dueArray.map(app=>{
//console.log('app',app)
if(app.length){
app.map(bill=>{
//console.log(Math.max(parseFloat(bill.amount_due ? bill.amount_due : 0), billDetails[bill.appName].due ))
console.log('yes',formatMonth(bill.bill_due_month) == (new Date().getMonth()+1).toString())
if(bill.bill_due_month && formatMonth(bill.bill_due_month) == (new Date().getMonth()+1).toString()){
billDetails[bill.appName] = {... billDetails[bill.appName], ...bill}
delete billDetails[bill.appName]['amount_due']
billDetails[bill.appName].due = Math.max(parseFloat(convertAmountStringToInt(bill.amount_due) ? convertAmountStringToInt(bill.amount_due) : 0), billDetails[bill.appName].due )
// console.log('11',billDetails)
}
})
}
})
console.log('bill',billDetails)
paidArray.map(app=>{
if(app.length){
app.map(bill=>{
if(bill.bill_paid_month && Object.keys(bill).length && formatMonth(bill.bill_due_month) == (new Date().getMonth()+1).toString()){
billDetails[bill.appName] = {... billDetails[bill.appName], ...bill}
delete billDetails[bill.appName]['paid_amount']
if(billDetails[bill.appName].paid != Math.max(convertAmountStringToInt(bill.paid_amount))) billDetails[bill.appName].paid += Math.max(convertAmountStringToInt(bill.paid_amount))
}
})
}
})
// console.log('bill', billDetails)
Object.entries(billDetails).map(([key, val]) => {if(val.paid) billDetails[key].due-= val.paid})
// for (const [key, value] of Object.entries(billDetails)) {
// //console.log(key,value)
// // console.log(value.paid)
// if(value.paid) billDetails[key].due-= value.paid
// // //console.log(billDetails[key].due)
// // total_dues += billDetails[key].due
// }
Object.entries(billDetails).map(([key, value])=> {
total_dues += convertAmountStringToInt(billDetails[key].due)
})
billDetails = {...billDetails, total_dues}
return billDetails
}
async function getMailReport(){
return new Promise(async(resolve, reject)=>{
try {
let res = await Promise.all(gmailList.map(gmail=> resolveGmailClients(gmail)))
//console.log('res',res)
let final_bills = {}
res.map(bill=> final_bills = {...final_bills, ...bill})
resolve(final_bills)
} catch (error) {
// console.log(error)
reject(error)
}
})
}
async function resolveGmailClients (gmail) {
return new Promise(async(resolve, reject)=>{
try {
DueMailList =
await Promise.all(
usedApps.map((appName) =>
resolveAppSpecific(gmail, searchPhrase[appName].due, appName )
)
)
PaidMailList =
await Promise.all(
usedApps.map((appName) =>
resolveAppSpecific(gmail, searchPhrase[appName].paid, appName)
)
)
let cal = calculate(DueMailList,PaidMailList)
//console.log('a',cal)
resolve(cal)
} catch (error) {
reject(error)
}
})
}
async function resolveAppSpecific(gmail,search,appName){
return new Promise(async(resolve, reject)=>{
const emails = await Promise.all(tokens.map(token=> getUserInfoFromCred(token) ))
let email = buildEmailSearchString(emails.map(email=> email.email))
// console.log('email',email)
const year = new Date().getFullYear()
const month = new Date().getMonth()
console.log('ffvff',month,year)
gmail.users.messages.list(
{
userId: "me",
q: `after:${11}/11/${year} AND ${search}`,
//q: `${search}`,
},
async (err, res) => {
//console.log(err, res)
if (err) {
console.log(err)
reject("The API returned an error: " + err);}
else {
console.log("data", res.data);
if(res.data.messages){
ans = await Promise.allSettled(res.data.messages.map(message=> resolveMessageList(gmail,message.id,appName)))
ans = ans.filter(a=> a.status != 'rejected')
ans = ans.map(a=> a.value)
// console.log('ans', ans)
resolve(ans)
}else{
resolve([])
}
// resolve(res.data);
}
// message = await retriveMessageFromId(gmail, res.data.messages[0].id)
// a = new Buffer.from(message.payload.parts[0].body.data.toString('ascii'), 'base64')
// console.log(a.toString('ascii'))
// res1.send(a)
}
);
})
}
async function resolveMessageList(gmail,messageId,appName){
return new Promise(async(resolve,reject)=>{
try {
let message = await retriveMessageFromId(gmail,messageId)
//console.log('message',message)
message = await formatMail(message, appName)
//console.log('dd', message)
resolve(message)
// if(checkValidEmail(message, fromTable[appName].from)){
// message = await formatMail(message,appName)
// //console.log(message)
// resolve(message)
// }else reject("error")
//console.log('c',message)
} catch (error) {
// console.log('error',error)
reject(error)
}
})
}
async function analyzeEmail(mail,appName){
return new Promise(async(resolve,reject)=>{
//console.log(mail)
lambda.invoke({
FunctionName: 'email-analysis-dev-hello',
Payload: JSON.stringify({'email': mail, 'appName': appName}) // pass params
}, function(error, data) {
if (error) {
console.log(error)
reject(error);
}
if(data.Payload){
console.log('lamb',JSON.parse(data.Payload))
resolve(JSON.parse(data.Payload))
}
});
})
}
async function formatMail(message,appName){
return new Promise(async(resolve,reject)=>{
try {
let a = await generateTextFromMail(message)
console.log('messagell',a)
//console.log(process.memoryUsage())
a = await analyzeEmail(a,appName)
//console.log('email',a)
resolve(a)
} catch (error) {
console.log(error)
reject(error)
}
// console.log('name',a)
//console.log('re',a)
})
}
try {
gmailList = await Promise.all(tokens.map(token=> initialize(token)))
//console.log(gmailList)
let res = await getMailReport()
let resObject = {
statusCode: 200,
res: res
}
//console.log('res')
return callback(null, resObject);
} catch (error) {
console.log(error)
//console.log('error',error)
//lambda return error here
var myErrorObj = {
"isBase64Encoded" : false,
"statusCode": 500,
"body": error
}
return callback(null, myErrorObj);
}
// Use this code if you don't use the http event with the LAMBDA-PROXY integration
// return { message: 'Go Serverless v1.0! Your function executed successfully!', event };
};