feat(medusa): added list price list products endpoint (#6617)

what:

- adds an endpoint to list price list products
This commit is contained in:
Riqwan Thamir
2024-03-11 17:00:16 +00:00
committed by GitHub
parent c154336433
commit 7c46b0f88b
15 changed files with 514 additions and 516 deletions
@@ -1,255 +0,0 @@
import { simpleProductFactory } from "../../../../factories"
import {
IPricingModuleService,
PriceListStatus,
PriceListType,
} from "@medusajs/types"
import adminSeeder from "../../../../helpers/admin-seeder"
import { createVariantPriceSet } from "../../../helpers/create-variant-price-set"
import { medusaIntegrationTestRunner } from "medusa-test-utils"
jest.setTimeout(50000)
const adminHeaders = {
headers: {
"x-medusa-access-token": "test_token",
},
}
const env = {
MEDUSA_FF_MEDUSA_V2: true,
}
medusaIntegrationTestRunner({
env,
testSuite: ({ dbConnection, getContainer, api }) => {
describe.skip("GET /admin/price-lists/:id/products", () => {
let appContainer
let product
let product2
let variant
let pricingModuleService: IPricingModuleService
beforeAll(async () => {
appContainer = getContainer()
pricingModuleService = appContainer.resolve("pricingModuleService")
})
beforeEach(async () => {
await adminSeeder(dbConnection)
product = await simpleProductFactory(dbConnection, {
id: "test-product-with-variant",
title: "uniquely fun product",
variants: [
{
options: [{ option_id: "test-product-option-1", value: "test" }],
},
],
options: [
{
id: "test-product-option-1",
title: "Test option 1",
},
],
})
variant = product.variants[0]
product2 = await simpleProductFactory(dbConnection, {
id: "test-product-with-variant-2",
title: "uniquely fun product 2",
variants: [
{
options: [
{ option_id: "test-product-option-2", value: "test 2" },
],
},
],
options: [
{
id: "test-product-option-2",
title: "Test option 2",
},
],
})
})
it("should list all products in a price list", async () => {
const priceSet = await createVariantPriceSet({
container: appContainer,
variantId: variant.id,
prices: [
{
amount: 3000,
currency_code: "usd",
},
],
rules: [],
})
const [priceList] = await pricingModuleService.createPriceLists([
{
title: "test price list",
description: "test",
ends_at: new Date(),
starts_at: new Date(),
status: PriceListStatus.ACTIVE,
type: PriceListType.OVERRIDE,
prices: [
{
amount: 5000,
currency_code: "usd",
price_set_id: priceSet.id,
},
],
},
])
let response = await api.get(
`/admin/price-lists/${priceList.id}/products`,
adminHeaders
)
expect(response.status).toEqual(200)
expect(response.data.count).toEqual(1)
expect(response.data.products).toEqual([
expect.objectContaining({
id: expect.any(String),
title: expect.any(String),
handle: expect.any(String),
subtitle: null,
description: null,
is_giftcard: false,
status: "draft",
thumbnail: null,
weight: null,
length: null,
height: null,
width: null,
origin_country: null,
hs_code: null,
mid_code: null,
material: null,
collection_id: null,
collection: null,
type_id: null,
type: null,
discountable: true,
external_id: null,
created_at: expect.any(String),
updated_at: expect.any(String),
deleted_at: null,
metadata: null,
}),
])
response = await api.get(
`/admin/products?price_list_id[]=${priceList.id}`,
adminHeaders
)
expect(response.status).toEqual(200)
expect(response.data.count).toEqual(1)
expect(response.data.products).toEqual([
expect.objectContaining({
id: expect.any(String),
title: expect.any(String),
handle: expect.any(String),
subtitle: null,
description: null,
is_giftcard: false,
status: "draft",
thumbnail: null,
weight: null,
length: null,
height: null,
width: null,
origin_country: null,
hs_code: null,
mid_code: null,
material: null,
collection_id: null,
collection: null,
type_id: null,
type: null,
discountable: true,
external_id: null,
created_at: expect.any(String),
updated_at: expect.any(String),
deleted_at: null,
metadata: null,
}),
])
})
it("should list all products constrained by search query in a price list", async () => {
const priceSet = await createVariantPriceSet({
container: appContainer,
variantId: variant.id,
prices: [
{
amount: 3000,
currency_code: "usd",
},
],
rules: [],
})
const [priceList] = await pricingModuleService.createPriceLists([
{
title: "test price list",
description: "test",
ends_at: new Date(),
starts_at: new Date(),
status: PriceListStatus.ACTIVE,
type: PriceListType.OVERRIDE,
prices: [
{
amount: 5000,
currency_code: "usd",
price_set_id: priceSet.id,
},
],
},
])
let response = await api.get(
`/admin/price-lists/${priceList.id}/products?q=shouldnotreturnanything`,
adminHeaders
)
expect(response.status).toEqual(200)
expect(response.data.count).toEqual(0)
expect(response.data.products).toEqual([])
response = await api.get(
`/admin/price-lists/${priceList.id}/products?q=uniquely`,
adminHeaders
)
expect(response.status).toEqual(200)
expect(response.data.count).toEqual(1)
expect(response.data.products).toEqual([
expect.objectContaining({
id: expect.any(String),
}),
])
response = await api.get(
`/admin/price-lists/${priceList.id}/products?q=`,
adminHeaders
)
expect(response.status).toEqual(200)
expect(response.data.count).toEqual(1)
expect(response.data.products).toEqual([
expect.objectContaining({
id: expect.any(String),
}),
])
})
})
},
})
@@ -8,7 +8,7 @@ import {
PriceListType,
} from "@medusajs/types"
import { medusaIntegrationTestRunner } from "medusa-test-utils"
import adminSeeder from "../../../../helpers/admin-seeder"
import { createAdminUser } from "../../../../helpers/create-admin-user"
import { createVariantPriceSet } from "../../../helpers/create-variant-price-set"
jest.setTimeout(50000)
@@ -24,7 +24,9 @@ medusaIntegrationTestRunner({
describe("Admin: Price Lists API", () => {
let appContainer
let product
let product2
let variant
let variant2
let region
let customerGroup
let pricingModule: IPricingModuleService
@@ -41,7 +43,8 @@ medusaIntegrationTestRunner({
})
beforeEach(async () => {
await adminSeeder(dbConnection)
await createAdminUser(dbConnection, adminHeaders, appContainer)
customerGroup = await customerModule.createCustomerGroup({
name: "VIP",
})
@@ -67,16 +70,11 @@ medusaIntegrationTestRunner({
})
describe("GET /admin/price-lists", () => {
it("should get price list and its money amounts with variants", async () => {
it("should get all price lists and its prices with rules", async () => {
const priceSet = await createVariantPriceSet({
container: appContainer,
variantId: variant.id,
prices: [
{
amount: 3000,
currency_code: "usd",
},
],
prices: [],
})
await pricingModule.createPriceLists([
@@ -92,6 +90,9 @@ medusaIntegrationTestRunner({
amount: 5000,
currency_code: "usd",
price_set_id: priceSet.id,
rules: {
region_id: region.id,
},
},
],
rules: {
@@ -100,8 +101,42 @@ medusaIntegrationTestRunner({
},
])
let response = await api.get(
`/admin/price-lists?fields=id,created_at,customer_groups.id,customer_groups.name,prices.id,prices.currency_code,prices.amount,prices.min_quantity,prices.max_quantity,prices.region_id,prices.variant_id`,
let response = await api.get(`/admin/price-lists`, adminHeaders)
expect(response.status).toEqual(200)
expect(response.data.count).toEqual(1)
expect(response.data.price_lists).toEqual([
{
id: expect.any(String),
type: "override",
description: "test",
title: "test price list",
status: "active",
starts_at: expect.any(String),
ends_at: expect.any(String),
created_at: expect.any(String),
updated_at: expect.any(String),
rules: {
customer_group_id: [customerGroup.id],
},
prices: [
{
id: expect.any(String),
currency_code: "usd",
amount: 5000,
min_quantity: null,
max_quantity: null,
variant_id: variant.id,
rules: {
region_id: region.id,
},
},
],
},
])
response = await api.get(
`/admin/price-lists?fields=id,created_at,rules,prices.rules,prices.amount`,
adminHeaders
)
@@ -111,59 +146,28 @@ medusaIntegrationTestRunner({
{
id: expect.any(String),
created_at: expect.any(String),
rules: {
customer_group_id: [customerGroup.id],
},
prices: [
{
id: expect.any(String),
currency_code: "usd",
amount: 5000,
min_quantity: null,
max_quantity: null,
variant_id: expect.any(String),
region_id: null,
rules: {
region_id: region.id,
},
},
],
customer_groups: [
{
id: expect.any(String),
name: "VIP",
},
],
},
])
response = await api.get(`/admin/price-lists`, adminHeaders)
expect(response.status).toEqual(200)
expect(response.data.count).toEqual(1)
expect(response.data.price_lists).toEqual([
{
id: expect.any(String),
created_at: expect.any(String),
updated_at: expect.any(String),
deleted_at: null,
name: "test price list",
description: "test",
type: "override",
status: "active",
starts_at: expect.any(String),
ends_at: expect.any(String),
},
])
})
})
describe("GET /admin/price-lists/:id", () => {
it("should get price list and its money amounts with variants", async () => {
it("should retrieve a price list and its prices with rules", async () => {
const priceSet = await createVariantPriceSet({
container: appContainer,
variantId: variant.id,
prices: [
{
amount: 3000,
currency_code: "usd",
},
],
rules: [],
prices: [],
})
const [priceList] = await pricingModule.createPriceLists([
@@ -179,26 +183,14 @@ medusaIntegrationTestRunner({
amount: 5000,
currency_code: "usd",
price_set_id: priceSet.id,
rules: {
region_id: region.id,
},
},
],
},
])
await pricingModule.createPriceLists([
{
title: "test price list 1",
description: "test 1",
ends_at: new Date(),
starts_at: new Date(),
status: PriceListStatus.ACTIVE,
type: PriceListType.OVERRIDE,
prices: [
{
amount: 5000,
currency_code: "usd",
price_set_id: priceSet.id,
},
],
rules: {
customer_group_id: [customerGroup.id],
},
},
])
@@ -211,15 +203,30 @@ medusaIntegrationTestRunner({
expect(response.data.price_list).toEqual(
expect.objectContaining({
id: expect.any(String),
created_at: expect.any(String),
updated_at: expect.any(String),
deleted_at: null,
name: "test price list",
description: "test",
type: "override",
description: "test",
title: "test price list",
status: "active",
starts_at: expect.any(String),
ends_at: expect.any(String),
created_at: expect.any(String),
updated_at: expect.any(String),
rules: {
customer_group_id: [customerGroup.id],
},
prices: [
{
id: expect.any(String),
currency_code: "usd",
amount: 5000,
min_quantity: null,
max_quantity: null,
variant_id: variant.id,
rules: {
region_id: region.id,
},
},
],
})
)
@@ -0,0 +1,200 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import {
CreateProductDTO,
IPricingModuleService,
IProductModuleService,
PriceListStatus,
PriceListType,
ProductDTO,
ProductVariantDTO,
} from "@medusajs/types"
import { medusaIntegrationTestRunner } from "medusa-test-utils"
import { createAdminUser } from "../../../../helpers/create-admin-user"
import { createVariantPriceSet } from "../../../helpers/create-variant-price-set"
jest.setTimeout(50000)
const env = { MEDUSA_FF_MEDUSA_V2: true }
const adminHeaders = { headers: { "x-medusa-access-token": "test_token" } }
async function createProductsWithVariants(
productModule: IProductModuleService,
productsData: CreateProductDTO
): Promise<[ProductDTO, ProductVariantDTO[]]> {
const { variants: variantsData, ...productData } = productsData
const [product] = await productModule.create([productData])
const variantsDataWithProductId = variantsData?.map((variantData) => {
return { ...variantData, product_id: product.id }
})
const variants = variantsDataWithProductId
? await productModule.createVariants(variantsDataWithProductId)
: []
return [product, variants]
}
medusaIntegrationTestRunner({
env,
testSuite: ({ dbConnection, getContainer, api }) => {
describe("Admin: Products API", () => {
let appContainer
let product
let product2
let product3
let variant
let variant2
let variant3
let pricingModule: IPricingModuleService
let productModule: IProductModuleService
beforeAll(async () => {
appContainer = getContainer()
pricingModule = appContainer.resolve(ModuleRegistrationName.PRICING)
productModule = appContainer.resolve(ModuleRegistrationName.PRODUCT)
})
beforeEach(async () => {
await createAdminUser(dbConnection, adminHeaders, appContainer)
})
describe("GET /admin/products", () => {
describe("should filter products by price lists", () => {
beforeEach(async () => {
;[product, [variant]] = await createProductsWithVariants(
productModule,
{
title: "test product 1",
variants: [{ title: "test variant 1" }],
}
)
;[product2, [variant2]] = await createProductsWithVariants(
productModule,
{
title: "test product 2 uniquely",
variants: [{ title: "test variant 2" }],
}
)
;[product3, [variant3]] = await createProductsWithVariants(
productModule,
{
title: "product not in price list",
variants: [{ title: "test variant 3" }],
}
)
})
it("should list all products in a price list", async () => {
const priceSet = await createVariantPriceSet({
container: appContainer,
variantId: variant.id,
prices: [],
rules: [],
})
const priceSet2 = await createVariantPriceSet({
container: appContainer,
variantId: variant2.id,
prices: [],
rules: [],
})
const [priceList] = await pricingModule.createPriceLists([
{
title: "test price list",
description: "test",
ends_at: new Date(),
starts_at: new Date(),
status: PriceListStatus.ACTIVE,
type: PriceListType.OVERRIDE,
prices: [
{
amount: 5000,
currency_code: "usd",
price_set_id: priceSet.id,
},
{
amount: 6000,
currency_code: "usd",
price_set_id: priceSet2.id,
},
],
},
])
let response = await api.get(
`/admin/products?price_list_id[]=${priceList.id}`,
adminHeaders
)
expect(response.status).toEqual(200)
expect(response.data.count).toEqual(2)
expect(response.data.products).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: product.id,
title: product.title,
}),
expect.objectContaining({
id: product2.id,
title: product2.title,
}),
])
)
})
it("should list all products constrained by search query in a price list", async () => {
const priceSet = await createVariantPriceSet({
container: appContainer,
variantId: variant2.id,
prices: [],
rules: [],
})
const [priceList] = await pricingModule.createPriceLists([
{
title: "test price list",
description: "test",
ends_at: new Date(),
starts_at: new Date(),
status: PriceListStatus.ACTIVE,
type: PriceListType.OVERRIDE,
prices: [
{
amount: 5000,
currency_code: "usd",
price_set_id: priceSet.id,
},
],
},
])
let response = await api.get(
`/admin/products?price_list_id[]=${priceList.id}&q=shouldnotreturnanything`,
adminHeaders
)
expect(response.status).toEqual(200)
expect(response.data.count).toEqual(0)
expect(response.data.products).toEqual([])
response = await api.get(
`/admin/products?price_list_id[]=${priceList.id}&q=uniquely`,
adminHeaders
)
expect(response.status).toEqual(200)
expect(response.data.count).toEqual(1)
expect(response.data.products).toEqual([
expect.objectContaining({
id: product2.id,
}),
])
})
})
})
})
},
})