Skip to content

Commit

Permalink
Merge 5e8a906 into 9879dc3
Browse files Browse the repository at this point in the history
  • Loading branch information
Kasulejoseph committed Dec 15, 2019
2 parents 9879dc3 + 5e8a906 commit 0bdc78f
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/helper/queryHelper.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Expense from "../model/expense";

export default async (queryParams, queryKeys, currentPage) => {
const expPerPage = 9;
const expPerPage = queryParams.limit ? Number(queryParams.limit): 0;
const requiredParams = [
"status",
"uuid",
Expand All @@ -12,9 +12,10 @@ export default async (queryParams, queryKeys, currentPage) => {
"employee"
];

// eliminate page
const newArray = queryKeys.filter(value => value !== "page");
// eliminate page and limit
const newArray = queryKeys.filter(value => (value !== "page" && value !== "limit"));
delete queryParams.page;
delete queryParams.limit;

const isValidParam = newArray.every(key => requiredParams.includes(key));
if (!isValidParam) {
Expand All @@ -23,14 +24,14 @@ export default async (queryParams, queryKeys, currentPage) => {
error: "Invalid query param(s)"
};
}

const data = await Expense.find(queryParams)
.skip(expPerPage * currentPage - expPerPage)
.limit(expPerPage);
const count = await Expense.countDocuments(queryParams);
const pages = expPerPage === 0 ? 1: Math.ceil(count / expPerPage)
return {
status: 200,
pages: Math.ceil(count / expPerPage),
pages,
count,
data
};
Expand Down

0 comments on commit 0bdc78f

Please sign in to comment.