chore(medusa,core-flows): cart payment collection link (#8457)
This commit is contained in:
@@ -1,41 +0,0 @@
|
||||
import { ContainerRegistrationKeys, Modules } from "@medusajs/utils"
|
||||
import { createStep, StepResponse } from "@medusajs/workflows-sdk"
|
||||
|
||||
type StepInput = {
|
||||
links: {
|
||||
cart_id: string
|
||||
payment_collection_id: string
|
||||
}[]
|
||||
}
|
||||
|
||||
export const linkCartAndPaymentCollectionsStepId =
|
||||
"link-cart-payment-collection"
|
||||
export const linkCartAndPaymentCollectionsStep = createStep(
|
||||
linkCartAndPaymentCollectionsStepId,
|
||||
async (data: StepInput, { container }) => {
|
||||
const remoteLink = container.resolve(ContainerRegistrationKeys.REMOTE_LINK)
|
||||
|
||||
const links = data.links.map((d) => ({
|
||||
[Modules.CART]: { cart_id: d.cart_id },
|
||||
[Modules.PAYMENT]: { payment_collection_id: d.payment_collection_id },
|
||||
}))
|
||||
|
||||
await remoteLink.create(links)
|
||||
|
||||
return new StepResponse(void 0, data)
|
||||
},
|
||||
async (data, { container }) => {
|
||||
if (!data) {
|
||||
return
|
||||
}
|
||||
|
||||
const remoteLink = container.resolve(ContainerRegistrationKeys.REMOTE_LINK)
|
||||
|
||||
const links = data.links.map((d) => ({
|
||||
[Modules.CART]: { cart_id: d.cart_id },
|
||||
[Modules.PAYMENT]: { payment_collection_id: d.payment_collection_id },
|
||||
}))
|
||||
|
||||
await remoteLink.dismiss(links)
|
||||
}
|
||||
)
|
||||
+6
-8
@@ -1,7 +1,6 @@
|
||||
import {
|
||||
WorkflowData,
|
||||
createWorkflow,
|
||||
parallelize,
|
||||
transform,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import { useRemoteQueryStep } from "../../../common/steps/use-remote-query"
|
||||
@@ -97,12 +96,11 @@ export const addShippingMethodToWorkflow = createWorkflow(
|
||||
shipping_methods: shippingMethodInput,
|
||||
})
|
||||
|
||||
parallelize(
|
||||
refreshCartPromotionsStep({ id: input.cart_id }),
|
||||
updateTaxLinesStep({
|
||||
cart_or_cart_id: input.cart_id,
|
||||
shipping_methods: shippingMethodsToAdd,
|
||||
})
|
||||
)
|
||||
updateTaxLinesStep({
|
||||
cart_or_cart_id: input.cart_id,
|
||||
shipping_methods: shippingMethodsToAdd,
|
||||
})
|
||||
|
||||
refreshCartPromotionsStep({ id: input.cart_id })
|
||||
}
|
||||
)
|
||||
|
||||
@@ -25,7 +25,7 @@ import {
|
||||
} from "../utils/fields"
|
||||
import { prepareLineItemData } from "../utils/prepare-line-item-data"
|
||||
import { confirmVariantInventoryWorkflow } from "./confirm-variant-inventory"
|
||||
import { refreshPaymentCollectionForCartStep } from "./refresh-payment-collection"
|
||||
import { refreshPaymentCollectionForCartWorkflow } from "./refresh-payment-collection"
|
||||
|
||||
export const addToCartWorkflowId = "add-to-cart"
|
||||
export const addToCartWorkflow = createWorkflow(
|
||||
@@ -114,11 +114,17 @@ export const addToCartWorkflow = createWorkflow(
|
||||
|
||||
parallelize(
|
||||
refreshCartShippingMethodsStep({ cart }),
|
||||
updateTaxLinesStep({ cart_or_cart_id: input.cart.id, items }),
|
||||
refreshCartPromotionsStep({ id: input.cart.id }),
|
||||
refreshPaymentCollectionForCartStep({ cart_id: input.cart.id })
|
||||
updateTaxLinesStep({ cart_or_cart_id: input.cart.id, items })
|
||||
)
|
||||
|
||||
refreshCartPromotionsStep({ id: input.cart.id })
|
||||
|
||||
refreshPaymentCollectionForCartWorkflow.runAsStep({
|
||||
input: {
|
||||
cart_id: input.cart.id,
|
||||
},
|
||||
})
|
||||
|
||||
return new WorkflowResponse(items)
|
||||
}
|
||||
)
|
||||
|
||||
@@ -21,7 +21,7 @@ import { validateVariantPricesStep } from "../steps/validate-variant-prices"
|
||||
import { productVariantsFields } from "../utils/fields"
|
||||
import { prepareLineItemData } from "../utils/prepare-line-item-data"
|
||||
import { confirmVariantInventoryWorkflow } from "./confirm-variant-inventory"
|
||||
import { refreshPaymentCollectionForCartStep } from "./refresh-payment-collection"
|
||||
import { refreshPaymentCollectionForCartWorkflow } from "./refresh-payment-collection"
|
||||
|
||||
// TODO: The createCartWorkflow are missing the following steps:
|
||||
// - Refresh/delete shipping methods (fulfillment module)
|
||||
@@ -145,16 +145,18 @@ export const createCartWorkflow = createWorkflow(
|
||||
const carts = createCartsStep([cartToCreate])
|
||||
const cart = transform({ carts }, (data) => data.carts?.[0])
|
||||
|
||||
parallelize(
|
||||
refreshCartPromotionsStep({
|
||||
id: cart.id,
|
||||
promo_codes: input.promo_codes,
|
||||
}),
|
||||
updateTaxLinesStep({ cart_or_cart_id: cart.id }),
|
||||
refreshPaymentCollectionForCartStep({
|
||||
updateTaxLinesStep({ cart_or_cart_id: cart.id })
|
||||
|
||||
refreshCartPromotionsStep({
|
||||
id: cart.id,
|
||||
promo_codes: input.promo_codes,
|
||||
})
|
||||
|
||||
refreshPaymentCollectionForCartWorkflow.runAsStep({
|
||||
input: {
|
||||
cart_id: cart.id,
|
||||
})
|
||||
)
|
||||
},
|
||||
})
|
||||
|
||||
return new WorkflowResponse(cart)
|
||||
}
|
||||
|
||||
+58
-12
@@ -1,11 +1,26 @@
|
||||
import { CreatePaymentCollectionForCartWorkflowInputDTO } from "@medusajs/types"
|
||||
import {
|
||||
CartDTO,
|
||||
CreatePaymentCollectionForCartWorkflowInputDTO,
|
||||
} from "@medusajs/types"
|
||||
import { Modules } from "@medusajs/utils"
|
||||
import {
|
||||
WorkflowData,
|
||||
createStep,
|
||||
createWorkflow,
|
||||
transform,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import { createRemoteLinkStep } from "../../../common/steps/create-remote-links"
|
||||
import { useRemoteQueryStep } from "../../../common/steps/use-remote-query"
|
||||
import { createPaymentCollectionsStep } from "../steps/create-payment-collection"
|
||||
import { linkCartAndPaymentCollectionsStep } from "../steps/link-cart-payment-collection"
|
||||
|
||||
const validateExistingPaymentCollection = createStep(
|
||||
"validate-existing-payment-collection",
|
||||
({ cart }: { cart: CartDTO & { payment_collection?: any } }) => {
|
||||
if (cart.payment_collection) {
|
||||
throw new Error(`Cart ${cart.id} already has a payment collection`)
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
export const createPaymentCollectionForCartWorkflowId =
|
||||
"create-payment-collection-for-cart"
|
||||
@@ -14,17 +29,48 @@ export const createPaymentCollectionForCartWorkflow = createWorkflow(
|
||||
(
|
||||
input: WorkflowData<CreatePaymentCollectionForCartWorkflowInputDTO>
|
||||
): WorkflowData<void> => {
|
||||
const created = createPaymentCollectionsStep([input])
|
||||
|
||||
const link = transform({ cartId: input.cart_id, created }, (data) => ({
|
||||
links: [
|
||||
{
|
||||
cart_id: data.cartId,
|
||||
payment_collection_id: data.created[0].id,
|
||||
},
|
||||
const cart = useRemoteQueryStep({
|
||||
entry_point: "cart",
|
||||
fields: [
|
||||
"id",
|
||||
"region_id",
|
||||
"currency_code",
|
||||
"total",
|
||||
"raw_total",
|
||||
"payment_collection.id",
|
||||
],
|
||||
}))
|
||||
variables: { id: input.cart_id },
|
||||
throw_if_key_not_found: true,
|
||||
list: false,
|
||||
})
|
||||
|
||||
linkCartAndPaymentCollectionsStep(link)
|
||||
validateExistingPaymentCollection({ cart })
|
||||
|
||||
const paymentData = transform({ cart }, ({ cart }) => {
|
||||
return {
|
||||
cart_id: cart.id,
|
||||
currency_code: cart.currency_code,
|
||||
amount: cart.raw_total,
|
||||
region_id: cart.region_id,
|
||||
}
|
||||
})
|
||||
|
||||
const created = createPaymentCollectionsStep([paymentData])
|
||||
|
||||
const cartPaymentLink = transform(
|
||||
{ cartId: input.cart_id, created },
|
||||
(data) => {
|
||||
return [
|
||||
{
|
||||
[Modules.CART]: { cart_id: data.cartId },
|
||||
[Modules.PAYMENT]: { payment_collection_id: data.created[0].id },
|
||||
},
|
||||
]
|
||||
}
|
||||
)
|
||||
|
||||
createRemoteLinkStep(cartPaymentLink).config({
|
||||
name: "cart-payment-collection-link",
|
||||
})
|
||||
}
|
||||
)
|
||||
|
||||
+50
-52
@@ -1,11 +1,10 @@
|
||||
import { isPresent } from "@medusajs/utils"
|
||||
import { MathBN, isPresent } from "@medusajs/utils"
|
||||
import {
|
||||
StepResponse,
|
||||
WorkflowData,
|
||||
createStep,
|
||||
createWorkflow,
|
||||
parallelize,
|
||||
transform,
|
||||
when,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import { useRemoteQueryStep } from "../../../common/steps/use-remote-query"
|
||||
import { updatePaymentCollectionStep } from "../../../payment-collection"
|
||||
@@ -15,26 +14,6 @@ type WorklowInput = {
|
||||
cart_id: string
|
||||
}
|
||||
|
||||
interface StepInput {
|
||||
cart_id: string
|
||||
}
|
||||
|
||||
// We export a step running the workflow too, so that we can use it as a subworkflow e.g. in the update cart workflows
|
||||
export const refreshPaymentCollectionForCartStepId =
|
||||
"refresh-payment-collection-for-cart"
|
||||
export const refreshPaymentCollectionForCartStep = createStep(
|
||||
refreshPaymentCollectionForCartStepId,
|
||||
async (data: StepInput, { container }) => {
|
||||
await refreshPaymentCollectionForCartWorkflow(container).run({
|
||||
input: {
|
||||
cart_id: data.cart_id,
|
||||
},
|
||||
})
|
||||
|
||||
return new StepResponse(null)
|
||||
}
|
||||
)
|
||||
|
||||
export const refreshPaymentCollectionForCartWorkflowId =
|
||||
"refresh-payment-collection-for-cart"
|
||||
export const refreshPaymentCollectionForCartWorkflow = createWorkflow(
|
||||
@@ -44,9 +23,14 @@ export const refreshPaymentCollectionForCartWorkflow = createWorkflow(
|
||||
entry_point: "cart",
|
||||
fields: [
|
||||
"id",
|
||||
"total",
|
||||
"region_id",
|
||||
"currency_code",
|
||||
"total",
|
||||
"raw_total",
|
||||
"payment_collection.id",
|
||||
"payment_collection.raw_amount",
|
||||
"payment_collection.amount",
|
||||
"payment_collection.currency_code",
|
||||
"payment_collection.payment_sessions.id",
|
||||
],
|
||||
variables: { id: input.cart_id },
|
||||
@@ -54,37 +38,51 @@ export const refreshPaymentCollectionForCartWorkflow = createWorkflow(
|
||||
list: false,
|
||||
})
|
||||
|
||||
const deletePaymentSessionInput = transform(
|
||||
{ paymentCollection: cart.payment_collection },
|
||||
(data) => {
|
||||
return {
|
||||
ids:
|
||||
data.paymentCollection?.payment_sessions
|
||||
?.map((ps) => ps.id)
|
||||
?.flat(1) || [],
|
||||
when({ cart }, ({ cart }) => {
|
||||
const valueIsEqual = MathBN.eq(
|
||||
cart.payment_collection?.raw_amount ?? -1,
|
||||
cart.raw_total
|
||||
)
|
||||
|
||||
if (valueIsEqual) {
|
||||
return cart.payment_collection.currency_code !== cart.currency_code
|
||||
}
|
||||
|
||||
return true
|
||||
}).then(() => {
|
||||
const deletePaymentSessionInput = transform(
|
||||
{ paymentCollection: cart.payment_collection },
|
||||
(data) => {
|
||||
return {
|
||||
ids:
|
||||
data.paymentCollection?.payment_sessions
|
||||
?.map((ps) => ps.id)
|
||||
?.flat(1) || [],
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
)
|
||||
|
||||
const updatePaymentCollectionInput = transform({ cart }, (data) => {
|
||||
if (!isPresent(data.cart?.payment_collection?.id)) {
|
||||
return
|
||||
}
|
||||
const updatePaymentCollectionInput = transform({ cart }, ({ cart }) => {
|
||||
if (!isPresent(cart.payment_collection?.id)) {
|
||||
return
|
||||
}
|
||||
|
||||
return {
|
||||
selector: { id: data.cart.payment_collection.id },
|
||||
update: {
|
||||
amount: data.cart.total,
|
||||
currency_code: data.cart.currency_code,
|
||||
},
|
||||
}
|
||||
return {
|
||||
selector: { id: cart.payment_collection.id },
|
||||
update: {
|
||||
amount: cart.total,
|
||||
currency_code: cart.currency_code,
|
||||
region_id: cart.region_id,
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
parallelize(
|
||||
deletePaymentSessionsWorkflow.runAsStep({
|
||||
input: deletePaymentSessionInput,
|
||||
}),
|
||||
updatePaymentCollectionStep(updatePaymentCollectionInput)
|
||||
)
|
||||
})
|
||||
|
||||
parallelize(
|
||||
deletePaymentSessionsWorkflow.runAsStep({
|
||||
input: deletePaymentSessionInput,
|
||||
}),
|
||||
updatePaymentCollectionStep(updatePaymentCollectionInput)
|
||||
)
|
||||
}
|
||||
)
|
||||
|
||||
@@ -17,7 +17,7 @@ import {
|
||||
import { refreshCartPromotionsStep } from "../steps/refresh-cart-promotions"
|
||||
import { updateTaxLinesStep } from "../steps/update-tax-lines"
|
||||
import { cartFieldsForRefreshSteps } from "../utils/fields"
|
||||
import { refreshPaymentCollectionForCartStep } from "./refresh-payment-collection"
|
||||
import { refreshPaymentCollectionForCartWorkflow } from "./refresh-payment-collection"
|
||||
|
||||
export const updateCartWorkflowId = "update-cart"
|
||||
export const updateCartWorkflow = createWorkflow(
|
||||
@@ -82,15 +82,19 @@ export const updateCartWorkflow = createWorkflow(
|
||||
|
||||
parallelize(
|
||||
refreshCartShippingMethodsStep({ cart }),
|
||||
updateTaxLinesStep({ cart_or_cart_id: carts[0].id }),
|
||||
refreshCartPromotionsStep({
|
||||
id: input.id,
|
||||
promo_codes: input.promo_codes,
|
||||
action: PromotionActions.REPLACE,
|
||||
}),
|
||||
refreshPaymentCollectionForCartStep({
|
||||
cart_id: input.id,
|
||||
})
|
||||
updateTaxLinesStep({ cart_or_cart_id: carts[0].id })
|
||||
)
|
||||
|
||||
refreshCartPromotionsStep({
|
||||
id: input.id,
|
||||
promo_codes: input.promo_codes,
|
||||
action: PromotionActions.REPLACE,
|
||||
})
|
||||
|
||||
refreshPaymentCollectionForCartWorkflow.runAsStep({
|
||||
input: {
|
||||
cart_id: input.id,
|
||||
},
|
||||
})
|
||||
}
|
||||
)
|
||||
|
||||
@@ -3,7 +3,6 @@ import {
|
||||
WorkflowData,
|
||||
WorkflowResponse,
|
||||
createWorkflow,
|
||||
parallelize,
|
||||
transform,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import { useRemoteQueryStep } from "../../../common/steps/use-remote-query"
|
||||
@@ -16,7 +15,7 @@ import {
|
||||
productVariantsFields,
|
||||
} from "../utils/fields"
|
||||
import { confirmVariantInventoryWorkflow } from "./confirm-variant-inventory"
|
||||
import { refreshPaymentCollectionForCartStep } from "./refresh-payment-collection"
|
||||
import { refreshPaymentCollectionForCartWorkflow } from "./refresh-payment-collection"
|
||||
|
||||
// TODO: The UpdateLineItemsWorkflow are missing the following steps:
|
||||
// - Validate shipping methods for new items (fulfillment module)
|
||||
@@ -90,11 +89,13 @@ export const updateLineItemInCartWorkflow = createWorkflow(
|
||||
list: false,
|
||||
}).config({ name: "refetch–cart" })
|
||||
|
||||
parallelize(
|
||||
refreshCartShippingMethodsStep({ cart }),
|
||||
refreshCartPromotionsStep({ id: input.cart.id }),
|
||||
refreshPaymentCollectionForCartStep({ cart_id: input.cart.id })
|
||||
)
|
||||
refreshCartShippingMethodsStep({ cart })
|
||||
|
||||
refreshCartPromotionsStep({ id: input.cart.id })
|
||||
|
||||
refreshPaymentCollectionForCartWorkflow.runAsStep({
|
||||
input: { cart_id: input.cart.id },
|
||||
})
|
||||
|
||||
const updatedItem = transform({ result }, (data) => data.result?.[0])
|
||||
|
||||
|
||||
Reference in New Issue
Block a user