@@ -18,6 +18,8 @@ var requestEvent = require("../../event/requestEvent");
1818var messageEvent = require ( "../../event/messageEvent" ) ;
1919var authEvent = require ( "../../event/authEvent" ) ;
2020var webhookEvent = require ( "../../event/webhookEvent" ) ;
21+ var kbEvent = require ( "../../event/kbEvent" ) ;
22+ var departmentEvent = require ( "../../event/departmentEvent" ) ;
2123var { track } = require ( "../../lib/analyticsClient" ) ;
2224
2325// ─── helpers ────────────────────────────────────────────────────────────────
@@ -202,54 +204,7 @@ function listen() {
202204 } ) ;
203205 } ) ;
204206
205- // ── 5. handover_to_human ──────────────────────────────────────────────────
206- // Contract: packages/contracts/src/payloads/handover-to-human.ts
207- // id_request string (required)
208- // human_id string|null
209- // reason string|null
210- // department_id string|null
211- // waiting_time_seconds number int>=0 | null
212- // agent_id string|null (optional)
213- // trigger_intent string|null (optional)
214- requestEvent . on ( "request.participants.update" , function ( data ) {
215- var request = data . request || { } ;
216- var removedParticipants = data . removedParticipants || [ ] ;
217- var addedParticipants = data . addedParticipants || [ ] ;
218-
219- var botRemoved = removedParticipants . some ( function ( p ) {
220- return p . startsWith ( "bot_" ) ;
221- } ) ;
222- var humanAdded = addedParticipants . some ( function ( p ) {
223- return ! p . startsWith ( "bot_" ) ;
224- } ) ;
225- if ( ! botRemoved || ! humanAdded ) return ;
226-
227- var botId =
228- removedParticipants . find ( function ( p ) {
229- return p . startsWith ( "bot_" ) ;
230- } ) || null ;
231- var humanId =
232- addedParticipants . find ( function ( p ) {
233- return ! p . startsWith ( "bot_" ) ;
234- } ) || null ;
235-
236- var waitingTimeSecs = null ;
237- if ( request . waiting_time != null ) {
238- waitingTimeSecs = Math . round ( request . waiting_time / 1000 ) ;
239- }
240-
241- track ( "handover_to_human" , request . id_project , {
242- id_request : request . request_id || toStringId ( request ) ,
243- human_id : humanId ,
244- reason : null ,
245- department_id : departmentId ( request . department ) ,
246- waiting_time_seconds : waitingTimeSecs ,
247- agent_id : null ,
248- trigger_intent : null ,
249- } ) ;
250- } ) ;
251-
252- // ── 6. project_user.activated ─────────────────────────────────────────────
207+ // ── 5. project_user.activated ─────────────────────────────────────────────
253208 // Contract: packages/contracts/src/payloads/project-user-activated.ts
254209 // id_user string (required)
255210 // user_email string (required, email format — skip if unavailable)
@@ -365,6 +320,50 @@ function listen() {
365320 null ,
366321 } ) ;
367322 } ) ;
323+
324+ // ── 11. kb.metadata_updated ───────────────────────────────────────────────
325+ // Emitted by routes/kb.js on namespace create and update (rename).
326+ // kb_id = Namespace.id (logical string key, NOT the ObjectId _id)
327+ // kb_name = Namespace.name
328+ // The consumer writes these into the kb_dimensions ReplacingMergeTree table
329+ // so that dashboard queries always resolve the current KB name.
330+ kbEvent . on ( "kb.namespace.create" , function ( { savedNamespace, project_id } ) {
331+ if ( ! savedNamespace || ! project_id ) return ;
332+ track ( "kb.metadata_updated" , project_id , {
333+ kb_id : savedNamespace . id ,
334+ kb_name : savedNamespace . name ,
335+ } ) ;
336+ } ) ;
337+
338+ kbEvent . on ( "kb.namespace.update" , function ( { updatedNamespace } ) {
339+ if ( ! updatedNamespace || ! updatedNamespace . id_project ) return ;
340+ track ( "kb.metadata_updated" , updatedNamespace . id_project , {
341+ kb_id : updatedNamespace . id ,
342+ kb_name : updatedNamespace . name ,
343+ } ) ;
344+ } ) ;
345+
346+ // ── 12. department.metadata_updated ──────────────────────────────────────
347+ // Emitted by routes/department.js on department create, PUT, and PATCH.
348+ // department_id = Department._id.toString()
349+ // department_name = Department.name
350+ // The consumer writes these into the department_dimensions table so that
351+ // dashboard queries always resolve the current department name.
352+ departmentEvent . on ( "department.create" , function ( savedDepartment ) {
353+ if ( ! savedDepartment || ! savedDepartment . id_project ) return ;
354+ track ( "department.metadata_updated" , savedDepartment . id_project , {
355+ department_id : savedDepartment . _id . toString ( ) ,
356+ department_name : savedDepartment . name ,
357+ } ) ;
358+ } ) ;
359+
360+ departmentEvent . on ( "department.update" , function ( updatedDepartment ) {
361+ if ( ! updatedDepartment || ! updatedDepartment . id_project ) return ;
362+ track ( "department.metadata_updated" , updatedDepartment . id_project , {
363+ department_id : updatedDepartment . _id . toString ( ) ,
364+ department_name : updatedDepartment . name ,
365+ } ) ;
366+ } ) ;
368367}
369368
370369module . exports = { listen : listen } ;
0 commit comments