feat(medusa,inventory,types): Expand list-reservation capabilities (#3979)
**What** - Add filter capabilities to reservation items based on: - description query: "contains", "startsWith", "endsWith", "equals" - date querying **How** - Introducing a new filtering primitive: "StringSearchOperator" resembling the "dateComparisonOperator" Fixes CORE-1373
This commit is contained in:
@@ -37,6 +37,46 @@ export interface AdminGetReservationsParams {
|
||||
*/
|
||||
gte?: number
|
||||
}
|
||||
/**
|
||||
* A param for search reservation descriptions
|
||||
*/
|
||||
description?:
|
||||
| string
|
||||
| {
|
||||
/**
|
||||
* filter by reservation description containing search string.
|
||||
*/
|
||||
contains?: string
|
||||
/**
|
||||
* filter by reservation description starting with search string.
|
||||
*/
|
||||
starts_with?: string
|
||||
/**
|
||||
* filter by reservation description ending with search string.
|
||||
*/
|
||||
ends_with?: string
|
||||
}
|
||||
/**
|
||||
* Date comparison for when resulting reservations were created.
|
||||
*/
|
||||
created_at?: {
|
||||
/**
|
||||
* filter by dates less than this date
|
||||
*/
|
||||
lt?: string
|
||||
/**
|
||||
* filter by dates greater than this date
|
||||
*/
|
||||
gt?: string
|
||||
/**
|
||||
* filter by dates less than or equal to this date
|
||||
*/
|
||||
lte?: string
|
||||
/**
|
||||
* filter by dates greater than or equal to this date
|
||||
*/
|
||||
gte?: string
|
||||
}
|
||||
/**
|
||||
* How many Reservations to skip in the result.
|
||||
*/
|
||||
|
||||
@@ -60,6 +60,7 @@ export default class ReservationItemService {
|
||||
const itemRepository = manager.getRepository(ReservationItem)
|
||||
|
||||
const query = buildQuery(selector, config) as FindManyOptions
|
||||
|
||||
return await itemRepository.find(query)
|
||||
}
|
||||
|
||||
@@ -79,6 +80,7 @@ export default class ReservationItemService {
|
||||
const itemRepository = manager.getRepository(ReservationItem)
|
||||
|
||||
const query = buildQuery(selector, config) as FindManyOptions
|
||||
|
||||
return await itemRepository.findAndCount(query)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { EntityManager, FindOptionsWhere, ILike } from "typeorm"
|
||||
import {
|
||||
ExtendedFindConfig,
|
||||
FilterableInventoryItemProps,
|
||||
FindConfig,
|
||||
} from "@medusajs/types"
|
||||
import { objectToStringPath, buildQuery } from "@medusajs/utils"
|
||||
import { EntityManager, FindOptionsWhere, ILike } from "typeorm"
|
||||
import { InventoryItem } from "../models"
|
||||
import { InventoryItem, ReservationItem } from "../models"
|
||||
import { buildQuery, objectToStringPath } from "@medusajs/utils"
|
||||
|
||||
export function getListQuery(
|
||||
manager: EntityManager,
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
import { IsArray, IsOptional, IsString, ValidateNested } from "class-validator"
|
||||
import {
|
||||
DateComparisonOperator,
|
||||
NumericalComparisonOperator,
|
||||
StringComparisonOperator,
|
||||
extendedFindParamsMixin,
|
||||
} from "../../../../types/common"
|
||||
import { IsArray, IsOptional, IsString, ValidateNested } from "class-validator"
|
||||
import { Request, Response } from "express"
|
||||
|
||||
import { EntityManager } from "typeorm"
|
||||
@@ -65,6 +67,45 @@ import { joinLineItems } from "./utils/join-line-items"
|
||||
* gte:
|
||||
* type: number
|
||||
* description: filter by reservation quantity greater than or equal to this number
|
||||
* - in: query
|
||||
* name: description
|
||||
* description: A param for search reservation descriptions
|
||||
* schema:
|
||||
* oneOf:
|
||||
* - type: string
|
||||
* - type: object
|
||||
* properties:
|
||||
* contains:
|
||||
* type: string
|
||||
* description: filter by reservation description containing search string.
|
||||
* starts_with:
|
||||
* type: string
|
||||
* description: filter by reservation description starting with search string.
|
||||
* ends_with:
|
||||
* type: string
|
||||
* description: filter by reservation description ending with search string.
|
||||
* - in: query
|
||||
* name: created_at
|
||||
* description: Date comparison for when resulting reservations were created.
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* lt:
|
||||
* type: string
|
||||
* description: filter by dates less than this date
|
||||
* format: date
|
||||
* gt:
|
||||
* type: string
|
||||
* description: filter by dates greater than this date
|
||||
* format: date
|
||||
* lte:
|
||||
* type: string
|
||||
* description: filter by dates less than or equal to this date
|
||||
* format: date
|
||||
* gte:
|
||||
* type: string
|
||||
* description: filter by dates greater than or equal to this date
|
||||
* format: date
|
||||
* - (query) offset=0 {integer} How many Reservations to skip in the result.
|
||||
* - (query) limit=20 {integer} Limit the number of Reservations returned.
|
||||
* - (query) expand {string} (Comma separated) Which fields should be expanded in the product category.
|
||||
@@ -184,4 +225,13 @@ export class AdminGetReservationsParams extends extendedFindParamsMixin({
|
||||
@ValidateNested()
|
||||
@Type(() => NumericalComparisonOperator)
|
||||
quantity?: NumericalComparisonOperator
|
||||
|
||||
@IsOptional()
|
||||
@ValidateNested()
|
||||
@Type(() => DateComparisonOperator)
|
||||
created_at?: DateComparisonOperator
|
||||
|
||||
@IsOptional()
|
||||
@IsType([StringComparisonOperator, String])
|
||||
description?: string | StringComparisonOperator
|
||||
}
|
||||
|
||||
@@ -1,12 +1,5 @@
|
||||
import { Transform, Type } from "class-transformer"
|
||||
import {
|
||||
IsDate,
|
||||
IsNumber,
|
||||
IsObject,
|
||||
IsOptional,
|
||||
IsString,
|
||||
} from "class-validator"
|
||||
import "reflect-metadata"
|
||||
|
||||
import {
|
||||
FindManyOptions,
|
||||
FindOneOptions,
|
||||
@@ -15,11 +8,22 @@ import {
|
||||
FindOptionsWhere,
|
||||
OrderByCondition,
|
||||
} from "typeorm"
|
||||
import {
|
||||
IsDate,
|
||||
IsNumber,
|
||||
IsObject,
|
||||
IsOptional,
|
||||
IsString,
|
||||
Validate,
|
||||
} from "class-validator"
|
||||
import { Transform, Type } from "class-transformer"
|
||||
|
||||
import { BaseEntity } from "../interfaces"
|
||||
import { ClassConstructor } from "./global"
|
||||
import { ExactlyOne } from "./validators/exactly-one"
|
||||
import { FindOptionsOrder } from "typeorm/find-options/FindOptionsOrder"
|
||||
import { FindOptionsRelations } from "typeorm/find-options/FindOptionsRelations"
|
||||
import { BaseEntity } from "../interfaces"
|
||||
import { transformDate } from "../utils/validators/date-transform"
|
||||
import { ClassConstructor } from "./global"
|
||||
|
||||
/**
|
||||
* Utility type used to remove some optional attributes (coming from K) from a type T
|
||||
@@ -162,6 +166,18 @@ export class StringComparisonOperator {
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
lte?: string
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
contains?: string
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
starts_with?: string
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
ends_with?: string
|
||||
}
|
||||
|
||||
export class NumericalComparisonOperator {
|
||||
|
||||
@@ -5,6 +5,7 @@ import {
|
||||
FindOptionsRelations,
|
||||
FindOptionsSelect,
|
||||
FindOptionsWhere,
|
||||
ILike,
|
||||
In,
|
||||
IsNull,
|
||||
LessThan,
|
||||
@@ -12,8 +13,9 @@ import {
|
||||
MoreThan,
|
||||
MoreThanOrEqual,
|
||||
} from "typeorm"
|
||||
import { FindOptionsOrder } from "typeorm/find-options/FindOptionsOrder"
|
||||
import { ExtendedFindConfig, FindConfig } from "../types/common"
|
||||
|
||||
import { FindOptionsOrder } from "typeorm/find-options/FindOptionsOrder"
|
||||
import { isObject } from "./is-object"
|
||||
|
||||
/**
|
||||
@@ -120,6 +122,15 @@ function buildWhere<TWhereKeys extends object, TEntity>(
|
||||
case "gte":
|
||||
where[key].push(MoreThanOrEqual(objectValue))
|
||||
break
|
||||
case "contains":
|
||||
where[key].push(ILike(`%${objectValue}%`))
|
||||
break
|
||||
case "starts_with":
|
||||
where[key].push(ILike(`${objectValue}%`))
|
||||
break
|
||||
case "ends_with":
|
||||
where[key].push(ILike(`%${objectValue}`))
|
||||
break
|
||||
default:
|
||||
if (objectValue != undefined && typeof objectValue === "object") {
|
||||
where[key] = buildWhere<any, TEntity>(objectValue)
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
FindOptionsWhere,
|
||||
OrderByCondition,
|
||||
} from "typeorm"
|
||||
|
||||
import { FindOptionsOrder } from "typeorm/find-options/FindOptionsOrder"
|
||||
import { FindOptionsRelations } from "typeorm/find-options/FindOptionsRelations"
|
||||
|
||||
@@ -138,6 +139,9 @@ export interface StringComparisonOperator {
|
||||
gt?: string
|
||||
gte?: string
|
||||
lte?: string
|
||||
contains?: string
|
||||
starts_with?: string
|
||||
ends_with?: string
|
||||
}
|
||||
|
||||
export interface NumericalComparisonOperator {
|
||||
|
||||
@@ -204,7 +204,7 @@ export type FilterableReservationItemProps = {
|
||||
line_item_id?: string | string[]
|
||||
inventory_item_id?: string | string[]
|
||||
location_id?: string | string[]
|
||||
description?: string
|
||||
description?: string | StringComparisonOperator
|
||||
created_by?: string | string[]
|
||||
quantity?: number | NumericalComparisonOperator
|
||||
}
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import { ExtendedFindConfig, FindConfig } from "@medusajs/types"
|
||||
import {
|
||||
And,
|
||||
FindManyOptions,
|
||||
@@ -6,6 +5,7 @@ import {
|
||||
FindOptionsRelations,
|
||||
FindOptionsSelect,
|
||||
FindOptionsWhere,
|
||||
ILike,
|
||||
In,
|
||||
IsNull,
|
||||
LessThan,
|
||||
@@ -13,6 +13,8 @@ import {
|
||||
MoreThan,
|
||||
MoreThanOrEqual,
|
||||
} from "typeorm"
|
||||
import { ExtendedFindConfig, FindConfig } from "@medusajs/types"
|
||||
|
||||
import { FindOptionsOrder } from "typeorm/find-options/FindOptionsOrder"
|
||||
import { isObject } from "./is-object"
|
||||
|
||||
@@ -120,6 +122,15 @@ function buildWhere<TWhereKeys extends object, TEntity>(
|
||||
case "gte":
|
||||
where[key].push(MoreThanOrEqual(objectValue))
|
||||
break
|
||||
case "contains":
|
||||
where[key].push(ILike(`%${objectValue}%`))
|
||||
break
|
||||
case "starts_with":
|
||||
where[key].push(ILike(`${objectValue}%`))
|
||||
break
|
||||
case "ends_with":
|
||||
where[key].push(ILike(`%${objectValue}`))
|
||||
break
|
||||
default:
|
||||
if (objectValue != undefined && typeof objectValue === "object") {
|
||||
where[key] = buildWhere<any, TEntity>(objectValue)
|
||||
|
||||
Reference in New Issue
Block a user