-
Notifications
You must be signed in to change notification settings - Fork 0
/
save_incoming_gmail.js
39 lines (34 loc) · 1.04 KB
/
save_incoming_gmail.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
'use latest';
import { MongoClient } from 'mongodb';
const connectDb = (mongoDbUrl) => {
return new Promise((resolve, reject) => {
MongoClient.connect(mongoDbUrl, (err, db) => {
if (err)
reject(err);
else {
resolve(db);
}
})
})
}
module.exports = (context, cb) => {
connectDb(context.secrets.MONGO_URL)
.then(db => {
let newGmail = {
from: context.data.from,
name: context.data.name,
received: context.data.received,
subject: context.data.subject
};
db.collection('gmails').insert(newGmail, (err, result) => {
console.log('Received and saved new GMail.');
db.close();
if (err)
cb(null, { result: 'Error on saving gmail.' });
cb(null, { result: 'Ok' });
})
})
.catch(() => {
cb(null, { result: 'DB Connection Error.' });
})
};