feat(dashboard, core-flows): associate shipping option to type (#13226)
* feat(dashboard, core-flows): allow associating shipping option type to a shipping option * edit as well * fix translation schema * fix some tests * changeset * add new test to update shipping option type * add new test to create shipping option with shipping option type * pr comments * pr comments * rename variable * make zod great again
This commit is contained in:
@@ -17,6 +17,7 @@ medusaIntegrationTestRunner({
|
||||
let appContainer
|
||||
let location
|
||||
let location2
|
||||
let type
|
||||
|
||||
const shippingOptionRule = {
|
||||
operator: RuleOperator.EQ,
|
||||
@@ -92,6 +93,17 @@ medusaIntegrationTestRunner({
|
||||
adminHeaders
|
||||
)
|
||||
).data.region
|
||||
|
||||
type = (
|
||||
await api.post(
|
||||
`/admin/shipping-option-types`,
|
||||
{
|
||||
label: "Test",
|
||||
code: 'test',
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
).data.shipping_option_type
|
||||
})
|
||||
|
||||
describe("GET /admin/shipping-options", () => {
|
||||
@@ -404,6 +416,103 @@ medusaIntegrationTestRunner({
|
||||
)
|
||||
})
|
||||
|
||||
it("should create a shipping option successfully with the provided shipping option type", async () => {
|
||||
const shippingOptionPayload = {
|
||||
name: "Test shipping option",
|
||||
service_zone_id: fulfillmentSet.service_zones[0].id,
|
||||
shipping_profile_id: shippingProfile.id,
|
||||
provider_id: "manual_test-provider",
|
||||
price_type: "flat",
|
||||
type_id: type.id,
|
||||
prices: [
|
||||
{
|
||||
currency_code: "usd",
|
||||
amount: 1000,
|
||||
},
|
||||
{
|
||||
region_id: region.id,
|
||||
amount: 1000,
|
||||
},
|
||||
{
|
||||
region_id: region.id,
|
||||
amount: 500,
|
||||
rules: [
|
||||
{
|
||||
attribute: "item_total",
|
||||
operator: "gt",
|
||||
value: 200,
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
rules: [shippingOptionRule],
|
||||
}
|
||||
|
||||
const response = await api.post(
|
||||
`/admin/shipping-options`,
|
||||
shippingOptionPayload,
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.shipping_option).toEqual(
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
name: shippingOptionPayload.name,
|
||||
provider: expect.objectContaining({
|
||||
id: shippingOptionPayload.provider_id,
|
||||
}),
|
||||
price_type: shippingOptionPayload.price_type,
|
||||
type: expect.objectContaining({
|
||||
id: type.id,
|
||||
label: type.label,
|
||||
description: type.description,
|
||||
code: type.code,
|
||||
}),
|
||||
service_zone_id: fulfillmentSet.service_zones[0].id,
|
||||
shipping_profile_id: shippingProfile.id,
|
||||
prices: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
currency_code: "usd",
|
||||
amount: 1000,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
currency_code: "eur",
|
||||
amount: 1000,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
currency_code: "eur",
|
||||
amount: 500,
|
||||
rules_count: 2,
|
||||
price_rules: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
attribute: "item_total",
|
||||
operator: "gt",
|
||||
value: "200",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
attribute: "region_id",
|
||||
operator: "eq",
|
||||
value: region.id,
|
||||
}),
|
||||
]),
|
||||
}),
|
||||
]),
|
||||
rules: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
operator: "eq",
|
||||
attribute: "old_attr",
|
||||
value: "old value",
|
||||
}),
|
||||
]),
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw error when creating a price rule with a non white listed attribute", async () => {
|
||||
const shippingOptionPayload = {
|
||||
name: "Test shipping option",
|
||||
@@ -552,6 +661,72 @@ medusaIntegrationTestRunner({
|
||||
"Providers (does-not-exist) are not enabled for the service location"
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw error if both type and type_id are missing", async () => {
|
||||
const shippingOptionPayload = {
|
||||
name: "Test shipping option",
|
||||
service_zone_id: fulfillmentSet.service_zones[0].id,
|
||||
shipping_profile_id: shippingProfile.id,
|
||||
provider_id: "manual_test-provider",
|
||||
price_type: "flat",
|
||||
prices: [
|
||||
{
|
||||
currency_code: "usd",
|
||||
amount: 1000,
|
||||
},
|
||||
],
|
||||
rules: [shippingOptionRule],
|
||||
}
|
||||
|
||||
const error = await api
|
||||
.post(
|
||||
`/admin/shipping-options`,
|
||||
shippingOptionPayload,
|
||||
adminHeaders
|
||||
)
|
||||
.catch((e) => e)
|
||||
|
||||
expect(error.response.status).toEqual(400)
|
||||
expect(error.response.data.message).toEqual(
|
||||
"Invalid request: Exactly one of 'type' or 'type_id' must be provided, but not both"
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw error if both type and type_id are defined", async () => {
|
||||
const shippingOptionPayload = {
|
||||
name: "Test shipping option",
|
||||
service_zone_id: fulfillmentSet.service_zones[0].id,
|
||||
shipping_profile_id: shippingProfile.id,
|
||||
provider_id: "manual_test-provider",
|
||||
type: {
|
||||
label: "Test type",
|
||||
description: "Test description",
|
||||
code: "test-code",
|
||||
},
|
||||
type_id: "test_type_id",
|
||||
price_type: "flat",
|
||||
prices: [
|
||||
{
|
||||
currency_code: "usd",
|
||||
amount: 1000,
|
||||
},
|
||||
],
|
||||
rules: [shippingOptionRule],
|
||||
}
|
||||
|
||||
const error = await api
|
||||
.post(
|
||||
`/admin/shipping-options`,
|
||||
shippingOptionPayload,
|
||||
adminHeaders
|
||||
)
|
||||
.catch((e) => e)
|
||||
|
||||
expect(error.response.status).toEqual(400)
|
||||
expect(error.response.data.message).toEqual(
|
||||
"Invalid request: Exactly one of 'type' or 'type_id' must be provided, but not both"
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("POST /admin/shipping-options/:id", () => {
|
||||
@@ -637,7 +812,7 @@ medusaIntegrationTestRunner({
|
||||
],
|
||||
rules: [
|
||||
{
|
||||
// Un touched
|
||||
// Untouched
|
||||
id: oldAttrRule.id,
|
||||
operator: RuleOperator.EQ,
|
||||
attribute: "old_attr",
|
||||
@@ -735,6 +910,135 @@ medusaIntegrationTestRunner({
|
||||
)
|
||||
})
|
||||
|
||||
it("should update a shipping option with a provided shipping option type successfully", async () => {
|
||||
const shippingOptionPayload = {
|
||||
name: "Test shipping option",
|
||||
service_zone_id: fulfillmentSet.service_zones[0].id,
|
||||
shipping_profile_id: shippingProfile.id,
|
||||
provider_id: "manual_test-provider",
|
||||
price_type: "flat",
|
||||
type: {
|
||||
label: "Test type",
|
||||
description: "Test description",
|
||||
code: "test-code",
|
||||
},
|
||||
prices: [
|
||||
{
|
||||
currency_code: "usd",
|
||||
amount: 1000,
|
||||
},
|
||||
{
|
||||
region_id: region.id,
|
||||
amount: 1000,
|
||||
},
|
||||
],
|
||||
rules: [
|
||||
{
|
||||
operator: RuleOperator.EQ,
|
||||
attribute: "old_attr",
|
||||
value: "old value",
|
||||
},
|
||||
{
|
||||
operator: RuleOperator.EQ,
|
||||
attribute: "old_attr_2",
|
||||
value: "true",
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
const response = await api.post(
|
||||
`/admin/shipping-options`,
|
||||
shippingOptionPayload,
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
const shippingOptionId = response.data.shipping_option.id
|
||||
|
||||
const updateResponse = await api.post(
|
||||
`/admin/shipping-options/${shippingOptionId}`,
|
||||
{
|
||||
name: "Updated shipping option",
|
||||
type_id: type.id,
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(updateResponse.status).toEqual(200)
|
||||
expect(updateResponse.data.shipping_option).toEqual(
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
name: "Updated shipping option",
|
||||
type: expect.objectContaining({
|
||||
id: type.id,
|
||||
label: type.label,
|
||||
description: type.description,
|
||||
code: type.code,
|
||||
}),
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should update a shipping option without providing shipping option type successfully", async () => {
|
||||
const shippingOptionPayload = {
|
||||
name: "Test shipping option",
|
||||
service_zone_id: fulfillmentSet.service_zones[0].id,
|
||||
shipping_profile_id: shippingProfile.id,
|
||||
provider_id: "manual_test-provider",
|
||||
price_type: "flat",
|
||||
type: {
|
||||
label: "Test type",
|
||||
description: "Test description",
|
||||
code: "test-code",
|
||||
},
|
||||
prices: [
|
||||
{
|
||||
currency_code: "usd",
|
||||
amount: 1000,
|
||||
},
|
||||
{
|
||||
region_id: region.id,
|
||||
amount: 1000,
|
||||
},
|
||||
],
|
||||
rules: [
|
||||
{
|
||||
operator: RuleOperator.EQ,
|
||||
attribute: "old_attr",
|
||||
value: "old value",
|
||||
},
|
||||
{
|
||||
operator: RuleOperator.EQ,
|
||||
attribute: "old_attr_2",
|
||||
value: "true",
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
const response = await api.post(
|
||||
`/admin/shipping-options`,
|
||||
shippingOptionPayload,
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
const shippingOptionId = response.data.shipping_option.id
|
||||
|
||||
const updateResponse = await api.post(
|
||||
`/admin/shipping-options/${shippingOptionId}`,
|
||||
{
|
||||
name: "Updated shipping option"
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(updateResponse.status).toEqual(200)
|
||||
expect(updateResponse.data.shipping_option).toEqual(
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
name: "Updated shipping option"
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw an error when provider does not belong to service location", async () => {
|
||||
const shippingOptionPayload = {
|
||||
name: "Test shipping option",
|
||||
@@ -785,6 +1089,60 @@ medusaIntegrationTestRunner({
|
||||
"Providers (another_test-provider) are not enabled for the service location"
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw an error when type and type_id are both defined", async () => {
|
||||
const shippingOptionPayload = {
|
||||
name: "Test shipping option",
|
||||
service_zone_id: fulfillmentSet.service_zones[0].id,
|
||||
shipping_profile_id: shippingProfile.id,
|
||||
provider_id: "manual_test-provider",
|
||||
price_type: "flat",
|
||||
type: {
|
||||
label: "Test type",
|
||||
description: "Test description",
|
||||
code: "test-code",
|
||||
},
|
||||
prices: [
|
||||
{
|
||||
currency_code: "usd",
|
||||
amount: 1000,
|
||||
},
|
||||
{
|
||||
region_id: region.id,
|
||||
amount: 1000,
|
||||
},
|
||||
],
|
||||
rules: [shippingOptionRule],
|
||||
}
|
||||
|
||||
const response = await api.post(
|
||||
`/admin/shipping-options`,
|
||||
shippingOptionPayload,
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
const shippingOptionId = response.data.shipping_option.id
|
||||
|
||||
const updateShippingOptionPayload = {
|
||||
type: {
|
||||
label: "Test type",
|
||||
description: "Test description",
|
||||
code: "test-code",
|
||||
},
|
||||
type_id: "test_type_id"
|
||||
}
|
||||
|
||||
const error = await api
|
||||
.post(
|
||||
`/admin/shipping-options/${shippingOptionId}`,
|
||||
updateShippingOptionPayload,
|
||||
adminHeaders
|
||||
)
|
||||
.catch((e) => e)
|
||||
|
||||
expect(error.response.status).toEqual(400)
|
||||
expect(error.response.data.message).toEqual("Invalid request: Only one of 'type' or 'type_id' can be provided")
|
||||
})
|
||||
})
|
||||
|
||||
describe("DELETE /admin/shipping-options/:id", () => {
|
||||
|
||||
Reference in New Issue
Block a user