fix(medusa): Support q search and order in list regions (#6202)
This commit is contained in:
1 parent
2b35700dbc
commit
8ad7539ebc
5 files changed
+91
-5
No files matched your search
@@ -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,
|
tax_rate: 0,
|
||||||
updated_at: new Date("10/10/2000"),
|
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 () => {
|
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 () => {
|
it("should only return non-deleted regions", async () => {
|
||||||
const api = useApi()
|
const api = useApi()
|
||||||
|
|
||||||
@@ -171,7 +207,7 @@ describe("/admin/regions", () => {
|
|||||||
console.log(err)
|
console.log(err)
|
||||||
})
|
})
|
||||||
|
|
||||||
expect(response.data.regions).toHaveLength(3)
|
expect(response.data.regions).toHaveLength(5)
|
||||||
expect(response.data.regions).toEqual(
|
expect(response.data.regions).toEqual(
|
||||||
expect.arrayContaining([
|
expect.arrayContaining([
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
@@ -194,7 +230,7 @@ describe("/admin/regions", () => {
|
|||||||
const response = await api.get(`/admin/regions?limit=2`, adminReqConfig)
|
const response = await api.get(`/admin/regions?limit=2`, adminReqConfig)
|
||||||
|
|
||||||
expect(response.data.regions).toHaveLength(2)
|
expect(response.data.regions).toHaveLength(2)
|
||||||
expect(response.data.count).toEqual(3)
|
expect(response.data.count).toEqual(5)
|
||||||
expect(response.status).toEqual(200)
|
expect(response.status).toEqual(200)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,14 @@
|
|||||||
import { SetRelation, Merge } from "../core/ModelUtils"
|
import { SetRelation, Merge } from "../core/ModelUtils"
|
||||||
|
|
||||||
export interface AdminGetRegionsParams {
|
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.
|
* Limit the number of regions returned.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { Type } from "class-transformer"
|
import { Type } from "class-transformer"
|
||||||
import { IsOptional, ValidateNested } from "class-validator"
|
import { IsOptional, IsString, ValidateNested } from "class-validator"
|
||||||
import RegionService from "../../../../services/region"
|
import RegionService from "../../../../services/region"
|
||||||
import {
|
import {
|
||||||
DateComparisonOperator,
|
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."
|
* 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
|
* x-authenticated: true
|
||||||
* parameters:
|
* parameters:
|
||||||
|
* - (query) q {string} Term used to search regions' name.
|
||||||
|
* - (query) order {string} A field to sort-order the retrieved regions by.
|
||||||
* - in: query
|
* - in: query
|
||||||
* name: limit
|
* name: limit
|
||||||
* schema:
|
* schema:
|
||||||
@@ -190,6 +192,20 @@ export class AdminGetRegionsParams extends extendedFindParamsMixin({
|
|||||||
limit: 50,
|
limit: 50,
|
||||||
offset: 0,
|
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.
|
* Date filters to apply on the regions' `created_at` date.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { isDefined, MedusaError } from "medusa-core-utils"
|
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 { Country, Currency, Region } from "../models"
|
||||||
import { FindConfig, Selector } from "../types/common"
|
import { FindConfig, Selector } from "../types/common"
|
||||||
import { CreateRegionInput, UpdateRegionInput } from "../types/region"
|
import { CreateRegionInput, UpdateRegionInput } from "../types/region"
|
||||||
@@ -522,7 +522,7 @@ class RegionService extends TransactionBaseService {
|
|||||||
* @return {Promise} result of the find operation
|
* @return {Promise} result of the find operation
|
||||||
*/
|
*/
|
||||||
async listAndCount(
|
async listAndCount(
|
||||||
selector: Selector<Region> = {},
|
selector: Selector<Region> & { q?: string } = {},
|
||||||
config: FindConfig<Region> = {
|
config: FindConfig<Region> = {
|
||||||
relations: [],
|
relations: [],
|
||||||
skip: 0,
|
skip: 0,
|
||||||
@@ -533,7 +533,28 @@ class RegionService extends TransactionBaseService {
|
|||||||
this.regionRepository_
|
this.regionRepository_
|
||||||
)
|
)
|
||||||
|
|
||||||
|
let q: string | undefined
|
||||||
|
|
||||||
|
if (selector.q) {
|
||||||
|
q = selector.q
|
||||||
|
delete selector.q
|
||||||
|
}
|
||||||
|
|
||||||
const query = buildQuery(selector, config)
|
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)
|
return await regionRepo.findAndCount(query)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user