feat(core-flows,utils): add payment + shipment workflow events (#7749)

This commit is contained in:
Riqwan Thamir authored and GitHub committed 2024-06-17 18:05:05 +02:00
1 parent 2e8e7b27b6
commit 263d9d0f22
9 files changed
+39 -17

No files matched your search

@@ -21,9 +21,9 @@ export const updateFulfillmentStep = createStep(
relations, relations,
}) })
await service.updateFulfillment(id, data) const updated = await service.updateFulfillment(id, data)
return new StepResponse(void 0, fulfillment) return new StepResponse(updated, fulfillment)
}, },
async (fulfillment, { container }) => { async (fulfillment, { container }) => {
if (!fulfillment) { if (!fulfillment) {
@@ -10,9 +10,7 @@ import { updateFulfillmentWorkflow } from "./update-fulfillment"
export const createShipmentWorkflowId = "create-shipment-workflow" export const createShipmentWorkflowId = "create-shipment-workflow"
export const createShipmentWorkflow = createWorkflow( export const createShipmentWorkflow = createWorkflow(
createShipmentWorkflowId, createShipmentWorkflowId,
( (input: WorkflowData<FulfillmentWorkflow.CreateShipmentWorkflowInput>) => {
input: WorkflowData<FulfillmentWorkflow.CreateShipmentWorkflowInput>
): WorkflowData<void> => {
validateShipmentStep(input.id) validateShipmentStep(input.id)
const update = transform({ input }, (data) => ({ const update = transform({ input }, (data) => ({
@@ -20,7 +18,7 @@ export const createShipmentWorkflow = createWorkflow(
shipped_at: new Date(), shipped_at: new Date(),
})) }))
updateFulfillmentWorkflow.runAsStep({ return updateFulfillmentWorkflow.runAsStep({
input: update, input: update,
}) })
} }
@@ -5,9 +5,7 @@ import { updateFulfillmentStep } from "../steps"
export const updateFulfillmentWorkflowId = "update-fulfillment-workflow" export const updateFulfillmentWorkflowId = "update-fulfillment-workflow"
export const updateFulfillmentWorkflow = createWorkflow( export const updateFulfillmentWorkflow = createWorkflow(
updateFulfillmentWorkflowId, updateFulfillmentWorkflowId,
( (input: WorkflowData<FulfillmentWorkflow.UpdateFulfillmentWorkflowInput>) => {
input: WorkflowData<FulfillmentWorkflow.UpdateFulfillmentWorkflowInput> return updateFulfillmentStep(input)
): WorkflowData<void> => {
updateFulfillmentStep(input)
} }
) )
@@ -1,5 +1,5 @@
import { FulfillmentDTO, OrderDTO, OrderWorkflow } from "@medusajs/types" import { FulfillmentDTO, OrderDTO, OrderWorkflow } from "@medusajs/types"
import { Modules } from "@medusajs/utils" import { FulfillmentEvents, Modules } from "@medusajs/utils"
import { import {
WorkflowData, WorkflowData,
createStep, createStep,
@@ -7,7 +7,7 @@ import {
parallelize, parallelize,
transform, transform,
} from "@medusajs/workflows-sdk" } from "@medusajs/workflows-sdk"
import { useRemoteQueryStep } from "../../common" import { emitEventStep, useRemoteQueryStep } from "../../common"
import { createShipmentWorkflow } from "../../fulfillment" import { createShipmentWorkflow } from "../../fulfillment"
import { registerOrderShipmentStep } from "../steps" import { registerOrderShipmentStep } from "../steps"
import { import {
@@ -101,11 +101,16 @@ export const createOrderShipmentWorkflow = createWorkflow(
prepareRegisterShipmentData prepareRegisterShipmentData
) )
parallelize( const [shipment] = parallelize(
createShipmentWorkflow.runAsStep({ createShipmentWorkflow.runAsStep({
input: fulfillmentData, input: fulfillmentData,
}), }),
registerOrderShipmentStep(shipmentData) registerOrderShipmentStep(shipmentData)
) )
emitEventStep({
eventName: FulfillmentEvents.SHIPMENT_CREATED,
data: { id: shipment.id },
})
} }
) )
@@ -1,5 +1,7 @@
import { BigNumberInput, PaymentDTO } from "@medusajs/types" import { BigNumberInput, PaymentDTO } from "@medusajs/types"
import { PaymentEvents } from "@medusajs/utils"
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk" import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { emitEventStep } from "../../common"
import { capturePaymentStep } from "../steps/capture-payment" import { capturePaymentStep } from "../steps/capture-payment"
export const capturePaymentWorkflowId = "capture-payment-workflow" export const capturePaymentWorkflowId = "capture-payment-workflow"
@@ -13,6 +15,12 @@ export const capturePaymentWorkflow = createWorkflow(
}> }>
): WorkflowData<PaymentDTO> => { ): WorkflowData<PaymentDTO> => {
const payment = capturePaymentStep(input) const payment = capturePaymentStep(input)
emitEventStep({
eventName: PaymentEvents.CAPTURED,
data: { id: payment.id },
})
return payment return payment
} }
) )
@@ -1,5 +1,7 @@
import { BigNumberInput } from "@medusajs/types" import { BigNumberInput } from "@medusajs/types"
import { PaymentEvents } from "@medusajs/utils"
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk" import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { emitEventStep } from "../../common"
import { refundPaymentStep } from "../steps/refund-payment" import { refundPaymentStep } from "../steps/refund-payment"
export const refundPaymentWorkflowId = "refund-payment-workflow" export const refundPaymentWorkflowId = "refund-payment-workflow"
@@ -13,6 +15,12 @@ export const refundPaymentWorkflow = createWorkflow(
}> }>
) => { ) => {
const payment = refundPaymentStep(input) const payment = refundPaymentStep(input)
emitEventStep({
eventName: PaymentEvents.REFUNDED,
data: { id: payment.id },
})
return payment return payment
} }
) )
@@ -27,7 +27,7 @@ const eventBaseNames: [
"fulfillmentLabel", "fulfillmentLabel",
] ]
export const FulfillmentEvents = buildEventNamesFromEntityName( export const FulfillmentEvents = {
eventBaseNames, ...buildEventNamesFromEntityName(eventBaseNames, Modules.FULFILLMENT),
Modules.FULFILLMENT SHIPMENT_CREATED: "shipment.created",
) }
@@ -0,0 +1,4 @@
export const PaymentEvents = {
CAPTURED: "payment.captured",
REFUNDED: "payment.refunded",
}
+1
View File
@@ -1,4 +1,5 @@
export * from "./abstract-payment-provider" export * from "./abstract-payment-provider"
export * from "./events"
export * from "./payment-collection" export * from "./payment-collection"
export * from "./payment-session" export * from "./payment-session"
export * from "./webhook" export * from "./webhook"