Skip to content

Commit

Permalink
feat(core-flows): order events (#9702)
Browse files Browse the repository at this point in the history
  • Loading branch information
carlos-r-l-rodrigues authored Oct 21, 2024
1 parent 9f53683 commit d8e3e04
Show file tree
Hide file tree
Showing 11 changed files with 159 additions and 16 deletions.
12 changes: 12 additions & 0 deletions packages/core/core-flows/src/order/workflows/archive-orders.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { OrderDTO } from "@medusajs/framework/types"
import { OrderWorkflowEvents } from "@medusajs/framework/utils"
import {
WorkflowData,
WorkflowResponse,
createWorkflow,
transform,
} from "@medusajs/framework/workflows-sdk"
import { emitEventStep } from "../../common/steps/emit-event"
import { archiveOrdersStep } from "../steps"

export type ArchiveOrdersWorkflowInput = {
Expand All @@ -19,6 +22,15 @@ export const archiveOrderWorkflow = createWorkflow(
(
input: WorkflowData<ArchiveOrdersWorkflowInput>
): WorkflowResponse<OrderDTO[]> => {
const eventData = transform({ input }, (data) => {
return data.input.orderIds.map((id) => ({ id }))
})

emitEventStep({
eventName: OrderWorkflowEvents.ARCHIVED,
data: eventData,
})

return new WorkflowResponse(archiveOrdersStep(input))
}
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import {
OrderDTO,
OrderWorkflow,
} from "@medusajs/framework/types"
import { MedusaError, Modules } from "@medusajs/framework/utils"
import {
MedusaError,
Modules,
OrderWorkflowEvents,
} from "@medusajs/framework/utils"
import {
WorkflowData,
WorkflowResponse,
Expand All @@ -15,7 +19,7 @@ import {
parallelize,
transform,
} from "@medusajs/framework/workflows-sdk"
import { useRemoteQueryStep } from "../../common"
import { emitEventStep, useRemoteQueryStep } from "../../common"
import { cancelFulfillmentWorkflow } from "../../fulfillment"
import { adjustInventoryLevelsStep } from "../../inventory"
import { cancelOrderFulfillmentStep } from "../steps/cancel-fulfillment"
Expand Down Expand Up @@ -153,9 +157,20 @@ export const cancelOrderFulfillmentWorkflow = createWorkflow(
prepareInventoryUpdate
)

const eventData = transform({ order, fulfillment }, (data) => {
return {
order_id: data.order.id,
fulfillment_id: data.fulfillment.id,
}
})

parallelize(
cancelOrderFulfillmentStep(cancelOrderFulfillmentData),
adjustInventoryLevelsStep(inventoryAdjustment)
adjustInventoryLevelsStep(inventoryAdjustment),
emitEventStep({
eventName: OrderWorkflowEvents.FULFILLMENT_CANCELED,
data: eventData,
})
)

// last step because there is no compensation for this step
Expand Down
14 changes: 11 additions & 3 deletions packages/core/core-flows/src/order/workflows/cancel-order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import {
OrderWorkflow,
PaymentCollectionDTO,
} from "@medusajs/framework/types"
import { MedusaError, deepFlatMap } from "@medusajs/framework/utils"
import {
MedusaError,
OrderWorkflowEvents,
deepFlatMap,
} from "@medusajs/framework/utils"
import {
WorkflowData,
WorkflowResponse,
Expand All @@ -14,7 +18,7 @@ import {
parallelize,
transform,
} from "@medusajs/framework/workflows-sdk"
import { useRemoteQueryStep } from "../../common"
import { emitEventStep, useRemoteQueryStep } from "../../common"
import { cancelPaymentStep } from "../../payment/steps"
import { deleteReservationsByLineItemsStep } from "../../reservation/steps"
import { cancelOrdersStep } from "../steps/cancel-orders"
Expand Down Expand Up @@ -122,7 +126,11 @@ export const cancelOrderWorkflow = createWorkflow(
parallelize(
deleteReservationsByLineItemsStep(lineItemIds),
cancelPaymentStep({ paymentIds }),
cancelOrdersStep({ orderIds: [order.id] })
cancelOrdersStep({ orderIds: [order.id] }),
emitEventStep({
eventName: OrderWorkflowEvents.CANCELED,
data: { id: order.id },
})
)

const orderCanceled = createHook("orderCanceled", {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
MedusaError,
Modules,
OrderChangeStatus,
OrderWorkflowEvents,
ReturnStatus,
} from "@medusajs/framework/utils"
import {
Expand All @@ -25,7 +26,11 @@ import {
} from "@medusajs/framework/workflows-sdk"
import { reserveInventoryStep } from "../../../cart/steps/reserve-inventory"
import { prepareConfirmInventoryInput } from "../../../cart/utils/prepare-confirm-inventory-input"
import { createRemoteLinkStep, useRemoteQueryStep } from "../../../common"
import {
createRemoteLinkStep,
emitEventStep,
useRemoteQueryStep,
} from "../../../common"
import { createReturnFulfillmentWorkflow } from "../../../fulfillment/workflows/create-return-fulfillment"
import { previewOrderChangeStep, updateReturnsStep } from "../../steps"
import { createOrderClaimItemsFromActionsStep } from "../../steps/claim/create-claim-items-from-actions"
Expand Down Expand Up @@ -439,6 +444,14 @@ export const confirmClaimRequestWorkflow = createWorkflow(
},
})

emitEventStep({
eventName: OrderWorkflowEvents.CLAIM_CREATED,
data: {
order_id: order.id,
claim_id: orderClaim.id,
},
})

return new WorkflowResponse(orderPreview)
}
)
15 changes: 14 additions & 1 deletion packages/core/core-flows/src/order/workflows/complete-orders.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { AdditionalData } from "@medusajs/framework/types"
import { OrderWorkflowEvents } from "@medusajs/framework/utils"
import {
WorkflowData,
WorkflowResponse,
createHook,
createWorkflow,
transform,
} from "@medusajs/framework/workflows-sdk"
import { emitEventStep } from "../../common/steps/emit-event"
import { completeOrdersStep } from "../steps"
import { AdditionalData } from "@medusajs/framework/types"

export type CompleteOrdersWorkflowInput = {
orderIds: string[]
Expand All @@ -19,6 +22,16 @@ export const completeOrderWorkflow = createWorkflow(
completeOrderWorkflowId,
(input: WorkflowData<CompleteOrdersWorkflowInput>) => {
const completedOrders = completeOrdersStep(input)

const eventData = transform({ input }, (data) => {
return data.input.orderIds.map((id) => ({ id }))
})

emitEventStep({
eventName: OrderWorkflowEvents.COMPLETED,
data: eventData,
})

const ordersCompleted = createHook("ordersCompleted", {
orders: completedOrders,
additional_data: input.additional_data,
Expand Down
22 changes: 19 additions & 3 deletions packages/core/core-flows/src/order/workflows/create-fulfillment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ import {
OrderWorkflow,
ReservationItemDTO,
} from "@medusajs/framework/types"
import { MathBN, MedusaError, Modules } from "@medusajs/framework/utils"
import {
MathBN,
MedusaError,
Modules,
OrderWorkflowEvents,
} from "@medusajs/framework/utils"
import {
WorkflowData,
WorkflowResponse,
Expand All @@ -17,7 +22,11 @@ import {
parallelize,
transform,
} from "@medusajs/framework/workflows-sdk"
import { createRemoteLinkStep, useRemoteQueryStep } from "../../common"
import {
createRemoteLinkStep,
emitEventStep,
useRemoteQueryStep,
} from "../../common"
import { createFulfillmentWorkflow } from "../../fulfillment"
import { adjustInventoryLevelsStep } from "../../inventory"
import {
Expand Down Expand Up @@ -351,7 +360,14 @@ export const createOrderFulfillmentWorkflow = createWorkflow(
registerOrderFulfillmentStep(registerOrderFulfillmentData),
createRemoteLinkStep(link),
updateReservationsStep(toUpdate),
deleteReservationsStep(toDelete)
deleteReservationsStep(toDelete),
emitEventStep({
eventName: OrderWorkflowEvents.FULFILLMENT_CREATED,
data: {
order_id: input.order_id,
fulfillment_id: fulfillment.id,
},
})
)

const fulfillmentCreated = createHook("fulfillmentCreated", {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
MedusaError,
Modules,
OrderChangeStatus,
OrderWorkflowEvents,
ReturnStatus,
} from "@medusajs/framework/utils"
import {
Expand All @@ -25,7 +26,11 @@ import {
} from "@medusajs/framework/workflows-sdk"
import { reserveInventoryStep } from "../../../cart/steps/reserve-inventory"
import { prepareConfirmInventoryInput } from "../../../cart/utils/prepare-confirm-inventory-input"
import { createRemoteLinkStep, useRemoteQueryStep } from "../../../common"
import {
createRemoteLinkStep,
emitEventStep,
useRemoteQueryStep,
} from "../../../common"
import { createReturnFulfillmentWorkflow } from "../../../fulfillment/workflows/create-return-fulfillment"
import { previewOrderChangeStep, updateReturnsStep } from "../../steps"
import { confirmOrderChanges } from "../../steps/confirm-order-changes"
Expand Down Expand Up @@ -426,6 +431,14 @@ export const confirmExchangeRequestWorkflow = createWorkflow(
},
})

emitEventStep({
eventName: OrderWorkflowEvents.EXCHANGE_CREATED,
data: {
order_id: order.id,
exchange_id: orderExchange.id,
},
})

return new WorkflowResponse(orderPreview)
}
)
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
ChangeActionType,
MathBN,
OrderChangeStatus,
OrderWorkflowEvents,
ReturnStatus,
deepFlatMap,
} from "@medusajs/framework/utils"
Expand All @@ -21,7 +22,7 @@ import {
parallelize,
transform,
} from "@medusajs/framework/workflows-sdk"
import { useRemoteQueryStep } from "../../../common"
import { emitEventStep, useRemoteQueryStep } from "../../../common"
import { adjustInventoryLevelsStep } from "../../../inventory/steps"
import {
previewOrderChangeStep,
Expand Down Expand Up @@ -299,7 +300,14 @@ export const confirmReturnReceiveWorkflow = createWorkflow(
orderId: order.id,
confirmed_by: input.confirmed_by,
}),
adjustInventoryLevelsStep(inventoryAdjustment)
adjustInventoryLevelsStep(inventoryAdjustment),
emitEventStep({
eventName: OrderWorkflowEvents.RETURN_RECEIVED,
data: {
order_id: order.id,
return_id: orderReturn.id,
},
})
)

return new WorkflowResponse(previewOrderChangeStep(order.id))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
MedusaError,
Modules,
OrderChangeStatus,
OrderWorkflowEvents,
ReturnStatus,
} from "@medusajs/framework/utils"
import {
Expand All @@ -21,7 +22,11 @@ import {
transform,
when,
} from "@medusajs/framework/workflows-sdk"
import { createRemoteLinkStep, useRemoteQueryStep } from "../../../common"
import {
createRemoteLinkStep,
emitEventStep,
useRemoteQueryStep,
} from "../../../common"
import { createReturnFulfillmentWorkflow } from "../../../fulfillment/workflows/create-return-fulfillment"
import { previewOrderChangeStep, updateReturnsStep } from "../../steps"
import { confirmOrderChanges } from "../../steps/confirm-order-changes"
Expand Down Expand Up @@ -284,6 +289,13 @@ export const confirmReturnRequestWorkflow = createWorkflow(
changes: [orderChange],
orderId: order.id,
confirmed_by: input.confirmed_by,
}),
emitEventStep({
eventName: OrderWorkflowEvents.RETURN_REQUESTED,
data: {
order_id: order.id,
return_id: orderReturn.id,
},
})
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,21 @@ import {
MathBN,
MedusaError,
Modules,
OrderWorkflowEvents,
isDefined,
} from "@medusajs/framework/utils"
import {
WorkflowData,
createStep,
createWorkflow,
parallelize,
transform,
} from "@medusajs/framework/workflows-sdk"
import { createRemoteLinkStep, useRemoteQueryStep } from "../../../common"
import {
createRemoteLinkStep,
emitEventStep,
useRemoteQueryStep,
} from "../../../common"
import { createReturnFulfillmentWorkflow } from "../../../fulfillment"
import { createCompleteReturnStep } from "../../steps/return/create-complete-return"
import { receiveReturnStep } from "../../steps/return/receive-return"
Expand Down Expand Up @@ -332,5 +338,22 @@ export const createAndCompleteReturnOrderWorkflow = createWorkflow(
prepareReceiveItems
)
receiveReturnStep(receiveItems)

parallelize(
emitEventStep({
eventName: OrderWorkflowEvents.RETURN_REQUESTED,
data: {
order_id: order.id,
return_id: returnCreated.id,
},
}).config({ name: "emit-return-requested-event" }),
emitEventStep({
eventName: OrderWorkflowEvents.RETURN_RECEIVED,
data: {
order_id: order.id,
return_id: returnCreated.id,
},
}).config({ name: "emit-return-received-event" })
)
}
)
10 changes: 10 additions & 0 deletions packages/core/utils/src/core-flows/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ export const OrderWorkflowEvents = {
PLACED: "order.placed",
CANCELED: "order.canceled",
COMPLETED: "order.completed",
ARCHIVED: "order.archived",

FULFILLMENT_CREATED: "order.fulfillment_created",
FULFILLMENT_CANCELED: "order.fulfillment_canceled",

RETURN_REQUESTED: "order.return_requested",
RETURN_RECEIVED: "order.return_received",

CLAIM_CREATED: "order.claim_created",
EXCHANGE_CREATED: "order.exchange_created",
}

export const UserWorkflowEvents = {
Expand Down

0 comments on commit d8e3e04

Please sign in to comment.