feat: Add shipping method to cart (#6661)

This commit is contained in:
Oli Juhl
2024-03-12 17:52:48 +01:00
committed by GitHub
parent c3c4f49fc2
commit f0c8142635
18 changed files with 491 additions and 23 deletions
@@ -1,4 +1,5 @@
import {
addShippingMethodToWorkflow,
addToCartWorkflow,
createCartWorkflow,
createPaymentCollectionForCartWorkflow,
@@ -15,6 +16,7 @@ import { ModuleRegistrationName, Modules } from "@medusajs/modules-sdk"
import {
ICartModuleService,
ICustomerModuleService,
IFulfillmentModuleService,
IPaymentModuleService,
IPricingModuleService,
IProductModuleService,
@@ -40,6 +42,7 @@ medusaIntegrationTestRunner({
let productModule: IProductModuleService
let pricingModule: IPricingModuleService
let paymentModule: IPaymentModuleService
let fulfillmentModule: IFulfillmentModuleService
let remoteLink, remoteQuery
let defaultRegion
@@ -57,6 +60,9 @@ medusaIntegrationTestRunner({
productModule = appContainer.resolve(ModuleRegistrationName.PRODUCT)
pricingModule = appContainer.resolve(ModuleRegistrationName.PRICING)
paymentModule = appContainer.resolve(ModuleRegistrationName.PAYMENT)
fulfillmentModule = appContainer.resolve(
ModuleRegistrationName.FULFILLMENT
)
remoteLink = appContainer.resolve("remoteLink")
remoteQuery = appContainer.resolve("remoteQuery")
})
@@ -978,6 +984,99 @@ medusaIntegrationTestRunner({
})
})
})
describe("AddShippingMethodToCartWorkflow", () => {
it("should add shipping method to cart", async () => {
let cart = await cartModuleService.create({
currency_code: "usd",
})
const shippingProfile =
await fulfillmentModule.createShippingProfiles({
name: "Test",
type: "default",
})
const fulfillmentSet = await fulfillmentModule.create({
name: "Test",
type: "test-type",
})
const serviceZone = await fulfillmentModule.createServiceZones({
name: "Test",
fulfillment_set_id: fulfillmentSet.id,
geo_zones: [
{
type: "country",
country_code: "us",
},
],
})
const shippingOption = await fulfillmentModule.createShippingOptions({
name: "Test shipping option",
service_zone_id: serviceZone.id,
shipping_profile_id: shippingProfile.id,
provider_id: "manual_test-provider",
price_type: "flat",
type: {
label: "Test type",
description: "Test description",
code: "test-code",
},
})
const priceSet = await pricingModule.create({
prices: [
{
amount: 3000,
currency_code: "usd",
},
],
})
await remoteLink.create([
{
[Modules.FULFILLMENT]: {
shipping_option_id: shippingOption.id,
},
[Modules.PRICING]: {
price_set_id: priceSet.id,
},
},
])
cart = await cartModuleService.retrieve(cart.id, {
select: ["id", "region_id", "currency_code"],
})
await addShippingMethodToWorkflow(appContainer).run({
input: {
options: [
{
id: shippingOption.id,
},
],
cart_id: cart.id,
currency_code: cart.currency_code,
},
})
cart = await cartModuleService.retrieve(cart.id, {
relations: ["shipping_methods"],
})
expect(cart).toEqual(
expect.objectContaining({
id: cart.id,
currency_code: "usd",
shipping_methods: expect.arrayContaining([
expect.objectContaining({
amount: 3000,
name: "Test shipping option",
}),
]),
})
)
})
})
})
},
})
@@ -1,7 +1,7 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { IFulfillmentModuleService } from "@medusajs/types"
import { medusaIntegrationTestRunner } from "medusa-test-utils/dist"
import { setupFullDataFulfillmentStructure } from "../fixtures"
import { IFulfillmentModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
jest.setTimeout(100000)
@@ -0,0 +1,112 @@
import { ModuleRegistrationName, Modules } from "@medusajs/modules-sdk"
import {
IFulfillmentModuleService,
IPricingModuleService,
} from "@medusajs/types"
import { ContainerRegistrationKeys } from "@medusajs/utils"
import { medusaIntegrationTestRunner } from "medusa-test-utils"
jest.setTimeout(50000)
const env = { MEDUSA_FF_MEDUSA_V2: true }
medusaIntegrationTestRunner({
env,
testSuite: ({ getContainer }) => {
describe("Region and Payment Providers", () => {
let appContainer
let fulfillmentModule: IFulfillmentModuleService
let pricingModule: IPricingModuleService
let remoteQuery
let remoteLink
beforeAll(async () => {
appContainer = getContainer()
fulfillmentModule = appContainer.resolve(
ModuleRegistrationName.FULFILLMENT
)
pricingModule = appContainer.resolve(ModuleRegistrationName.PRICING)
remoteQuery = appContainer.resolve(
ContainerRegistrationKeys.REMOTE_QUERY
)
remoteLink = appContainer.resolve(ContainerRegistrationKeys.REMOTE_LINK)
})
it("should query shipping option and price set link with remote query", async () => {
const shippingProfile = await fulfillmentModule.createShippingProfiles({
name: "Test",
type: "default",
})
const fulfillmentSet = await fulfillmentModule.create({
name: "Test",
type: "test-type",
})
const serviceZone = await fulfillmentModule.createServiceZones({
name: "Test",
fulfillment_set_id: fulfillmentSet.id,
geo_zones: [
{
type: "country",
country_code: "us",
},
],
})
const shippingOption = await fulfillmentModule.createShippingOptions({
name: "Test shipping option",
service_zone_id: serviceZone.id,
shipping_profile_id: shippingProfile.id,
provider_id: "manual_test-provider",
price_type: "flat",
type: {
label: "Test type",
description: "Test description",
code: "test-code",
},
})
const priceSet = await pricingModule.create({
prices: [
{
amount: 3000,
currency_code: "usd",
},
],
})
await remoteLink.create([
{
[Modules.FULFILLMENT]: {
shipping_option_id: shippingOption.id,
},
[Modules.PRICING]: {
price_set_id: priceSet.id,
},
},
])
const link = await remoteQuery({
shipping_option: {
fields: ["id"],
price: {
fields: ["id", "price_set_id", "shipping_option_id"],
},
},
})
expect(link).toHaveLength(1)
expect(link).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: shippingOption.id,
price: expect.objectContaining({
price_set_id: priceSet.id,
shipping_option_id: shippingOption.id,
}),
}),
])
)
})
})
},
})