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

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

View File

@@ -21,9 +21,9 @@ export const updateFulfillmentStep = createStep(
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 }) => {
if (!fulfillment) {

View File

@@ -10,9 +10,7 @@ import { updateFulfillmentWorkflow } from "./update-fulfillment"
export const createShipmentWorkflowId = "create-shipment-workflow"
export const createShipmentWorkflow = createWorkflow(
createShipmentWorkflowId,
(
input: WorkflowData<FulfillmentWorkflow.CreateShipmentWorkflowInput>
): WorkflowData<void> => {
(input: WorkflowData<FulfillmentWorkflow.CreateShipmentWorkflowInput>) => {
validateShipmentStep(input.id)
const update = transform({ input }, (data) => ({
@@ -20,7 +18,7 @@ export const createShipmentWorkflow = createWorkflow(
shipped_at: new Date(),
}))
updateFulfillmentWorkflow.runAsStep({
return updateFulfillmentWorkflow.runAsStep({
input: update,
})
}

View File

@@ -5,9 +5,7 @@ import { updateFulfillmentStep } from "../steps"
export const updateFulfillmentWorkflowId = "update-fulfillment-workflow"
export const updateFulfillmentWorkflow = createWorkflow(
updateFulfillmentWorkflowId,
(
input: WorkflowData<FulfillmentWorkflow.UpdateFulfillmentWorkflowInput>
): WorkflowData<void> => {
updateFulfillmentStep(input)
(input: WorkflowData<FulfillmentWorkflow.UpdateFulfillmentWorkflowInput>) => {
return updateFulfillmentStep(input)
}
)

View File

@@ -1,5 +1,5 @@
import { FulfillmentDTO, OrderDTO, OrderWorkflow } from "@medusajs/types"
import { Modules } from "@medusajs/utils"
import { FulfillmentEvents, Modules } from "@medusajs/utils"
import {
WorkflowData,
createStep,
@@ -7,7 +7,7 @@ import {
parallelize,
transform,
} from "@medusajs/workflows-sdk"
import { useRemoteQueryStep } from "../../common"
import { emitEventStep, useRemoteQueryStep } from "../../common"
import { createShipmentWorkflow } from "../../fulfillment"
import { registerOrderShipmentStep } from "../steps"
import {
@@ -101,11 +101,16 @@ export const createOrderShipmentWorkflow = createWorkflow(
prepareRegisterShipmentData
)
parallelize(
const [shipment] = parallelize(
createShipmentWorkflow.runAsStep({
input: fulfillmentData,
}),
registerOrderShipmentStep(shipmentData)
)
emitEventStep({
eventName: FulfillmentEvents.SHIPMENT_CREATED,
data: { id: shipment.id },
})
}
)

View File

@@ -1,5 +1,7 @@
import { BigNumberInput, PaymentDTO } from "@medusajs/types"
import { PaymentEvents } from "@medusajs/utils"
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { emitEventStep } from "../../common"
import { capturePaymentStep } from "../steps/capture-payment"
export const capturePaymentWorkflowId = "capture-payment-workflow"
@@ -13,6 +15,12 @@ export const capturePaymentWorkflow = createWorkflow(
}>
): WorkflowData<PaymentDTO> => {
const payment = capturePaymentStep(input)
emitEventStep({
eventName: PaymentEvents.CAPTURED,
data: { id: payment.id },
})
return payment
}
)

View File

@@ -1,5 +1,7 @@
import { BigNumberInput } from "@medusajs/types"
import { PaymentEvents } from "@medusajs/utils"
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { emitEventStep } from "../../common"
import { refundPaymentStep } from "../steps/refund-payment"
export const refundPaymentWorkflowId = "refund-payment-workflow"
@@ -13,6 +15,12 @@ export const refundPaymentWorkflow = createWorkflow(
}>
) => {
const payment = refundPaymentStep(input)
emitEventStep({
eventName: PaymentEvents.REFUNDED,
data: { id: payment.id },
})
return payment
}
)

View File

@@ -27,7 +27,7 @@ const eventBaseNames: [
"fulfillmentLabel",
]
export const FulfillmentEvents = buildEventNamesFromEntityName(
eventBaseNames,
Modules.FULFILLMENT
)
export const FulfillmentEvents = {
...buildEventNamesFromEntityName(eventBaseNames, Modules.FULFILLMENT),
SHIPMENT_CREATED: "shipment.created",
}

View File

@@ -0,0 +1,4 @@
export const PaymentEvents = {
CAPTURED: "payment.captured",
REFUNDED: "payment.refunded",
}

View File

@@ -1,4 +1,5 @@
export * from "./abstract-payment-provider"
export * from "./events"
export * from "./payment-collection"
export * from "./payment-session"
export * from "./webhook"