diff --git a/helpers/Db.js b/helpers/Db.js index 45a1902..d382431 100644 --- a/helpers/Db.js +++ b/helpers/Db.js @@ -11,18 +11,19 @@ const dbConfig = { const db = pgp(dbConfig); +const cs = new pgp.helpers.ColumnSet(['indicator', 'yearmonth', 'platformid', 'merchantnumber', 'merchantname', 'level1', 'level2', 'level3', 'element', 'buyrate', 'merchantbillingcode', 'merchantbillingcodename', 'revn_byrt_in', 'clientormerchant_in', 'volume', 'amount', 'bnkid', 'date'], { table: 'consolidated_residual_details' }); + exports.importToDb = async (csvData) => { return new Promise((resolve, reject) => { try { - db.none(`DELETE FROM consolidated_residual_details;`) + db.none(`DELETE FROM consolidated_residual_details`) .then(() => { - const cs = new pgp.helpers.ColumnSet(['indicator', 'yearmonth', 'platformid', 'merchantnumber', 'merchantname', 'level1', 'level2', 'level3', 'element', 'buyrate', 'merchantbillingcode', 'merchantbillingcodename', 'revn_byrt_in', 'clientormerchant_in', 'volume', 'amount', 'bnkid', 'date'], { table: 'consolidated_residual_details' }); - + const query = pgp.helpers.insert(csvData, cs); db.none(query) .then(() => { - db.one(`SELECT COUNT(*) FROM consolidated_residual_details WHERE date = '${csvData[0].date}';`) + db.one(`SELECT COUNT(*) FROM consolidated_residual_details WHERE date = '${csvData[0].date}'`) .then((result) => { resolve(result.count); }) @@ -45,78 +46,24 @@ exports.importToDb = async (csvData) => { }; exports.selectResultsFull = async () => { - return new Promise((resolve, reject) => { - try { - db.many(queries.selectResultsFull) - .then((result) => { - resolve(result); - }) - .catch(error => { - reject(error); - }); - } - catch (err) { - reject(err); - } - }); + return db.many(queries.selectResultsFull); } exports.selectResultsSum = async () => { - return new Promise((resolve, reject) => { - try { - db.task('get-sums', async t => { + return db.task('get-sums', async t => { const amtExpenseSum = await t.one(queries.selectResultsSum('amount', 'EXPENSE')); const amtRevenueSum = await t.one(queries.selectResultsSum('amount', 'REVENUE')); const amtAfterCostExpenseSum = await t.one(queries.selectResultsSum('amount_after_cost', 'EXPENSE')); const amtAfterCostRevenueSum = await t.one(queries.selectResultsSum('amount_after_cost', 'REVENUE')); return { amtExpenseSum, amtRevenueSum, amtAfterCostExpenseSum, amtAfterCostRevenueSum }; - }) - .then(({ amtExpenseSum, amtRevenueSum, amtAfterCostExpenseSum, amtAfterCostRevenueSum }) => { - resolve({ amtExpenseSum, amtRevenueSum, amtAfterCostExpenseSum, amtAfterCostRevenueSum }); - }) - .catch(error => { - reject(error); - - }); - } - catch (err) { - reject(err); - } - }); + }); } - exports.runNatesQuery = async () => { - return new Promise((resolve, reject) => { - try { - db.none(queries.natesInsertAndUpdates) - .then(() => { - resolve(); - }) - .catch(error => { - reject(error); - }); - } - catch (err) { - reject(err); - } - }); + return db.none(queries.natesInsertAndUpdates); } exports.reset = async () => { - return new Promise((resolve, reject) => { - try { - db.none(`DELETE FROM consolidated_residual_details;`) - .then(() => { - resolve(); - }) - .catch(error => { - reject(error); - }); - } - catch (err) { - reject(err); - } - }); -}; \ No newline at end of file + return db.none(`DELETE FROM consolidated_residual_details`) +};