-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevents.ts
More file actions
63 lines (58 loc) · 2.03 KB
/
events.ts
File metadata and controls
63 lines (58 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import type { RedisClient } from "../storage/redis.js";
import type { AuctionRoundStatus, AuctionStatus } from "../storage/mongoSchemas.js";
export const realtimeEventChannel = "realtime:events";
export type RealtimeAuctionSnapshot = {
auctionId: string;
status: AuctionStatus;
title: string;
currency: string;
currentRoundIndex: number | null;
roundStatus: AuctionRoundStatus | null;
roundEffectiveEndAt: string | null;
roundLastBidAt: string | null;
lastBidAmount: number | null;
updatedAt: string;
serverTime: string;
};
export type RealtimeEvent =
| { type: "auction.list.updated"; reason?: string; auctionId?: string }
| { type: "auction.snapshot.updated"; auctionId: string; snapshot: RealtimeAuctionSnapshot }
| { type: "auction.bids.updated"; auctionId: string }
| { type: "bids.active.updated"; userIds: string[] }
| { type: "balance.updated"; userIds: string[]; currency?: string };
export function toRealtimeSnapshot(input: {
auctionId: string;
status: AuctionStatus;
title: string;
currency: string;
currentRoundIndex: number | null;
roundStatus: AuctionRoundStatus | null;
roundEffectiveEndAt: Date | null;
roundLastBidAt: Date | null;
lastBidAmount: number | null;
updatedAt: Date;
serverTime?: Date;
}): RealtimeAuctionSnapshot {
const serverTime = input.serverTime ?? new Date();
return {
auctionId: input.auctionId,
status: input.status,
title: input.title,
currency: input.currency,
currentRoundIndex: input.currentRoundIndex,
roundStatus: input.roundStatus,
roundEffectiveEndAt: input.roundEffectiveEndAt
? input.roundEffectiveEndAt.toISOString()
: null,
roundLastBidAt: input.roundLastBidAt ? input.roundLastBidAt.toISOString() : null,
lastBidAmount: input.lastBidAmount,
updatedAt: input.updatedAt.toISOString(),
serverTime: serverTime.toISOString()
};
}
export async function publishRealtimeEvent(
redis: RedisClient,
event: RealtimeEvent
): Promise<void> {
await redis.publish(realtimeEventChannel, JSON.stringify(event));
}