add dashboard summary

pull/1/head
Aji Kamaludin 3 years ago
parent 9db00d1fa9
commit fceac33d34
No known key found for this signature in database
GPG Key ID: 670E1F26AD5A8099

@ -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;

@ -13,11 +13,11 @@ class SalesHandler {
this._validator.validatePostSalePayload(request.payload);
const { id: userId } = request.auth.credentials;
const {
date, invoice, description, amount, discount, items, officeId, customerId
date, invoice, description, amount, discount, items, officeId, customerId,
} = request.payload;
const saleId = await this._service.createTransaction({
date, invoice, description, amount, discount, items, userId, officeId, customerId
date, invoice, description, amount, discount, items, userId, officeId, customerId,
});
const response = h.response({

@ -30,7 +30,7 @@ const categories = require('./api/categories');
const CategoriesService = require('./services/postgres/CategoriesService');
const CategoriesValidator = require('./validator/categories');
// customers
// customers
const customers = require('./api/customers');
const CustomersService = require('./services/postgres/CustomersService');
const CustomerValidator = require('./validator/customers');
@ -50,6 +50,10 @@ const purchases = require('./api/purchases');
const PurchasesService = require('./services/postgres/PurchasesService');
const PurchasesValidator = require('./validator/purchases');
// dashboard
const general = require('./api/general');
const GeneralService = require('./services/postgres/GeneralService');
const init = async () => {
// instances
const usersService = new UsersService();
@ -61,6 +65,7 @@ const init = async () => {
const productsService = new ProductsService();
const salesService = new SalesService();
const purchasesService = new PurchasesService();
const generalService = new GeneralService();
// server
const server = Hapi.server({
@ -210,6 +215,12 @@ const init = async () => {
validator: PurchasesValidator,
},
},
{
plugin: general,
options: {
service: generalService,
},
},
]);
await server.start();

@ -62,7 +62,9 @@ class CustomersService {
return results.rows[0];
}
async addCustomer({ name, phone, address, description, companyId }) {
async addCustomer({
name, phone, address, description, companyId,
}) {
const id = uuid();
const query = {
text: 'INSERT INTO customers(id, name, phone, address, description, company_id) VALUES ($1, $2, $3, $4, $5, $6)',
@ -74,7 +76,9 @@ class CustomersService {
return id;
}
async updateCustomerById(customerId, { name, phone, address, description }) {
async updateCustomerById(customerId, {
name, phone, address, description,
}) {
validateUuid(customerId);
const query = {

@ -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;

@ -70,7 +70,7 @@ class SalesService {
const recordsQuery = await this._pool.query(`
SELECT count(sales.id) as total
FROM sales
${customerId ? `LEFT JOIN customers ON customers.id = sales.customer_id` : ''}
${customerId ? 'LEFT JOIN customers ON customers.id = sales.customer_id' : ''}
WHERE
sales.office_id = (SELECT id FROM offices WHERE company_id = '${companyId}' LIMIT 1)
${q ? `AND invoice ILIKE '%${q}%'` : ''}

Loading…
Cancel
Save