From bcd54d7412eaadfcd366c34d18c2b7e11b36adf6 Mon Sep 17 00:00:00 2001 From: olivermrbl Date: Thu, 3 Sep 2020 12:32:28 +0200 Subject: [PATCH] Adds pagination to customerService --- .../src/api/routes/admin/customers/list-customers.js | 9 +++++++-- packages/medusa/src/services/customer.js | 12 ++++++++++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/packages/medusa/src/api/routes/admin/customers/list-customers.js b/packages/medusa/src/api/routes/admin/customers/list-customers.js index 013cbd99c2..f1aa3fcebd 100644 --- a/packages/medusa/src/api/routes/admin/customers/list-customers.js +++ b/packages/medusa/src/api/routes/admin/customers/list-customers.js @@ -9,9 +9,14 @@ export default async (req, res) => { "last_name", ]) - const customers = await customerService.list(query) + const limit = parseInt(req.query.limit) || 0 + const offset = parseInt(req.query.offset) || 0 - res.json({ customers }) + const customers = await customerService.list(query, offset, limit) + + const numCustomers = await customerService.count() + + res.json({ customers, total_count: numCustomers }) } catch (error) { throw error } diff --git a/packages/medusa/src/services/customer.js b/packages/medusa/src/services/customer.js index 03397b785f..1950e1b470 100644 --- a/packages/medusa/src/services/customer.js +++ b/packages/medusa/src/services/customer.js @@ -113,8 +113,16 @@ class CustomerService extends BaseService { * @param {Object} selector - the query object for find * @return {Promise} the result of the find operation */ - list(selector) { - return this.customerModel_.find(selector) + list(selector, offset, limit) { + return this.customerModel_.find(selector, {}, offset, limit) + } + + /** + * Return the total number of documents in database + * @return {Promise} the result of the count operation + */ + count() { + return this.customerModel_.count() } /**