Files
api_health/scripts/check_orphan_payments.js
lik f13af01a95 chore(schema): organization 移除医院相关定义并留档数据清理脚本
医院数据统一走 hospital 集合后,organization 移除 hospital 枚举、
level 与就医信息字段(overview/photos/departments/doctors/guides)。
新增一次性清理脚本(删医院记录、删 2026-11 前订单)与只读体检脚本
(索引同步、孤儿引用、脏数据检查)。
2026-11-06 17:49:03 +08:00

55 lines
2.5 KiB
JavaScript

/**
* 只读补查:孤儿 payment_record 明细 + user/users 集合归属确认。
* 用法(在 api/health 下):node scripts/check_orphan_payments.js
*/
import { DBModel } from "../models/index.js";
DBModel.init();
const conn = DBModel.dbConnection;
async function main() {
const db = conn.db;
// 找出 recordId 在 escort_record 中不存在的 payment_record
const orphanPayments = await db.collection("payment_record").aggregate([
{ $match: { recordId: { $ne: null } } },
{ $lookup: { from: "escort_record", localField: "recordId", foreignField: "_id", as: "hit" } },
{ $match: { hit: { $size: 0 } } },
]).toArray();
console.log(`orphan payment_record docs: ${orphanPayments.length}`);
for (const p of orphanPayments) {
console.log(` ${p._id} outTradeNo=${p.outTradeNo} status=${p.status} totalFee=${p.totalFee} payerTotal=${p.payerTotal} userId=${p.userId} createdAt=${p.createdAt?.toISOString?.() ?? p.createdAt}`);
const refunds = await db.collection("refund_record").find({ paymentId: p._id }).toArray();
refunds.forEach((r) => console.log(` refund: ${r._id} outRefundNo=${r.outRefundNo} status=${r.status} amount=${r.refundAmount}`));
}
// user / users 集合情况
for (const name of ["user", "users"]) {
const exists = await db.listCollections({ name }).hasNext();
if (!exists) {
console.log(`\ncollection ${name}: NOT EXIST`);
continue;
}
const n = await db.collection(name).countDocuments({});
const sample = await db.collection(name).findOne({}, { projection: { mobile: 1, phone: 1, name: 1, nickname: 1, createdAt: 1, "meta.createtime": 1 } });
console.log(`\ncollection ${name}: ${n} docs, sample=${JSON.stringify(sample)}`);
}
// 现存 escort_record 的 userId 是否在 users 里能对上
const records = await db.collection("escort_record").find({}, { projection: { userId: 1 } }).toArray();
for (const r of records) {
const inUsers = r.userId ? await db.collection("users").countDocuments({ _id: r.userId }) : 0;
console.log(`escort_record ${r._id} userId=${r.userId} in users: ${inUsers}`);
}
console.log("\n(check done, read-only)");
}
if (conn.readyState === 1) {
main().catch((e) => { console.error(e); process.exitCode = 1; }).finally(() => conn.close());
} else {
conn.once("connected", () => main().catch((e) => { console.error(e); process.exitCode = 1; }).finally(() => conn.close()));
conn.once("error", (e) => { console.error(e); process.exit(1); });
}