From 72c499ba4da68a625e50ab66da2486ed3b6ad493 Mon Sep 17 00:00:00 2001 From: adrien2p Date: Thu, 14 Nov 2024 14:16:18 +0100 Subject: [PATCH 1/2] fix(core-flows): Fix date usage accross workflows --- .../workflows/claim/confirm-claim-request.ts | 18 ++++++----- .../exchange/confirm-exchange-request.ts | 21 ++++++++----- .../order-edit/request-order-edit.ts | 30 ++++++++++++++----- .../return/confirm-return-request.ts | 22 +++++++++----- 4 files changed, 62 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 7b7aa1e0a351f..e3bf55d67a3fe 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 @@ -306,13 +306,17 @@ export const confirmClaimRequestWorkflow = createWorkflow( when({ returnId }, ({ returnId }) => { return !!returnId }).then(() => { - updateReturnsStep([ - { - id: returnId, - status: ReturnStatus.REQUESTED, - requested_at: new Date(), - }, - ]) + const data = transform({ returnId }, ({ returnId }) => { + return [ + { + id: returnId, + status: ReturnStatus.REQUESTED, + requested_at: new Date(), + }, + ] + }) + + updateReturnsStep(data) }) 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 ff2279fa03856..78fecca7fc838 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 d66a924dc85f8..312c816615312 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 3e0d476d77cc9..3ee2aa1345afd 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, From 8096bee4d897d0e533217e74d5b402c125e3f210 Mon Sep 17 00:00:00 2001 From: adrien2p Date: Thu, 14 Nov 2024 14:19:31 +0100 Subject: [PATCH 2/2] cleanup --- .../workflows/claim/confirm-claim-request.ts | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 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 e3bf55d67a3fe..6f8a2999780e6 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,17 +318,8 @@ export const confirmClaimRequestWorkflow = createWorkflow( when({ returnId }, ({ returnId }) => { return !!returnId }).then(() => { - const data = transform({ returnId }, ({ returnId }) => { - return [ - { - id: returnId, - status: ReturnStatus.REQUESTED, - requested_at: new Date(), - }, - ] - }) - - updateReturnsStep(data) + const updateReturnDate = getUpdateReturnData({ returnId }) + updateReturnsStep(updateReturnDate) }) const claimId = transform(