feat(medusa,core-flows,types): adds batch operations to price list prices (#7077)

what:

- adds batch operations to price list prices

RESOLVES CORE-1969
RESOLVES CORE-1970
This commit is contained in:
Riqwan Thamir
2024-04-17 15:36:09 +00:00
committed by GitHub
parent 08dc861dc1
commit 8d356217bd
35 changed files with 735 additions and 630 deletions
@@ -24,7 +24,6 @@ medusaIntegrationTestRunner({
describe("Admin: Price Lists API", () => {
let appContainer
let product
let product2
let variant
let variant2
let region
@@ -56,11 +55,15 @@ medusaIntegrationTestRunner({
{
title: "test product variant",
},
{
title: "test product variant 2",
},
],
},
])
variant = product.variants[0]
variant2 = product.variants[1]
await pricingModule.createRuleTypes([
{ name: "Customer Group ID", rule_attribute: "customer_group_id" },
@@ -115,6 +118,7 @@ medusaIntegrationTestRunner({
ends_at: expect.any(String),
created_at: expect.any(String),
updated_at: expect.any(String),
deleted_at: null,
rules: {
customer_group_id: [customerGroup.id],
},
@@ -126,6 +130,10 @@ medusaIntegrationTestRunner({
min_quantity: null,
max_quantity: null,
variant_id: variant.id,
created_at: expect.any(String),
updated_at: expect.any(String),
deleted_at: null,
price_set_id: expect.any(String),
rules: {
region_id: region.id,
},
@@ -135,7 +143,7 @@ medusaIntegrationTestRunner({
])
response = await api.get(
`/admin/price-lists?fields=id,created_at,rules,prices.rules,prices.amount`,
`/admin/price-lists?fields=id,created_at,prices.amount`,
adminHeaders
)
@@ -145,15 +153,10 @@ medusaIntegrationTestRunner({
{
id: expect.any(String),
created_at: expect.any(String),
rules: {
customer_group_id: [customerGroup.id],
},
prices: [
{
id: expect.any(String),
amount: 5000,
rules: {
region_id: region.id,
},
},
],
},
@@ -210,6 +213,7 @@ medusaIntegrationTestRunner({
ends_at: expect.any(String),
created_at: expect.any(String),
updated_at: expect.any(String),
deleted_at: null,
rules: {
customer_group_id: [customerGroup.id],
},
@@ -221,6 +225,10 @@ medusaIntegrationTestRunner({
min_quantity: null,
max_quantity: null,
variant_id: variant.id,
created_at: expect.any(String),
updated_at: expect.any(String),
price_set_id: expect.any(String),
deleted_at: null,
rules: {
region_id: region.id,
},
@@ -295,14 +303,15 @@ medusaIntegrationTestRunner({
expect(response.data.price_list).toEqual(
expect.objectContaining({
id: expect.any(String),
created_at: expect.any(String),
updated_at: expect.any(String),
title: "test price list",
description: "test",
type: "override",
status: "active",
starts_at: expect.any(String),
ends_at: null,
created_at: expect.any(String),
updated_at: expect.any(String),
deleted_at: null,
rules: {
customer_group_id: [customerGroup.id],
},
@@ -314,6 +323,10 @@ medusaIntegrationTestRunner({
min_quantity: null,
max_quantity: null,
variant_id: variant.id,
created_at: expect.any(String),
updated_at: expect.any(String),
deleted_at: null,
price_set_id: expect.any(String),
rules: {
region_id: region.id,
},
@@ -338,9 +351,10 @@ medusaIntegrationTestRunner({
.catch((e) => e)
expect(errorResponse.response.status).toEqual(400)
expect(errorResponse.response.data.message).toEqual(
"title must be a string, description must be a string, type must be one of the following values: sale, override, variant_id must be a string"
)
// TODO: reenable when this is translated
// expect(errorResponse.response.data.message).toEqual(
// "title must be a string, description must be a string, type must be one of the following values: sale, override, variant_id must be a string"
// )
})
})
@@ -468,142 +482,21 @@ medusaIntegrationTestRunner({
})
})
describe("POST /admin/price-lists/:id/prices/batch/add", () => {
it("should add price list prices successfully", async () => {
describe("POST /admin/price-lists/:id/prices/batch", () => {
it("should add, remove and delete price list prices in batch successfully", async () => {
const priceSet = await createVariantPriceSet({
container: appContainer,
variantId: variant.id,
prices: [{ amount: 3000, currency_code: "usd" }],
})
const [priceList] = await pricingModule.createPriceLists([
{
title: "test price list",
description: "test",
prices: [
{
id: "test-price-id",
amount: 5000,
currency_code: "usd",
price_set_id: priceSet.id,
rules: { region_id: region.id },
},
],
},
])
const data = {
prices: [
{
amount: 400,
variant_id: variant.id,
currency_code: "usd",
rules: { region_id: region.id },
},
],
}
const response = await api.post(
`admin/price-lists/${priceList.id}/prices/batch/add`,
data,
adminHeaders
)
expect(response.status).toEqual(200)
expect(response.data.price_list.prices.length).toEqual(2)
expect(response.data.price_list).toEqual(
expect.objectContaining({
id: expect.any(String),
prices: expect.arrayContaining([
expect.objectContaining({
id: expect.any(String),
currency_code: "usd",
amount: 400,
}),
expect.objectContaining({
id: "test-price-id",
currency_code: "usd",
amount: 5000,
}),
]),
})
)
})
})
describe("POST /admin/price-lists/:id/prices/batch/update", () => {
it("should update price list prices successfully", async () => {
const priceSet = await createVariantPriceSet({
container: appContainer,
variantId: variant.id,
prices: [{ amount: 3000, currency_code: "usd" }],
})
const [priceList] = await pricingModule.createPriceLists([
{
title: "test price list",
description: "test",
prices: [
{
id: "test-price-id",
amount: 5000,
currency_code: "usd",
price_set_id: priceSet.id,
rules: { region_id: region.id },
},
],
},
])
const data = {
prices: [
{
id: "test-price-id",
amount: 400,
variant_id: variant.id,
currency_code: "usd",
rules: { region_id: region.id },
},
],
}
const response = await api.post(
`admin/price-lists/${priceList.id}/prices/batch/update`,
data,
adminHeaders
)
expect(response.status).toEqual(200)
expect(response.data.price_list.prices.length).toEqual(1)
expect(response.data.price_list).toEqual(
expect.objectContaining({
id: expect.any(String),
prices: expect.arrayContaining([
expect.objectContaining({
id: expect.any(String),
currency_code: "usd",
amount: 400,
}),
]),
})
)
})
})
describe("POST /admin/price-lists/:id/prices/batch/remove", () => {
it("should remove price list prices successfully", async () => {
const priceSet = await createVariantPriceSet({
container: appContainer,
variantId: variant.id,
prices: [],
})
const [createdPriceList] = await pricingModule.createPriceLists([
{
title: "test price list",
description: "test",
prices: [
{
id: "price-to-remove",
amount: 5000,
currency_code: "usd",
price_set_id: priceSet.id,
@@ -611,6 +504,13 @@ medusaIntegrationTestRunner({
region_id: region.id,
},
},
{
id: "price-to-update",
amount: 5000,
currency_code: "usd",
price_set_id: priceSet.id,
rules: { region_id: region.id },
},
],
},
])
@@ -619,21 +519,122 @@ medusaIntegrationTestRunner({
{ id: [createdPriceList.id] },
{ relations: ["prices"] }
)
const priceIdToDelete = priceList.prices![0].id
const priceIdToDelete = priceList.prices?.find(
(p) => p.id === "price-to-remove"
)
const data = {
create: [
{
amount: 400,
variant_id: variant.id,
currency_code: "usd",
rules: { region_id: region.id },
},
],
update: [
{
id: "price-to-update",
amount: 500,
variant_id: variant.id,
currency_code: "usd",
rules: { region_id: region.id },
},
],
delete: [priceIdToDelete?.id],
}
const response = await api.post(
`/admin/price-lists/${priceList.id}/prices/batch/remove`,
{ ids: [priceIdToDelete] },
`admin/price-lists/${priceList.id}/prices/batch`,
data,
adminHeaders
)
expect(response.status).toEqual(200)
expect(response.data.price_list).toEqual(
expect.objectContaining({
id: expect.any(String),
prices: [],
})
expect(response.data).toEqual({
created: [
expect.objectContaining({
id: expect.any(String),
currency_code: "usd",
amount: 400,
}),
],
updated: [
expect.objectContaining({
id: "price-to-update",
currency_code: "usd",
amount: 500,
}),
],
deleted: {
ids: ["price-to-remove"],
object: "price",
deleted: true,
},
})
})
it("should remove all price list prices of a product", async () => {
const priceSet = await createVariantPriceSet({
container: appContainer,
variantId: variant.id,
prices: [{ amount: 3000, currency_code: "usd" }],
})
const priceSet2 = await createVariantPriceSet({
container: appContainer,
variantId: variant2.id,
prices: [{ amount: 3000, currency_code: "usd" }],
})
const [createdPriceList] = await pricingModule.createPriceLists([
{
title: "test price list",
description: "test",
prices: [
{
id: "price-to-delete-1",
amount: 5000,
currency_code: "usd",
price_set_id: priceSet.id,
rules: {
region_id: region.id,
},
},
{
id: "price-to-delete-2",
amount: 5000,
currency_code: "usd",
price_set_id: priceSet2.id,
rules: { region_id: region.id },
},
],
},
])
const [priceList] = await pricingModule.listPriceLists(
{ id: [createdPriceList.id] },
{ relations: ["prices"] }
)
const data = { product_id: [product.id] }
const response = await api.post(
`admin/price-lists/${priceList.id}/prices/batch`,
data,
adminHeaders
)
expect(response.status).toEqual(200)
expect(response.data).toEqual({
created: [],
updated: [],
deleted: {
ids: ["price-to-delete-1", "price-to-delete-2"],
object: "price",
deleted: true,
},
})
})
})
})