feat: price list products (#1239)
* feat: add product list for price lists * feat: add product list for price lists * refactor: product list controller * fix: add integration test for price list products * fix: use getListConfig
This commit is contained in:
@@ -4,6 +4,10 @@ const setupServer = require("../../../helpers/setup-server")
|
||||
const { useApi } = require("../../../helpers/use-api")
|
||||
const { useDb, initDb } = require("../../../helpers/use-db")
|
||||
|
||||
const {
|
||||
simpleProductFactory,
|
||||
simplePriceListFactory,
|
||||
} = require("../../factories")
|
||||
const adminSeeder = require("../../helpers/admin-seeder")
|
||||
const customerSeeder = require("../../helpers/customer-seeder")
|
||||
const priceListSeeder = require("../../helpers/price-list-seeder")
|
||||
@@ -750,4 +754,101 @@ describe("/admin/price-lists", () => {
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("GET /admin/price-lists/:id/products", () => {
|
||||
let tag
|
||||
beforeEach(async () => {
|
||||
try {
|
||||
await adminSeeder(dbConnection)
|
||||
|
||||
await simpleProductFactory(
|
||||
dbConnection,
|
||||
{
|
||||
id: "test-prod-1",
|
||||
title: "MedusaHeadphones",
|
||||
variants: [{ id: "test-variant-1" }, { id: "test-variant-2" }],
|
||||
},
|
||||
1
|
||||
)
|
||||
|
||||
const prod = await simpleProductFactory(
|
||||
dbConnection,
|
||||
{
|
||||
id: "test-prod-2",
|
||||
tags: ["test-tag"],
|
||||
variants: [{ id: "test-variant-3" }, { id: "test-variant-4" }],
|
||||
},
|
||||
2
|
||||
)
|
||||
|
||||
tag = prod.tags[0].id
|
||||
|
||||
await simpleProductFactory(
|
||||
dbConnection,
|
||||
{
|
||||
id: "test-prod-3",
|
||||
variants: [{ id: "test-variant-5" }],
|
||||
},
|
||||
3
|
||||
)
|
||||
|
||||
await simplePriceListFactory(dbConnection, {
|
||||
id: "test-list",
|
||||
prices: [
|
||||
{ variant_id: "test-variant-1", currency_code: "usd", amount: 100 },
|
||||
{ variant_id: "test-variant-4", currency_code: "usd", amount: 100 },
|
||||
],
|
||||
})
|
||||
} catch (err) {
|
||||
console.log(err)
|
||||
throw err
|
||||
}
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
const db = useDb()
|
||||
await db.teardown()
|
||||
})
|
||||
|
||||
it("lists only product 1, 2", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const response = await api
|
||||
.get(`/admin/price-lists/test-list/products?order=-created_at`, {
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
},
|
||||
})
|
||||
.catch((err) => {
|
||||
console.warn(err.response.data)
|
||||
})
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.count).toEqual(2)
|
||||
expect(response.data.products).toEqual([
|
||||
expect.objectContaining({ id: "test-prod-1" }),
|
||||
expect.objectContaining({ id: "test-prod-2" }),
|
||||
])
|
||||
})
|
||||
|
||||
it("lists only product 2", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const response = await api
|
||||
.get(`/admin/price-lists/test-list/products?tags[]=${tag}`, {
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
},
|
||||
})
|
||||
.catch((err) => {
|
||||
console.warn(err.response.data)
|
||||
})
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.count).toEqual(1)
|
||||
expect(response.data.products).toEqual([
|
||||
expect.objectContaining({ id: "test-prod-2" }),
|
||||
])
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -11,3 +11,4 @@ export * from "./simple-tax-rate-factory"
|
||||
export * from "./simple-shipping-option-factory"
|
||||
export * from "./simple-shipping-method-factory"
|
||||
export * from "./simple-product-type-tax-rate-factory"
|
||||
export * from "./simple-price-list-factory"
|
||||
|
||||
66
integration-tests/api/factories/simple-price-list-factory.ts
Normal file
66
integration-tests/api/factories/simple-price-list-factory.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
import {
|
||||
PriceList,
|
||||
MoneyAmount,
|
||||
PriceListType,
|
||||
PriceListStatus,
|
||||
} from "@medusajs/medusa"
|
||||
import faker from "faker"
|
||||
import { Connection } from "typeorm"
|
||||
|
||||
type ProductListPrice = {
|
||||
variant_id: string
|
||||
currency_code: string
|
||||
region_id: string
|
||||
amount: number
|
||||
}
|
||||
|
||||
export type PriceListFactoryData = {
|
||||
id?: string
|
||||
name?: string
|
||||
description?: string
|
||||
type?: PriceListType
|
||||
status?: PriceListStatus
|
||||
starts_at?: Date
|
||||
ends_at?: Date
|
||||
customer_groups?: string[]
|
||||
prices?: ProductListPrice[]
|
||||
}
|
||||
|
||||
export const simplePriceListFactory = async (
|
||||
connection: Connection,
|
||||
data: PriceListFactoryData = {},
|
||||
seed?: number
|
||||
): Promise<PriceList> => {
|
||||
if (typeof seed !== "undefined") {
|
||||
faker.seed(seed)
|
||||
}
|
||||
|
||||
const manager = connection.manager
|
||||
|
||||
const listId = data.id || `simple-price-list-${Math.random() * 1000}`
|
||||
const toCreate = {
|
||||
id: listId,
|
||||
name: data.name || faker.commerce.productName(),
|
||||
description: data.description || "Some text",
|
||||
status: data.status || PriceListStatus.ACTIVE,
|
||||
type: data.type || PriceListType.OVERRIDE,
|
||||
starts_at: data.starts_at || null,
|
||||
ends_at: data.ends_at || null,
|
||||
}
|
||||
|
||||
const toSave = manager.create(PriceList, toCreate)
|
||||
const toReturn = await manager.save(toSave)
|
||||
|
||||
if (typeof data.prices !== "undefined") {
|
||||
for (const ma of data.prices) {
|
||||
const factoryData = {
|
||||
...ma,
|
||||
price_list_id: listId,
|
||||
}
|
||||
const toSave = manager.create(MoneyAmount, factoryData)
|
||||
await manager.save(toSave)
|
||||
}
|
||||
}
|
||||
|
||||
return toReturn
|
||||
}
|
||||
Reference in New Issue
Block a user