|
| 1 | +/** |
| 2 | + * Dual-write to unified events collection (API layer) |
| 3 | + * |
| 4 | + * When USE_UNIFIED_EVENTS_COLLECTIONS=true, duplicates updates to the unified |
| 5 | + * events collection. Errors are logged, not thrown. |
| 6 | + * |
| 7 | + * @see docs/mongodb-unified-collections/ |
| 8 | + */ |
| 9 | + |
| 10 | +const { ObjectId } = require('mongodb'); |
| 11 | +const { incrementDualWriteFailure } = require('../../metrics/dualWrite'); |
| 12 | + |
| 13 | +const EVENTS_COLLECTION = 'events'; |
| 14 | + |
| 15 | +function isUnifiedEnabled() { |
| 16 | + return process.env.USE_UNIFIED_EVENTS_COLLECTIONS === 'true'; |
| 17 | +} |
| 18 | + |
| 19 | +function getEventsDb() { |
| 20 | + const mongo = require('../../mongo'); |
| 21 | + return mongo.databases?.events; |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * Add user to visitedBy in unified events collection |
| 26 | + * |
| 27 | + * @param {string} projectId - project ObjectId |
| 28 | + * @param {string} eventId - event ObjectId |
| 29 | + * @param {string} userId - user ObjectId |
| 30 | + */ |
| 31 | +async function visitEventUnified(projectId, eventId, userId) { |
| 32 | + if (!isUnifiedEnabled()) return; |
| 33 | + |
| 34 | + try { |
| 35 | + const db = getEventsDb(); |
| 36 | + if (!db) return; |
| 37 | + |
| 38 | + await db.collection(EVENTS_COLLECTION).updateOne( |
| 39 | + { projectId: new ObjectId(projectId), _id: new ObjectId(eventId) }, |
| 40 | + { $addToSet: { visitedBy: new ObjectId(userId) } } |
| 41 | + ); |
| 42 | + } catch (err) { |
| 43 | + incrementDualWriteFailure('events'); |
| 44 | + console.error('[dualWrite] visitEventUnified failed', { projectId, eventId, err }); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Toggle event mark (resolved, ignored, starred) in unified events collection |
| 50 | + * |
| 51 | + * @param {string} projectId - project ObjectId |
| 52 | + * @param {string} eventId - event ObjectId |
| 53 | + * @param {string} mark - mark name (e.g. 'resolved', 'ignored', 'starred') |
| 54 | + * @param {boolean} isUnset - if true, remove mark; if false, set mark with timestamp |
| 55 | + */ |
| 56 | +async function toggleEventMarkUnified(projectId, eventId, mark, isUnset) { |
| 57 | + if (!isUnifiedEnabled()) return; |
| 58 | + |
| 59 | + try { |
| 60 | + const db = getEventsDb(); |
| 61 | + if (!db) return; |
| 62 | + |
| 63 | + const markKey = `marks.${mark}`; |
| 64 | + const update = isUnset |
| 65 | + ? { $unset: { [markKey]: '' } } |
| 66 | + : { $set: { [markKey]: Math.floor(Date.now() / 1000) } }; |
| 67 | + |
| 68 | + await db.collection(EVENTS_COLLECTION).updateOne( |
| 69 | + { projectId: new ObjectId(projectId), _id: new ObjectId(eventId) }, |
| 70 | + update |
| 71 | + ); |
| 72 | + } catch (err) { |
| 73 | + incrementDualWriteFailure('events'); |
| 74 | + console.error('[dualWrite] toggleEventMarkUnified failed', { projectId, eventId, mark, err }); |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +/** |
| 79 | + * Update assignee in unified events collection |
| 80 | + * |
| 81 | + * @param {string} projectId - project ObjectId |
| 82 | + * @param {string} eventId - event ObjectId |
| 83 | + * @param {string} assignee - assignee user id or '' to unassign |
| 84 | + */ |
| 85 | +async function updateAssigneeUnified(projectId, eventId, assignee) { |
| 86 | + if (!isUnifiedEnabled()) return; |
| 87 | + |
| 88 | + try { |
| 89 | + const db = getEventsDb(); |
| 90 | + if (!db) return; |
| 91 | + |
| 92 | + await db.collection(EVENTS_COLLECTION).updateOne( |
| 93 | + { projectId: new ObjectId(projectId), _id: new ObjectId(eventId) }, |
| 94 | + { $set: { assignee } } |
| 95 | + ); |
| 96 | + } catch (err) { |
| 97 | + incrementDualWriteFailure('events'); |
| 98 | + console.error('[dualWrite] updateAssigneeUnified failed', { projectId, eventId, err }); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +module.exports = { |
| 103 | + visitEventUnified, |
| 104 | + toggleEventMarkUnified, |
| 105 | + updateAssigneeUnified, |
| 106 | +}; |
0 commit comments