fix: Update tax lines on cart when region changes (#9367)

**What**
- Removes broken cart retrieval step `retrieveWithCartLinks` in favor of `useRemoteQueryStep` in `updateTaxLinesWorkflows`

**Why**
- Filters variables in the step were passed with an incorrect shape
  - I removed the step, since it's only used once and the behavior is identical to the generic remote query step

**Note**
- Because the filters were passed incorrectly, the, now-removed, step has always returned the first cart in the database. As a result all tax calculations so far have been based on whatever the shape of that cart has. It basically ignores all input to this workflow
This commit is contained in:
Oli Juhl
2024-09-30 11:55:54 +00:00
committed by GitHub
parent 852df3f764
commit 8851ae7b51
9 changed files with 151 additions and 63 deletions
@@ -16,12 +16,11 @@ export * from "./get-variant-price-sets"
export * from "./get-variants"
export * from "./prepare-adjustments-from-promotion-actions"
export * from "./refresh-cart-shipping-methods"
export * from "./reserve-inventory"
export * from "./remove-line-item-adjustments"
export * from "./remove-shipping-method-adjustments"
export * from "./remove-shipping-method-from-cart"
export * from "./reserve-inventory"
export * from "./retrieve-cart"
export * from "./retrieve-cart-with-links"
export * from "./set-tax-lines-for-items"
export * from "./update-cart-promotions"
export * from "./update-carts"
@@ -29,3 +28,4 @@ export * from "./update-line-items"
export * from "./validate-cart-payments"
export * from "./validate-cart-shipping-options"
export * from "./validate-variant-prices"
@@ -1,41 +0,0 @@
import { CartWorkflowDTO } from "@medusajs/framework/types"
import { ContainerRegistrationKeys, isObject } from "@medusajs/framework/utils"
import { createStep, StepResponse } from "@medusajs/framework/workflows-sdk"
export interface RetrieveCartWithLinksStepInput {
cart_or_cart_id: string | CartWorkflowDTO
fields: string[]
}
export const retrieveCartWithLinksStepId = "retrieve-cart-with-links"
/**
* This step retrieves a cart's details with its linked records.
*/
export const retrieveCartWithLinksStep = createStep(
retrieveCartWithLinksStepId,
async (data: RetrieveCartWithLinksStepInput, { container }) => {
const { cart_or_cart_id: cartOrCartId, fields } = data
if (isObject(cartOrCartId)) {
return new StepResponse(cartOrCartId)
}
const id = cartOrCartId
const remoteQuery = container.resolve(
ContainerRegistrationKeys.REMOTE_QUERY
)
const query = {
entryPoint: "cart",
fields,
variables: {
cart: {
id,
},
},
}
const [cart] = await remoteQuery(query)
return new StepResponse(cart)
}
)
@@ -112,7 +112,7 @@ export const addShippingMethodToWorkflow = createWorkflow(
updateTaxLinesWorkflow.runAsStep({
input: {
cart_or_cart_id: input.cart_id,
cart_id: input.cart_id,
shipping_methods: shippingMethodsToAdd,
},
})
@@ -124,7 +124,7 @@ export const addToCartWorkflow = createWorkflow(
refreshCartShippingMethodsStep({ cart }),
updateTaxLinesWorkflow.runAsStep({
input: {
cart_or_cart_id: input.cart.id,
cart_id: input.cart.id,
items,
},
})
@@ -155,7 +155,7 @@ export const createCartWorkflow = createWorkflow(
updateTaxLinesWorkflow.runAsStep({
input: {
cart_or_cart_id: cart.id,
cart_id: cart.id,
},
})
@@ -96,7 +96,7 @@ export const updateCartWorkflow = createWorkflow(
refreshCartShippingMethodsStep({ cart }),
updateTaxLinesWorkflow.runAsStep({
input: {
cart_or_cart_id: carts[0].id,
cart_id: carts[0].id,
},
})
)
@@ -1,18 +1,14 @@
import {
CartLineItemDTO,
CartShippingMethodDTO,
CartWorkflowDTO,
} from "@medusajs/framework/types"
import {
WorkflowData,
createWorkflow,
transform,
} from "@medusajs/framework/workflows-sdk"
import {
getItemTaxLinesStep,
retrieveCartWithLinksStep,
setTaxLinesForItemsStep,
} from "../steps"
import { useRemoteQueryStep } from "../../common"
import { getItemTaxLinesStep, setTaxLinesForItemsStep } from "../steps"
const cartFields = [
"id",
@@ -61,7 +57,7 @@ const cartFields = [
]
export type UpdateTaxLinesWorkflowInput = {
cart_or_cart_id: string | CartWorkflowDTO
cart_id: string
items?: CartLineItemDTO[]
shipping_methods?: CartShippingMethodDTO[]
force_tax_calculation?: boolean
@@ -74,17 +70,20 @@ export const updateTaxLinesWorkflowId = "update-tax-lines"
export const updateTaxLinesWorkflow = createWorkflow(
updateTaxLinesWorkflowId,
(input: WorkflowData<UpdateTaxLinesWorkflowInput>): WorkflowData<void> => {
const cart = retrieveCartWithLinksStep({
cart_or_cart_id: input.cart_or_cart_id,
const cart = useRemoteQueryStep({
entry_point: "cart",
fields: cartFields,
variables: {
id: input.cart_id,
},
list: false,
})
const taxLineItems = getItemTaxLinesStep(
transform({ input, cart }, (data) => ({
cart: data.cart,
items: data.input.items || data.cart.items,
shipping_methods:
data.input.shipping_methods || data.cart.shipping_methods,
shipping_methods: data.input.shipping_methods || data.cart.shipping_methods,
force_tax_calculation: data.input.force_tax_calculation,
}))
)