feat(pagination): Adds MVP pagination to orders and products for admin routes
This commit is contained in:
1 parent
d8aac759cf
commit
9dc6999a9a
9 files changed
+70
-27
No files matched your search
@@ -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([]),
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user