diff --git a/documents/postman-collection/KasirAja API.postman_collection.json b/documents/postman-collection/KasirAja API.postman_collection.json index 799bdd3..4d60be9 100644 --- a/documents/postman-collection/KasirAja API.postman_collection.json +++ b/documents/postman-collection/KasirAja API.postman_collection.json @@ -1292,7 +1292,7 @@ "method": "GET", "header": [], "url": { - "raw": "{{host}}/products?page=1&q=e&withStock=true", + "raw": "{{host}}/products?page=1&q=e&withStock=true&withCategory=true", "host": [ "{{host}}" ], @@ -1311,6 +1311,10 @@ { "key": "withStock", "value": "true" + }, + { + "key": "withCategory", + "value": "true" } ] } diff --git a/src/services/postgres/ProductsService.js b/src/services/postgres/ProductsService.js index 1a6ce6f..5257e0b 100644 --- a/src/services/postgres/ProductsService.js +++ b/src/services/postgres/ProductsService.js @@ -10,7 +10,7 @@ class ProductsService { } async getProducts(companyId, { - page = 1, q = null, withStock, limit = 10, + page = 1, q = null, withStock = false, withCategory = false, limit = 10, }) { const recordsQuery = await this._pool.query(` SELECT count(id) as total @@ -25,30 +25,21 @@ class ProductsService { const totalPages = Math.ceil(total / limit); const offsets = limit * (page - 1); - let query = { + const query = { text: `SELECT - id, name, description, price, cost + products.id, name, description, price, cost + ${withStock === 'true' ? ', stock' : ''} + ${withCategory === 'true' ? ', categories.name as category_name' : ''} FROM products - WHERE company_id = $1 - ${q !== null ? `AND name ILIKE '%${q}%'` : ''} + ${withStock === 'true' ? 'LEFT JOIN stocks ON stocks.product_id = products.id' : ''} + ${withCategory === 'true' ? 'LEFT JOIN categories ON categories.id = products.category_id' : ''} + WHERE company_id = $1 + ${q ? `AND name ILIKE '%${q}%'` : ''} + ORDER BY products.created_at DESC LIMIT $2 OFFSET $3`, values: [companyId, limit, offsets], }; - if (withStock && withStock === 'true') { - query = { - text: `SELECT - name, description, price, cost, stock - FROM products - LEFT JOIN stocks ON stocks.product_id = products.id - WHERE company_id = $1 - ${q !== null ? `AND name ILIKE '%${q}%'` : ''} - ORDER BY created_at DESC - LIMIT $2 OFFSET $3`, - values: [companyId, limit, offsets], - }; - } - const { rows } = await this._pool.query(query); return { diff --git a/src/services/postgres/PurchasesService.js b/src/services/postgres/PurchasesService.js index 68c9b74..f0d26c5 100644 --- a/src/services/postgres/PurchasesService.js +++ b/src/services/postgres/PurchasesService.js @@ -75,7 +75,7 @@ class PurchasesService { LEFT JOIN offices ON offices.id = purchases.office_id LEFT JOIN users ON users.id = purchases.created_by WHERE purchases.id = $1 - ORDER BY created_at DESC`, + ORDER BY purchases.created_at DESC`, values: [purchaseId], }; diff --git a/src/services/postgres/SalesService.js b/src/services/postgres/SalesService.js index f414396..3eeb7cb 100644 --- a/src/services/postgres/SalesService.js +++ b/src/services/postgres/SalesService.js @@ -73,7 +73,7 @@ class SalesService { WHERE sales.office_id = (SELECT id FROM offices WHERE company_id = $1 LIMIT 1) AND date::DATE BETWEEN $2 AND $3 - ORDER BY created_at DESC`, + ORDER BY sales.created_at DESC`, values: [companyId, startDate, endDate], }; diff --git a/src/services/postgres/UnitsService.js b/src/services/postgres/UnitsService.js index dc626ba..03f674a 100644 --- a/src/services/postgres/UnitsService.js +++ b/src/services/postgres/UnitsService.js @@ -26,8 +26,9 @@ class UnitsService { text: ` SELECT id, name, description FROM units WHERE company_id = $1 ${q !== null ? `AND name ILIKE '%${q}%'` : ''} + ORDER BY created_at DESC LIMIT $2 OFFSET $3 - ORDER BY created_at DESC`, + `, values: [companyId, limit, offsets], }; diff --git a/src/validator/products/schema.js b/src/validator/products/schema.js index 7cdd8a5..12e8817 100644 --- a/src/validator/products/schema.js +++ b/src/validator/products/schema.js @@ -3,8 +3,8 @@ const Joi = require('joi'); const PostProductPayloadSchema = Joi.object({ name: Joi.string().required(), description: Joi.string().allow(''), - cost: Joi.number().required(), - price: Joi.number().required(), + cost: Joi.number().required().greater(0), + price: Joi.number().required().greater(Joi.ref('cost')), stock: Joi.number().required(), category_id: Joi.string().guid().required(), });