Skip to content

Commit

Permalink
Add pagination for stock end-points
Browse files Browse the repository at this point in the history
  • Loading branch information
mbayopanda committed Mar 15, 2023
1 parent 790207c commit aa4f14f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
24 changes: 12 additions & 12 deletions server/controllers/stock/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,6 @@ async function getAssets(params) {
params.scan_status === 'scanned' ? 'last_scan.uuid IS NOT NULL' : 'last_scan.uuid IS NULL');
}
filters.setGroup(groupByClause);

filters.setHaving(havingClause);
filters.setOrder('ORDER BY i.code, l.label');
const query = filters.applyQuery(sql);
Expand Down Expand Up @@ -434,7 +433,6 @@ async function getLotsDepot(depotUuid, params, finalClause) {
`;

const groupByClause = finalClause || ` GROUP BY l.uuid, m.depot_uuid ${emptyLotToken} ORDER BY i.code, l.label `;

const filters = getLotFilters(params);
filters.setGroup(groupByClause);

Expand Down Expand Up @@ -484,7 +482,7 @@ async function getLotsDepot(depotUuid, params, finalClause) {

if (params.paging) {
return {
pager : paginatedResults,
pager : paginatedResults.pager,
rows : inventoriesWithLotsProcessed,
};
}
Expand Down Expand Up @@ -979,39 +977,41 @@ async function getInventoryQuantityAndConsumption(params) {

const clause = ` GROUP BY l.inventory_uuid, m.depot_uuid ${emptyLotToken} ORDER BY ig.name, i.text `;

let filteredRows = await getLots(sql, params, clause);
if (filteredRows.length === 0) { return []; }
const filteredRows = await getLots(sql, params, clause);
let _filteredRows = params.paging ? filteredRows.rows : filteredRows;

if (_filteredRows.length === 0) { return []; }

const settingsql = `
SELECT month_average_consumption, average_consumption_algo, min_delay, default_purchase_interval
FROM stock_setting WHERE enterprise_id = ?
`;

const opts = await db.one(settingsql, filteredRows[0].enterprise_id);
const opts = await db.one(settingsql, _filteredRows[0].enterprise_id);

// add the minimum delay to the rows
filteredRows.forEach(row => {
_filteredRows.forEach(row => {
row.min_delay = opts.min_delay;
});

// add the CMM
filteredRows = await getBulkInventoryCMM(
filteredRows,
_filteredRows = await getBulkInventoryCMM(
_filteredRows,
opts.month_average_consumption,
opts.average_consumption_algo,
opts.default_purchase_interval,
params,
);

if (_status) {
filteredRows = filteredRows.filter(row => row.status === _status);
_filteredRows = _filteredRows.filter(row => row.status === _status);
}

if (requirePurchaseOrder) {
filteredRows = filteredRows.filter(row => row.S_Q > 0);
_filteredRows = _filteredRows.filter(row => row.S_Q > 0);
}

return filteredRows;
return params.paging ? { ...filteredRows, rows : _filteredRows } : _filteredRows;
}

/**
Expand Down
23 changes: 15 additions & 8 deletions server/controllers/stock/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1099,10 +1099,14 @@ async function listLotsDepot(req, res, next) {

// if no data is returned or if the skipTags flag is set, we don't need to do any processing
// of tags. Skip the SQL query and JS loops.
if (data.length !== 0 && !params.skipTags) {
if (!params.paging && data.length !== 0 && !params.skipTags) {
await core.addLotTags(data);
}

if (params.paging && data.rows.length !== 0 && !params.skipTags) {
await core.addLotTags(data.rows);
}

res.status(200).json(data);
} catch (error) {
next(error);
Expand Down Expand Up @@ -1169,18 +1173,21 @@ async function listLotsDepotDetailed(req, res, next) {
db.exec(sqlGetMonthlyStockMovements, [db.bid(params.depot_uuid), params.startDate, params.dateTo]),
]);

data.forEach(current => {
const _data = !params.paging ? data : data.rows;
const _dataPreviousMonth = !params.paging ? dataPreviousMonth : dataPreviousMonth.rows;

(_data || []).forEach(current => {
current.quantity_opening = 0;
current.total_quantity_entry = 0;
current.total_quantity_exit = 0;

dataPreviousMonth.forEach(previous => {
(_dataPreviousMonth || []).forEach(previous => {
if (current.uuid === previous.uuid) {
current.quantity_opening = previous.quantity;
}
});

dataStockMovements.forEach(row => {
(dataStockMovements || []).forEach(row => {
if (current.uuid === row.lot_uuid) {
current.total_quantity_entry = row.entry_quantity;
current.total_quantity_exit = row.exit_quantity;
Expand All @@ -1196,19 +1203,19 @@ async function listLotsDepotDetailed(req, res, next) {
`;

// if we have an empty set, do not query tags.
if (data.length !== 0) {
const lotUuids = data.map(row => db.bid(row.uuid));
if (_data.length !== 0) {
const lotUuids = _data.map(row => db.bid(row.uuid));
const tags = await db.exec(queryTags, [lotUuids]);

// make a lot_uuid -> tags map.
const tagMap = _.groupBy(tags, 'lot_uuid');

data.forEach(lot => {
_data.forEach(lot => {
lot.tags = tagMap[lot.uuid] || [];
});
}

res.status(200).json(data);
res.status(200).json(params.paging ? { pager : data.pager, rows : _data } : _data);
} catch (error) {
next(error);
}
Expand Down

0 comments on commit aa4f14f

Please sign in to comment.