// 一次性清理:上线前自测累积的数据(课堂/排期/孤儿自习/测试申请与预约)。 // 大师课堂/排期是本轮新功能,目前库里全部课堂与排期均为自测生成 → 清空重置为干净初始态。 // 大师本体(masters)与真实用户分身(twins)一律不动。 // 用法:node scripts/cleanup-test-data.js import 'dotenv/config'; import { connectDb, getDb, closeDb } from '../src/db.js'; // 测试用的申请/预约按提交名清理(都是自测占位)。 const TEST_NAME = /QA|测试|验证|探测|阿测|走查|截图|类别验证|排期|徽匠老王|小林|徽州阿测/i; async function main() { await connectDb(); const db = getDb(); // 1) 课堂 + 排期:本功能刚上线,库里全是自测数据 → 清空。 const classrooms = (await db.collection('classrooms').deleteMany({})).deletedCount; const schedules = (await db.collection('class_schedules').deleteMany({})).deletedCount; // 2) 孤儿自习记录:twinId 指向已不存在的分身(历史级联漏删留下的)。 const liveTwins = new Set( (await db.collection('twins').find({}).project({ _id: 1 }).toArray()).map((t) => String(t._id)) ); const learnings = await db.collection('twin_learnings').find({}).project({ _id: 1, twinId: 1 }).toArray(); const orphanIds = learnings.filter((l) => !liveTwins.has(String(l.twinId))).map((l) => l._id); const orphanLearnings = orphanIds.length ? (await db.collection('twin_learnings').deleteMany({ _id: { $in: orphanIds } })).deletedCount : 0; // 3) 测试用的大师/合作申请、文旅预约(按测试名)。 const masterApps = (await db.collection('master_applications').deleteMany({ name: TEST_NAME })).deletedCount; const partnerApps = (await db.collection('partner_applications').deleteMany({ name: TEST_NAME })).deletedCount; const bookings = (await db.collection('bookings').deleteMany({ name: TEST_NAME })).deletedCount; // 4) 孤儿通知:userId 指向已不存在的用户。 const liveUsers = new Set( (await db.collection('users').find({}).project({ _id: 1 }).toArray()).map((u) => String(u._id)) ); const notifs = await db.collection('notifications').find({}).project({ _id: 1, userId: 1 }).toArray(); const orphanNotifIds = notifs.filter((n) => !liveUsers.has(String(n.userId))).map((n) => n._id); const orphanNotifs = orphanNotifIds.length ? (await db.collection('notifications').deleteMany({ _id: { $in: orphanNotifIds } })).deletedCount : 0; console.log('[cleanup] done:', JSON.stringify({ classrooms, schedules, orphanLearnings, masterApps, partnerApps, bookings, orphanNotifs, })); await closeDb(); } main().catch((e) => { console.error('[cleanup] failed:', e); process.exit(1); });