This repository was archived by the owner on Mar 21, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubscriptions.ts
More file actions
50 lines (46 loc) · 1.74 KB
/
subscriptions.ts
File metadata and controls
50 lines (46 loc) · 1.74 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
import { z } from "zod";
import { observable } from "@trpc/server/observable";
import { nfcScanInputSchema } from "@hackz/shared";
import type { ProjectorEvent, SessionEvent } from "@hackz/shared";
import { publicProcedure, protectedProcedure, router } from "../trpc";
import { ee, emitProjectorEvent } from "../ee";
import { createDynamoDBUserRepository } from "../../repositories/dynamodb/user-repository";
const userRepo = createDynamoDBUserRepository();
export const subscriptionRouter = router({
// Projector room: NFC scans + gacha results
onProjector: publicProcedure.subscription(() =>
observable<ProjectorEvent>((emit) => {
const handler = (event: ProjectorEvent) => emit.next(event);
ee.on("projector", handler);
return () => {
ee.off("projector", handler);
};
}),
),
// Session-specific: session updates + synthesis completion
onSession: protectedProcedure
.input(z.object({ sessionId: z.string() }))
.subscription(({ input }) =>
observable<SessionEvent>((emit) => {
const key = `session:${input.sessionId}`;
const handler = (event: SessionEvent) => emit.next(event);
ee.on(key, handler);
return () => {
ee.off(key, handler);
};
}),
),
// Admin action: report NFC scan (replaces Socket.IO "nfc:scan" client event)
nfcScan: protectedProcedure
.input(nfcScanInputSchema)
.output(z.object({ success: z.boolean() }))
.mutation(async ({ input }) => {
const user = await userRepo.findByNfcId(input.nfcId);
emitProjectorEvent({
type: "nfc:scanned",
userId: user?.id ?? input.nfcId,
userName: user?.name ?? `User-${input.nfcId.slice(0, 6)}`,
});
return { success: true };
}),
});