fix(medusa): Support q search and order in list regions (#6202)
This commit is contained in:
5
.changeset/thick-insects-raise.md
Normal file
5
.changeset/thick-insects-raise.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@medusajs/medusa": patch
|
||||
---
|
||||
|
||||
fix(medusa): Support q search and order in list regions
|
||||
@@ -138,6 +138,20 @@ describe("/admin/regions", () => {
|
||||
tax_rate: 0,
|
||||
updated_at: new Date("10/10/2000"),
|
||||
})
|
||||
await manager.insert(Region, {
|
||||
id: "us-region",
|
||||
name: "United States",
|
||||
currency_code: "usd",
|
||||
tax_rate: 0,
|
||||
updated_at: new Date("10/10/2000"),
|
||||
})
|
||||
await manager.insert(Region, {
|
||||
id: "uk-region",
|
||||
name: "United Kingdom",
|
||||
currency_code: "gbp",
|
||||
tax_rate: 0,
|
||||
updated_at: new Date("10/10/2000"),
|
||||
})
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
@@ -162,6 +176,28 @@ describe("/admin/regions", () => {
|
||||
)
|
||||
})
|
||||
|
||||
it("should list the regions with q and order params", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const response = await api.get(
|
||||
"/admin/regions?q=united&order=currency_code",
|
||||
adminReqConfig
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
|
||||
expect(response.data.regions).toEqual([
|
||||
expect.objectContaining({
|
||||
name: "United Kingdom",
|
||||
currency_code: "gbp",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
name: "United States",
|
||||
currency_code: "usd",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should only return non-deleted regions", async () => {
|
||||
const api = useApi()
|
||||
|
||||
@@ -171,7 +207,7 @@ describe("/admin/regions", () => {
|
||||
console.log(err)
|
||||
})
|
||||
|
||||
expect(response.data.regions).toHaveLength(3)
|
||||
expect(response.data.regions).toHaveLength(5)
|
||||
expect(response.data.regions).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
@@ -194,7 +230,7 @@ describe("/admin/regions", () => {
|
||||
const response = await api.get(`/admin/regions?limit=2`, adminReqConfig)
|
||||
|
||||
expect(response.data.regions).toHaveLength(2)
|
||||
expect(response.data.count).toEqual(3)
|
||||
expect(response.data.count).toEqual(5)
|
||||
expect(response.status).toEqual(200)
|
||||
})
|
||||
|
||||
|
||||
@@ -4,6 +4,14 @@
|
||||
import { SetRelation, Merge } from "../core/ModelUtils"
|
||||
|
||||
export interface AdminGetRegionsParams {
|
||||
/**
|
||||
* Term used to search regions' name.
|
||||
*/
|
||||
q?: string
|
||||
/**
|
||||
* A field to sort-order the retrieved regions by.
|
||||
*/
|
||||
order?: string
|
||||
/**
|
||||
* Limit the number of regions returned.
|
||||
*/
|
||||
|
||||
@@ -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.
|
||||
*/
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user