|
| 1 | +import type { AnalyticsService } from "../../../lib/analytics"; |
| 2 | +import type { EventBus } from "../../../lib/event-bus"; |
| 3 | + |
| 4 | +import { makeWriteEvent } from "./utils"; |
| 5 | + |
| 6 | +export function registerAssistPoolSubscribers( |
| 7 | + events: EventBus, |
| 8 | + analytics: AnalyticsService, |
| 9 | +): void { |
| 10 | + const write = makeWriteEvent(analytics); |
| 11 | + |
| 12 | + events.on("assist_pool.instance_created", (p) => { |
| 13 | + write({ |
| 14 | + orgId: p.organizationId, |
| 15 | + // `endUserId` is the pool owner — the one who "initiated" it. |
| 16 | + endUserId: p.endUserId, |
| 17 | + event: "assist_pool.instance_created", |
| 18 | + source: "assist-pool", |
| 19 | + amount: p.targetAmount, |
| 20 | + eventData: { |
| 21 | + configId: p.configId, |
| 22 | + instanceId: p.instanceId, |
| 23 | + expiresAt: p.expiresAt, |
| 24 | + }, |
| 25 | + }); |
| 26 | + }); |
| 27 | + |
| 28 | + events.on("assist_pool.contributed", (p) => { |
| 29 | + write({ |
| 30 | + orgId: p.organizationId, |
| 31 | + // Session actor = the contributor (initiator), not the pool owner. |
| 32 | + // Keeps per-user contribution timeseries queryable. |
| 33 | + endUserId: p.initiatorEndUserId, |
| 34 | + event: "assist_pool.contributed", |
| 35 | + source: "assist-pool", |
| 36 | + amount: p.amount, |
| 37 | + eventData: { |
| 38 | + configId: p.configId, |
| 39 | + instanceId: p.instanceId, |
| 40 | + poolOwnerEndUserId: p.endUserId, |
| 41 | + remaining: p.remaining, |
| 42 | + }, |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + events.on("assist_pool.completed", (p) => { |
| 47 | + write({ |
| 48 | + orgId: p.organizationId, |
| 49 | + endUserId: p.endUserId, |
| 50 | + event: "assist_pool.completed", |
| 51 | + source: "assist-pool", |
| 52 | + eventData: { |
| 53 | + configId: p.configId, |
| 54 | + instanceId: p.instanceId, |
| 55 | + rewards: p.rewards, |
| 56 | + }, |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + events.on("assist_pool.expired", (p) => { |
| 61 | + write({ |
| 62 | + orgId: p.organizationId, |
| 63 | + endUserId: p.endUserId, |
| 64 | + event: "assist_pool.expired", |
| 65 | + // `outcome` stays "ok" — expiry is a normal lifecycle terminal |
| 66 | + // state, not a system error. The reason is queryable via |
| 67 | + // `event_data.reason` ("timeout" | "force"). |
| 68 | + source: "assist-pool", |
| 69 | + eventData: { |
| 70 | + configId: p.configId, |
| 71 | + instanceId: p.instanceId, |
| 72 | + reason: p.reason, |
| 73 | + }, |
| 74 | + }); |
| 75 | + }); |
| 76 | +} |
0 commit comments