Skip to content

Commit

Permalink
refactor(payments): replace MessagePattern and nack without re-queue
Browse files Browse the repository at this point in the history
  • Loading branch information
getlarge committed Jan 7, 2024
1 parent 9dd05ca commit 4b6c43f
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 16 deletions.
9 changes: 4 additions & 5 deletions apps/expiration/src/app/orders/orders.processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,10 @@ export class OrdersProcessor {
const { data } = job;
this.logger.debug(`Expire order ${data.id}`);
await lastValueFrom(
this.client
.emit<
ExpirationCompletedEvent['name'],
ExpirationCompletedEvent['data']
>(Patterns.ExpirationCompleted, data)
this.client.emit<
ExpirationCompletedEvent['name'],
ExpirationCompletedEvent['data']
>(Patterns.ExpirationCompleted, data),
);
}

Expand Down
4 changes: 2 additions & 2 deletions apps/orders/test/orders.service.integration.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('OrdersService', () => {
ordersService = app.get(OrdersService);
orderModel = app.get<Model<OrderDocument>>(getModelToken(OrderSchema.name));
ticketModel = app.get<Model<TicketDocument>>(
getModelToken(TicketSchema.name)
getModelToken(TicketSchema.name),
);
});

Expand All @@ -66,7 +66,7 @@ describe('OrdersService', () => {
expect(updatedDBOrder.status).toBe(OrderStatus.Cancelled);
expect(ordersService['sendEvent']).toBeCalledWith(
Patterns.OrderCancelled,
updatedOrder
updatedOrder,
);
});
});
Expand Down
16 changes: 9 additions & 7 deletions apps/payments/src/app/orders/orders-ms.controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Controller, Inject, Logger } from '@nestjs/common';
import {
Ctx,
EventPattern,
MessagePattern,
Payload,
RmqContext,
Transport,
Expand All @@ -23,7 +23,7 @@ export class OrdersMSController {
) {}

@ApiExcludeEndpoint()
@EventPattern(Patterns.OrderCreated, Transport.RMQ)
@MessagePattern(Patterns.OrderCreated, Transport.RMQ)
async onCreated(
@Payload() data: Order,
@Ctx() context: RmqContext,
Expand All @@ -34,16 +34,17 @@ export class OrdersMSController {
this.logger.debug(`received message on ${pattern}`, {
data,
});
// TODO: conditional ack
try {
await this.ordersService.create(data);
} finally {
channel.ack(message);
} catch (e) {
channel.nack(message, false, false);
throw e;
}
}

@ApiExcludeEndpoint()
@EventPattern(Patterns.OrderCancelled, Transport.RMQ)
@MessagePattern(Patterns.OrderCancelled, Transport.RMQ)
async onCancelled(
@Payload() data: Order,
@Ctx() context: RmqContext,
Expand All @@ -54,11 +55,12 @@ export class OrdersMSController {
this.logger.debug(`received message on ${pattern}`, {
data,
});
// TODO: conditional ack
try {
await this.ordersService.cancel(data);
} finally {
channel.ack(message);
} catch (e) {
channel.nack(message, false, false);
throw e;
}
}
}
4 changes: 2 additions & 2 deletions apps/payments/src/app/payments/payments.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ export class PaymentsService {
);
await this.createRelationShip(relationTupleWithUser);

// 7. emit payment:create event
// 7. send payment:create event
await firstValueFrom(
this.client.emit<
this.client.send<
PaymentCreatedEvent['name'],
PaymentCreatedEvent['data']
>(Patterns.PaymentCreated, payment),
Expand Down

0 comments on commit 4b6c43f

Please sign in to comment.