Skip to content

Commit

Permalink
Merge pull request #228 from alwinsimon/v1
Browse files Browse the repository at this point in the history
V1 - Completed Basic Back-end Implementation.
  • Loading branch information
alwinsimon authored Oct 26, 2023
2 parents 27023b9 + ae9b4de commit 28c099e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
5 changes: 5 additions & 0 deletions orders/src/events/listeners/expiration-complete-listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ export class OrderExpirationListener extends Listener<OrderExpiredEvent> {
throw new Error("Order not found !!!");
}

// If the order is already paid and marked complete, return early and acknowledge the event to NATS.
if (order.status === OrderStatus.Complete) {
return msg.ack();
}

order.set({ status: OrderStatus.Cancelled });
await order.save();

Expand Down
39 changes: 39 additions & 0 deletions orders/src/events/listeners/payment-created-listener.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Message } from "node-nats-streaming";
import {
Listener,
EventSubjects,
PaymentCreatedEvent,
NotFoundError,
OrderStatus,
} from "@bookmyseat/common";

import { queueGroupName } from "../order-service-queue-group-name";
import { Order } from "../../models/order";

export class PaymentCreatedListener extends Listener<PaymentCreatedEvent> {
readonly subject = EventSubjects.PaymentCreated;

queueGroupName = queueGroupName;

async onMessage(data: PaymentCreatedEvent["data"], msg: Message) {
try {
// Destructure the ticket id, title, and price from data argument.
const { id, version, orderId, stripeId } = data;

const order = await Order.findById(data.orderId);

if (!order) {
throw new NotFoundError();
}

order.set({ status: OrderStatus.Complete });

await order.save();

// Acknowledge the Payment Created event to NATS server.
msg.ack();
} catch (error) {
console.error("Error processing PaymentCreatedEvent", error);
}
}
}
2 changes: 2 additions & 0 deletions orders/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { natsClient } from "./nats-client";
import { TicketCreatedListener } from "./events/listeners/ticket-created-listener";
import { TicketUpdatedListener } from "./events/listeners/ticket-updated-listener";
import { OrderExpirationListener } from "./events/listeners/expiration-complete-listener";
import { PaymentCreatedListener } from "./events/listeners/payment-created-listener";

const startServer = async () => {
// Server Configuration
Expand Down Expand Up @@ -84,6 +85,7 @@ const startServer = async () => {
new TicketCreatedListener(natsClient.client).listen();
new TicketUpdatedListener(natsClient.client).listen();
new OrderExpirationListener(natsClient.client).listen();
new PaymentCreatedListener(natsClient.client).listen();
} catch (err) {
console.error(
`Error Connecting ${SERVICE_NAME} Service to NATS CLUSTER: ${NATS_CLUSTER_ID}:`,
Expand Down

0 comments on commit 28c099e

Please sign in to comment.