Skip to content

Commit

Permalink
feat(core-flows,framework,medusa): list shipping options pass in cart…
Browse files Browse the repository at this point in the history
… as pricing context (medusajs#10374)

* feat(core-flows,framework,medusa): list shipping options pass in cart as pricing context

* chore: add test for shipping options returning free shipping
  • Loading branch information
riqwan authored Dec 1, 2024
1 parent eacd691 commit 11bd556
Show file tree
Hide file tree
Showing 12 changed files with 283 additions and 155 deletions.
7 changes: 7 additions & 0 deletions .changeset/spicy-beds-rescue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@medusajs/core-flows": patch
"@medusajs/framework": patch
"@medusajs/medusa": patch
---

feat(core-flows,framework,medusa): list shipping options pass in cart as pricing context
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,12 @@ medusaIntegrationTestRunner({
amount: 500,
rules: [
{
attribute: "cart_total",
attribute: "total",
operator: "gte",
value: 100,
},
{
attribute: "cart_total",
attribute: "total",
operator: "lte",
value: 200,
},
Expand Down Expand Up @@ -218,12 +218,12 @@ medusaIntegrationTestRunner({
rules_count: 2,
price_rules: expect.arrayContaining([
expect.objectContaining({
attribute: "cart_total",
attribute: "total",
operator: "gte",
value: "100",
}),
expect.objectContaining({
attribute: "cart_total",
attribute: "total",
operator: "lte",
value: "200",
}),
Expand Down Expand Up @@ -327,7 +327,7 @@ medusaIntegrationTestRunner({
amount: 500,
rules: [
{
attribute: "cart_total",
attribute: "total",
operator: "gt",
value: 200,
},
Expand Down Expand Up @@ -378,7 +378,7 @@ medusaIntegrationTestRunner({
rules_count: 2,
price_rules: expect.arrayContaining([
expect.objectContaining({
attribute: "cart_total",
attribute: "total",
operator: "gt",
value: "200",
}),
Expand Down Expand Up @@ -458,7 +458,7 @@ medusaIntegrationTestRunner({
amount: 500,
rules: [
{
attribute: "cart_total",
attribute: "total",
operator: "not_whitelisted",
value: 100,
},
Expand Down Expand Up @@ -496,7 +496,7 @@ medusaIntegrationTestRunner({
amount: 500,
rules: [
{
attribute: "cart_total",
attribute: "total",
operator: "gt",
value: "string",
},
Expand Down Expand Up @@ -626,7 +626,7 @@ medusaIntegrationTestRunner({
amount: 5,
rules: [
{
attribute: "cart_total",
attribute: "total",
operator: "gt",
value: 200,
},
Expand Down Expand Up @@ -702,7 +702,7 @@ medusaIntegrationTestRunner({
amount: 5,
price_rules: [
expect.objectContaining({
attribute: "cart_total",
attribute: "total",
operator: "gt",
value: "200",
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,17 @@ medusaIntegrationTestRunner({
region_id: region.id,
amount: 1100,
},
{
region_id: region.id,
amount: 0,
rules: [
{
operator: "gt",
attribute: "total",
value: 2000,
},
],
},
{
region_id: regionTwo.id,
amount: 500,
Expand Down Expand Up @@ -266,6 +277,83 @@ medusaIntegrationTestRunner({
})
)
})

it("should return prices based on cart total", async () => {
cart = (
await api.post(
`/store/carts`,
{
region_id: region.id,
sales_channel_id: salesChannel.id,
currency_code: "usd",
email: "[email protected]",
items: [
{
variant_id: product.variants[0].id,
// Adding a quantity of 100 to emulate total being greater than 2000
quantity: 100,
},
],
},
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",
// Free shipping due to cart total being greater than 2000
amount: 0,
price_type: "flat",
})
)
})

it("should throw when required fields of a cart are not present", async () => {
cart = (
await api.post(
`/store/carts`,
{
region_id: region.id,
currency_code: "usd",
sales_channel_id: null,
email: "[email protected]",
items: [],
},
storeHeaders
)
).data.cart

const { response } = await api
.get(`/store/shipping-options?cart_id=${cart.id}`, storeHeaders)
.catch((e) => e)

expect(response.data).toEqual({
type: "invalid_data",
message:
"Field(s) are required to have value to continue - sales_channel_id",
})
})

it("should throw error when cart_id is not passed as a parameter", async () => {
const { response } = await api
.get(`/store/shipping-options`, storeHeaders)
.catch((e) => e)

expect(response.data).toEqual({
type: "invalid_data",
message: "Invalid request: Field 'cart_id' is required",
})
})
})
})
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
updatePaymentCollectionStepId,
updateTaxLinesWorkflow,
} from "@medusajs/core-flows"
import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
import {
ICartModuleService,
ICustomerModuleService,
Expand All @@ -30,7 +31,6 @@ import {
Modules,
RuleOperator,
} from "@medusajs/utils"
import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
import {
adminHeaders,
createAdminUser,
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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,
},
},
})

Expand All @@ -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",
Expand Down Expand Up @@ -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([])
Expand All @@ -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",
Expand Down Expand Up @@ -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,
})

Expand Down
Loading

0 comments on commit 11bd556

Please sign in to comment.