fix(medusa): Support q search and order in list regions (#6202)

This commit is contained in:
Oli Juhl
2024-01-24 12:52:39 +01:00
committed by GitHub
parent 2b35700dbc
commit 8ad7539ebc
5 changed files with 91 additions and 5 deletions
@@ -1,5 +1,5 @@
import { Type } from "class-transformer"
import { IsOptional, ValidateNested } from "class-validator"
import { IsOptional, IsString, ValidateNested } from "class-validator"
import RegionService from "../../../../services/region"
import {
DateComparisonOperator,
@@ -13,6 +13,8 @@ import {
* description: "Retrieve a list of Regions. The regions can be filtered by fields such as `created_at`. The regions can also be paginated."
* x-authenticated: true
* parameters:
* - (query) q {string} Term used to search regions' name.
* - (query) order {string} A field to sort-order the retrieved regions by.
* - in: query
* name: limit
* schema:
@@ -190,6 +192,20 @@ export class AdminGetRegionsParams extends extendedFindParamsMixin({
limit: 50,
offset: 0,
}) {
/**
* Search parameter for regions.
*/
@IsString()
@IsOptional()
q?: string
/**
* The field to sort the data by. By default, the sort order is ascending. To change the order to descending, prefix the field name with `-`.
*/
@IsString()
@IsOptional()
order?: string
/**
* Date filters to apply on the regions' `created_at` date.
*/
+23 -2
View File
@@ -1,5 +1,5 @@
import { isDefined, MedusaError } from "medusa-core-utils"
import { DeepPartial, EntityManager } from "typeorm"
import { DeepPartial, EntityManager, FindOptionsWhere, ILike } from "typeorm"
import { Country, Currency, Region } from "../models"
import { FindConfig, Selector } from "../types/common"
import { CreateRegionInput, UpdateRegionInput } from "../types/region"
@@ -522,7 +522,7 @@ class RegionService extends TransactionBaseService {
* @return {Promise} result of the find operation
*/
async listAndCount(
selector: Selector<Region> = {},
selector: Selector<Region> & { q?: string } = {},
config: FindConfig<Region> = {
relations: [],
skip: 0,
@@ -533,7 +533,28 @@ class RegionService extends TransactionBaseService {
this.regionRepository_
)
let q: string | undefined
if (selector.q) {
q = selector.q
delete selector.q
}
const query = buildQuery(selector, config)
if (q) {
const where = query.where as FindOptionsWhere<Region>
delete where.name
query.where = [
{
...where,
name: ILike(`%${q}%`),
},
]
}
return await regionRepo.findAndCount(query)
}