feat(medusa): Add support for filtering with gt, lt, gte and lte (#190)

- Add support for filtering with lt, gt, lte, gte in the following format: created_at[gt]=01-26-1997
- Add middleware for cleaning request queries
- Add order query validation
- Temp. disables tags in claim integration tests
This commit is contained in:
Oliver Windall Juhl authored and GitHub committed 2021-03-01 11:32:17 +01:00
1 parent bb7dcbd706
commit dd0491f521
8 files changed
+357 -71

No files matched your search

@@ -1,9 +1,11 @@
import { default as authenticateCustomer } from "./authenticate-customer"
import { default as authenticate } from "./authenticate"
import { default as normalizeQuery } from "./normalized-query"
import { default as wrap } from "./await-middleware"
export default {
authenticate,
authenticateCustomer,
normalizeQuery,
wrap,
}
@@ -0,0 +1,16 @@
export default () => {
return (req, res, next) => {
const clean = Object.entries(req.query).reduce((acc, [key, val]) => {
if (Array.isArray(val) && val.length === 1) {
acc[key] = val[0].split(",")
} else {
acc[key] = val
}
return acc
}, {})
req.query = clean
next()
}
}
@@ -9,7 +9,11 @@ export default app => {
/**
* List orders
*/
route.get("/", middlewares.wrap(require("./list-orders").default))
route.get(
"/",
middlewares.normalizeQuery(),
middlewares.wrap(require("./list-orders").default)
)
/**
* Get an order
@@ -280,3 +284,20 @@ export const allowedRelations = [
"swaps.return_order",
"swaps.additional_items",
]
export const filterableFields = [
"id",
"status",
"fulfillment_status",
"payment_status",
"display_id",
"cart_id",
"customer_id",
"email",
"region_id",
"currency_code",
"tax_rate",
"canceled_at",
"created_at",
"updated_at",
]
@@ -1,8 +1,16 @@
import _ from "lodash"
import { Not } from "typeorm"
import { defaultRelations, defaultFields } from "./"
import { defaultRelations, defaultFields, filterableFields } from "./"
import { MedusaError, Validator } from "medusa-core-utils"
export default async (req, res) => {
const schema = Validator.orderFilter()
const { value, error } = schema.validate(req.query)
if (error) {
throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details)
}
try {
const orderService = req.scope.resolve("orderService")
@@ -18,6 +26,8 @@ export default async (req, res) => {
let includeFields = []
if ("fields" in req.query) {
includeFields = req.query.fields.split(",")
// Ensure created_at is included, since we are sorting on this
includeFields.push("created_at")
}
let expandFields = []
@@ -25,17 +35,9 @@ export default async (req, res) => {
expandFields = req.query.expand.split(",")
}
if ("new" in req.query) {
selector = {
payment_status: Not("captured"),
fulfillment_status: Not("shipped"),
}
}
if ("requires_more" in req.query) {
selector = {
payment_status: Not("captured"),
fulfillment_status: Not("shipped"),
for (const k of filterableFields) {
if (k in value) {
selector[k] = value[k]
}
}
@@ -52,8 +54,12 @@ export default async (req, res) => {
listConfig
)
let data = orders
const fields = [...includeFields, ...expandFields]
const data = orders.map(o => _.pick(o, fields))
if (fields.length) {
data = orders.map(o => _.pick(o, fields))
}
res.json({ orders: data, count, offset, limit })
} catch (error) {