Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Db.js #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 11 additions & 64 deletions helpers/Db.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
})
Expand All @@ -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);
}
});
};
return db.none(`DELETE FROM consolidated_residual_details`)
};