-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrorHandling.js
More file actions
40 lines (32 loc) · 1.24 KB
/
errorHandling.js
File metadata and controls
40 lines (32 loc) · 1.24 KB
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
const moment = require("moment");
const {Transaction} = require("./classes");
const log4js = require("log4js");
const logger = log4js.getLogger('program.js');
const isDateValid = (d) => moment(d, 'DD/MM/YYYY', true).isValid();
const isAmountValid = (a) => {
if (!parseFloat(a) || a.toString().split('.')[1].length !== 2) {
return false
}
return true
}
const checkTransactions = (transactions) => {
transactions.forEach(transaction => {
try {
isDateValid(transaction.date);
parseFloat(transaction.amount);
if (!isDateValid(transaction.date)) {
logger.error(`Date is not in a valid format for transaction dated ${transaction.date}`)
} else if (!isAmountValid(transaction.amount)) {
logger.error(`Please provide a suitable value for field 'amount' for transaction
dated ${transaction.date}. Note that this must be a number with two decimal places`)
return
} else {
return transactions
}
} catch (err) {
logger.error(err);
return
}
})
}
module.exports = {isDateValid, isAmountValid, checkTransactions}