feat(medusa,pricing): Cart pricing context with customer group (#10579)

* fix(carts): Fixes cart modifications not accounting for certain price lists (#10493)

*What*

* Fixes #10490
* Expands any available customer_id into its customer_group_ids for cart
  updates that add line items.

*Why*

* Cart updates from the storefront were overriding any valid price lists
  that were correctly being shown in the storefront's product pages.

*How*

* Adds a new workflow step that expands an optional customer_id into the
  customer_group_ids it belongs to.
* Uses this step in the addToCartWorkflow and
  updateLineItemInCartWorkflow workflows.

*Testing*
* Using medusa-dev to test on a local backend.
* Adds integration tests for the addToCart and updateLineItemInCart
  workflows.

Co-authored-by: Riqwan Thamir <rmthamir@gmail.com>

* chore: update cart workflows to accept new pricing context

* chore: add transfer specs

* chore: fix specs

* chore: modify types + specs

* chore: add data migration + dashboard changes

* chore: fix update line item workflow

* chore: add changeset + unskip spec

---------

Co-authored-by: Sergio Campamá <sergiocampama@gmail.com>
This commit is contained in:
Riqwan Thamir
2024-12-17 11:10:30 +01:00
committed by GitHub
co-authored by Sergio Campamá
parent 0c49470066
commit 6367bccde8
29 changed files with 1090 additions and 166 deletions
@@ -2,10 +2,10 @@ import {
deleteLineItemsWorkflow,
updateLineItemInCartWorkflow,
} from "@medusajs/core-flows"
import { prepareListQuery } from "@medusajs/framework"
import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
import { HttpTypes } from "@medusajs/framework/types"
import { MedusaError } from "@medusajs/framework/utils"
import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
import { prepareListQuery } from "@medusajs/framework"
import { refetchCart } from "../../../helpers"
import { StoreUpdateCartLineItemType } from "../../../validators"
@@ -13,6 +13,7 @@ export const POST = async (
req: MedusaRequest<StoreUpdateCartLineItemType>,
res: MedusaResponse<HttpTypes.StoreCartResponse>
) => {
// TODO: Move this to the workflow when the query to line item is fixed
const cart = await refetchCart(
req.params.id,
req.scope,
@@ -39,14 +40,12 @@ export const POST = async (
)
}
const input = {
cart,
item,
update: req.validatedBody,
}
await updateLineItemInCartWorkflow(req.scope).run({
input,
input: {
cart_id: req.params.id,
item_id: item.id,
update: req.validatedBody,
},
})
const updatedCart = await refetchCart(
@@ -1,33 +1,25 @@
import { addToCartWorkflow } from "@medusajs/core-flows"
import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
import { HttpTypes } from "@medusajs/framework/types"
import { refetchCart } from "../../helpers"
import { StoreAddCartLineItemType } from "../../validators"
import { HttpTypes } from "@medusajs/framework/types"
export const POST = async (
req: MedusaRequest<StoreAddCartLineItemType>,
res: MedusaResponse<HttpTypes.StoreCartResponse>
) => {
await addToCartWorkflow(req.scope).run({
input: {
cart_id: req.params.id,
items: [req.validatedBody],
},
})
const cart = await refetchCart(
req.params.id,
req.scope,
req.remoteQueryConfig.fields
)
const workflowInput = {
items: [req.validatedBody],
cart,
}
await addToCartWorkflow(req.scope).run({
input: workflowInput,
} as any)
const updatedCart = await refetchCart(
req.params.id,
req.scope,
req.remoteQueryConfig.fields
)
res.status(200).json({ cart: updatedCart })
res.status(200).json({ cart })
}
@@ -49,7 +49,10 @@ export function setPricingContext() {
["id"]
)
pricingContext.customer_group_id = customerGroups.map((cg) => cg.id)
pricingContext.customer = { groups: [] }
customerGroups.map((cg) =>
pricingContext.customer?.groups?.push({ id: cg.id })
)
}
req.pricingContext = pricingContext