feat: Clean up shipping options management (#6977)
**What** - cleanup existing route on some aspects but not all - add new create shipping options end point and types - add set of integration tests - cleanup existing fulfillment routes - align existing integration tests - transform old type to zod types and validators - fix stock location route
This commit is contained in:
committed by
GitHub
parent
5724d80286
commit
bc06ad2db4
@@ -63,11 +63,11 @@ medusaIntegrationTestRunner({
|
||||
})
|
||||
})
|
||||
|
||||
describe("POST /admin/fulfillment/shipping-options/:id/rules/batch/add", () => {
|
||||
describe("POST /admin/shipping-options/:id/rules/batch/add", () => {
|
||||
it("should throw error when required params are missing", async () => {
|
||||
const { response } = await api
|
||||
.post(
|
||||
`/admin/fulfillment/shipping-options/${shippingOption.id}/rules/batch/add`,
|
||||
`/admin/shipping-options/${shippingOption.id}/rules/batch/add`,
|
||||
{
|
||||
rules: [{ operator: RuleOperator.EQ, value: "new_value" }],
|
||||
},
|
||||
@@ -86,7 +86,7 @@ medusaIntegrationTestRunner({
|
||||
it.only("should throw error when shipping option does not exist", async () => {
|
||||
const { response } = await api
|
||||
.post(
|
||||
`/admin/fulfillment/shipping-options/does-not-exist/rules/batch/add`,
|
||||
`/admin/shipping-options/does-not-exist/rules/batch/add`,
|
||||
{
|
||||
rules: [
|
||||
{ attribute: "new_attr", operator: "eq", value: "new value" },
|
||||
@@ -106,7 +106,7 @@ medusaIntegrationTestRunner({
|
||||
|
||||
it("should add rules to a shipping option successfully", async () => {
|
||||
const response = await api.post(
|
||||
`/admin/fulfillment/shipping-options/${shippingOption.id}/rules/batch/add`,
|
||||
`/admin/shipping-options/${shippingOption.id}/rules/batch/add`,
|
||||
{
|
||||
rules: [
|
||||
{ operator: "eq", attribute: "new_attr", value: "new value" },
|
||||
@@ -140,11 +140,11 @@ medusaIntegrationTestRunner({
|
||||
})
|
||||
})
|
||||
|
||||
describe("POST /admin/fulfillment/shipping-options/:id/rules/batch/remove", () => {
|
||||
describe("POST /admin/shipping-options/:id/rules/batch/remove", () => {
|
||||
it("should throw error when required params are missing", async () => {
|
||||
const { response } = await api
|
||||
.post(
|
||||
`/admin/fulfillment/shipping-options/${shippingOption.id}/rules/batch/remove`,
|
||||
`/admin/shipping-options/${shippingOption.id}/rules/batch/remove`,
|
||||
{},
|
||||
adminHeaders
|
||||
)
|
||||
@@ -161,7 +161,7 @@ medusaIntegrationTestRunner({
|
||||
it("should throw error when shipping option does not exist", async () => {
|
||||
const { response } = await api
|
||||
.post(
|
||||
`/admin/fulfillment/shipping-options/does-not-exist/rules/batch/remove`,
|
||||
`/admin/shipping-options/does-not-exist/rules/batch/remove`,
|
||||
{ rule_ids: ["test"] },
|
||||
adminHeaders
|
||||
)
|
||||
@@ -176,7 +176,7 @@ medusaIntegrationTestRunner({
|
||||
|
||||
it("should add rules to a shipping option successfully", async () => {
|
||||
const response = await api.post(
|
||||
`/admin/fulfillment/shipping-options/${shippingOption.id}/rules/batch/remove`,
|
||||
`/admin/shipping-options/${shippingOption.id}/rules/batch/remove`,
|
||||
{
|
||||
rule_ids: [shippingOption.rules[0].id],
|
||||
},
|
||||
|
||||
@@ -0,0 +1,207 @@
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import {
|
||||
IFulfillmentModuleService,
|
||||
IRegionModuleService,
|
||||
} from "@medusajs/types"
|
||||
import { RuleOperator } from "@medusajs/utils"
|
||||
import { medusaIntegrationTestRunner } from "medusa-test-utils"
|
||||
import { createAdminUser } from "../../../../helpers/create-admin-user"
|
||||
|
||||
jest.setTimeout(50000)
|
||||
|
||||
const env = { MEDUSA_FF_MEDUSA_V2: true }
|
||||
const adminHeaders = { headers: { "x-medusa-access-token": "test_token" } }
|
||||
|
||||
medusaIntegrationTestRunner({
|
||||
env,
|
||||
testSuite: ({ dbConnection, getContainer, api }) => {
|
||||
describe("Admin: Shipping Option API", () => {
|
||||
let appContainer
|
||||
let fulfillmentModule: IFulfillmentModuleService
|
||||
let regionService: IRegionModuleService
|
||||
|
||||
let shippingProfile
|
||||
let fulfillmentSet
|
||||
let region
|
||||
|
||||
const shippingOptionRule = {
|
||||
operator: RuleOperator.EQ,
|
||||
attribute: "old_attr",
|
||||
value: "old value",
|
||||
}
|
||||
|
||||
beforeAll(async () => {
|
||||
appContainer = getContainer()
|
||||
fulfillmentModule = appContainer.resolve(
|
||||
ModuleRegistrationName.FULFILLMENT
|
||||
)
|
||||
regionService = appContainer.resolve(ModuleRegistrationName.REGION)
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await createAdminUser(dbConnection, adminHeaders, appContainer)
|
||||
|
||||
shippingProfile = await fulfillmentModule.createShippingProfiles({
|
||||
name: "Test",
|
||||
type: "default",
|
||||
})
|
||||
|
||||
fulfillmentSet = await fulfillmentModule.create({
|
||||
name: "Test",
|
||||
type: "test-type",
|
||||
service_zones: [
|
||||
{
|
||||
name: "Test",
|
||||
geo_zones: [{ type: "country", country_code: "us" }],
|
||||
},
|
||||
],
|
||||
})
|
||||
|
||||
region = await regionService.create({
|
||||
name: "Test region",
|
||||
countries: ["FR"],
|
||||
currency_code: "eur",
|
||||
})
|
||||
})
|
||||
|
||||
describe("POST /admin/shipping-options", () => {
|
||||
it("should throw error when required params are missing", async () => {
|
||||
const shippingOptionPayload = {
|
||||
name: "Test shipping option",
|
||||
}
|
||||
|
||||
let err = await api
|
||||
.post(
|
||||
`/admin/shipping-options`,
|
||||
shippingOptionPayload,
|
||||
adminHeaders
|
||||
)
|
||||
.catch((e) => e.response)
|
||||
|
||||
const errorsFields = [
|
||||
{
|
||||
code: "invalid_type",
|
||||
expected: "string",
|
||||
received: "undefined",
|
||||
path: ["service_zone_id"],
|
||||
message: "Required",
|
||||
},
|
||||
{
|
||||
code: "invalid_type",
|
||||
expected: "string",
|
||||
received: "undefined",
|
||||
path: ["shipping_profile_id"],
|
||||
message: "Required",
|
||||
},
|
||||
{
|
||||
expected: "'calculated' | 'flat'",
|
||||
received: "undefined",
|
||||
code: "invalid_type",
|
||||
path: ["price_type"],
|
||||
message: "Required",
|
||||
},
|
||||
{
|
||||
code: "invalid_type",
|
||||
expected: "string",
|
||||
received: "undefined",
|
||||
path: ["provider_id"],
|
||||
message: "Required",
|
||||
},
|
||||
{
|
||||
code: "invalid_type",
|
||||
expected: "object",
|
||||
received: "undefined",
|
||||
path: ["type"],
|
||||
message: "Required",
|
||||
},
|
||||
{
|
||||
code: "invalid_type",
|
||||
expected: "array",
|
||||
received: "undefined",
|
||||
path: ["prices"],
|
||||
message: "Required",
|
||||
},
|
||||
]
|
||||
|
||||
expect(err.status).toEqual(400)
|
||||
expect(err.data).toEqual({
|
||||
type: "invalid_data",
|
||||
message: `Invalid request body: ${JSON.stringify(errorsFields)}`,
|
||||
})
|
||||
})
|
||||
|
||||
it("should create a shipping option 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: [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: expect.any(String),
|
||||
label: shippingOptionPayload.type.label,
|
||||
description: shippingOptionPayload.type.description,
|
||||
code: shippingOptionPayload.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,
|
||||
}),
|
||||
]),
|
||||
rules: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
operator: "eq",
|
||||
attribute: "old_attr",
|
||||
value: "old value",
|
||||
}),
|
||||
]),
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
},
|
||||
})
|
||||
Reference in New Issue
Block a user