Skip to content

Commit

Permalink
feat(core-flows, medusa): add shipping methods to cart API (#7150)
Browse files Browse the repository at this point in the history
* feat(core-flows, medusa): add shipping methods to cart API

* chore: change id to option_id

* chore: use list listShippingOptionsForContext instead of validateShippingOption

* chore: remove comment

* chore: add refresh shipping methods step

* chore: set cart step

* chore: update all workflows to refresh shipping methods

* chore: add tests + cleanup
  • Loading branch information
riqwan committed Apr 29, 2024
1 parent 4b57c5d commit d2393f0
Show file tree
Hide file tree
Showing 19 changed files with 689 additions and 81 deletions.
6 changes: 6 additions & 0 deletions .changeset/thick-bats-rescue.md
@@ -0,0 +1,6 @@
---
"@medusajs/core-flows": patch
"@medusajs/medusa": patch
---

feat(core-flows, medusa): add shipping methods to cart API
140 changes: 103 additions & 37 deletions integration-tests/modules/__tests__/cart/store/cart.workflows.spec.ts
Expand Up @@ -26,7 +26,7 @@ import {
ISalesChannelModuleService,
IStockLocationServiceNext,
} from "@medusajs/types"
import { ContainerRegistrationKeys } from "@medusajs/utils"
import { ContainerRegistrationKeys, RuleOperator } from "@medusajs/utils"
import { medusaIntegrationTestRunner } from "medusa-test-utils"
import adminSeeder from "../../../../helpers/admin-seeder"

Expand Down Expand Up @@ -1397,34 +1397,44 @@ medusaIntegrationTestRunner({
})
})
})

describe("AddShippingMethodToCartWorkflow", () => {
it("should add shipping method to cart", async () => {
let cart = await cartModuleService.create({
let cart
let shippingProfile
let fulfillmentSet
let priceSet

beforeEach(async () => {
cart = await cartModuleService.create({
currency_code: "usd",
shipping_address: {
country_code: "us",
province: "ny",
},
})

const shippingProfile =
await fulfillmentModule.createShippingProfiles({
name: "Test",
type: "default",
})
shippingProfile = await fulfillmentModule.createShippingProfiles({
name: "Test",
type: "default",
})

const fulfillmentSet = await fulfillmentModule.create({
fulfillmentSet = await fulfillmentModule.create({
name: "Test",
type: "test-type",
service_zones: [
{
name: "Test",
geo_zones: [
{
type: "country",
country_code: "us",
},
],
geo_zones: [{ type: "country", country_code: "us" }],
},
],
})

priceSet = await pricingModule.create({
prices: [{ amount: 3000, currency_code: "usd" }],
})
})

it("should add shipping method to cart", async () => {
const shippingOption = await fulfillmentModule.createShippingOptions({
name: "Test shipping option",
service_zone_id: fulfillmentSet.service_zones[0].id,
Expand All @@ -1436,41 +1446,26 @@ medusaIntegrationTestRunner({
description: "Test description",
code: "test-code",
},
})

const priceSet = await pricingModule.create({
prices: [
rules: [
{
amount: 3000,
currency_code: "usd",
operator: RuleOperator.EQ,
attribute: "shipping_address.province",
value: "ny",
},
],
})

await remoteLink.create([
{
[Modules.FULFILLMENT]: {
shipping_option_id: shippingOption.id,
},
[Modules.PRICING]: {
price_set_id: priceSet.id,
},
[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,
},
],
options: [{ id: shippingOption.id }],
cart_id: cart.id,
currency_code: cart.currency_code,
},
})

Expand All @@ -1491,6 +1486,77 @@ medusaIntegrationTestRunner({
})
)
})

it("should throw error when shipping option is not valid", async () => {
const shippingOption = await fulfillmentModule.createShippingOptions({
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",
},
rules: [
{
operator: RuleOperator.EQ,
attribute: "shipping_address.city",
value: "sf",
},
],
})

await remoteLink.create([
{
[Modules.FULFILLMENT]: { shipping_option_id: shippingOption.id },
[Modules.PRICING]: { price_set_id: priceSet.id },
},
])

const { errors } = await addShippingMethodToWorkflow(
appContainer
).run({
input: {
options: [{ id: shippingOption.id }],
cart_id: cart.id,
},
throwOnError: false,
})

// Rules are setup only for Germany, this should throw an error
expect(errors).toEqual([
expect.objectContaining({
error: expect.objectContaining({
message: `Shipping Options are invalid for cart.`,
type: "invalid_data",
}),
}),
])
})

it("should throw error when shipping option is not present in the db", async () => {
const { errors } = await addShippingMethodToWorkflow(
appContainer
).run({
input: {
options: [{ id: "does-not-exist" }],
cart_id: cart.id,
},
throwOnError: false,
})

// Rules are setup only for Berlin, this should throw an error
expect(errors).toEqual([
expect.objectContaining({
error: expect.objectContaining({
message: "Shipping Options are invalid for cart.",
type: "invalid_data",
}),
}),
])
})
})

describe("listShippingOptionsForCartWorkflow", () => {
Expand Down

0 comments on commit d2393f0

Please sign in to comment.