-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimportJsonFile.js
38 lines (29 loc) · 993 Bytes
/
importJsonFile.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
const fs = require('fs');
const {Transaction} = require('./classes.js')
const log4js = require('log4js');
const {checkTransactions} = require("./errorHandling");
const logger = log4js.getLogger('program.js');
const importJsonFile = (fileName) => {
try {
const jsonString = fs.readFileSync(fileName, 'utf-8')
const result = JSON.parse(jsonString);
let transactions = [];
for(const t of result) {
t.Date = new Date();
const newTransaction = new Transaction(
t.Date.toLocaleDateString(),
t.FromAccount,
t.ToAccount,
t.Narrative,
t.Amount
)
transactions.push(newTransaction);
}
return transactions;
}
catch(err) {
logger.error("Unable to parse JSON, please review the file and rerun the program")
return
}
}
module.exports = {importJsonFile}