add dashboard summary
parent
9db00d1fa9
commit
fceac33d34
@ -0,0 +1,23 @@
|
||||
class GeneralHandler {
|
||||
constructor(service) {
|
||||
this._service = service;
|
||||
|
||||
this.getDashboardHandler = this.getDashboardHandler.bind(this);
|
||||
}
|
||||
|
||||
async getDashboardHandler(request) {
|
||||
try {
|
||||
const { companyId } = request.auth.credentials;
|
||||
|
||||
const data = await this._service.dashboardSummary(companyId);
|
||||
|
||||
return {
|
||||
status: 'success',
|
||||
data,
|
||||
};
|
||||
} catch (error) {
|
||||
return error;
|
||||
}
|
||||
}
|
||||
}
|
||||
module.exports = GeneralHandler;
|
@ -0,0 +1,11 @@
|
||||
const GeneralHandler = require('./handler');
|
||||
const routes = require('./routes');
|
||||
|
||||
module.exports = {
|
||||
name: 'general',
|
||||
version: '1.0.0',
|
||||
register: async (server, { service }) => {
|
||||
const generalHandler = new GeneralHandler(service);
|
||||
server.route(routes(generalHandler));
|
||||
},
|
||||
};
|
@ -0,0 +1,12 @@
|
||||
const routes = (handler) => [
|
||||
{
|
||||
method: 'GET',
|
||||
path: '/dashboard',
|
||||
handler: handler.getDashboardHandler,
|
||||
options: {
|
||||
auth: 'kasiraja_jwt',
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
module.exports = routes;
|
@ -0,0 +1,58 @@
|
||||
const { Pool } = require('pg');
|
||||
|
||||
class GeneralService {
|
||||
constructor() {
|
||||
this._pool = new Pool();
|
||||
}
|
||||
|
||||
async dashboardSummary(companyId) {
|
||||
const saleTodayCount = await this._pool.query(
|
||||
`SELECT COUNT(id) as sale_count FROM sales WHERE office_id = (SELECT id FROM offices WHERE company_id = '${companyId}' LIMIT 1) AND date::DATE = CURRENT_DATE`,
|
||||
);
|
||||
|
||||
const purchaseTodayCount = await this._pool.query(
|
||||
`SELECT COUNT(id) as purchase_count FROM purchases WHERE office_id = (SELECT id FROM offices WHERE company_id = '${companyId}' LIMIT 1) AND date::DATE = CURRENT_DATE`,
|
||||
);
|
||||
|
||||
const saleYesterdayCount = await this._pool.query(
|
||||
`SELECT COUNT(id) as sale_count FROM sales WHERE office_id = (SELECT id FROM offices WHERE company_id = '${companyId}' LIMIT 1) AND date::DATE = CURRENT_DATE - 1`,
|
||||
);
|
||||
|
||||
const purchaseYesterdayCount = await this._pool.query(
|
||||
`SELECT COUNT(id) as purchase_count FROM purchases WHERE office_id = (SELECT id FROM offices WHERE company_id = '${companyId}' LIMIT 1) AND date::DATE = CURRENT_DATE - 1`,
|
||||
);
|
||||
|
||||
const graphSale = await this._pool.query(
|
||||
`SELECT COUNT(date), date::DATE
|
||||
FROM sales
|
||||
WHERE office_id = (SELECT id FROM offices WHERE company_id = '${companyId}' LIMIT 1) AND date::DATE BETWEEN CURRENT_DATE - 7 AND CURRENT_DATE
|
||||
GROUP BY date::DATE`,
|
||||
);
|
||||
|
||||
const graphPurchase = await this._pool.query(
|
||||
`SELECT COUNT(date), date::DATE
|
||||
FROM purchases
|
||||
WHERE office_id = (SELECT id FROM offices WHERE company_id = '${companyId}' LIMIT 1) AND date::DATE BETWEEN CURRENT_DATE - 7 AND CURRENT_DATE
|
||||
GROUP BY date::DATE `,
|
||||
);
|
||||
|
||||
const grownSale = (+saleYesterdayCount.rows[0].sale_count - +saleTodayCount.rows[0].sale_count)
|
||||
/ +saleYesterdayCount.rows[0].sale_count;
|
||||
const grownPurchase = (+purchaseYesterdayCount.rows[0].purchase_count
|
||||
- +purchaseTodayCount.rows[0].purchase_count)
|
||||
/ +purchaseYesterdayCount.rows[0].purchase_count;
|
||||
|
||||
return {
|
||||
saleCount: saleTodayCount.rows[0].sale_count,
|
||||
purchaseCount: purchaseTodayCount.rows[0].purchase_count,
|
||||
saleYesterdayCount: saleYesterdayCount.rows[0].sale_count,
|
||||
purchaseYesterdayCount: purchaseYesterdayCount.rows[0].purchase_count,
|
||||
grownSale,
|
||||
grownPurchase,
|
||||
graphSale: graphSale.rows,
|
||||
graphPurchase: graphPurchase.rows,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = GeneralService;
|
Loading…
Reference in New Issue