chore: workflow internals improvementss (#9455)
This commit is contained in:
committed by
GitHub
parent
1b9379be62
commit
34d57870ad
@@ -1,8 +1,9 @@
|
||||
import { MedusaError } from "@medusajs/framework/utils"
|
||||
import {
|
||||
WorkflowData,
|
||||
createWorkflow,
|
||||
parallelize,
|
||||
transform,
|
||||
WorkflowData,
|
||||
} from "@medusajs/framework/workflows-sdk"
|
||||
import { useRemoteQueryStep } from "../../common/steps/use-remote-query"
|
||||
import {
|
||||
@@ -102,13 +103,14 @@ export const addShippingMethodToWorkflow = createWorkflow(
|
||||
return cart.shipping_methods.map((sm) => sm.id)
|
||||
})
|
||||
|
||||
removeShippingMethodFromCartStep({
|
||||
shipping_method_ids: currentShippingMethods,
|
||||
})
|
||||
|
||||
const shippingMethodsToAdd = addShippingMethodToCartStep({
|
||||
shipping_methods: shippingMethodInput,
|
||||
})
|
||||
const [, shippingMethodsToAdd] = parallelize(
|
||||
removeShippingMethodFromCartStep({
|
||||
shipping_method_ids: currentShippingMethods,
|
||||
}),
|
||||
addShippingMethodToCartStep({
|
||||
shipping_methods: shippingMethodInput,
|
||||
})
|
||||
)
|
||||
|
||||
updateTaxLinesWorkflow.runAsStep({
|
||||
input: {
|
||||
|
||||
@@ -49,7 +49,6 @@ export const completeCartWorkflow = createWorkflow(
|
||||
name: completeCartWorkflowId,
|
||||
store: true,
|
||||
idempotent: true,
|
||||
// 3 days of retention time
|
||||
retentionTime: THREE_DAYS,
|
||||
},
|
||||
(
|
||||
|
||||
@@ -117,7 +117,10 @@ export const createCartWorkflow = createWorkflow(
|
||||
}
|
||||
|
||||
// If there is only one country in the region, we prepare a shipping address with that country's code.
|
||||
if (!data.input.shipping_address && data.region.countries.length === 1) {
|
||||
if (
|
||||
!data.input.shipping_address &&
|
||||
data.region.countries.length === 1
|
||||
) {
|
||||
data_.shipping_address = {
|
||||
country_code: data.region.countries[0].iso_2,
|
||||
}
|
||||
|
||||
@@ -4,10 +4,11 @@ import {
|
||||
} from "@medusajs/framework/types"
|
||||
import { Modules } from "@medusajs/framework/utils"
|
||||
import {
|
||||
WorkflowData,
|
||||
createStep,
|
||||
createWorkflow,
|
||||
parallelize,
|
||||
transform,
|
||||
WorkflowData,
|
||||
} from "@medusajs/framework/workflows-sdk"
|
||||
import { createRemoteLinkStep } from "../../common/steps/create-remote-links"
|
||||
import { useRemoteQueryStep } from "../../common/steps/use-remote-query"
|
||||
@@ -52,9 +53,10 @@ export const createPaymentCollectionForCartWorkflow = createWorkflow(
|
||||
list: false,
|
||||
})
|
||||
|
||||
validateCartStep({ cart })
|
||||
|
||||
validateExistingPaymentCollectionStep({ cart })
|
||||
parallelize(
|
||||
validateCartStep({ cart }),
|
||||
validateExistingPaymentCollectionStep({ cart })
|
||||
)
|
||||
|
||||
const paymentData = transform({ cart }, ({ cart }) => {
|
||||
return {
|
||||
|
||||
@@ -4,13 +4,13 @@ import {
|
||||
} from "@medusajs/framework/types"
|
||||
import { MedusaError, PromotionActions } from "@medusajs/framework/utils"
|
||||
import {
|
||||
WorkflowData,
|
||||
WorkflowResponse,
|
||||
createHook,
|
||||
createWorkflow,
|
||||
parallelize,
|
||||
transform,
|
||||
when,
|
||||
WorkflowData,
|
||||
WorkflowResponse,
|
||||
} from "@medusajs/framework/workflows-sdk"
|
||||
import { useRemoteQueryStep } from "../../common"
|
||||
import {
|
||||
@@ -138,14 +138,13 @@ export const updateCartWorkflow = createWorkflow(
|
||||
list: false,
|
||||
}).config({ name: "refetch–cart" })
|
||||
|
||||
parallelize(
|
||||
refreshCartShippingMethodsStep({ cart }),
|
||||
updateTaxLinesWorkflow.runAsStep({
|
||||
input: {
|
||||
cart_id: carts[0].id,
|
||||
},
|
||||
})
|
||||
)
|
||||
refreshCartShippingMethodsStep({ cart })
|
||||
|
||||
updateTaxLinesWorkflow.runAsStep({
|
||||
input: {
|
||||
cart_id: carts[0].id,
|
||||
},
|
||||
})
|
||||
|
||||
updateCartPromotionsWorkflow.runAsStep({
|
||||
input: {
|
||||
|
||||
@@ -3,8 +3,12 @@ import {
|
||||
Logger,
|
||||
PaymentSessionDTO,
|
||||
} from "@medusajs/framework/types"
|
||||
import { ContainerRegistrationKeys, Modules } from "@medusajs/framework/utils"
|
||||
import { StepResponse, createStep } from "@medusajs/framework/workflows-sdk"
|
||||
import {
|
||||
ContainerRegistrationKeys,
|
||||
Modules,
|
||||
promiseAll,
|
||||
} from "@medusajs/framework/utils"
|
||||
import { createStep, StepResponse } from "@medusajs/framework/workflows-sdk"
|
||||
|
||||
export interface DeletePaymentSessionStepInput {
|
||||
ids: string[]
|
||||
@@ -29,32 +33,42 @@ export const deletePaymentSessionsStep = createStep(
|
||||
return new StepResponse([], null)
|
||||
}
|
||||
|
||||
for (const id of ids) {
|
||||
const select = [
|
||||
"provider_id",
|
||||
"currency_code",
|
||||
"amount",
|
||||
"data",
|
||||
"context",
|
||||
"payment_collection.id",
|
||||
]
|
||||
const select = [
|
||||
"provider_id",
|
||||
"currency_code",
|
||||
"amount",
|
||||
"data",
|
||||
"context",
|
||||
"payment_collection.id",
|
||||
]
|
||||
|
||||
const [session] = await service.listPaymentSessions({ id }, { select })
|
||||
const sessions = await service.listPaymentSessions({ id: ids }, { select })
|
||||
const sessionMap = new Map(sessions.map((s) => [s.id, s]))
|
||||
|
||||
const promises: Promise<void>[] = []
|
||||
|
||||
for (const id of ids) {
|
||||
const session = sessionMap.get(id)!
|
||||
|
||||
// As this requires an external method call, we will try to delete as many successful calls
|
||||
// as possible and pass them over to the compensation step to be recreated if any of the
|
||||
// payment sessions fails to delete.
|
||||
try {
|
||||
await service.deletePaymentSession(id)
|
||||
const promise = service
|
||||
.deletePaymentSession(id)
|
||||
.then((res) => {
|
||||
deleted.push(session)
|
||||
})
|
||||
.catch((e) => {
|
||||
logger.error(
|
||||
`Encountered an error when trying to delete payment session - ${id} - ${e}`
|
||||
)
|
||||
})
|
||||
|
||||
deleted.push(session)
|
||||
} catch (e) {
|
||||
logger.error(
|
||||
`Encountered an error when trying to delete payment session - ${id} - ${e}`
|
||||
)
|
||||
}
|
||||
promises.push(promise)
|
||||
}
|
||||
|
||||
await promiseAll(promises)
|
||||
|
||||
return new StepResponse(
|
||||
deleted.map((d) => d.id),
|
||||
deleted
|
||||
|
||||
Reference in New Issue
Block a user