Skip to content

Commit

Permalink
fix(core-flows): Fix date usage accross workflows (#10100)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
adrien2p authored Nov 14, 2024
1 parent af66ac5 commit 2e5fd9f
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ export type ConfirmClaimRequestWorkflowInput = {
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.
*/
Expand Down Expand Up @@ -306,13 +318,8 @@ export const confirmClaimRequestWorkflow = createWorkflow(
when({ returnId }, ({ returnId }) => {
return !!returnId
}).then(() => {
updateReturnsStep([
{
id: returnId,
status: ReturnStatus.REQUESTED,
requested_at: new Date(),
},
])
const updateReturnDate = getUpdateReturnData({ returnId })
updateReturnsStep(updateReturnDate)
})

const claimId = transform(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
/**
* This workflow confirms an exchange request.
Expand Down Expand Up @@ -294,13 +306,8 @@ export const confirmExchangeRequestWorkflow = createWorkflow(
when({ returnId }, ({ returnId }) => {
return !!returnId
}).then(() => {
updateReturnsStep([
{
id: returnId,
status: ReturnStatus.REQUESTED,
requested_at: new Date(),
},
])
const updateReturnData = getUpdateReturnData({ returnId })
updateReturnsStep(updateReturnData)
})

const exchangeId = transform(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
WorkflowResponse,
createStep,
createWorkflow,
transform,
} from "@medusajs/framework/workflows-sdk"
import { useRemoteQueryStep } from "../../../common"
import { previewOrderChangeStep } from "../../steps"
Expand All @@ -23,6 +24,25 @@ export type OrderEditRequestWorkflowInput = {
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.
*/
Expand Down Expand Up @@ -74,14 +94,8 @@ export const requestOrderEditRequestWorkflow = createWorkflow(
orderChange,
})

updateOrderChangesStep([
{
id: orderChange.id,
status: OrderChangeStatus.REQUESTED,
requested_at: new Date(),
requested_by: input.requested_by,
},
])
const updateOrderChangesData = getOrderChangesData({ input, orderChange })
updateOrderChangesStep(updateOrderChangesData)

createOrUpdateOrderPaymentCollectionWorkflow.runAsStep({
input: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,18 @@ function extractReturnShippingOptionId({ orderPreview, orderReturn }) {
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"
/**
* This workflow confirms a return request.
Expand Down Expand Up @@ -277,14 +289,10 @@ export const confirmReturnRequestWorkflow = createWorkflow(
createRemoteLinkStep(link)
})

const updateReturnData = getUpdateReturnData({ orderReturn })

parallelize(
updateReturnsStep([
{
id: orderReturn.id,
status: ReturnStatus.REQUESTED,
requested_at: new Date(),
},
]),
updateReturnsStep(updateReturnData),
confirmOrderChanges({
changes: [orderChange],
orderId: order.id,
Expand Down

0 comments on commit 2e5fd9f

Please sign in to comment.