-
Notifications
You must be signed in to change notification settings - Fork 0
/
import.js
52 lines (43 loc) · 1.81 KB
/
import.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
const { readFileSync, writeFileSync } = require('fs');
const parse = require('util').promisify(require('csv-parse'));
const {
dataColumns,
dataYears,
purchaserDepartments,
purchaseTypes
} = require('./common');
var processedData = [dataColumns];
dataYears.forEach((dataYear, i) => {
parse(readFileSync(`data/FY${dataYear.toString().slice(-2)}_contracts.csv`)).then((transactions) => {
// Remove headers and footers
transactions.shift();
if ([2014, 2017, 2018, 2020, 2021].includes(dataYear)) { transactions.pop(); }
if ([2017, 2018].includes(dataYear)) { transactions.pop(); }
transactions.forEach((transaction, i2) => {
const vendorAddress = `${transaction[5]}\n${transaction[6]}${transaction[6] ? "\n" : ""}${transaction[7]}, ${transaction[8]} ${transaction[9]}`;
const purchaseType = purchaseTypes[Number(transaction[2])];
if (purchaseType === undefined) {
console.log(transaction[2]);
}
const purchaserDepartment = purchaserDepartments[Number(transaction[0])];
if (purchaserDepartment === undefined) {
console.log(`Can't find purchaser department: ${transaction[1]}`);
}
if (dataYears.indexOf(dataYear) === dataYears.length - 1 && purchaserDepartment != transaction[1]) {
console.log(`Possible rename of purchaser department ${transaction[0]}: ${transaction[1]}`);
}
processedData.push([
dataYear,
parseFloat(transaction[10].replace(/,/g, '')),
Number(transaction[3]),
`"${transaction[4]}"`,
`"${vendorAddress}"`,
`"${purchaserDepartment}"`,
`"${purchaseType}"`
]);
if (i2 === transactions.length - 1 && i === dataYears.length - 1) {
writeFileSync('data/db.csv', processedData.map((pd) => { return pd.join(','); }).join("\n"));
}
});
});
});