diff --git a/apps/expiration/src/app/orders/orders-ms.controller.ts b/apps/expiration/src/app/orders/orders-ms.controller.ts index f6c046eb..7df9a43f 100644 --- a/apps/expiration/src/app/orders/orders-ms.controller.ts +++ b/apps/expiration/src/app/orders/orders-ms.controller.ts @@ -14,7 +14,7 @@ import { Transport, } from '@nestjs/microservices'; import { ApiExcludeEndpoint } from '@nestjs/swagger'; -import { Patterns } from '@ticketing/microservices/shared/events'; +import { EventsMap, Patterns } from '@ticketing/microservices/shared/events'; import { GlobalErrorFilter } from '@ticketing/microservices/shared/filters'; import { requestValidationErrorFactory } from '@ticketing/shared/errors'; import { Order } from '@ticketing/shared/models'; @@ -44,7 +44,7 @@ export class OrdersMSController { @EventPattern(Patterns.OrderCreated, Transport.RMQ) async onCreated( @Payload(new ValidationPipe(validationPipeOptions)) - data: Order, + data: EventsMap[Patterns.OrderCreated], @Ctx() context: RmqContext, ): Promise<{ ok: boolean; diff --git a/apps/orders/src/app/orders/orders-ms.controller.ts b/apps/orders/src/app/orders/orders-ms.controller.ts index 53ca99a1..7f2430a6 100644 --- a/apps/orders/src/app/orders/orders-ms.controller.ts +++ b/apps/orders/src/app/orders/orders-ms.controller.ts @@ -14,12 +14,8 @@ import { Transport, } from '@nestjs/microservices'; import { ApiExcludeEndpoint } from '@nestjs/swagger'; -import { - ExpirationCompletedEvent, - Patterns, -} from '@ticketing/microservices/shared/events'; +import { EventsMap, Patterns } from '@ticketing/microservices/shared/events'; import { requestValidationErrorFactory } from '@ticketing/shared/errors'; -import { Payment } from '@ticketing/shared/models'; import type { Channel } from 'amqp-connection-manager'; import type { Message } from 'amqplib'; @@ -44,7 +40,7 @@ export class OrdersMSController { @ApiExcludeEndpoint() @EventPattern(Patterns.ExpirationCompleted, Transport.RMQ) async onExpiration( - @Payload() data: ExpirationCompletedEvent['data'], + @Payload() data: EventsMap[Patterns.ExpirationCompleted], @Ctx() context: RmqContext, ): Promise { const channel = context.getChannelRef() as Channel; @@ -67,7 +63,7 @@ export class OrdersMSController { @MessagePattern(Patterns.PaymentCreated, Transport.RMQ) async onPaymentCreated( @Payload(new ValidationPipe(validationPipeOptions)) - data: Payment, + data: EventsMap[Patterns.PaymentCreated], @Ctx() context: RmqContext, ): Promise { const channel = context.getChannelRef() as Channel; diff --git a/apps/orders/src/app/orders/orders.service.ts b/apps/orders/src/app/orders/orders.service.ts index 96bc56fc..c2c36389 100644 --- a/apps/orders/src/app/orders/orders.service.ts +++ b/apps/orders/src/app/orders/orders.service.ts @@ -10,8 +10,7 @@ import { ClientProxy } from '@nestjs/microservices'; import { InjectModel } from '@nestjs/mongoose'; import { OryPermissionsService } from '@ticketing/microservices/ory-client'; import { - OrderCancelledEvent, - OrderCreatedEvent, + EventsMap, Patterns, PaymentCreatedEvent, } from '@ticketing/microservices/shared/events'; @@ -56,9 +55,12 @@ export class OrdersService { ); } - private sendEvent( - pattern: Patterns.OrderCreated | Patterns.OrderCancelled, - event: OrderCreatedEvent['data'] | OrderCancelledEvent['data'], + private sendEvent< + P extends Patterns.OrderCreated | Patterns.OrderCancelled, + E extends EventsMap[P], + >( + pattern: P, + event: E, ): Observable<[Ticket, { ok: boolean }, { ok: boolean }]> { return zip( this.ticketsClient.send(pattern, event).pipe(timeout(5000)), diff --git a/apps/orders/src/app/tickets/tickets-ms.controller.ts b/apps/orders/src/app/tickets/tickets-ms.controller.ts index 7ef24ebc..140b412a 100644 --- a/apps/orders/src/app/tickets/tickets-ms.controller.ts +++ b/apps/orders/src/app/tickets/tickets-ms.controller.ts @@ -13,9 +13,8 @@ import { Transport, } from '@nestjs/microservices'; import { ApiExcludeEndpoint } from '@nestjs/swagger'; -import { Patterns } from '@ticketing/microservices/shared/events'; +import { EventsMap, Patterns } from '@ticketing/microservices/shared/events'; import { requestValidationErrorFactory } from '@ticketing/shared/errors'; -import { Ticket } from '@ticketing/shared/models'; import type { Channel } from 'amqp-connection-manager'; import type { Message } from 'amqplib'; @@ -42,7 +41,7 @@ export class TicketsMSController { @MessagePattern(Patterns.TicketCreated, Transport.RMQ) async onCreated( @Payload(new ValidationPipe(validationPipeOptions)) - data: Ticket, + data: EventsMap[Patterns.TicketCreated], @Ctx() context: RmqContext, ): Promise { const channel = context.getChannelRef() as Channel; @@ -66,7 +65,7 @@ export class TicketsMSController { @MessagePattern(Patterns.TicketUpdated, Transport.RMQ) async onUpdated( @Payload(new ValidationPipe(validationPipeOptions)) - data: Ticket, + data: EventsMap[Patterns.TicketUpdated], @Ctx() context: RmqContext, ): Promise { const channel = context.getChannelRef() as Channel; diff --git a/apps/payments/src/app/orders/orders-ms.controller.ts b/apps/payments/src/app/orders/orders-ms.controller.ts index f14e05a7..c1dac634 100644 --- a/apps/payments/src/app/orders/orders-ms.controller.ts +++ b/apps/payments/src/app/orders/orders-ms.controller.ts @@ -7,8 +7,7 @@ import { Transport, } from '@nestjs/microservices'; import { ApiExcludeEndpoint } from '@nestjs/swagger'; -import { Patterns } from '@ticketing/microservices/shared/events'; -import { Order } from '@ticketing/shared/models'; +import { EventsMap, Patterns } from '@ticketing/microservices/shared/events'; import type { Channel } from 'amqp-connection-manager'; import type { Message } from 'amqplib'; @@ -25,7 +24,7 @@ export class OrdersMSController { @ApiExcludeEndpoint() @MessagePattern(Patterns.OrderCreated, Transport.RMQ) async onCreated( - @Payload() data: Order, + @Payload() data: EventsMap[Patterns.OrderCreated], @Ctx() context: RmqContext, ): Promise<{ ok: boolean; @@ -49,7 +48,7 @@ export class OrdersMSController { @ApiExcludeEndpoint() @MessagePattern(Patterns.OrderCancelled, Transport.RMQ) async onCancelled( - @Payload() data: Order, + @Payload() data: EventsMap[Patterns.OrderCancelled], @Ctx() context: RmqContext, ): Promise<{ ok: boolean; diff --git a/apps/tickets/src/app/orders/orders-ms.controller.ts b/apps/tickets/src/app/orders/orders-ms.controller.ts index e5075c1e..f566bfec 100644 --- a/apps/tickets/src/app/orders/orders-ms.controller.ts +++ b/apps/tickets/src/app/orders/orders-ms.controller.ts @@ -13,9 +13,8 @@ import { Transport, } from '@nestjs/microservices'; import { ApiExcludeEndpoint } from '@nestjs/swagger'; -import { Patterns } from '@ticketing/microservices/shared/events'; +import { EventsMap, Patterns } from '@ticketing/microservices/shared/events'; import { requestValidationErrorFactory } from '@ticketing/shared/errors'; -import { Order } from '@ticketing/shared/models'; import type { Channel } from 'amqp-connection-manager'; import type { Message } from 'amqplib'; @@ -42,7 +41,7 @@ export class OrdersMSController { @MessagePattern(Patterns.OrderCreated, Transport.RMQ) async onCreated( @Payload(new ValidationPipe(validationPipeOptions)) - data: Order, + data: EventsMap[Patterns.OrderCreated], @Ctx() context: RmqContext, ): Promise { const channel = context.getChannelRef() as Channel; @@ -66,7 +65,7 @@ export class OrdersMSController { @MessagePattern(Patterns.OrderCancelled, Transport.RMQ) async onCancelled( @Payload(new ValidationPipe(validationPipeOptions)) - data: Order, + data: EventsMap[Patterns.OrderCancelled], @Ctx() context: RmqContext, ): Promise { const channel = context.getChannelRef() as Channel; diff --git a/apps/tickets/src/app/tickets/tickets.service.ts b/apps/tickets/src/app/tickets/tickets.service.ts index da397a49..ee51d60b 100644 --- a/apps/tickets/src/app/tickets/tickets.service.ts +++ b/apps/tickets/src/app/tickets/tickets.service.ts @@ -9,11 +9,10 @@ import { ClientProxy } from '@nestjs/microservices'; import { InjectModel } from '@nestjs/mongoose'; import { OryPermissionsService } from '@ticketing/microservices/ory-client'; import { + EventsMap, OrderCancelledEvent, OrderCreatedEvent, Patterns, - TicketCreatedEvent, - TicketUpdatedEvent, } from '@ticketing/microservices/shared/events'; import { NextPaginationDto, @@ -45,10 +44,10 @@ export class TicketsService { @Inject(ORDERS_CLIENT) private readonly client: ClientProxy, ) {} - private sendEvent( - pattern: Patterns.TicketCreated | Patterns.TicketUpdated, - event: TicketCreatedEvent['data'] | TicketUpdatedEvent['data'], - ): Observable { + private sendEvent< + P extends Patterns.TicketCreated | Patterns.TicketUpdated, + E extends EventsMap[P], + >(pattern: P, event: E): Observable { return this.client.send(pattern, event).pipe(timeout(5000)); } @@ -189,7 +188,6 @@ export class TicketsService { async cancelOrder(event: OrderCancelledEvent['data']): Promise { const ticketId = event.ticket.id; - await using manager = await transactionManager(this.ticketModel); const result = await manager.wrap(async (session) => { const ticket = await this.ticketModel diff --git a/libs/microservices/shared/events/src/events-map.ts b/libs/microservices/shared/events/src/events-map.ts index 1213ce18..2e9f8274 100644 --- a/libs/microservices/shared/events/src/events-map.ts +++ b/libs/microservices/shared/events/src/events-map.ts @@ -5,10 +5,10 @@ import { PaymentCreatedEvent } from './payment-events'; import { TicketCreatedEvent, TicketUpdatedEvent } from './ticket-events'; export type EventsMap = { - [Patterns.TicketCreated]: TicketCreatedEvent; - [Patterns.TicketUpdated]: TicketUpdatedEvent; - [Patterns.OrderCreated]: OrderCreatedEvent; - [Patterns.OrderCancelled]: OrderCancelledEvent; - [Patterns.ExpirationCompleted]: ExpirationCompletedEvent; - [Patterns.PaymentCreated]: PaymentCreatedEvent; + [Patterns.TicketCreated]: TicketCreatedEvent['data']; + [Patterns.TicketUpdated]: TicketUpdatedEvent['data']; + [Patterns.OrderCreated]: OrderCreatedEvent['data']; + [Patterns.OrderCancelled]: OrderCancelledEvent['data']; + [Patterns.ExpirationCompleted]: ExpirationCompletedEvent['data']; + [Patterns.PaymentCreated]: PaymentCreatedEvent['data']; };