mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-08-29 18:48:11 +00:00
refactor: ( payment ) use rabbitmq for webhooks event
This commit is contained in:
@@ -14,6 +14,7 @@ import ebirrConfig from './config/ebirr.config';
|
||||
import cardConfig from './config/card.config';
|
||||
import waafiConfig from './config/waafi.config';
|
||||
import faydaConfig from './config/fayda.config';
|
||||
import rabbitmqConfig from './config/rabbitmq.config';
|
||||
import { AuthModule } from './modules/auth/auth.module';
|
||||
import { StationsModule } from './modules/stations/stations.module';
|
||||
import { FleetModule } from './modules/fleet/fleet.module';
|
||||
@@ -52,6 +53,7 @@ import { VerifaydaModule } from './modules/verifayda/verifayda.module';
|
||||
cardConfig,
|
||||
waafiConfig,
|
||||
faydaConfig,
|
||||
rabbitmqConfig,
|
||||
],
|
||||
}),
|
||||
ScheduleModule.forRoot(),
|
||||
|
||||
@@ -12,6 +12,12 @@ export class HttpExceptionFilter implements ExceptionFilter {
|
||||
private readonly logger = new Logger(HttpExceptionFilter.name);
|
||||
|
||||
catch(exception: unknown, host: ArgumentsHost): void {
|
||||
// Global filter — also reached by non-HTTP (e.g. RabbitMQ) handlers. switchToHttp() would
|
||||
// yield no response object there, so re-throw and let the transport (golevelup) handle it
|
||||
// (nack/dead-letter) instead of crashing on response.status().
|
||||
if (host.getType() !== 'http') {
|
||||
throw exception;
|
||||
}
|
||||
const ctx = host.switchToHttp();
|
||||
const response = ctx.getResponse();
|
||||
const request = ctx.getRequest();
|
||||
|
||||
@@ -4,7 +4,14 @@ import { map } from 'rxjs/operators';
|
||||
|
||||
@Injectable()
|
||||
export class ResponseTransformInterceptor<T> implements NestInterceptor<T, any> {
|
||||
intercept(_ctx: ExecutionContext, next: CallHandler<T>): Observable<any> {
|
||||
intercept(ctx: ExecutionContext, next: CallHandler<T>): Observable<any> {
|
||||
// Only wrap HTTP responses. This interceptor is global, so it also runs for RabbitMQ
|
||||
// message handlers (golevelup uses Nest's context system) — there, wrapping the return
|
||||
// value would corrupt the handler's contract (e.g. a returned Nack would be swallowed,
|
||||
// dropping a message instead of dead-lettering it). Let non-HTTP returns pass through.
|
||||
if (ctx.getType() !== 'http') {
|
||||
return next.handle();
|
||||
}
|
||||
return next.handle().pipe(
|
||||
map((data) => ({ success: true, data, timestamp: new Date().toISOString() })),
|
||||
);
|
||||
|
||||
12
apps/edr-passenger-api/src/config/rabbitmq.config.ts
Normal file
12
apps/edr-passenger-api/src/config/rabbitmq.config.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { registerAs } from '@nestjs/config';
|
||||
|
||||
/**
|
||||
* RabbitMQ connection for the payment-event consumer (payment microservice -> passenger).
|
||||
* Independent of the IAM/notification module's RABBITMQ_URL so the two broker connections
|
||||
* never interfere. Points at the dedicated `payment` vhost on the shared broker.
|
||||
*/
|
||||
export default registerAs('rabbitmq', () => ({
|
||||
url: process.env.PAYMENT_RABBITMQ_URL ?? 'amqp://localhost:5672/payment',
|
||||
/** Max unacked payment events held by this consumer at once. */
|
||||
prefetch: parseInt(process.env.PAYMENT_EVENTS_PREFETCH ?? '10', 10),
|
||||
}));
|
||||
@@ -0,0 +1,47 @@
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
import { Nack, RabbitSubscribe } from '@golevelup/nestjs-rabbitmq';
|
||||
import {
|
||||
PAYMENT_EVENTS_DLX,
|
||||
PAYMENT_EVENTS_EXCHANGE,
|
||||
PAYMENT_QUEUES,
|
||||
PaymentEvent,
|
||||
PaymentService,
|
||||
paymentServiceBindingPattern,
|
||||
} from '@edr/types';
|
||||
import { PaymentEventDto } from './internal-payments.dto';
|
||||
import { PaymentsService } from './payments.service';
|
||||
|
||||
const PASSENGER_QUEUE = PAYMENT_QUEUES[PaymentService.PASSENGER];
|
||||
|
||||
@Injectable()
|
||||
export class PaymentEventsConsumer {
|
||||
private readonly logger = new Logger(PaymentEventsConsumer.name);
|
||||
|
||||
constructor(private readonly paymentsService: PaymentsService) {}
|
||||
|
||||
@RabbitSubscribe({
|
||||
exchange: PAYMENT_EVENTS_EXCHANGE,
|
||||
routingKey: paymentServiceBindingPattern(PaymentService.PASSENGER), // payment.passenger.*
|
||||
queue: PASSENGER_QUEUE.main,
|
||||
queueOptions: {
|
||||
durable: true,
|
||||
deadLetterExchange: PAYMENT_EVENTS_DLX,
|
||||
},
|
||||
})
|
||||
async handle(event: PaymentEvent): Promise<Nack | void> {
|
||||
try {
|
||||
const result = await this.paymentsService.handlePaymentEvent(
|
||||
event as unknown as PaymentEventDto,
|
||||
);
|
||||
this.logger.log(
|
||||
`processed ${event.eventType} (${event.eventId}) ref=${event.referenceId}: ${JSON.stringify(result)}`,
|
||||
);
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : String(err);
|
||||
this.logger.error(
|
||||
`DEAD-LETTERING ${event.eventType} (${event.eventId}) ref=${event.referenceId}: ${message}`,
|
||||
);
|
||||
return new Nack(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,25 +1,61 @@
|
||||
import { Module } from "@nestjs/common";
|
||||
import { HttpModule } from "@nestjs/axios";
|
||||
import { ConfigService } from "@nestjs/config";
|
||||
import { RabbitMQModule } from "@golevelup/nestjs-rabbitmq";
|
||||
import {
|
||||
PAYMENT_EVENTS_DLX,
|
||||
PAYMENT_EVENTS_EXCHANGE,
|
||||
PAYMENT_QUEUES,
|
||||
PaymentService,
|
||||
paymentServiceBindingPattern,
|
||||
} from "@edr/types";
|
||||
import { PaymentsController } from "./payments.controller";
|
||||
import { PaymentsService } from "./payments.service";
|
||||
import { InternalPaymentsController } from "./internal-payments.controller";
|
||||
import { PaymentClientService } from "./payment-client.service";
|
||||
import { PaymentEventsConsumer } from "./payment-events.consumer";
|
||||
import { ServiceAuthGuard } from "../../common/guards/service-auth.guard";
|
||||
import { SeatsModule } from "../seats/seats.module";
|
||||
import { TicketsModule } from "../tickets/tickets.module";
|
||||
|
||||
/**
|
||||
* Post-cutover (docs/payment-service phase 6): provider gateways and webhook handlers live in
|
||||
* apps/edr-payment-api. This module keeps domain validation, the WALLET flow, the payment
|
||||
* client, and the idempotent mark-paid consumer.
|
||||
*/
|
||||
const PASSENGER_QUEUE = PAYMENT_QUEUES[PaymentService.PASSENGER];
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
SeatsModule,
|
||||
TicketsModule,
|
||||
HttpModule.register({ timeout: 10_000 }),
|
||||
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 } },
|
||||
],
|
||||
queues: [
|
||||
{
|
||||
name: PASSENGER_QUEUE.dlq,
|
||||
exchange: PAYMENT_EVENTS_DLX,
|
||||
routingKey: paymentServiceBindingPattern(PaymentService.PASSENGER),
|
||||
options: { durable: true },
|
||||
},
|
||||
],
|
||||
prefetchCount: config.get<number>("rabbitmq.prefetch") ?? 10,
|
||||
connectionInitOptions: { wait: false },
|
||||
}),
|
||||
}),
|
||||
],
|
||||
controllers: [PaymentsController, InternalPaymentsController],
|
||||
providers: [PaymentsService, PaymentClientService, ServiceAuthGuard],
|
||||
providers: [
|
||||
PaymentsService,
|
||||
PaymentClientService,
|
||||
PaymentEventsConsumer,
|
||||
ServiceAuthGuard,
|
||||
],
|
||||
})
|
||||
export class PaymentsModule {}
|
||||
|
||||
Reference in New Issue
Block a user