/* Shape of the acquisition collections, so the tools match reality. */ import mongoose from 'mongoose'; import { connectMongo } from '../src/data/mongo.js'; await connectMongo(); const db = mongoose.connection.db; async function probe(name, { distinct = [], dateField } = {}) { const col = db.collection(name); const n = await col.estimatedDocumentCount(); console.log(`\n${'='.repeat(60)}\n${name} — ${n} docs`); const doc = await col.findOne({}, { sort: { _id: -1 } }); if (!doc) return; console.log('newest doc:'); for (const [k, v] of Object.entries(doc)) { if (k === '__v') continue; const s = v == null ? 'null' : typeof v === 'object' ? JSON.stringify(v).slice(0, 90) : String(v).slice(0, 90); console.log(` ${k.padEnd(20)} ${s}`); } for (const f of distinct) { const vals = await col.aggregate([ { $group: { _id: `$${f}`, n: { $sum: 1 } } }, { $sort: { n: -1 } }, { $limit: 8 }, ]).toArray(); console.log(` ${f}: ` + vals.map((v) => `${v._id}=${v.n}`).join(' ')); } if (dateField) { const [first] = await col.find({}, { sort: { [dateField]: 1 }, limit: 1, projection: { [dateField]: 1 } }).toArray(); const [last] = await col.find({}, { sort: { [dateField]: -1 }, limit: 1, projection: { [dateField]: 1 } }).toArray(); console.log(` ${dateField}: ${first?.[dateField]} → ${last?.[dateField]}`); } } await probe('capievents', { distinct: ['status', 'event_name', 'stage'], dateField: 'createdAt' }); await probe('instaadleads', { distinct: ['platform', 'campaign_name', 'is_organic'], dateField: 'created_time' }); await probe('commentleads', { distinct: ['source'], dateField: 'createdAt' }); await probe('whatsapptemplleads', { distinct: ['button_text'], dateField: 'createdAt' }); // CAPI failures are the whole point — what do they look like? const errs = await db.collection('capievents').aggregate([ { $match: { status: { $ne: 'success' } } }, { $group: { _id: { status: '$status', error: '$error' }, n: { $sum: 1 } } }, { $sort: { n: -1 } }, { $limit: 6 }, ]).toArray(); console.log('\nCAPI non-success breakdown:'); for (const e of errs) console.log(` ${e.n.toString().padStart(5)} status=${e._id.status} error=${String(e._id.error).slice(0, 80)}`); process.exit(0);