-
Notifications
You must be signed in to change notification settings - Fork 0
/
job.js
88 lines (77 loc) · 2.42 KB
/
job.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
import fs from "node:fs";
import rawJson from "./data.json" assert { type: "json" };
const aggregateData = {};
let keys;
let result = [];
// aggregate totals
rawJson.forEach((item) => {
const {
"Grant Code": incomingGrantCode,
"Transaction Month": postingMonth,
"Transaction Year": postingYear,
"Project Code": projectCode,
"Cost Center": costCenter,
Currency: currency,
"Sum of Actual": sumOfActual,
Unit: unit,
} = item;
const grantCode = incomingGrantCode.replace(/(ME)$/, "_default");
const externalId = `${grantCode}_${projectCode}_${costCenter}_${postingMonth}_${postingYear}_${unit}`;
if (!aggregateData[externalId]) {
aggregateData[externalId] = {
grantCode: grantCode,
projectCode,
costCenter,
postingMonth,
postingYear,
unit,
currency,
sumOfActual: 0,
};
}
if (!keys) {
keys = Object.keys(aggregateData[externalId]);
}
aggregateData[externalId].sumOfActual += parseFloat(sumOfActual);
});
// now convert to the intermediate state
// TODO we could save more data if we aggregate as objects under grantCode, save repeating that key
const minified = {
keys,
data: Object.values(aggregateData).map(Object.values),
};
// output the converted data
fs.writeFileSync("./out/converted.json", JSON.stringify(minified));
// now read the data back in, we're in salesforce land now
// re-assemble the data
minified.data.forEach((item) => {
const obj = item.reduce((acc, value, idx) => {
acc[keys[idx]] = value;
return acc;
}, {});
const {
grantCode,
projectCode,
costCenter,
postingMonth,
postingYear,
unit,
currency,
sumOfActual,
} = obj;
const externalId = `${grantCode}_${projectCode}_${costCenter}_${postingMonth}_${postingYear}_${unit}`;
const mapped = {
ExternalId__c: externalId,
ampi__Type__c: "Expenditure",
Name: `${grantCode}_${projectCode}_${costCenter}_${postingMonth}_${postingYear}`,
"ampi__Budget__r.External_Id__c": `${grantCode}_${postingYear}_${unit}`,
"ampi__Reporting_Period__r.External_Id__c": `${grantCode}_${postingYear}_${postingMonth}_Monthly`,
Local_Currency__c: currency,
Amount_Actual_Local_Currency__c: sumOfActual,
"Project_Code__r.Name": projectCode,
"Implementing_Partner__r.BC_Cost_Center__c": costCenter,
};
result.push(mapped);
});
// dump the final converted data
fs.writeFileSync("./out/result.json", JSON.stringify(result));