fix sql error order

pull/1/head
Aji Kamaludin 3 years ago
parent 05de83dd17
commit 10493cc5c9
No known key found for this signature in database
GPG Key ID: 670E1F26AD5A8099

@ -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"
}
]
}

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

@ -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],
};

@ -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],
};

@ -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],
};

@ -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(),
});

Loading…
Cancel
Save