|
| 1 | +/** |
| 2 | + * @file Dual-write helper for unified events/repetitions collections |
| 3 | + * |
| 4 | + * Writes to unified collections (events, repetitions) with projectId. |
| 5 | + * Used when USE_UNIFIED_EVENTS_COLLECTIONS=true. |
| 6 | + * Errors are logged, not thrown — dual-write must not break main flow. |
| 7 | + * |
| 8 | + * @see docs/mongodb-unified-collections/ |
| 9 | + */ |
| 10 | + |
| 11 | +import { ObjectId } from 'mongodb'; |
| 12 | +import type { Db } from 'mongodb'; |
| 13 | +import { incrementDualWriteFailure } from '../../metrics/dualWrite'; |
| 14 | + |
| 15 | +const EVENTS_COLLECTION = 'events'; |
| 16 | +const REPETITIONS_COLLECTION = 'repetitions'; |
| 17 | + |
| 18 | +let _projectIdsCache: Set<string> | null = null; |
| 19 | + |
| 20 | +function getUnifiedProjectIds(): Set<string> { |
| 21 | + if (_projectIdsCache !== null) return _projectIdsCache; |
| 22 | + const raw = process.env.UNIFIED_EVENTS_PROJECT_IDS ?? ''; |
| 23 | + const ids = raw.split(',').map((id) => id.trim()).filter(Boolean); |
| 24 | + _projectIdsCache = new Set(ids); |
| 25 | + return _projectIdsCache; |
| 26 | +} |
| 27 | + |
| 28 | +function isUnifiedEnabledForProject(projectId: string): boolean { |
| 29 | + if (process.env.USE_UNIFIED_EVENTS_COLLECTIONS !== 'true') return false; |
| 30 | + const ids = getUnifiedProjectIds(); |
| 31 | + if (ids.size === 0) return false; |
| 32 | + return ids.has(projectId); |
| 33 | +} |
| 34 | + |
| 35 | +function toObjectId(projectId: string): ObjectId { |
| 36 | + return new ObjectId(projectId); |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * Insert event into unified events collection with projectId |
| 41 | + * |
| 42 | + * @param db - MongoDB Db instance (hawk_events) |
| 43 | + * @param projectId - project ObjectId as string |
| 44 | + * @param doc - event document (without projectId) |
| 45 | + * @param insertedId - _id from original insert (to keep consistency) |
| 46 | + */ |
| 47 | +export async function insertEventUnified( |
| 48 | + db: Db, |
| 49 | + projectId: string, |
| 50 | + doc: Record<string, unknown>, |
| 51 | + insertedId: ObjectId |
| 52 | +): Promise<void> { |
| 53 | + if (!isUnifiedEnabledForProject(projectId)) return; |
| 54 | + |
| 55 | + try { |
| 56 | + const unifiedDoc = { |
| 57 | + ...doc, |
| 58 | + projectId: toObjectId(projectId), |
| 59 | + _id: insertedId, |
| 60 | + }; |
| 61 | + await db.collection(EVENTS_COLLECTION).insertOne(unifiedDoc); |
| 62 | + } catch { |
| 63 | + incrementDualWriteFailure('events'); |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +/** |
| 68 | + * Insert repetition into unified repetitions collection with projectId |
| 69 | + * |
| 70 | + * @param db - MongoDB Db instance (hawk_events) |
| 71 | + * @param projectId - project ObjectId as string |
| 72 | + * @param doc - repetition document (without projectId) |
| 73 | + * @param insertedId - _id from original insert (to keep consistency) |
| 74 | + */ |
| 75 | +export async function insertRepetitionUnified( |
| 76 | + db: Db, |
| 77 | + projectId: string, |
| 78 | + doc: Record<string, unknown>, |
| 79 | + insertedId: ObjectId |
| 80 | +): Promise<void> { |
| 81 | + if (!isUnifiedEnabledForProject(projectId)) return; |
| 82 | + |
| 83 | + try { |
| 84 | + const unifiedDoc = { |
| 85 | + ...doc, |
| 86 | + projectId: toObjectId(projectId), |
| 87 | + _id: insertedId, |
| 88 | + }; |
| 89 | + await db.collection(REPETITIONS_COLLECTION).insertOne(unifiedDoc); |
| 90 | + } catch { |
| 91 | + incrementDualWriteFailure('repetitions'); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +/** |
| 96 | + * Ensure event exists in unified collection, then increment counter. |
| 97 | + * Used when processing repetitions for events created before dual-write was enabled. |
| 98 | + */ |
| 99 | +export async function ensureEventAndIncrementUnified( |
| 100 | + db: Db, |
| 101 | + projectId: string, |
| 102 | + groupHash: string, |
| 103 | + eventDoc: Record<string, unknown>, |
| 104 | + incrementAffected: boolean |
| 105 | +): Promise<void> { |
| 106 | + if (!isUnifiedEnabledForProject(projectId)) return; |
| 107 | + |
| 108 | + const projectIdObj = toObjectId(projectId); |
| 109 | + const collection = db.collection(EVENTS_COLLECTION); |
| 110 | + |
| 111 | + try { |
| 112 | + const existing = await collection.findOne({ projectId: projectIdObj, groupHash }); |
| 113 | + if (!existing) { |
| 114 | + const unifiedDoc = { ...eventDoc, projectId: projectIdObj }; |
| 115 | + await collection.insertOne(unifiedDoc); |
| 116 | + } |
| 117 | + |
| 118 | + const updateQuery = incrementAffected |
| 119 | + ? { $inc: { totalCount: 1, usersAffected: 1 } } |
| 120 | + : { $inc: { totalCount: 1 } }; |
| 121 | + |
| 122 | + await collection.updateOne( |
| 123 | + { projectId: projectIdObj, groupHash }, |
| 124 | + updateQuery |
| 125 | + ); |
| 126 | + } catch { |
| 127 | + incrementDualWriteFailure('events'); |
| 128 | + } |
| 129 | +} |
0 commit comments