mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-08-28 05:30:55 +00:00
refactor: ( payment ) use rabbitmq for webhooks event
This commit is contained in:
@@ -1,19 +1,56 @@
|
||||
import { Module } from "@nestjs/common";
|
||||
import { HttpModule } from "@nestjs/axios";
|
||||
import { ConfigService } from "@nestjs/config";
|
||||
import { TypeOrmModule } from "@nestjs/typeorm";
|
||||
import { RabbitMQModule } from "@golevelup/nestjs-rabbitmq";
|
||||
import { PAYMENT_EVENTS_DLX, PAYMENT_EVENTS_EXCHANGE } from "@edr/types";
|
||||
import { isRabbitPublisher } from "../../config/rabbitmq.config";
|
||||
import { NotificationOutbox } from "./entities/notification-outbox.entity";
|
||||
import { OutboxRelayService } from "./outbox-relay.service";
|
||||
import { OutboxRepository } from "./outbox.repository";
|
||||
import { HttpPaymentEventPublisher } from "./publisher/http-payment-event-publisher";
|
||||
import { PAYMENT_EVENT_PUBLISHER } from "./publisher/payment-event-publisher";
|
||||
import { RabbitMqPaymentEventPublisher } from "./publisher/rabbitmq-payment-event-publisher";
|
||||
|
||||
const rabbitImports = isRabbitPublisher()
|
||||
? [
|
||||
RabbitMQModule.forRootAsync({
|
||||
inject: [ConfigService],
|
||||
useFactory: (config: ConfigService) => ({
|
||||
uri: config.get<string>("rabbitmq.url") as string,
|
||||
exchanges: [
|
||||
{
|
||||
name: PAYMENT_EVENTS_EXCHANGE,
|
||||
type: "topic",
|
||||
options: { durable: true },
|
||||
},
|
||||
{
|
||||
name: PAYMENT_EVENTS_DLX,
|
||||
type: "topic",
|
||||
options: { durable: true },
|
||||
},
|
||||
],
|
||||
connectionInitOptions: { wait: false },
|
||||
}),
|
||||
}),
|
||||
]
|
||||
: [];
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([NotificationOutbox]), HttpModule],
|
||||
imports: [
|
||||
TypeOrmModule.forFeature([NotificationOutbox]),
|
||||
HttpModule,
|
||||
...rabbitImports,
|
||||
],
|
||||
providers: [
|
||||
OutboxRepository,
|
||||
OutboxRelayService,
|
||||
// Swap to RabbitPaymentEventPublisher here when the broker lands — nothing else changes.
|
||||
{ provide: PAYMENT_EVENT_PUBLISHER, useClass: HttpPaymentEventPublisher },
|
||||
{
|
||||
provide: PAYMENT_EVENT_PUBLISHER,
|
||||
useClass: isRabbitPublisher()
|
||||
? RabbitMqPaymentEventPublisher
|
||||
: HttpPaymentEventPublisher,
|
||||
},
|
||||
],
|
||||
exports: [OutboxRepository],
|
||||
})
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
import { Injectable, Logger } from "@nestjs/common";
|
||||
import { ConfigService } from "@nestjs/config";
|
||||
import { AmqpConnection } from "@golevelup/nestjs-rabbitmq";
|
||||
import {
|
||||
PAYMENT_EVENTS_EXCHANGE,
|
||||
PaymentEvent,
|
||||
paymentRoutingKey,
|
||||
} from "@edr/types";
|
||||
import { PaymentEventPublisher } from "./payment-event-publisher";
|
||||
|
||||
@Injectable()
|
||||
export class RabbitMqPaymentEventPublisher implements PaymentEventPublisher {
|
||||
private readonly logger = new Logger(RabbitMqPaymentEventPublisher.name);
|
||||
private readonly publishTimeoutMs: number;
|
||||
|
||||
constructor(
|
||||
private readonly amqp: AmqpConnection,
|
||||
config: ConfigService,
|
||||
) {
|
||||
this.publishTimeoutMs =
|
||||
config.get<number>("rabbitmq.publishTimeoutMs") ?? 10_000;
|
||||
}
|
||||
|
||||
async publish(event: PaymentEvent): Promise<void> {
|
||||
const routingKey = paymentRoutingKey(event.service, event.eventType);
|
||||
|
||||
const confirmed = await this.withTimeout(
|
||||
this.amqp.publish(PAYMENT_EVENTS_EXCHANGE, routingKey, event, {
|
||||
persistent: true,
|
||||
messageId: event.eventId,
|
||||
contentType: "application/json",
|
||||
type: event.eventType,
|
||||
headers: { "x-event-version": event.version },
|
||||
}),
|
||||
);
|
||||
|
||||
if (!confirmed) {
|
||||
throw new Error(
|
||||
`broker did not confirm publish of ${event.eventType} (${event.eventId})`,
|
||||
);
|
||||
}
|
||||
|
||||
this.logger.log(
|
||||
`published ${event.eventType} (${event.eventId}) -> ${routingKey}`,
|
||||
);
|
||||
}
|
||||
|
||||
private withTimeout(p: Promise<boolean>): Promise<boolean> {
|
||||
let timer: NodeJS.Timeout;
|
||||
const timeout = new Promise<boolean>((_, reject) => {
|
||||
timer = setTimeout(
|
||||
() =>
|
||||
reject(
|
||||
new Error(`publish confirm timed out after ${this.publishTimeoutMs}ms`),
|
||||
),
|
||||
this.publishTimeoutMs,
|
||||
);
|
||||
});
|
||||
return Promise.race([p, timeout]).finally(() => clearTimeout(timer));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user