Orders Service Events
? back to top
创建 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
实现 Publishers
import { Publisher, OrderCreatedEvent, Subjects } from '@heysirius-common/common';
export class OrderCreatedPublisher extends Publisher<OrderCreatedEvent> {
subject: Subjects.OrderCreated = Subjects.OrderCreated;
}
import { Subjects, Publisher, OrderCancelledEvent } from '@heysirius-common/common';
export class OrderCancelledPublisher extends Publisher<OrderCancelledEvent> {
subject: Subjects.OrderCancelled = Subjects.OrderCancelled;
}
? back to top
Publishing Order Creation Event
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 Event
new OrderCancelledPublisher(natsWrapper.client).publish({
id: order.id,
ticket: {
id: order.ticket.id,
},
});
? back to top
测试 Event Publishing
? back to top
|