feat(pagination): Adds MVP pagination to orders and products for admin routes

This commit is contained in:
Oliver Windall Juhl
2020-08-27 17:51:23 +02:00
committed by GitHub
parent d8aac759cf
commit 9dc6999a9a
9 changed files with 70 additions and 27 deletions
+10 -2
View File
@@ -67,8 +67,16 @@ class BaseModel {
* @return {Array<mongoose.Document>} the retreived mongoose documents or
* an empty array
*/
find(query, options) {
return this.mongooseModel_.find(query, options).lean()
find(query, options, offset, limit) {
return this.mongooseModel_
.find(query, options)
.skip(offset)
.limit(limit)
.lean()
}
count() {
return this.mongooseModel_.count({})
}
/**
+1 -1
View File
@@ -75,4 +75,4 @@
"winston": "^3.2.1"
},
"gitHead": "27d4e07c5251e43ba6be2d5fa35f1d5287b11043"
}
}
@@ -13,13 +13,18 @@ export default async (req, res) => {
"payment_status",
])
let orders = await orderService.list(query)
const limit = parseInt(req.query.limit) || 0
const offset = parseInt(req.query.offset) || 0
let orders = await orderService.list(query, offset, limit)
orders = await Promise.all(
orders.map(order => orderService.decorate(order))
)
res.json({ orders })
let numOrders = await orderService.count()
res.json({ orders, total_count: numOrders })
} catch (error) {
throw error
}
@@ -10,7 +10,10 @@ export default async (req, res) => {
"description",
])
let products = await productService.list(query)
const limit = parseInt(req.query.limit) || 0
const offset = parseInt(req.query.offset) || 0
let products = await productService.list(query, offset, limit)
products = await Promise.all(
products.map(
@@ -32,7 +35,10 @@ export default async (req, res) => {
)
)
)
res.json({ products })
const numProducts = await productService.count()
res.json({ products, total_count: numProducts })
} catch (error) {
console.log(error)
throw error
@@ -10,14 +10,7 @@ describe("Get product by id", () => {
beforeAll(async () => {
subject = await request(
"GET",
`/admin/products/${IdMap.getId("product1")}`,
{
adminSession: {
jwt: {
userId: IdMap.getId("admin_user"),
},
},
}
`/store/products/${IdMap.getId("product1")}`
)
})
@@ -1,6 +1,7 @@
export const MiddlewareServiceMock = {
usePostAuthentication: jest.fn(),
usePreAuthentication: jest.fn(),
usePreCartCreation: jest.fn().mockReturnValue([]),
getRouters: jest.fn().mockReturnValue([]),
}
+12 -10
View File
@@ -145,8 +145,18 @@ class OrderService extends BaseService {
* @param {Object} selector - the query object for find
* @return {Promise} the result of the find operation
*/
list(selector) {
return this.orderModel_.find(selector)
list(selector, offset, limit) {
return this.orderModel_
.find(selector, {}, offset, limit)
.sort({ created: -1 })
}
/**
* Return the total number of documents in database
* @return {Promise} the result of the count operation
*/
count() {
return this.orderModel_.count()
}
/**
@@ -232,14 +242,6 @@ class OrderService extends BaseService {
return true
}
/**
* @param {Object} selector - the query object for find
* @return {Promise} the result of the find operation
*/
list(selector) {
return this.orderModel_.find(selector)
}
/**
* @param {string} orderId - id of the order to complete
* @return {Promise} the result of the find operation
+10 -2
View File
@@ -49,8 +49,16 @@ class ProductService extends BaseService {
* @param {Object} selector - the query object for find
* @return {Promise} the result of the find operation
*/
list(selector) {
return this.productModel_.find(selector)
list(selector, offset, limit) {
return this.productModel_.find(selector, {}, offset, limit)
}
/**
* Return the total number of documents in database
* @return {Promise} the result of the count operation
*/
count() {
return this.productModel_.count()
}
/**
+20
View File
@@ -1965,6 +1965,13 @@ cli-width@^2.0.0:
resolved "https://registry.yarnpkg.com/cli-width/-/cli-width-2.2.0.tgz#ff19ede8a9a5e579324147b0c11f0fbcbabed639"
integrity sha1-/xnt6Kml5XkyQUewwR8PvLq+1jk=
client-sessions@^0.8.0:
version "0.8.0"
resolved "https://registry.yarnpkg.com/client-sessions/-/client-sessions-0.8.0.tgz#a7d8c5558ad5d56f2a199f3533eb654b5df893fd"
integrity sha1-p9jFVYrV1W8qGZ81M+tlS134k/0=
dependencies:
cookies "^0.7.0"
cliui@^6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/cliui/-/cliui-6.0.0.tgz#511d702c0c4e41ca156d7d0e96021f23e13225b1"
@@ -2171,6 +2178,14 @@ cookiejar@^2.1.0:
resolved "https://registry.yarnpkg.com/cookiejar/-/cookiejar-2.1.2.tgz#dd8a235530752f988f9a0844f3fc589e3111125c"
integrity sha512-Mw+adcfzPxcPeI+0WlvRrr/3lGVO0bD75SxX6811cxSh1Wbxx7xZBGK1eVtDf6si8rg2lhnUjsVLMFMfbRIuwA==
cookies@^0.7.0:
version "0.7.3"
resolved "https://registry.yarnpkg.com/cookies/-/cookies-0.7.3.tgz#7912ce21fbf2e8c2da70cf1c3f351aecf59dadfa"
integrity sha512-+gixgxYSgQLTaTIilDHAdlNPZDENDQernEMiIcZpYYP14zgHsCt4Ce1FEjFtcp6GefhozebB6orvhAAWx/IS0A==
dependencies:
depd "~1.1.2"
keygrip "~1.0.3"
copy-descriptor@^0.1.0:
version "0.1.1"
resolved "https://registry.yarnpkg.com/copy-descriptor/-/copy-descriptor-0.1.1.tgz#676f6eb3c39997c2ee1ac3a924fd6124748f578d"
@@ -4348,6 +4363,11 @@ kareem@2.3.1:
resolved "https://registry.yarnpkg.com/kareem/-/kareem-2.3.1.tgz#def12d9c941017fabfb00f873af95e9c99e1be87"
integrity sha512-l3hLhffs9zqoDe8zjmb/mAN4B8VT3L56EUvKNqLFVs9YlFA+zx7ke1DO8STAdDyYNkeSo1nKmjuvQeI12So8Xw==
keygrip@~1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/keygrip/-/keygrip-1.0.3.tgz#399d709f0aed2bab0a059e0cdd3a5023a053e1dc"
integrity sha512-/PpesirAIfaklxUzp4Yb7xBper9MwP6hNRA6BGGUFCgbJ+BM5CKBtsoxinNXkLHAr+GXS1/lSlF2rP7cv5Fl+g==
kind-of@^3.0.2, kind-of@^3.0.3, kind-of@^3.2.0:
version "3.2.2"
resolved "https://registry.yarnpkg.com/kind-of/-/kind-of-3.2.2.tgz#31ea21a734bab9bbb0f32466d893aea51e4a3c64"