Skip to content

Commit

Permalink
eslint controllers/dashboard folder
Browse files Browse the repository at this point in the history
Closes: #1612
Approved by: jniles
  • Loading branch information
DedrickEnc authored and kwilu committed May 13, 2017
1 parent 4c4ee96 commit 530fa2f
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 42 deletions.
8 changes: 4 additions & 4 deletions server/config/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ exports.configure = function configure(app) {
app.use(helmet());
app.use(compress());

app.use(bodyParser.json({ limit: '8mb' }));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json({ limit : '8mb' }));
app.use(bodyParser.urlencoded({ extended : false }));

// this will disable the session from expiring on the server (redis-session)
// during development
Expand All @@ -50,7 +50,7 @@ exports.configure = function configure(app) {
resave : Boolean(process.env.SESS_RESAVE),
saveUninitialized : false,
unset : process.env.SESS_UNSET,
cookie : { httpOnly: true },
cookie : { httpOnly : true },
retries : 20,
};

Expand All @@ -70,7 +70,7 @@ exports.configure = function configure(app) {

// http logger setup
// options: combined | common | dev | short | tiny
app.use(morgan('combined', { stream: winston.stream }));
app.use(morgan('combined', { stream : winston.stream }));

// public static directories include the entire client and the uploads
// directory.
Expand Down
2 changes: 1 addition & 1 deletion server/config/interceptors.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const SQL_STATES = {
*
* This error handler interprets all errors and sends them to the client.
*/
exports.handler = function handler(err, req, res) {
exports.handler = function handler(err, req, res, next) {
let error = err;
// log the error to the error log (NOTE: in production, this should be 'error')
winston.log('debug', error);
Expand Down
22 changes: 12 additions & 10 deletions server/controllers/dashboard/debtorGroups.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
const moment = require('moment');
const Moment = require('moment');

const report = require('../finance/reports/debtors');

// time until the cached value expires - currently set at 5 minutes
const CACHE_TTL = 300000;

let cachedReport, timestamp;
let cachedReport;
let timestamp;

exports.getReport = getReport;

function getReport(req, res, next) {
let requestTime = new moment();
const requestTime = new Moment();

// if the current request is made within the cache life - send the cached report
if (timestamp) {
Expand All @@ -21,20 +22,21 @@ function getReport(req, res, next) {
}

// the cache has expired or has never been created - calculate the report
// ensure the lastest data from both the posting journal and the general ledger
// ensure the latest data from both the posting journal and the general ledger
// is used
report.context({combinedLedger : 1})
report.context({ combinedLedger : 1 })
.then(function (result) {
timestamp = new moment();
timestamp = new Moment();
cachedReport = result;
res.status(200).send(formatResponse(result));
});
})
.catch(next);
}

function formatResponse(report) {
function formatResponse(reportData) {
return {
data : report,
data : reportData,
timeCached : timestamp,
cacheLength : CACHE_TTL
cacheLength : CACHE_TTL,
};
}
73 changes: 46 additions & 27 deletions server/controllers/dashboard/stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
*/

// requirements
const Q = require('q');
const Q = require('q');
const moment = require('moment');
const db = require('../../lib/db');
const db = require('../../lib/db');

// expose to the API
exports.invoices = invoiceStat;
Expand All @@ -25,11 +25,11 @@ exports.patients = patientStats;
* @description This function help to get statistical data about invoices
*/
function invoiceStat(req, res, next) {
let params = req.query;
let bundle = {};
const params = req.query;
const bundle = {};

// date handler
let date = params.date ?
const date = params.date ?
moment(params.date).format('YYYY-MM-DD').toString() :
moment().format('YYYY-MM-DD').toString();

Expand All @@ -40,31 +40,51 @@ function invoiceStat(req, res, next) {
const DATE_CLAUSE = '(MONTH(invoice.date) = MONTH(?) AND YEAR(invoice.date) = YEAR(?))';

// query invoices which are not cancelled
let sqlInvoices =
`SELECT COUNT(*) AS total, SUM(cost) AS cost FROM invoice
WHERE ${DATE_CLAUSE} AND
invoice.uuid NOT IN (SELECT voucher.reference_uuid FROM voucher WHERE voucher.type_id = ${CANCELED_TRANSACTION_TYPE});`;
const sqlInvoices =
`
SELECT
COUNT(*) AS total, SUM(cost) AS cost
FROM
invoice
WHERE
${DATE_CLAUSE} AND
invoice.uuid NOT IN (
SELECT
voucher.reference_uuid
FROM
voucher
WHERE voucher.type_id = ${CANCELED_TRANSACTION_TYPE}
);`;

// query invoices
let sqlBalance =
`SELECT (debit - credit) as balance, project_id, cost
const sqlBalance =
`SELECT
(debit - credit) as balance, project_id, cost
FROM (
SELECT SUM(debit_equiv) as debit, SUM(credit_equiv) as credit, invoice.project_id, invoice.cost
FROM combined_ledger
JOIN invoice ON combined_ledger.record_uuid = invoice.uuid OR combined_ledger.reference_uuid = invoice.uuid
WHERE invoice.uuid NOT IN (SELECT voucher.reference_uuid FROM voucher WHERE voucher.type_id = ${CANCELED_TRANSACTION_TYPE})
SELECT
SUM(debit_equiv) as debit, SUM(credit_equiv) as credit, invoice.project_id, invoice.cost
FROM
combined_ledger
JOIN
invoice ON combined_ledger.record_uuid = invoice.uuid OR
combined_ledger.reference_uuid = invoice.uuid
WHERE
invoice.uuid NOT IN (
SELECT
voucher.reference_uuid
FROM voucher
WHERE voucher.type_id = ${CANCELED_TRANSACTION_TYPE})
AND ${DATE_CLAUSE} AND entity_uuid IS NOT NULL
GROUP BY uuid
) AS i
JOIN project ON i.project_id = project.id
`;

// promises requests
let dbPromise = [db.exec(sqlInvoices, [date, date]), db.exec(sqlBalance, [date, date])];
const dbPromise = [db.exec(sqlInvoices, [date, date]), db.exec(sqlBalance, [date, date])];

Q.all(dbPromise)
.spread((invoices, invoiceBalances) => {

// total invoices
bundle.total = invoices[0].total;
bundle.total_cost = invoices[0].cost;
Expand All @@ -73,7 +93,7 @@ function invoiceStat(req, res, next) {
* Paid Invoices
* Get list of invoices which are fully paid
*/
let paid = invoiceBalances.filter(item => {
const paid = invoiceBalances.filter(item => {
return item.balance === 0;
});
bundle.invoice_paid_amount = paid.reduce((previous, current) => {
Expand All @@ -85,19 +105,19 @@ function invoiceStat(req, res, next) {
* Partial Paid Invoices
* Get list of invoices which are partially paid
*/
let partial = invoiceBalances.filter(item => {
const partial = invoiceBalances.filter(item => {
return item.balance > 0 && item.balance !== item.cost;
});
bundle.invoice_partial_amount = partial.reduce((previous, current) => {
return current.cost - current.balance + previous;
return (current.cost - current.balance) + previous;
}, 0);
bundle.invoice_partial = partial.length;

/**
* Unpaid Invoices
* Get list of invoices which are not paid
*/
let unpaid = invoiceBalances.filter(item => {
const unpaid = invoiceBalances.filter(item => {
return item.balance > 0;
});
bundle.invoice_unpaid_amount = unpaid.reduce((previous, current) => {
Expand All @@ -112,7 +132,6 @@ function invoiceStat(req, res, next) {
})
.catch(next)
.done();

}

/**
Expand All @@ -121,11 +140,11 @@ function invoiceStat(req, res, next) {
* @description This method help to get patient stats for visits and registrations
*/
function patientStats(req, res, next) {
let params = req.query;
let bundle = {};
const params = req.query;
const bundle = {};

// date handler
let date = params.date ?
const date = params.date ?
moment(params.date).format('YYYY-MM-DD').toString() :
moment().format('YYYY-MM-DD').toString();

Expand All @@ -138,9 +157,9 @@ function patientStats(req, res, next) {
FROM patient_visit v JOIN patient p ON p.uuid = v.patient_uuid
WHERE MONTH(v.start_date) = MONTH(?) AND YEAR(v.start_date) = YEAR(?);`;

let dbPromise = [
const dbPromise = [
db.exec(sqlPatient, [date, date]),
db.exec(sqlVisit, [date, date])
db.exec(sqlVisit, [date, date]),
];

Q.all(dbPromise)
Expand Down

0 comments on commit 530fa2f

Please sign in to comment.