Skip to content

Latest commit

 

History

History
111 lines (88 loc) · 2.58 KB

section-18.md

File metadata and controls

111 lines (88 loc) · 2.58 KB

Section 18: Understanding Event Flow

Table of Contents

Orders Service Events

⬆ back to top

Creating the Events

import { Subjects } from './subjects';
import { OrderStatus } from './types/order-status';

export interface OrderCreatedEvent {
  subject: Subjects.OrderCreated;
  data: {
    id: string;
    status: OrderStatus;
    userId: string;
    expiresAt: string;
    ticket: {
      id: string;
      price: number;
    };
  };
}
import { Subjects } from './subjects';

export interface OrderCancelledEvent {
  subject: Subjects.OrderCancelled;
  data: {
    id: string;
    ticket: {
      id: string;
    };
  };
}

⬆ back to top

Implementing the Publishers

import { Publisher, OrderCreatedEvent, Subjects } from '@chticketing/common';

export class OrderCreatedPublisher extends Publisher<OrderCreatedEvent> {
  subject: Subjects.OrderCreated = Subjects.OrderCreated;
}
import { Subjects, Publisher, OrderCancelledEvent } from '@chticketing/common';

export class OrderCancelledPublisher extends Publisher<OrderCancelledEvent> {
  subject: Subjects.OrderCancelled = Subjects.OrderCancelled;
}

⬆ back to top

Publishing the Order Creation

  // Publish an event saying that an order was created
  new OrderCreatedPublisher(natsWrapper.client).publish({
    id: order.id,
    status: order.status,
    userId: order.userId,
    expiresAt: order.expiresAt.toISOString(),
    ticket: {
      id: ticket.id,
      price: ticket.price,
    },
  });

⬆ back to top

Publishing Order Cancellation

  // publishing an event saying this was cancelled!
  new OrderCancelledPublisher(natsWrapper.client).publish({
    id: order.id,
    ticket: {
      id: order.ticket.id,
    },
  });

⬆ back to top

Testing Event Publishing

⬆ back to top