feat: restructure events payload (#8143)
* refactor: restructure events payload Breaking change: This PR changes the event payload accepted by the event listeners * refactor: fix failing tests and implement feedback * add integration tests * fix timeout --------- Co-authored-by: Adrien de Peretti <adrien.deperetti@gmail.com>
This commit is contained in:
co-authored by
Adrien de Peretti
parent
5813216c88
commit
f579f0b3be
@@ -21,7 +21,7 @@ export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
// we delay the processing of the event to avoid a conflict caused by a race condition
|
||||
await eventBus.emit(
|
||||
{
|
||||
eventName: PaymentWebhookEvents.WebhookReceived,
|
||||
name: PaymentWebhookEvents.WebhookReceived,
|
||||
data: event,
|
||||
},
|
||||
{
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import {
|
||||
Event,
|
||||
IEventBusModuleService,
|
||||
MedusaContainer,
|
||||
MessageBody,
|
||||
Subscriber,
|
||||
} from "@medusajs/types"
|
||||
import { kebabCase, ModuleRegistrationName } from "@medusajs/utils"
|
||||
@@ -193,10 +193,9 @@ export class SubscriberLoader {
|
||||
const subscriberId = this.inferIdentifier(fileName, config, handler)
|
||||
|
||||
for (const e of events) {
|
||||
const subscriber = async (data: MessageBody<T>) => {
|
||||
const subscriber = async (data: T) => {
|
||||
return await handler({
|
||||
eventName: e,
|
||||
data,
|
||||
event: { name: e, ...data } as unknown as Event<T>,
|
||||
container: this.container_,
|
||||
pluginOptions: this.pluginOptions_,
|
||||
})
|
||||
|
||||
@@ -44,8 +44,7 @@ const configAsMap = handlerConfig.reduce(
|
||||
)
|
||||
|
||||
export default async function configurableNotifications({
|
||||
data,
|
||||
eventName,
|
||||
event,
|
||||
container,
|
||||
}: SubscriberArgs<any>) {
|
||||
const logger = container.resolve(ContainerRegistrationKeys.LOGGER)
|
||||
@@ -53,8 +52,8 @@ export default async function configurableNotifications({
|
||||
ModuleRegistrationName.NOTIFICATION
|
||||
)
|
||||
|
||||
const handlers = configAsMap[eventName] ?? []
|
||||
const payload = data.data
|
||||
const handlers = configAsMap[event.name] ?? []
|
||||
const payload = event.data
|
||||
|
||||
await promiseAll(
|
||||
handlers.map(async (handler) => {
|
||||
@@ -75,7 +74,7 @@ export default async function configurableNotifications({
|
||||
await notificationService.createNotifications(notificationData)
|
||||
} catch (err) {
|
||||
logger.error(
|
||||
`Failed to send notification for ${eventName}`,
|
||||
`Failed to send notification for ${event.name}`,
|
||||
err.message
|
||||
)
|
||||
}
|
||||
|
||||
@@ -8,14 +8,14 @@ type SerializedBuffer = {
|
||||
}
|
||||
|
||||
export default async function paymentWebhookhandler({
|
||||
data,
|
||||
event,
|
||||
container,
|
||||
}: SubscriberArgs<ProviderWebhookPayload>) {
|
||||
const paymentService: IPaymentModuleService = container.resolve(
|
||||
ModuleRegistrationName.PAYMENT
|
||||
)
|
||||
|
||||
const input = "data" in data ? data.data : data
|
||||
const input = event.data
|
||||
|
||||
if (
|
||||
(input.payload.rawData as unknown as SerializedBuffer).type === "Buffer"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { MedusaContainer, MessageBody } from "@medusajs/types"
|
||||
import { Event, MedusaContainer } from "@medusajs/types"
|
||||
|
||||
interface SubscriberContext extends Record<string, unknown> {
|
||||
subscriberId?: string
|
||||
@@ -10,8 +10,7 @@ export type SubscriberConfig = {
|
||||
}
|
||||
|
||||
export type SubscriberArgs<T = unknown> = {
|
||||
data: MessageBody<T>
|
||||
eventName: string
|
||||
event: Event<T>
|
||||
container: MedusaContainer
|
||||
pluginOptions: Record<string, unknown>
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user