-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
43 lines (39 loc) · 1.29 KB
/
index.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
var fs = require("fs");
const parseStringSync = require("xml2js-parser").parseStringSync;
const fileValidation = async (fileName, pathFile) => {
let extention = fileName.split(".")[1];
if (extention === "json") {
let fileJSON = await fs.readFileSync(pathFile, "utf8");
let parseJSON = JSON.parse(fileJSON);
let transaction = await taxValidation(parseJSON);
return transaction;
} else {
let fileXML = await fs.readFileSync(pathFile, "utf8");
let convertToJSON = await parseStringSync(fileXML);
return taxValidation(convertToJSON.root);
}
};
const taxValidation = transaction => {
return new Promise((resolve, reject) => {
try {
let tax = transaction.tax_money;
let collectedTaxAmount;
if (Array.isArray(tax)) {
collectedTaxAmount =
parseFloat(transaction.total_collected_money[0].amount[0]) * 0.16;
let taxAmount = parseFloat(tax[0].amount[0]);
taxAmount === collectedTaxAmount ? resolve(true) : resolve(false);
} else {
collectedTaxAmount = transaction.total_collected_money.amount * 0.16;
transaction.tax_money.amount === collectedTaxAmount
? resolve(true)
: resolve(false);
}
} catch (err) {
reject(err);
}
});
};
module.exports = {
fileValidation,
};