-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
69 lines (53 loc) · 2.22 KB
/
index.ts
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
import 'dotenv/config';
import fs from 'node:fs/promises';
import { promisify } from 'node:util';
import g from 'glob';
import type csvParser from 'csv-parser';
import { downloadDonorListDetail } from './ioby';
import { readCSV } from './csv';
import AirtableBase from './airtable';
const glob = promisify(g);
const downloadPath = './downloads';
const baseFilename = 'download-donor-detail-recurring';
const csvParseOptions: csvParser.Options = {
mapHeaders: ({header}) => header.toLowerCase(),
};
(async () => {
try {
// const downloadedBaseFilename = await downloadDonorListDetail({
// downloadPath,
// });
// const oldPreviousFile = (await glob(`${downloadPath}/${baseFilename}_*_previous.csv`))[0];
// await fs.rm(oldPreviousFile);
// let previousFile = (await glob(`${downloadPath}/${baseFilename}_*_current.csv`))[0];
// await fs.rename(
// previousFile,
// previousFile.replace('current', 'previous'),
// );
// previousFile = previousFile.replace('current', 'previous');
// const currentFile = `${downloadPath}/${baseFilename}_${Date.now()}_current.csv`;
// await fs.rename(
// `${downloadPath}/${downloadedBaseFilename}.csv`,
// currentFile,
// );
const previousFile = (await glob(`${downloadPath}/${baseFilename}_*_previous.csv`))[0]
const currentFile = (await glob(`${downloadPath}/${baseFilename}_*_current.csv`))[0]
const previousCSV = await readCSV(previousFile, csvParseOptions);
const currentCSV = await readCSV(currentFile, csvParseOptions);
const latestDateFromPreviousCSV = previousCSV[0].date;
const startingIndex = currentCSV.findIndex(row => row.date === latestDateFromPreviousCSV);
if (startingIndex <= 0) {
console.log('No new donation records');
process.exit(0);
}
const newRows = currentCSV.slice(0, startingIndex);
console.log(`Found ${newRows.length} new donation record(s)`);
const iobyAirtable = new AirtableBase('finance').table('ioby');
const createdRecords = await iobyAirtable.create(newRows)
console.log(`Added ${createdRecords.length} new donation record(s) to Airtable`);
process.exit(0);
} catch (error) {
console.error(error);
process.exit(1);
}
})();