From fceac33d34318f77b9654d8d43c8139f65bfb5fe Mon Sep 17 00:00:00 2001 From: Aji Kamaludin Date: Sat, 21 Aug 2021 02:56:12 +0700 Subject: [PATCH] add dashboard summary --- src/api/general/handler.js | 23 +++++++++ src/api/general/index.js | 11 +++++ src/api/general/routes.js | 12 +++++ src/api/sales/handler.js | 4 +- src/server.js | 13 ++++- src/services/postgres/CustomersService.js | 8 +++- src/services/postgres/GeneralService.js | 58 +++++++++++++++++++++++ src/services/postgres/SalesService.js | 2 +- 8 files changed, 125 insertions(+), 6 deletions(-) create mode 100644 src/api/general/handler.js create mode 100644 src/api/general/index.js create mode 100644 src/api/general/routes.js create mode 100644 src/services/postgres/GeneralService.js diff --git a/src/api/general/handler.js b/src/api/general/handler.js new file mode 100644 index 0000000..ad1b38a --- /dev/null +++ b/src/api/general/handler.js @@ -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; diff --git a/src/api/general/index.js b/src/api/general/index.js new file mode 100644 index 0000000..7a90059 --- /dev/null +++ b/src/api/general/index.js @@ -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)); + }, +}; diff --git a/src/api/general/routes.js b/src/api/general/routes.js new file mode 100644 index 0000000..e35d7fe --- /dev/null +++ b/src/api/general/routes.js @@ -0,0 +1,12 @@ +const routes = (handler) => [ + { + method: 'GET', + path: '/dashboard', + handler: handler.getDashboardHandler, + options: { + auth: 'kasiraja_jwt', + }, + }, +]; + +module.exports = routes; diff --git a/src/api/sales/handler.js b/src/api/sales/handler.js index ff92e08..39bc572 100644 --- a/src/api/sales/handler.js +++ b/src/api/sales/handler.js @@ -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({ diff --git a/src/server.js b/src/server.js index fabb1a0..f4352ac 100644 --- a/src/server.js +++ b/src/server.js @@ -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(); diff --git a/src/services/postgres/CustomersService.js b/src/services/postgres/CustomersService.js index f3570af..0a17230 100644 --- a/src/services/postgres/CustomersService.js +++ b/src/services/postgres/CustomersService.js @@ -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 = { diff --git a/src/services/postgres/GeneralService.js b/src/services/postgres/GeneralService.js new file mode 100644 index 0000000..9f7144b --- /dev/null +++ b/src/services/postgres/GeneralService.js @@ -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; diff --git a/src/services/postgres/SalesService.js b/src/services/postgres/SalesService.js index 660cd3a..bb550e6 100644 --- a/src/services/postgres/SalesService.js +++ b/src/services/postgres/SalesService.js @@ -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}%'` : ''}