fix(core-flows): Fix date usage accross workflows (#10100)

FIXES CMRC-691

**What**
`Date` is something that get executed, since workflows are meant to compose the definition of what will be executed, the date where always having the same value as they was executed once during composition.
Instead wrap those into transformer that will be executed when needed and fix the Date issues
This commit is contained in:
Adrien de Peretti
2024-11-14 14:51:30 +00:00
committed by GitHub
parent af66ac58cc
commit 2e5fd9fd71
4 changed files with 65 additions and 29 deletions
@@ -47,6 +47,18 @@ export type ConfirmClaimRequestWorkflowInput = {
confirmed_by?: string confirmed_by?: string
} }
function getUpdateReturnData({ returnId }: { returnId: string }) {
return transform({ returnId }, ({ returnId }) => {
return [
{
id: returnId,
status: ReturnStatus.REQUESTED,
requested_at: new Date(),
},
]
})
}
/** /**
* This step validates that a requested claim can be confirmed. * This step validates that a requested claim can be confirmed.
*/ */
@@ -306,13 +318,8 @@ export const confirmClaimRequestWorkflow = createWorkflow(
when({ returnId }, ({ returnId }) => { when({ returnId }, ({ returnId }) => {
return !!returnId return !!returnId
}).then(() => { }).then(() => {
updateReturnsStep([ const updateReturnDate = getUpdateReturnData({ returnId })
{ updateReturnsStep(updateReturnDate)
id: returnId,
status: ReturnStatus.REQUESTED,
requested_at: new Date(),
},
])
}) })
const claimId = transform( const claimId = transform(
@@ -203,6 +203,18 @@ function extractShippingOption({ orderPreview, orderExchange, returnId }) {
} }
} }
function getUpdateReturnData({ returnId }: { returnId: string }) {
return transform({ returnId }, ({ returnId }) => {
return [
{
id: returnId,
status: ReturnStatus.REQUESTED,
requested_at: new Date(),
},
]
})
}
export const confirmExchangeRequestWorkflowId = "confirm-exchange-request" export const confirmExchangeRequestWorkflowId = "confirm-exchange-request"
/** /**
* This workflow confirms an exchange request. * This workflow confirms an exchange request.
@@ -294,13 +306,8 @@ export const confirmExchangeRequestWorkflow = createWorkflow(
when({ returnId }, ({ returnId }) => { when({ returnId }, ({ returnId }) => {
return !!returnId return !!returnId
}).then(() => { }).then(() => {
updateReturnsStep([ const updateReturnData = getUpdateReturnData({ returnId })
{ updateReturnsStep(updateReturnData)
id: returnId,
status: ReturnStatus.REQUESTED,
requested_at: new Date(),
},
])
}) })
const exchangeId = transform( const exchangeId = transform(
@@ -8,6 +8,7 @@ import {
WorkflowResponse, WorkflowResponse,
createStep, createStep,
createWorkflow, createWorkflow,
transform,
} from "@medusajs/framework/workflows-sdk" } from "@medusajs/framework/workflows-sdk"
import { useRemoteQueryStep } from "../../../common" import { useRemoteQueryStep } from "../../../common"
import { previewOrderChangeStep } from "../../steps" import { previewOrderChangeStep } from "../../steps"
@@ -23,6 +24,25 @@ export type OrderEditRequestWorkflowInput = {
requested_by?: string requested_by?: string
} }
function getOrderChangesData({
input,
orderChange,
}: {
input: { requested_by?: string }
orderChange: { id: string }
}) {
return transform({ input, orderChange }, ({ input, orderChange }) => {
return [
{
id: orderChange.id,
status: OrderChangeStatus.REQUESTED,
requested_at: new Date(),
requested_by: input.requested_by,
},
]
})
}
/** /**
* This step validates that a order edit can be requested. * This step validates that a order edit can be requested.
*/ */
@@ -74,14 +94,8 @@ export const requestOrderEditRequestWorkflow = createWorkflow(
orderChange, orderChange,
}) })
updateOrderChangesStep([ const updateOrderChangesData = getOrderChangesData({ input, orderChange })
{ updateOrderChangesStep(updateOrderChangesData)
id: orderChange.id,
status: OrderChangeStatus.REQUESTED,
requested_at: new Date(),
requested_by: input.requested_by,
},
])
createOrUpdateOrderPaymentCollectionWorkflow.runAsStep({ createOrUpdateOrderPaymentCollectionWorkflow.runAsStep({
input: { input: {
@@ -158,6 +158,18 @@ function extractReturnShippingOptionId({ orderPreview, orderReturn }) {
return returnShippingMethod.shipping_option_id return returnShippingMethod.shipping_option_id
} }
function getUpdateReturnData({ orderReturn }: { orderReturn: { id: string } }) {
return transform({ orderReturn }, ({ orderReturn }) => {
return [
{
id: orderReturn.id,
status: ReturnStatus.REQUESTED,
requested_at: new Date(),
},
]
})
}
export const confirmReturnRequestWorkflowId = "confirm-return-request" export const confirmReturnRequestWorkflowId = "confirm-return-request"
/** /**
* This workflow confirms a return request. * This workflow confirms a return request.
@@ -277,14 +289,10 @@ export const confirmReturnRequestWorkflow = createWorkflow(
createRemoteLinkStep(link) createRemoteLinkStep(link)
}) })
const updateReturnData = getUpdateReturnData({ orderReturn })
parallelize( parallelize(
updateReturnsStep([ updateReturnsStep(updateReturnData),
{
id: orderReturn.id,
status: ReturnStatus.REQUESTED,
requested_at: new Date(),
},
]),
confirmOrderChanges({ confirmOrderChanges({
changes: [orderChange], changes: [orderChange],
orderId: order.id, orderId: order.id,