fix(medusa): add q param to listAndCount product types and product tags (#1531)
This commit is contained in:
@@ -22,3 +22,26 @@ Array [
|
|||||||
},
|
},
|
||||||
]
|
]
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
exports[`/admin/product-tags GET /admin/product-tags returns a list of product tags matching free text search param 1`] = `
|
||||||
|
Array [
|
||||||
|
Object {
|
||||||
|
"created_at": Any<String>,
|
||||||
|
"id": "tag1",
|
||||||
|
"updated_at": Any<String>,
|
||||||
|
"value": "123",
|
||||||
|
},
|
||||||
|
Object {
|
||||||
|
"created_at": Any<String>,
|
||||||
|
"id": "tag3",
|
||||||
|
"updated_at": Any<String>,
|
||||||
|
"value": "123",
|
||||||
|
},
|
||||||
|
Object {
|
||||||
|
"created_at": Any<String>,
|
||||||
|
"id": "tag4",
|
||||||
|
"updated_at": Any<String>,
|
||||||
|
"value": "123",
|
||||||
|
},
|
||||||
|
]
|
||||||
|
`;
|
||||||
|
|||||||
@@ -0,0 +1,29 @@
|
|||||||
|
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||||
|
|
||||||
|
exports[`/admin/product-types GET /admin/product-types returns a list of product types 1`] = `
|
||||||
|
Array [
|
||||||
|
Object {
|
||||||
|
"created_at": Any<String>,
|
||||||
|
"id": "test-type",
|
||||||
|
"updated_at": Any<String>,
|
||||||
|
"value": "test-type",
|
||||||
|
},
|
||||||
|
Object {
|
||||||
|
"created_at": Any<String>,
|
||||||
|
"id": "test-type-new",
|
||||||
|
"updated_at": Any<String>,
|
||||||
|
"value": "test-type-new",
|
||||||
|
},
|
||||||
|
]
|
||||||
|
`;
|
||||||
|
|
||||||
|
exports[`/admin/product-types GET /admin/product-types returns a list of product types matching free text search param 1`] = `
|
||||||
|
Array [
|
||||||
|
Object {
|
||||||
|
"created_at": Any<String>,
|
||||||
|
"id": "test-type-new",
|
||||||
|
"updated_at": Any<String>,
|
||||||
|
"value": "test-type-new",
|
||||||
|
},
|
||||||
|
]
|
||||||
|
`;
|
||||||
@@ -68,5 +68,38 @@ describe("/admin/product-tags", () => {
|
|||||||
tagMatch,
|
tagMatch,
|
||||||
])
|
])
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it("returns a list of product tags matching free text search param", async () => {
|
||||||
|
const api = useApi()
|
||||||
|
|
||||||
|
const res = await api
|
||||||
|
.get("/admin/product-tags?q=123", {
|
||||||
|
headers: {
|
||||||
|
Authorization: "Bearer test_token",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
console.log(err)
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(res.status).toEqual(200)
|
||||||
|
|
||||||
|
const tagMatch = {
|
||||||
|
created_at: expect.any(String),
|
||||||
|
updated_at: expect.any(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(res.data.product_tags.map((pt) => pt.value)).toEqual([
|
||||||
|
"123",
|
||||||
|
"123",
|
||||||
|
"123",
|
||||||
|
])
|
||||||
|
|
||||||
|
expect(res.data.product_tags).toMatchSnapshot([
|
||||||
|
tagMatch,
|
||||||
|
tagMatch,
|
||||||
|
tagMatch,
|
||||||
|
])
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -0,0 +1,102 @@
|
|||||||
|
const path = require("path")
|
||||||
|
|
||||||
|
const setupServer = require("../../../helpers/setup-server")
|
||||||
|
const { useApi } = require("../../../helpers/use-api")
|
||||||
|
const { initDb, useDb } = require("../../../helpers/use-db")
|
||||||
|
|
||||||
|
const adminSeeder = require("../../helpers/admin-seeder")
|
||||||
|
const productSeeder = require("../../helpers/product-seeder")
|
||||||
|
|
||||||
|
jest.setTimeout(50000)
|
||||||
|
|
||||||
|
describe("/admin/product-types", () => {
|
||||||
|
let medusaProcess
|
||||||
|
let dbConnection
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
const cwd = path.resolve(path.join(__dirname, "..", ".."))
|
||||||
|
dbConnection = await initDb({ cwd })
|
||||||
|
medusaProcess = await setupServer({ cwd })
|
||||||
|
})
|
||||||
|
|
||||||
|
afterAll(async () => {
|
||||||
|
const db = useDb()
|
||||||
|
await db.shutdown()
|
||||||
|
|
||||||
|
medusaProcess.kill()
|
||||||
|
})
|
||||||
|
|
||||||
|
describe("GET /admin/product-types", () => {
|
||||||
|
beforeEach(async () => {
|
||||||
|
try {
|
||||||
|
await productSeeder(dbConnection)
|
||||||
|
await adminSeeder(dbConnection)
|
||||||
|
} catch (err) {
|
||||||
|
console.log(err)
|
||||||
|
throw err
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
afterEach(async () => {
|
||||||
|
const db = useDb()
|
||||||
|
await db.teardown()
|
||||||
|
})
|
||||||
|
|
||||||
|
it("returns a list of product types", async () => {
|
||||||
|
const api = useApi()
|
||||||
|
|
||||||
|
const res = await api
|
||||||
|
.get("/admin/product-types", {
|
||||||
|
headers: {
|
||||||
|
Authorization: "Bearer test_token",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
console.log(err)
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(res.status).toEqual(200)
|
||||||
|
|
||||||
|
const typeMatch = {
|
||||||
|
created_at: expect.any(String),
|
||||||
|
updated_at: expect.any(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(res.data.product_types).toMatchSnapshot([
|
||||||
|
typeMatch,
|
||||||
|
typeMatch,
|
||||||
|
])
|
||||||
|
})
|
||||||
|
|
||||||
|
it("returns a list of product types matching free text search param", async () => {
|
||||||
|
const api = useApi()
|
||||||
|
|
||||||
|
const res = await api
|
||||||
|
.get("/admin/product-types?q=test-type-new", {
|
||||||
|
headers: {
|
||||||
|
Authorization: "Bearer test_token",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
console.log(err)
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(res.status).toEqual(200)
|
||||||
|
|
||||||
|
const typeMatch = {
|
||||||
|
created_at: expect.any(String),
|
||||||
|
updated_at: expect.any(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
// The value of the type should match the search param
|
||||||
|
expect(res.data.product_types.map((pt) => pt.value)).toEqual([
|
||||||
|
"test-type-new"
|
||||||
|
])
|
||||||
|
|
||||||
|
// Should only return one type as there is only one match to the search param
|
||||||
|
expect(res.data.product_types).toMatchSnapshot([
|
||||||
|
typeMatch
|
||||||
|
])
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -69,6 +69,13 @@ module.exports = async (connection, data = {}) => {
|
|||||||
|
|
||||||
await manager.save(type)
|
await manager.save(type)
|
||||||
|
|
||||||
|
const type2 = await manager.create(ProductType, {
|
||||||
|
id: "test-type-new",
|
||||||
|
value: "test-type-new",
|
||||||
|
})
|
||||||
|
|
||||||
|
await manager.save(type2)
|
||||||
|
|
||||||
const image = manager.create(Image, {
|
const image = manager.create(Image, {
|
||||||
id: "test-image",
|
id: "test-image",
|
||||||
url: "test-image.png",
|
url: "test-image.png",
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { Type } from "class-transformer"
|
import { Type } from "class-transformer"
|
||||||
|
import { IsNumber, IsOptional, IsString } from "class-validator"
|
||||||
|
import { identity, omit, pickBy } from "lodash"
|
||||||
import { MedusaError } from "medusa-core-utils"
|
import { MedusaError } from "medusa-core-utils"
|
||||||
import { IsNumber, IsString, IsOptional, ValidateNested } from "class-validator"
|
|
||||||
import { omit, pickBy, identity } from "lodash"
|
|
||||||
import {
|
import {
|
||||||
allowedAdminProductTagsFields,
|
allowedAdminProductTagsFields,
|
||||||
defaultAdminProductTagsFields,
|
defaultAdminProductTagsFields,
|
||||||
@@ -10,9 +10,9 @@ import {
|
|||||||
import { ProductTag } from "../../../../models/product-tag"
|
import { ProductTag } from "../../../../models/product-tag"
|
||||||
import ProductTagService from "../../../../services/product-tag"
|
import ProductTagService from "../../../../services/product-tag"
|
||||||
import {
|
import {
|
||||||
StringComparisonOperator,
|
|
||||||
DateComparisonOperator,
|
DateComparisonOperator,
|
||||||
FindConfig,
|
FindConfig,
|
||||||
|
StringComparisonOperator,
|
||||||
} from "../../../../types/common"
|
} from "../../../../types/common"
|
||||||
import { validator } from "../../../../utils/validator"
|
import { validator } from "../../../../utils/validator"
|
||||||
import { IsType } from "../../../../utils/validators/is-type"
|
import { IsType } from "../../../../utils/validators/is-type"
|
||||||
@@ -100,12 +100,14 @@ export class AdminGetProductTagsPaginationParams {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class AdminGetProductTagsParams extends AdminGetProductTagsPaginationParams {
|
export class AdminGetProductTagsParams extends AdminGetProductTagsPaginationParams {
|
||||||
@ValidateNested()
|
|
||||||
@IsType([String, [String], StringComparisonOperator])
|
@IsType([String, [String], StringComparisonOperator])
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
id?: string | string[] | StringComparisonOperator
|
id?: string | string[] | StringComparisonOperator
|
||||||
|
|
||||||
@ValidateNested()
|
@IsString()
|
||||||
|
@IsOptional()
|
||||||
|
q?: string
|
||||||
|
|
||||||
@IsType([String, [String], StringComparisonOperator])
|
@IsType([String, [String], StringComparisonOperator])
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
value?: string | string[] | StringComparisonOperator
|
value?: string | string[] | StringComparisonOperator
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { Type } from "class-transformer"
|
import { Type } from "class-transformer"
|
||||||
|
import { IsNumber, IsOptional, IsString } from "class-validator"
|
||||||
|
import { identity, omit, pickBy } from "lodash"
|
||||||
import { MedusaError } from "medusa-core-utils"
|
import { MedusaError } from "medusa-core-utils"
|
||||||
import { IsNumber, IsString, IsOptional, ValidateNested } from "class-validator"
|
|
||||||
import { omit, pickBy, identity } from "lodash"
|
|
||||||
import {
|
import {
|
||||||
allowedAdminProductTypeFields,
|
allowedAdminProductTypeFields,
|
||||||
defaultAdminProductTypeFields,
|
defaultAdminProductTypeFields,
|
||||||
@@ -10,9 +10,9 @@ import {
|
|||||||
import { ProductType } from "../../../../models/product-type"
|
import { ProductType } from "../../../../models/product-type"
|
||||||
import ProductTypeService from "../../../../services/product-type"
|
import ProductTypeService from "../../../../services/product-type"
|
||||||
import {
|
import {
|
||||||
StringComparisonOperator,
|
|
||||||
DateComparisonOperator,
|
DateComparisonOperator,
|
||||||
FindConfig,
|
FindConfig,
|
||||||
|
StringComparisonOperator,
|
||||||
} from "../../../../types/common"
|
} from "../../../../types/common"
|
||||||
import { validator } from "../../../../utils/validator"
|
import { validator } from "../../../../utils/validator"
|
||||||
import { IsType } from "../../../../utils/validators/is-type"
|
import { IsType } from "../../../../utils/validators/is-type"
|
||||||
@@ -101,12 +101,14 @@ export class AdminGetProductTypesPaginationParams {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class AdminGetProductTypesParams extends AdminGetProductTypesPaginationParams {
|
export class AdminGetProductTypesParams extends AdminGetProductTypesPaginationParams {
|
||||||
@ValidateNested()
|
|
||||||
@IsType([String, [String], StringComparisonOperator])
|
@IsType([String, [String], StringComparisonOperator])
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
id?: string | string[] | StringComparisonOperator
|
id?: string | string[] | StringComparisonOperator
|
||||||
|
|
||||||
@ValidateNested()
|
@IsString()
|
||||||
|
@IsOptional()
|
||||||
|
q?: string
|
||||||
|
|
||||||
@IsType([String, [String], StringComparisonOperator])
|
@IsType([String, [String], StringComparisonOperator])
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
value?: string | string[] | StringComparisonOperator
|
value?: string | string[] | StringComparisonOperator
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
import { EntityManager } from "typeorm"
|
|
||||||
import { BaseService } from "medusa-interfaces"
|
|
||||||
import { MedusaError } from "medusa-core-utils"
|
import { MedusaError } from "medusa-core-utils"
|
||||||
import { ProductTagRepository } from "../repositories/product-tag"
|
import { BaseService } from "medusa-interfaces"
|
||||||
|
import { EntityManager, ILike, SelectQueryBuilder } from "typeorm"
|
||||||
import { ProductTag } from "../models/product-tag"
|
import { ProductTag } from "../models/product-tag"
|
||||||
|
import { ProductTagRepository } from "../repositories/product-tag"
|
||||||
import { FindConfig } from "../types/common"
|
import { FindConfig } from "../types/common"
|
||||||
import { FilterableProductTagProps } from "../types/product"
|
import { FilterableProductTagProps } from "../types/product"
|
||||||
|
|
||||||
@@ -107,7 +107,24 @@ class ProductTagService extends BaseService {
|
|||||||
): Promise<[ProductTag[], number]> {
|
): Promise<[ProductTag[], number]> {
|
||||||
const tagRepo = this.manager_.getCustomRepository(this.tagRepo_)
|
const tagRepo = this.manager_.getCustomRepository(this.tagRepo_)
|
||||||
|
|
||||||
|
let q: string | undefined = undefined
|
||||||
|
if ("q" in selector) {
|
||||||
|
q = selector.q
|
||||||
|
delete selector.q
|
||||||
|
}
|
||||||
|
|
||||||
const query = this.buildQuery_(selector, config)
|
const query = this.buildQuery_(selector, config)
|
||||||
|
|
||||||
|
if (q) {
|
||||||
|
const where = query.where
|
||||||
|
|
||||||
|
delete where.value
|
||||||
|
|
||||||
|
query.where = (qb: SelectQueryBuilder<ProductTag>): void => {
|
||||||
|
qb.where(where).andWhere([{ value: ILike(`%${q}%`) }])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return await tagRepo.findAndCount(query)
|
return await tagRepo.findAndCount(query)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
import { MedusaError } from "medusa-core-utils"
|
import { MedusaError } from "medusa-core-utils"
|
||||||
import { BaseService } from "medusa-interfaces"
|
import { BaseService } from "medusa-interfaces"
|
||||||
import { EntityManager } from "typeorm"
|
import { EntityManager, ILike, SelectQueryBuilder } from "typeorm"
|
||||||
import { FindConfig } from "../types/common"
|
|
||||||
import { FilterableProductTypeProps } from "../types/product"
|
|
||||||
import { ProductType } from "../models/product-type"
|
import { ProductType } from "../models/product-type"
|
||||||
import { ProductTypeRepository } from "../repositories/product-type"
|
import { ProductTypeRepository } from "../repositories/product-type"
|
||||||
|
import { FindConfig } from "../types/common"
|
||||||
|
import { FilterableProductTypeProps } from "../types/product"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provides layer to manipulate products.
|
* Provides layer to manipulate products.
|
||||||
@@ -91,7 +91,24 @@ class ProductTypeService extends BaseService {
|
|||||||
): Promise<[ProductType[], number]> {
|
): Promise<[ProductType[], number]> {
|
||||||
const typeRepo = this.manager_.getCustomRepository(this.typeRepository_)
|
const typeRepo = this.manager_.getCustomRepository(this.typeRepository_)
|
||||||
|
|
||||||
|
let q: string | undefined = undefined
|
||||||
|
if ("q" in selector) {
|
||||||
|
q = selector.q
|
||||||
|
delete selector.q
|
||||||
|
}
|
||||||
|
|
||||||
const query = this.buildQuery_(selector, config)
|
const query = this.buildQuery_(selector, config)
|
||||||
|
|
||||||
|
if (q) {
|
||||||
|
const where = query.where
|
||||||
|
|
||||||
|
delete where.value
|
||||||
|
|
||||||
|
query.where = (qb: SelectQueryBuilder<ProductType>): void => {
|
||||||
|
qb.where(where).andWhere([{ value: ILike(`%${q}%`) }])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return await typeRepo.findAndCount(query)
|
return await typeRepo.findAndCount(query)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -81,33 +81,45 @@ export class FilterableProductProps {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class FilterableProductTagProps {
|
export class FilterableProductTagProps {
|
||||||
@ValidateNested()
|
@IsOptional()
|
||||||
@IsType([String, [String], StringComparisonOperator])
|
@IsType([String, [String], StringComparisonOperator])
|
||||||
id?: string | string[] | StringComparisonOperator
|
id?: string | string[] | StringComparisonOperator
|
||||||
|
|
||||||
@ValidateNested()
|
@IsOptional()
|
||||||
@IsType([String, [String], StringComparisonOperator])
|
@IsType([String, [String], StringComparisonOperator])
|
||||||
value?: string | string[] | StringComparisonOperator
|
value?: string | string[] | StringComparisonOperator
|
||||||
|
|
||||||
|
@IsOptional()
|
||||||
@IsType([DateComparisonOperator])
|
@IsType([DateComparisonOperator])
|
||||||
created_at?: DateComparisonOperator
|
created_at?: DateComparisonOperator
|
||||||
|
|
||||||
|
@IsOptional()
|
||||||
@IsType([DateComparisonOperator])
|
@IsType([DateComparisonOperator])
|
||||||
updated_at?: DateComparisonOperator
|
updated_at?: DateComparisonOperator
|
||||||
|
|
||||||
|
@IsString()
|
||||||
|
@IsOptional()
|
||||||
|
q?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export class FilterableProductTypeProps {
|
export class FilterableProductTypeProps {
|
||||||
@ValidateNested()
|
@IsOptional()
|
||||||
@IsType([String, [String], StringComparisonOperator])
|
@IsType([String, [String], StringComparisonOperator])
|
||||||
id?: string | string[] | StringComparisonOperator
|
id?: string | string[] | StringComparisonOperator
|
||||||
|
|
||||||
@ValidateNested()
|
@IsOptional()
|
||||||
@IsType([String, [String], StringComparisonOperator])
|
@IsType([String, [String], StringComparisonOperator])
|
||||||
value?: string | string[] | StringComparisonOperator
|
value?: string | string[] | StringComparisonOperator
|
||||||
|
|
||||||
|
@IsOptional()
|
||||||
@IsType([DateComparisonOperator])
|
@IsType([DateComparisonOperator])
|
||||||
created_at?: DateComparisonOperator
|
created_at?: DateComparisonOperator
|
||||||
|
|
||||||
|
@IsOptional()
|
||||||
@IsType([DateComparisonOperator])
|
@IsType([DateComparisonOperator])
|
||||||
updated_at?: DateComparisonOperator
|
updated_at?: DateComparisonOperator
|
||||||
|
|
||||||
|
@IsString()
|
||||||
|
@IsOptional()
|
||||||
|
q?: string
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user