-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
71 lines (64 loc) · 2.5 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
'use strict'
const AWS = require('aws-sdk')
const CSV = require('csv')
const sortObject = require('sort-object')
let keysOrder = Object.keys(require('./csv-template.js'))
const DynamoDB = new AWS.DynamoDB.DocumentClient()
const S3 = new AWS.S3()
const getTableContent = function (tableName) {
return new Promise(function (resolve, reject) {
DynamoDB.scan({ TableName: tableName }, function (err, data) {
if (err != null) reject(err)
else resolve(data.Items)
})
})
}
const csvTableFormatter = function (dataType, data) {
let csvTable = []
data.forEach(function (item) {
item.type = dataType
keysOrder.forEach(function (prop) {
if (!(prop in item)) item[prop] = null
})
csvTable.push(Object.values(sortObject(item, keysOrder)))
})
return csvTable
}
const csvStringify = function (table) {
return new Promise(function (resolve, reject) {
CSV.stringify(table, {
header: true,
columns: Object.values(require('./csv-template.js'))
}, function (err, data) {
if (err != null) reject(err)
else resolve(data)
})
})
}
const csvToS3 = function (csvFile, bucketName) {
return new Promise(function (resolve, reject) {
let now = new Date()
now = ' ' + (now.getDate() < 10 ? '0' : '') + now.getDate() + '.' + (now.getMonth() < 10 ? '0' : '') + (now.getMonth() + 1) + ' ' + (now.getHours() < 10 ? '0' : '') + now.getHours() + ':' + (now.getMinutes() < 10 ? '0' : '') + now.getMinutes()
let params = {
Bucket: bucketName,
Key: csvFile.name + now + '.csv',
Body: csvFile.text
}
S3.upload(params, function (err, data) {
if (err != null) reject(err)
else resolve(data)
})
})
}
module.exports.handler = async function (event, context, callback) {
try {
let csvTable = []
csvTable = csvTable.concat(csvTableFormatter('contact', await getTableContent(process.env.CRM_CONTACTS_TABLE_ARN.split('/')[1])))
csvTable = csvTable.concat(csvTableFormatter('group', await getTableContent(process.env.CRM_GROUPS_TABLE_ARN.split('/')[1])))
csvTable = csvTable.concat(csvTableFormatter('update', await getTableContent(process.env.CRM_UPDATES_TABLE_ARN.split('/')[1])))
csvTable = csvTable.concat(csvTableFormatter('company', await getTableContent(process.env.CRM_COMPANIES_TABLE_ARN.split('/')[1])))
await csvToS3({ name: 'cloud-crm-data-backup', text: await csvStringify(csvTable) }, process.env.PLUGIN_BUCKET_ARN.split(':::')[1])
} catch (err) {
console.log('Error: ' + JSON.stringify(err))
}
}