From 2e5fd9fd71602749e76e054a548fdccd0fa22c3a Mon Sep 17 00:00:00 2001 From: Adrien de Peretti Date: Thu, 14 Nov 2024 15:51:30 +0100 Subject: [PATCH] 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 --- .../workflows/claim/confirm-claim-request.ts | 21 ++++++++----- .../exchange/confirm-exchange-request.ts | 21 ++++++++----- .../order-edit/request-order-edit.ts | 30 ++++++++++++++----- .../return/confirm-return-request.ts | 22 +++++++++----- 4 files changed, 65 insertions(+), 29 deletions(-) diff --git a/packages/core/core-flows/src/order/workflows/claim/confirm-claim-request.ts b/packages/core/core-flows/src/order/workflows/claim/confirm-claim-request.ts index 7b7aa1e0a3..6f8a299978 100644 --- a/packages/core/core-flows/src/order/workflows/claim/confirm-claim-request.ts +++ b/packages/core/core-flows/src/order/workflows/claim/confirm-claim-request.ts @@ -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. */ @@ -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( diff --git a/packages/core/core-flows/src/order/workflows/exchange/confirm-exchange-request.ts b/packages/core/core-flows/src/order/workflows/exchange/confirm-exchange-request.ts index ff2279fa03..78fecca7fc 100644 --- a/packages/core/core-flows/src/order/workflows/exchange/confirm-exchange-request.ts +++ b/packages/core/core-flows/src/order/workflows/exchange/confirm-exchange-request.ts @@ -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. @@ -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( diff --git a/packages/core/core-flows/src/order/workflows/order-edit/request-order-edit.ts b/packages/core/core-flows/src/order/workflows/order-edit/request-order-edit.ts index d66a924dc8..312c816615 100644 --- a/packages/core/core-flows/src/order/workflows/order-edit/request-order-edit.ts +++ b/packages/core/core-flows/src/order/workflows/order-edit/request-order-edit.ts @@ -8,6 +8,7 @@ import { WorkflowResponse, createStep, createWorkflow, + transform, } from "@medusajs/framework/workflows-sdk" import { useRemoteQueryStep } from "../../../common" import { previewOrderChangeStep } from "../../steps" @@ -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. */ @@ -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: { diff --git a/packages/core/core-flows/src/order/workflows/return/confirm-return-request.ts b/packages/core/core-flows/src/order/workflows/return/confirm-return-request.ts index 3e0d476d77..3ee2aa1345 100644 --- a/packages/core/core-flows/src/order/workflows/return/confirm-return-request.ts +++ b/packages/core/core-flows/src/order/workflows/return/confirm-return-request.ts @@ -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. @@ -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,