This repository has been archived by the owner on Aug 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.js
292 lines (256 loc) · 9.44 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
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
const AWS = require("aws-sdk");
var thisRunDate = new Date(Date.now()).toISOString()
const axios = require('axios')
if (process.env.AWS_SAM_LOCAL) {
// To use local DynamoDB
console.log("Local environment detected")
AWS.config.update({
endpoint: "http://localhost:8000",
region: "eu-central-1"
});
}
const dynamo = new AWS.DynamoDB();
exports.lambdaHandler = async (event, context) => {
// Default Response. This is an asynchronous function.
// caller do not wait for it to finish processing
const response = {
'statusCode': 200,
'body': JSON.stringify({
message: 'get ByD objects Started'
})
}
try {
//Retrieve configuration from DynamoDB
thisRunDate = new Date(Date.now()).toISOString()
const data = await getConfig()
console.log("Config Loaded from DynamoDB")
//Add more objects if needed
const getBydObjectsPromises = [ SalesInvoices(data.lastRun.S),
Customers(data.lastRun.S),
SalesOrders(data.lastRun.S),
ServiceOrders(data.lastRun.S)]
await Promise.all(getBydObjectsPromises) //Retrieve delta from ByD Objects
.then(prepareSnsPromises) //Prepare msgs for publishing
.then(publishSNSMessage) //Publish Messages
.then(updateLastRun)
.then(() => {
console.log("All done!!")
});
} catch (err) {
console.error(err);
}
return response
};
let getConfig = function () {
// Returns Configuration Record from DynamoDB
return new Promise(function (resolve, reject) {
const params = {
Key: {
configId: {
"N": process.env.CONFIG_ID
}
},
TableName: process.env.CONFIG_TABLE,
};
//Workaround while LOCAL Dynamo isn't ready
if (process.env.AWS_SAM_LOCAL) {
var response = {
Item: {
lastRun: {
S: '2020-09-13T09:31:06.393Z'
},
configId: {
N: '0'
}
}
}
resolve(response.Item)
}
dynamo.getItem(params).promise()
.then(function (data) {
resolve(data.Item)
})
.catch(function (error) {
console.error("error loading from dynamo" + error)
reject(new Error("Error Loading Condiguration! - " + error))
})
})
}
let SalesInvoices = function (lastRun) {
// Returns Invoices from ByD
return new Promise(function (resolve, reject) {
console.log("Retrieving ByD Invoices")
getBydObject(lastRun, process.env.BYD_INVOICES, process.env.BYD_INVOICES_ID).then((data) => {
console.log(data.length + "Invoices Retrieved")
resolve(data)
})
})
}
let Customers = function (lastRun) {
// Returns Customers from BYD
return new Promise(function (resolve, reject) {
console.log("Retrieving ByD Customers")
getBydObject(lastRun, process.env.BYD_CUSTOMERS, process.env.BYD_CUSTOMERS_ID).then((data) => {
console.log(data.length + "Customers Retrieved")
resolve(data)
})
})
}
let SalesOrders = function (lastRun) {
// Returns Customers from BYD
return new Promise(function (resolve, reject) {
console.log("Retrieving ByD Sales Orders")
getBydObject(lastRun, process.env.BYD_SALESORDERS, process.env.BYD_SALESORDERS_ID).then((data) => {
console.log(data.length + "ByD Sales Orders Retrieved")
resolve(data)
})
})
}
let ServiceOrders = function (lastRun) {
// Returns Customers from BYD
return new Promise(function (resolve, reject) {
console.log("Retrieving ByD Service Orders")
getBydObject(lastRun, process.env.BYD_SERVICESORDERS, process.env.BYD_SERVICEORDERS_ID).then((data) => {
console.log(data.length + "ByD Service Orders Retrieved")
resolve(data)
})
})
}
let getBydObject = function (lastRun, endpoint, idAttribute, additionalAttributes) {
return new Promise(function (resolve, reject) {
console.log("Preparing request to " + endpoint)
var params = new URLSearchParams({
"$format": "json",
"$select": idAttribute + ",ObjectID,CreationDateTime,LastChangeDateTime",
"$filter": "LastChangeDateTime ge datetimeoffset" + quotes(lastRun)
})
const options = {
method: "GET",
baseURL: process.env.BYD_ODATA,
url: endpoint,
headers: {
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": "Basic " + process.env.BYD_AUTH,
"x-csrf-token": "fetch"
},
params: params
}
// console.debug(options)
axios.request(options).then((response) => {
console.log(`ByD Response: is ${response.status} - ${response.statusText}`)
if (response.statusCode < 200 || response.statusCode >= 300) {
return reject(
new Error(`${response.statusCode}: ${response.req.getHeader("host")} ${response.req.path}`)
);
} else {
var formatedData = []
response.data.d.results.forEach(function (elem) {
element = formatData(elem, idAttribute, additionalAttributes)
if (element) {
formatedData.push(element)
}
})
// console.debug(`Data ${JSON.stringify(formatedData)}`)
return resolve(formatedData)
}
})
.catch((err) => {
console.error("Error calling ByD -" + err)
reject(new Error(err));
})
})
}
function quotes(val) {
return "%27" + val + "%27";
}
function formatData(elem, idAttribute, additionalAttributes) {
try {
const updated = elem.CreationDateTime == elem.LastChangeDateTime ? false : true //If dates are the same the item was created
var element = elem
element.GenericId = elem[idAttribute]
element.Updated = updated
element.GenericType = elem.__metadata.type.split('.')[1]
element.DateStr = updated ? BydTimestampToHumanDate(elem.LastChangeDateTime) : BydTimestampToHumanDate(elem.CreationDateTime)
delete element['__metadata']
return element
} catch (error) {
console.error("Error formating data")
console.error(error)
return null
}
}
function BydTimestampToHumanDate(bydDate) {
try {
//DateFormat is /Date(1600183555000)/
const timestamp = bydDate.substring(bydDate.lastIndexOf("(") + 1, bydDate.lastIndexOf(")"));
const humanDate = new Date(timestamp * 1)
return humanDate
} catch (error) {
console.error("Error formating date to human readable")
console.error(error)
return null
}
}
let prepareSnsPromises = (bydData) => {
return new Promise((resolve) => {
// Create publish parameters
var SNSMessages = []
//Prepare all SNS promises calls
bydData.forEach(function (object) {
if (object[0]) {
console.log("Preparing messages for " + object[0].GenericType)
object.forEach(function (instance) {
var params = {
Message: JSON.stringify(instance),
TopicArn: process.env.SNS_TOPIC
};
SNSMessages.push(new AWS.SNS().publish(params).promise())
})
}
})
console.log(SNSMessages.length + " Messages ready to send to SNS")
resolve(SNSMessages)
})
}
let publishSNSMessage = (SNSMessages) => {
return new Promise(async (resolve, reject) => {
console.log("CALLING PROMISES ALL for " + SNSMessages.length + "Messages")
await Promise.all(SNSMessages).then((retPromises) => {
console.log("All messages published!")
console.log(retPromises)
resolve()
})
.catch((e) => {
console.error("Error sending publishing to SNS")
console.error(e)
reject()
})
})
}
let updateLastRun = function () {
// Update DynamoDB with the Date of the Last Run
return new Promise(function (resolve, reject) {
const params = {
Item: {
configId: {
"N": process.env.CONFIG_ID
},
lastRun: {
"S": thisRunDate
}
},
TableName: process.env.CONFIG_TABLE,
};
console.log("Updating LastRun on Dynamo")
dynamo.putItem(params).promise()
.then(function (data) {
console.log("Last Run updated on DynamoDB " + thisRunDate)
resolve()
})
.catch(function (error) {
console.error("error storing last run on DynamoDB" + error)
reject(new Error(error))
})
})
}