feat(core-flows,framework,medusa): list shipping options pass in cart as pricing context (#10374)
* feat(core-flows,framework,medusa): list shipping options pass in cart as pricing context * chore: add test for shipping options returning free shipping
This commit is contained in:
@@ -13,6 +13,7 @@ import {
|
||||
updatePaymentCollectionStepId,
|
||||
updateTaxLinesWorkflow,
|
||||
} from "@medusajs/core-flows"
|
||||
import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
|
||||
import {
|
||||
ICartModuleService,
|
||||
ICustomerModuleService,
|
||||
@@ -30,7 +31,6 @@ import {
|
||||
Modules,
|
||||
RuleOperator,
|
||||
} from "@medusajs/utils"
|
||||
import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
|
||||
import {
|
||||
adminHeaders,
|
||||
createAdminUser,
|
||||
@@ -1835,6 +1835,15 @@ medusaIntegrationTestRunner({
|
||||
})
|
||||
|
||||
describe("listShippingOptionsForCartWorkflow", () => {
|
||||
let region
|
||||
|
||||
beforeEach(async () => {
|
||||
region = await regionModuleService.createRegions({
|
||||
name: "US",
|
||||
currency_code: "usd",
|
||||
})
|
||||
})
|
||||
|
||||
it("should list shipping options for cart", async () => {
|
||||
const salesChannel = await scModuleService.createSalesChannels({
|
||||
name: "Webshop",
|
||||
@@ -1846,6 +1855,7 @@ medusaIntegrationTestRunner({
|
||||
|
||||
let cart = await cartModuleService.createCarts({
|
||||
currency_code: "usd",
|
||||
region_id: region.id,
|
||||
sales_channel_id: salesChannel.id,
|
||||
shipping_address: {
|
||||
city: "CPH",
|
||||
@@ -1935,13 +1945,6 @@ medusaIntegrationTestRunner({
|
||||
).run({
|
||||
input: {
|
||||
cart_id: cart.id,
|
||||
sales_channel_id: salesChannel.id,
|
||||
currency_code: "usd",
|
||||
shipping_address: {
|
||||
city: cart.shipping_address?.city,
|
||||
province: cart.shipping_address?.province,
|
||||
country_code: cart.shipping_address?.country_code,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
@@ -1965,6 +1968,7 @@ medusaIntegrationTestRunner({
|
||||
|
||||
let cart = await cartModuleService.createCarts({
|
||||
currency_code: "usd",
|
||||
region_id: region.id,
|
||||
sales_channel_id: salesChannel.id,
|
||||
shipping_address: {
|
||||
city: "CPH",
|
||||
@@ -2044,16 +2048,7 @@ medusaIntegrationTestRunner({
|
||||
const { result } = await listShippingOptionsForCartWorkflow(
|
||||
appContainer
|
||||
).run({
|
||||
input: {
|
||||
cart_id: cart.id,
|
||||
sales_channel_id: salesChannel.id,
|
||||
currency_code: "usd",
|
||||
shipping_address: {
|
||||
city: cart.shipping_address?.city,
|
||||
province: cart.shipping_address?.province,
|
||||
country_code: cart.shipping_address?.country_code,
|
||||
},
|
||||
},
|
||||
input: { cart_id: cart.id },
|
||||
})
|
||||
|
||||
expect(result).toEqual([])
|
||||
@@ -2070,6 +2065,7 @@ medusaIntegrationTestRunner({
|
||||
|
||||
let cart = await cartModuleService.createCarts({
|
||||
currency_code: "usd",
|
||||
region_id: region.id,
|
||||
sales_channel_id: salesChannel.id,
|
||||
shipping_address: {
|
||||
city: "CPH",
|
||||
@@ -2140,16 +2136,7 @@ medusaIntegrationTestRunner({
|
||||
const { errors } = await listShippingOptionsForCartWorkflow(
|
||||
appContainer
|
||||
).run({
|
||||
input: {
|
||||
cart_id: cart.id,
|
||||
sales_channel_id: salesChannel.id,
|
||||
currency_code: "usd",
|
||||
shipping_address: {
|
||||
city: cart.shipping_address?.city,
|
||||
province: cart.shipping_address?.province,
|
||||
country_code: cart.shipping_address?.country_code,
|
||||
},
|
||||
},
|
||||
input: { cart_id: cart.id },
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
|
||||
@@ -1,272 +0,0 @@
|
||||
import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
|
||||
import {
|
||||
IFulfillmentModuleService,
|
||||
IRegionModuleService,
|
||||
} from "@medusajs/types"
|
||||
import { ContainerRegistrationKeys, Modules } from "@medusajs/utils"
|
||||
import {
|
||||
createAdminUser,
|
||||
generatePublishableKey,
|
||||
generateStoreHeaders,
|
||||
} 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("Store: Shipping Option API", () => {
|
||||
let appContainer
|
||||
let fulfillmentModule: IFulfillmentModuleService
|
||||
let regionService: IRegionModuleService
|
||||
|
||||
let salesChannel
|
||||
let region
|
||||
let regionTwo
|
||||
let product
|
||||
let stockLocation
|
||||
let shippingProfile
|
||||
let fulfillmentSet
|
||||
let cart
|
||||
let shippingOption
|
||||
let storeHeaders
|
||||
|
||||
beforeAll(async () => {
|
||||
appContainer = getContainer()
|
||||
fulfillmentModule = appContainer.resolve(Modules.FULFILLMENT)
|
||||
regionService = appContainer.resolve(Modules.REGION)
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
const publishableKey = await generatePublishableKey(appContainer)
|
||||
storeHeaders = generateStoreHeaders({ publishableKey })
|
||||
|
||||
await createAdminUser(dbConnection, adminHeaders, appContainer)
|
||||
const remoteLinkService = appContainer.resolve(
|
||||
ContainerRegistrationKeys.REMOTE_LINK
|
||||
)
|
||||
|
||||
region = await regionService.createRegions({
|
||||
name: "Test region",
|
||||
countries: ["US"],
|
||||
currency_code: "usd",
|
||||
})
|
||||
|
||||
regionTwo = await regionService.createRegions({
|
||||
name: "Test region two",
|
||||
countries: ["DK"],
|
||||
currency_code: "dkk",
|
||||
})
|
||||
|
||||
await api.post(
|
||||
"/admin/price-preferences",
|
||||
{
|
||||
attribute: "region_id",
|
||||
value: regionTwo.id,
|
||||
is_tax_inclusive: true,
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
salesChannel = (
|
||||
await api.post(
|
||||
"/admin/sales-channels",
|
||||
{ name: "first channel", description: "channel" },
|
||||
adminHeaders
|
||||
)
|
||||
).data.sales_channel
|
||||
|
||||
product = (
|
||||
await api.post(
|
||||
"/admin/products",
|
||||
{
|
||||
title: "Test fixture",
|
||||
options: [
|
||||
{ title: "size", values: ["large", "small"] },
|
||||
{ title: "color", values: ["green"] },
|
||||
],
|
||||
variants: [
|
||||
{
|
||||
title: "Test variant",
|
||||
manage_inventory: false,
|
||||
prices: [
|
||||
{
|
||||
currency_code: "usd",
|
||||
amount: 100,
|
||||
},
|
||||
{
|
||||
currency_code: "dkk",
|
||||
amount: 100,
|
||||
},
|
||||
],
|
||||
options: {
|
||||
size: "large",
|
||||
color: "green",
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
).data.product
|
||||
|
||||
stockLocation = (
|
||||
await api.post(
|
||||
`/admin/stock-locations`,
|
||||
{
|
||||
name: "test location",
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
).data.stock_location
|
||||
|
||||
shippingProfile = await fulfillmentModule.createShippingProfiles({
|
||||
name: "Test",
|
||||
type: "default",
|
||||
})
|
||||
|
||||
fulfillmentSet = await fulfillmentModule.createFulfillmentSets({
|
||||
name: "Test",
|
||||
type: "test-type",
|
||||
service_zones: [
|
||||
{
|
||||
name: "Test",
|
||||
geo_zones: [
|
||||
{ type: "country", country_code: "us" },
|
||||
{ type: "country", country_code: "dk" },
|
||||
],
|
||||
},
|
||||
],
|
||||
})
|
||||
|
||||
await remoteLinkService.create([
|
||||
{
|
||||
[Modules.SALES_CHANNEL]: {
|
||||
sales_channel_id: salesChannel.id,
|
||||
},
|
||||
[Modules.STOCK_LOCATION]: {
|
||||
stock_location_id: stockLocation.id,
|
||||
},
|
||||
},
|
||||
{
|
||||
[Modules.STOCK_LOCATION]: {
|
||||
stock_location_id: stockLocation.id,
|
||||
},
|
||||
[Modules.FULFILLMENT]: {
|
||||
fulfillment_set_id: fulfillmentSet.id,
|
||||
},
|
||||
},
|
||||
])
|
||||
|
||||
await api.post(
|
||||
`/admin/stock-locations/${stockLocation.id}/fulfillment-providers`,
|
||||
{ add: ["manual_test-provider"] },
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
shippingOption = (
|
||||
await api.post(
|
||||
`/admin/shipping-options`,
|
||||
{
|
||||
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: 1100,
|
||||
},
|
||||
{
|
||||
region_id: regionTwo.id,
|
||||
amount: 500,
|
||||
},
|
||||
],
|
||||
rules: [],
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
).data.shipping_option
|
||||
})
|
||||
|
||||
describe("GET /store/shipping-options?cart_id=", () => {
|
||||
it("should get shipping options for a cart successfully", async () => {
|
||||
cart = (
|
||||
await api.post(
|
||||
`/store/carts`,
|
||||
{
|
||||
region_id: region.id,
|
||||
sales_channel_id: salesChannel.id,
|
||||
currency_code: "usd",
|
||||
email: "test@admin.com",
|
||||
items: [
|
||||
{
|
||||
variant_id: product.variants[0].id,
|
||||
quantity: 1,
|
||||
},
|
||||
],
|
||||
},
|
||||
storeHeaders
|
||||
)
|
||||
).data.cart
|
||||
|
||||
const resp = await api.get(
|
||||
`/store/shipping-options?cart_id=${cart.id}`,
|
||||
storeHeaders
|
||||
)
|
||||
|
||||
const shippingOptions = resp.data.shipping_options
|
||||
|
||||
expect(shippingOptions).toHaveLength(1)
|
||||
expect(shippingOptions[0]).toEqual(
|
||||
expect.objectContaining({
|
||||
id: shippingOption.id,
|
||||
name: "Test shipping option",
|
||||
amount: 1100,
|
||||
price_type: "flat",
|
||||
})
|
||||
)
|
||||
|
||||
cart = (
|
||||
await api.post(
|
||||
`/store/carts/${cart.id}`,
|
||||
{
|
||||
region_id: regionTwo.id,
|
||||
},
|
||||
storeHeaders
|
||||
)
|
||||
).data.cart
|
||||
|
||||
const secondResp = await api.get(
|
||||
`/store/shipping-options?cart_id=${cart.id}`,
|
||||
storeHeaders
|
||||
)
|
||||
|
||||
expect(secondResp.data.shipping_options).toHaveLength(1)
|
||||
expect(secondResp.data.shipping_options[0]).toEqual(
|
||||
expect.objectContaining({
|
||||
id: shippingOption.id,
|
||||
name: "Test shipping option",
|
||||
amount: 500,
|
||||
price_type: "flat",
|
||||
is_tax_inclusive: true,
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
},
|
||||
})
|
||||
Reference in New Issue
Block a user