Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pull] develop from medusajs:develop #8

Merged
merged 7 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changeset/clean-news-sin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@medusajs/fulfillment": patch
"@medusajs/types": patch
"@medusajs/utils": patch
"@medusajs/medusa": patch
---

chore: fulfillment module DML
5 changes: 5 additions & 0 deletions .changeset/eight-pigs-wave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@medusajs/product": patch
---

fix(product): updating collections with products fix
5 changes: 5 additions & 0 deletions .changeset/slimy-seas-join.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@medusajs/core-flows": patch
---

fix(core-flows): refresh payment collections upon shipping changes
131 changes: 106 additions & 25 deletions integration-tests/http/__tests__/cart/store/cart.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ medusaIntegrationTestRunner({
})

describe("POST /store/carts/:id/line-items", () => {
let shippingOption
let shippingOption, shippingOptionExpensive

beforeEach(async () => {
const stockLocation = (
Expand Down Expand Up @@ -358,25 +358,63 @@ medusaIntegrationTestRunner({
adminHeaders
)

const shippingOptionPayload = {
name: `Shipping`,
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 },
{
currency_code: "usd",
amount: 0,
rules: [
{
attribute: "item_total",
operator: "gt",
value: 5000,
},
],
},
],
rules: [
{
attribute: "enabled_in_store",
value: '"true"',
operator: "eq",
},
{
attribute: "is_return",
value: "false",
operator: "eq",
},
],
}

shippingOption = (
await api.post(
`/admin/shipping-options`,
shippingOptionPayload,
adminHeaders
)
).data.shipping_option

shippingOptionExpensive = (
await api.post(
`/admin/shipping-options`,
{
name: `Shipping`,
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",
},
...shippingOptionPayload,
prices: [
{ currency_code: "usd", amount: 1000 },
{ currency_code: "usd", amount: 10000 },
{
currency_code: "usd",
amount: 0,
amount: 5000,
rules: [
{
attribute: "item_total",
Expand All @@ -386,18 +424,6 @@ medusaIntegrationTestRunner({
],
},
],
rules: [
{
attribute: "enabled_in_store",
value: '"true"',
operator: "eq",
},
{
attribute: "is_return",
value: "false",
operator: "eq",
},
],
},
adminHeaders
)
Expand Down Expand Up @@ -555,6 +581,61 @@ medusaIntegrationTestRunner({
})
)
})

it("should update payment collection upon changing shipping option", async () => {
await api.post(
`/store/carts/${cart.id}/shipping-methods`,
{ option_id: shippingOption.id },
storeHeaders
)

await api.post(
`/store/payment-collections`,
{ cart_id: cart.id },
storeHeaders
)

const cartAfterCollection = (
await api.get(`/store/carts/${cart.id}`, storeHeaders)
).data.cart

expect(cartAfterCollection).toEqual(
expect.objectContaining({
id: cart.id,
shipping_methods: expect.arrayContaining([
expect.objectContaining({
shipping_option_id: shippingOption.id,
}),
]),
payment_collection: expect.objectContaining({
amount: 2398,
}),
})
)

let cartAfterExpensiveShipping = (
await api.post(
`/store/carts/${cart.id}/shipping-methods`,
{ option_id: shippingOptionExpensive.id },
storeHeaders
)
).data.cart

expect(cartAfterExpensiveShipping).toEqual(
expect.objectContaining({
id: cartAfterExpensiveShipping.id,
shipping_methods: expect.arrayContaining([
expect.objectContaining({
shipping_option_id: shippingOptionExpensive.id,
amount: 5000,
}),
]),
payment_collection: expect.objectContaining({
amount: 6398,
}),
})
)
})
})

it("should add item to cart with tax lines multiple times", async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
import {
createAdminUser,
adminHeaders,
createAdminUser,
} from "../../../../helpers/create-admin-user"

jest.setTimeout(30000)
Expand Down Expand Up @@ -216,9 +216,7 @@ medusaIntegrationTestRunner({
it("adds products to collection", async () => {
const response = await api.post(
`/admin/collections/${baseCollection.id}/products?fields=*products`,
{
add: [baseProduct.id, baseProduct1.id],
},
{ add: [baseProduct.id, baseProduct1.id] },
adminHeaders
)

Expand All @@ -242,6 +240,54 @@ medusaIntegrationTestRunner({
)
})

it("should not remove products from collection when updating collection", async () => {
const addProductsResponse = await api.post(
`/admin/collections/${baseCollection.id}/products?fields=*products`,
{ add: [baseProduct.id, baseProduct1.id] },
adminHeaders
)

expect(addProductsResponse.status).toEqual(200)
expect(addProductsResponse.data.collection).toEqual(
expect.objectContaining({
id: baseCollection.id,
products: expect.arrayContaining([
expect.objectContaining({
collection_id: baseCollection.id,
title: "test-product",
}),
expect.objectContaining({
collection_id: baseCollection.id,
title: "test-product1",
}),
]),
})
)

const updateCollectionResponse = await api.post(
`/admin/collections/${baseCollection.id}?fields=*products`,
{ title: "test collection update" },
adminHeaders
)

expect(updateCollectionResponse.status).toEqual(200)
expect(updateCollectionResponse.data.collection).toEqual(
expect.objectContaining({
title: "test collection update",
products: expect.arrayContaining([
expect.objectContaining({
collection_id: baseCollection.id,
title: "test-product",
}),
expect.objectContaining({
collection_id: baseCollection.id,
title: "test-product1",
}),
]),
})
)
})

it("removes products from collection", async () => {
await api.post(
`/admin/collections/${baseCollection.id}/products`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,10 @@ medusaIntegrationTestRunner({
},
]),
provider_id: "manual_test-provider",
provider: {
provider: expect.objectContaining({
id: "manual_test-provider",
is_enabled: true,
},
}),
rules: [],
service_zone_id: expect.any(String),
service_zone: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ medusaIntegrationTestRunner({

expect(response.status).toEqual(200)
expect(response.data.stock_location.fulfillment_providers).toEqual([
{ id: "manual_test-provider", is_enabled: true },
expect.objectContaining({
id: "manual_test-provider",
is_enabled: true,
}),
])
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,7 @@ medusaIntegrationTestRunner({
provider_id: provider_id,
data: null,
metadata: null,
shipping_option_type_id: expect.any(String),
type: expect.objectContaining({
id: expect.any(String),
code: shippingOptionData.type.code,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ import { validateAndReturnShippingMethodsDataStep } from "../steps/validate-ship
import { validateCartShippingOptionsPriceStep } from "../steps/validate-shipping-options-price"
import { cartFieldsForRefreshSteps } from "../utils/fields"
import { listShippingOptionsForCartWithPricingWorkflow } from "./list-shipping-options-for-cart-with-pricing"
import { updateCartPromotionsWorkflow } from "./update-cart-promotions"
import { updateTaxLinesWorkflow } from "./update-tax-lines"
import { refreshCartItemsWorkflow } from "./refresh-cart-items"

export interface AddShippingMethodToCartWorkflowInput {
cart_id: string
Expand Down Expand Up @@ -132,9 +131,9 @@ export const addShippingMethodToCartWorkflow = createWorkflow(
}
)

const currentShippingMethods = transform({ cart }, ({ cart }) => {
return cart.shipping_methods.map((sm) => sm.id)
})
const currentShippingMethods = transform({ cart }, ({ cart }) =>
cart.shipping_methods.map((sm) => sm.id)
)

parallelize(
removeShippingMethodFromCartStep({
Expand All @@ -149,16 +148,8 @@ export const addShippingMethodToCartWorkflow = createWorkflow(
})
)

updateTaxLinesWorkflow.runAsStep({
input: {
cart_id: input.cart_id,
},
})

updateCartPromotionsWorkflow.runAsStep({
input: {
cart_id: input.cart_id,
},
refreshCartItemsWorkflow.runAsStep({
input: { cart_id: cart.id },
})
}
)
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,12 @@ export const createOrderFulfillmentWorkflow = createWorkflow(
"items.*",
"items.variant.manage_inventory",
"items.variant.allow_backorder",
"items.variant.product.id",
"items.variant.weight",
"items.variant.length",
"items.variant.height",
"items.variant.width",
"items.variant.material",
"shipping_address.*",
"shipping_methods.shipping_option_id",
"shipping_methods.data",
Expand Down
4 changes: 3 additions & 1 deletion packages/core/types/src/dml/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,9 @@ export interface EntityConstructor<Props> extends Function {
*/
export type InferForeignKeys<Schema extends DMLSchema> = {
[K in keyof Schema as Schema[K] extends { $foreignKey: true }
? `${K & string}_id`
? Schema[K] extends { $foreignKeyName: `${infer FkName}` }
? `${FkName & string}`
: `${K & string}_id`
: never]: Schema[K] extends { $foreignKey: true }
? null extends Schema[K]["$dataType"]
? string | null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export interface UpdateShippingOptionDTO {
/**
* The shipping option type associated with the shipping option.
*/
type:
type?:
| Omit<CreateShippingOptionTypeDTO, "shipping_option_id">
| {
/**
Expand Down
Loading
Loading