feat(core-flows, fulfillment): validate shipping options to stock locations (#8291)

what:

- validate shipping options to stock locations

RESOLVES CC-232
This commit is contained in:
Riqwan Thamir
2024-07-26 12:26:26 +00:00
committed by GitHub
parent 74d0d166b3
commit 91c17651f1
23 changed files with 607 additions and 18 deletions
@@ -215,6 +215,14 @@ medusaIntegrationTestRunner({
fulfillment_set_id: fulfillmentSet.id,
},
},
{
[Modules.STOCK_LOCATION]: {
stock_location_id: location.id,
},
[Modules.FULFILLMENT]: {
fulfillment_provider_id: "manual_test-provider",
},
},
{
[Modules.SALES_CHANNEL]: {
sales_channel_id: "test",
@@ -1,4 +1,8 @@
import { RuleOperator } from "@medusajs/utils"
import {
ContainerRegistrationKeys,
Modules,
RuleOperator,
} from "@medusajs/utils"
import { medusaIntegrationTestRunner } from "medusa-test-utils"
import {
adminHeaders,
@@ -14,6 +18,9 @@ medusaIntegrationTestRunner({
let shippingProfile
let fulfillmentSet
let region
let appContainer
let location
let location2
const shippingOptionRule = {
operator: RuleOperator.EQ,
@@ -22,7 +29,8 @@ medusaIntegrationTestRunner({
}
beforeEach(async () => {
const appContainer = getContainer()
appContainer = getContainer()
await createAdminUser(dbConnection, adminHeaders, appContainer)
shippingProfile = (
@@ -36,7 +44,7 @@ medusaIntegrationTestRunner({
)
).data.shipping_profile
let location = (
location = (
await api.post(
`/admin/stock-locations`,
{
@@ -103,6 +111,22 @@ medusaIntegrationTestRunner({
})
it("should create a shipping option successfully", async () => {
const remoteLink = appContainer.resolve(
ContainerRegistrationKeys.REMOTE_LINK
)
// TODO: move this to an endpoint when available
await remoteLink.create([
{
[Modules.STOCK_LOCATION]: {
stock_location_id: location.id,
},
[Modules.FULFILLMENT]: {
fulfillment_provider_id: "manual_test-provider",
},
},
])
const shippingOptionPayload = {
name: "Test shipping option",
service_zone_id: fulfillmentSet.service_zones[0].id,
@@ -173,6 +197,41 @@ medusaIntegrationTestRunner({
})
)
})
it("should throw error when provider does not exist on a location", async () => {
const shippingOptionPayload = {
name: "Test shipping option",
service_zone_id: fulfillmentSet.service_zones[0].id,
shipping_profile_id: shippingProfile.id,
provider_id: "does-not-exist",
price_type: "flat",
type: {
label: "Test type",
description: "Test description",
code: "test-code",
},
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(
"Providers (does-not-exist) are not enabled for the service location"
)
})
})
describe("POST /admin/shipping-options/:id", () => {
@@ -212,6 +271,22 @@ medusaIntegrationTestRunner({
],
}
const remoteLink = appContainer.resolve(
ContainerRegistrationKeys.REMOTE_LINK
)
// TODO: move this to an endpoint when available
await remoteLink.create([
{
[Modules.STOCK_LOCATION]: {
stock_location_id: location.id,
},
[Modules.FULFILLMENT]: {
fulfillment_provider_id: "manual_test-provider",
},
},
])
const response = await api.post(
`/admin/shipping-options`,
shippingOptionPayload,
@@ -331,6 +406,73 @@ medusaIntegrationTestRunner({
})
)
})
it("should throw an error when provider does not belong to service location", 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 remoteLink = appContainer.resolve(
ContainerRegistrationKeys.REMOTE_LINK
)
// TODO: move this to an endpoint when available
await remoteLink.create([
{
[Modules.STOCK_LOCATION]: {
stock_location_id: location.id,
},
[Modules.FULFILLMENT]: {
fulfillment_provider_id: "manual_test-provider",
},
},
])
const response = await api.post(
`/admin/shipping-options`,
shippingOptionPayload,
adminHeaders
)
const shippingOptionId = response.data.shipping_option.id
const updateShippingOptionPayload = {
provider_id: "another_test-provider",
}
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(
"Providers (another_test-provider) are not enabled for the service location"
)
})
})
describe("DELETE /admin/shipping-options/:id", () => {
@@ -359,6 +501,22 @@ medusaIntegrationTestRunner({
rules: [shippingOptionRule],
}
const remoteLink = appContainer.resolve(
ContainerRegistrationKeys.REMOTE_LINK
)
// TODO: move this to an endpoint when available
await remoteLink.create([
{
[Modules.STOCK_LOCATION]: {
stock_location_id: location.id,
},
[Modules.FULFILLMENT]: {
fulfillment_provider_id: "manual_test-provider",
},
},
])
const response = await api.post(
`/admin/shipping-options`,
shippingOptionPayload,
@@ -1480,6 +1480,36 @@ medusaIntegrationTestRunner({
})
it("should add shipping method to cart", async () => {
const stockLocation = (
await api.post(
`/admin/stock-locations`,
{ name: "test location" },
adminHeaders
)
).data.stock_location
await remoteLink.create([
{
[Modules.STOCK_LOCATION]: {
stock_location_id: stockLocation.id,
},
[Modules.FULFILLMENT]: {
fulfillment_set_id: fulfillmentSet.id,
},
},
])
await remoteLink.create([
{
[Modules.STOCK_LOCATION]: {
stock_location_id: stockLocation.id,
},
[Modules.FULFILLMENT]: {
fulfillment_provider_id: "manual_test-provider",
},
},
])
const shippingOption = await fulfillmentModule.createShippingOptions({
name: "Test shipping option",
service_zone_id: fulfillmentSet.service_zones[0].id,
@@ -1930,6 +1930,28 @@ medusaIntegrationTestRunner({
)
).data.product
await remoteLink.create([
{
[Modules.STOCK_LOCATION]: {
stock_location_id: stockLocation.id,
},
[Modules.FULFILLMENT]: {
fulfillment_set_id: fulfillmentSet.id,
},
},
])
await remoteLink.create([
{
[Modules.STOCK_LOCATION]: {
stock_location_id: stockLocation.id,
},
[Modules.FULFILLMENT]: {
fulfillment_provider_id: "manual_test-provider",
},
},
])
shippingOption = (
await api.post(
`/admin/shipping-options`,
@@ -8,6 +8,10 @@ import {
Modules,
} from "@medusajs/utils"
import { medusaIntegrationTestRunner } from "medusa-test-utils"
import {
adminHeaders,
createAdminUser,
} from "../../../helpers/create-admin-user"
jest.setTimeout(50000)
@@ -15,13 +19,12 @@ const env = { MEDUSA_FF_MEDUSA_V2: true }
medusaIntegrationTestRunner({
env,
testSuite: ({ getContainer }) => {
testSuite: ({ dbConnection, getContainer, api }) => {
describe("Region and Payment Providers", () => {
let appContainer
let fulfillmentModule: IFulfillmentModuleService
let pricingModule: IPricingModuleService
let remoteQuery
let remoteLink
beforeAll(async () => {
appContainer = getContainer()
@@ -32,7 +35,7 @@ medusaIntegrationTestRunner({
remoteQuery = appContainer.resolve(
ContainerRegistrationKeys.REMOTE_QUERY
)
remoteLink = appContainer.resolve(ContainerRegistrationKeys.REMOTE_LINK)
await createAdminUser(dbConnection, adminHeaders, getContainer())
})
it("should query shipping option and price set link with remote query", async () => {
@@ -44,6 +47,38 @@ medusaIntegrationTestRunner({
name: "Test",
type: "test-type",
})
const remoteLink = appContainer.resolve(
ContainerRegistrationKeys.REMOTE_LINK
)
const location = (
await api.post(
`/admin/stock-locations`,
{ name: "Test location" },
adminHeaders
)
).data.stock_location
await remoteLink.create([
{
[Modules.STOCK_LOCATION]: {
stock_location_id: location.id,
},
[Modules.FULFILLMENT]: {
fulfillment_set_id: fulfillmentSet.id,
},
},
])
await remoteLink.create([
{
[Modules.STOCK_LOCATION]: {
stock_location_id: location.id,
},
[Modules.FULFILLMENT]: {
fulfillment_provider_id: "manual_test-provider",
},
},
])
const serviceZone = await fulfillmentModule.createServiceZones({
name: "Test",
fulfillment_set_id: fulfillmentSet.id,
@@ -130,6 +130,17 @@ export async function prepareDataFixtures({ container }) {
},
])
await remoteLink.create([
{
[Modules.STOCK_LOCATION]: {
stock_location_id: location.id,
},
[Modules.FULFILLMENT]: {
fulfillment_provider_id: "manual_test-provider",
},
},
])
const shippingOptionData: FulfillmentWorkflow.CreateShippingOptionsWorkflowInput =
{
name: "Shipping option",
@@ -117,6 +117,17 @@ async function prepareDataFixtures({ container }) {
const remoteLink = container.resolve(ContainerRegistrationKeys.REMOTE_LINK)
await remoteLink.create([
{
[Modules.STOCK_LOCATION]: {
stock_location_id: location.id,
},
[Modules.FULFILLMENT]: {
fulfillment_provider_id: "manual_test-provider",
},
},
])
await remoteLink.create([
{
[Modules.STOCK_LOCATION]: {
@@ -43,6 +43,7 @@ async function prepareDataFixtures({ container }) {
const pricingModule = container.resolve(ModuleRegistrationName.PRICING)
const inventoryModule = container.resolve(ModuleRegistrationName.INVENTORY)
const customerService = container.resolve(ModuleRegistrationName.CUSTOMER)
const remoteLink = container.resolve(ContainerRegistrationKeys.REMOTE_LINK)
const customer = await customerService.createCustomers({
first_name: "joe",
@@ -98,6 +99,17 @@ async function prepareDataFixtures({ container }) {
},
})
await remoteLink.create([
{
[Modules.STOCK_LOCATION]: {
stock_location_id: location.id,
},
[Modules.FULFILLMENT]: {
fulfillment_provider_id: "manual_test-provider",
},
},
])
const [product] = await productModule.createProducts([
{
title: "Test product",
@@ -150,8 +162,6 @@ async function prepareDataFixtures({ container }) {
},
])
const remoteLink = container.resolve(ContainerRegistrationKeys.REMOTE_LINK)
await remoteLink.create([
{
[Modules.STOCK_LOCATION]: {
@@ -115,6 +115,17 @@ async function prepareDataFixtures({ container }) {
const remoteLink = container.resolve(ContainerRegistrationKeys.REMOTE_LINK)
await remoteLink.create([
{
[Modules.STOCK_LOCATION]: {
stock_location_id: location.id,
},
[Modules.FULFILLMENT]: {
fulfillment_provider_id: "manual_test-provider",
},
},
])
await remoteLink.create([
{
[Modules.STOCK_LOCATION]: {
@@ -42,6 +42,7 @@ async function prepareDataFixtures({ container }) {
const productModule = container.resolve(ModuleRegistrationName.PRODUCT)
const pricingModule = container.resolve(ModuleRegistrationName.PRICING)
const inventoryModule = container.resolve(ModuleRegistrationName.INVENTORY)
const remoteLink = container.resolve(ContainerRegistrationKeys.REMOTE_LINK)
const shippingProfile = await fulfillmentService.createShippingProfiles({
name: "test",
@@ -92,6 +93,17 @@ async function prepareDataFixtures({ container }) {
},
})
await remoteLink.create([
{
[Modules.STOCK_LOCATION]: {
stock_location_id: location.id,
},
[Modules.FULFILLMENT]: {
fulfillment_provider_id: "manual_test-provider",
},
},
])
const [product] = await productModule.createProducts([
{
title: "Test product",
@@ -117,8 +129,6 @@ async function prepareDataFixtures({ container }) {
},
])
const remoteLink = container.resolve(ContainerRegistrationKeys.REMOTE_LINK)
await remoteLink.create([
{
[Modules.STOCK_LOCATION]: {
@@ -124,6 +124,17 @@ async function prepareDataFixtures({ container }) {
const remoteLink = container.resolve(ContainerRegistrationKeys.REMOTE_LINK)
await remoteLink.create([
{
[Modules.STOCK_LOCATION]: {
stock_location_id: location.id,
},
[Modules.FULFILLMENT]: {
fulfillment_provider_id: "manual_test-provider",
},
},
])
await remoteLink.create([
{
[Modules.STOCK_LOCATION]: {
@@ -118,6 +118,17 @@ async function prepareDataFixtures({ container }) {
const remoteLink = container.resolve(ContainerRegistrationKeys.REMOTE_LINK)
await remoteLink.create([
{
[Modules.STOCK_LOCATION]: {
stock_location_id: location.id,
},
[Modules.FULFILLMENT]: {
fulfillment_provider_id: "manual_test-provider",
},
},
])
await remoteLink.create([
{
[Modules.STOCK_LOCATION]: {
@@ -29,7 +29,6 @@ medusaIntegrationTestRunner({
beforeEach(async () => {
fixtures = await prepareDataFixtures({ container })
order = await createOrderFixture({
container,
product: fixtures.product,
@@ -126,9 +126,6 @@ medusaIntegrationTestRunner({
stock_location_id: stockLocation.id,
},
},
])
await remoteLinkService.create([
{
[Modules.STOCK_LOCATION]: {
stock_location_id: stockLocation.id,
@@ -137,6 +134,14 @@ medusaIntegrationTestRunner({
fulfillment_set_id: fulfillmentSet.id,
},
},
{
[Modules.STOCK_LOCATION]: {
stock_location_id: stockLocation.id,
},
[Modules.FULFILLMENT]: {
fulfillment_provider_id: "manual_test-provider",
},
},
])
shippingOption = (
@@ -16,6 +16,7 @@ import {
import {
ContainerRegistrationKeys,
ModuleRegistrationName,
Modules,
RuleOperator,
remoteQueryObjectFromString,
} from "@medusajs/utils"
@@ -151,6 +152,37 @@ medusaIntegrationTestRunner({
},
],
})
const stockLocationModule = container.resolve(
ModuleRegistrationName.STOCK_LOCATION
)
const location = await stockLocationModule.createStockLocations({
name: "Europe",
})
const remoteLink = container.resolve(
ContainerRegistrationKeys.REMOTE_LINK
)
await remoteLink.create([
{
[Modules.STOCK_LOCATION]: {
stock_location_id: location.id,
},
[Modules.FULFILLMENT]: {
fulfillment_set_id: fulfillmentSet.id,
},
},
{
[Modules.STOCK_LOCATION]: {
stock_location_id: location.id,
},
[Modules.FULFILLMENT]: {
fulfillment_provider_id: "manual_test-provider",
},
},
])
})
it("should create, update and delete rules in batch", async () => {
@@ -10,6 +10,7 @@ import {
import {
ContainerRegistrationKeys,
ModuleRegistrationName,
Modules,
RuleOperator,
remoteQueryObjectFromString,
} from "@medusajs/utils"
@@ -57,6 +58,37 @@ medusaIntegrationTestRunner({
},
],
})
const stockLocationModule = container.resolve(
ModuleRegistrationName.STOCK_LOCATION
)
const location = await stockLocationModule.createStockLocations({
name: "Europe",
})
const remoteLink = container.resolve(
ContainerRegistrationKeys.REMOTE_LINK
)
await remoteLink.create([
{
[Modules.STOCK_LOCATION]: {
stock_location_id: location.id,
},
[Modules.FULFILLMENT]: {
fulfillment_set_id: fulfillmentSet.id,
},
},
{
[Modules.STOCK_LOCATION]: {
stock_location_id: location.id,
},
[Modules.FULFILLMENT]: {
fulfillment_provider_id: "manual_test-provider",
},
},
])
})
it("should create shipping options and prices", async () => {
@@ -13,6 +13,7 @@ import {
import {
ContainerRegistrationKeys,
ModuleRegistrationName,
Modules,
RuleOperator,
remoteQueryObjectFromString,
} from "@medusajs/utils"
@@ -60,6 +61,37 @@ medusaIntegrationTestRunner({
},
],
})
const stockLocationModule = container.resolve(
ModuleRegistrationName.STOCK_LOCATION
)
const location = await stockLocationModule.createStockLocations({
name: "Europe",
})
const remoteLink = container.resolve(
ContainerRegistrationKeys.REMOTE_LINK
)
await remoteLink.create([
{
[Modules.STOCK_LOCATION]: {
stock_location_id: location.id,
},
[Modules.FULFILLMENT]: {
fulfillment_set_id: fulfillmentSet.id,
},
},
{
[Modules.STOCK_LOCATION]: {
stock_location_id: location.id,
},
[Modules.FULFILLMENT]: {
fulfillment_provider_id: "manual_test-provider",
},
},
])
})
it("should delete shipping options", async () => {
@@ -14,6 +14,7 @@ import {
import {
ContainerRegistrationKeys,
ModuleRegistrationName,
Modules,
RuleOperator,
remoteQueryObjectFromString,
} from "@medusajs/utils"
@@ -61,6 +62,37 @@ medusaIntegrationTestRunner({
},
],
})
const stockLocationModule = container.resolve(
ModuleRegistrationName.STOCK_LOCATION
)
const location = await stockLocationModule.createStockLocations({
name: "Europe",
})
const remoteLink = container.resolve(
ContainerRegistrationKeys.REMOTE_LINK
)
await remoteLink.create([
{
[Modules.STOCK_LOCATION]: {
stock_location_id: location.id,
},
[Modules.FULFILLMENT]: {
fulfillment_set_id: fulfillmentSet.id,
},
},
{
[Modules.STOCK_LOCATION]: {
stock_location_id: location.id,
},
[Modules.FULFILLMENT]: {
fulfillment_provider_id: "manual_test-provider",
},
},
])
})
it("should update shipping options", async () => {