-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshipment-receipts.js
83 lines (67 loc) · 2.7 KB
/
shipment-receipts.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
'use strict';
import bsi from './bsi/module.js';
import dynamo from './dynamo/module.js';
import got from 'got';
import { SecretsManager } from '@aws-sdk/client-secrets-manager';
const secretClient = new SecretsManager();
var receiptModule = (() => {
async function getCdrSecrets() {
return secretClient.getSecretValue({SecretId: process.env.CDR_SECRET});
}
async function createCdrRequest(receipt) {
let data = await getCdrSecrets();
var secrets = JSON.parse(data.SecretString);
const url = process.env.CDR_BASE_URL + 'shippingEvent/' + receipt.shipmentId;
let response = await got.post(url, {
json: receipt,
username: secrets.username,
password: secrets.password,
throwHttpErrors: false,
});
return response.statusCode;
}
return {
pullRecentChanges: async () => {
await bsi.login();
var lastUpdated = await dynamo.getLatest('Shipment Receipt');
var receipts = await bsi.receipts.getUpdated(lastUpdated.lastModified);
var filteredReceipts = await dynamo.receipts.filter(receipts.rows);
for (let index = 0; index < filteredReceipts.length; index++) {
var receipt = await bsi.receipts.get(filteredReceipts[index]);
if (lastUpdated.lastModified < receipt.lastModified) {
lastUpdated.lastModified = receipt.lastModified;
}
await dynamo.receipts.update(receipt);
};
await dynamo.updateLatest(lastUpdated);
await bsi.logoff();
},
rebuild: async (shipmentId) => {
await bsi.login();
var receipt = await bsi.receipts.get(shipmentId);
await dynamo.receipts.update(receipt);
await bsi.logoff();
},
dynamoToJson: (data) => {
return dynamo.toJson(data);
},
sync: async (receipt) => {
console.log("Syncing " + receipt.shipmentId + " to CDR.");
var statusCode = await createCdrRequest(receipt);
console.log("Recording status code " + statusCode + ".");
return dynamo.receipts.updateSync({
shipmentId: receipt.shipmentId,
lastModified: receipt.lastModified,
lastSynced: new Date().toISOString(),
syncResult: statusCode
});
},
get: async (shipmentId) => {
return dynamo.receipts.get(shipmentId);
},
getSync: async (shipmentId) => {
return dynamo.receipts.getSync(shipmentId);
}
};
})();
export default receiptModule;