feat(org): 平铺就医信息并支持分院/院区
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
/**
|
||||
* 一次性迁移:把 organization 里旧的 forAgent 嵌套字段平铺到顶层。
|
||||
*
|
||||
* 用法:在 api/health 下执行 `node scripts/migrate_organization_flat.js`
|
||||
* 迁移后删除 forAgent,工具只读写顶层字段。
|
||||
*/
|
||||
import { DBModel } from "../models/index.js";
|
||||
|
||||
DBModel.init();
|
||||
const conn = DBModel.dbConnection;
|
||||
|
||||
async function migrate() {
|
||||
const result = await DBModel.Organization.updateMany(
|
||||
{ forAgent: { $exists: true } },
|
||||
[
|
||||
{
|
||||
$set: {
|
||||
overview: { $ifNull: ["$overview", "$forAgent.overview"] },
|
||||
photos: { $ifNull: ["$photos", "$forAgent.photos"] },
|
||||
departments: { $ifNull: ["$departments", "$forAgent.departments"] },
|
||||
doctors: { $ifNull: ["$doctors", "$forAgent.doctors"] },
|
||||
guides: { $ifNull: ["$guides", "$forAgent.guides"] },
|
||||
infoUpdatedAt: { $ifNull: ["$infoUpdatedAt", "$forAgent.updatedAt"] },
|
||||
infoUpdatedBy: { $ifNull: ["$infoUpdatedBy", "$forAgent.updatedBy"] },
|
||||
},
|
||||
},
|
||||
{ $unset: "forAgent" },
|
||||
]
|
||||
);
|
||||
|
||||
console.log(`migrated: matched=${result.matchedCount}, modified=${result.modifiedCount}`);
|
||||
await conn.close();
|
||||
}
|
||||
|
||||
if (conn.readyState === 1) {
|
||||
migrate().catch((error) => {
|
||||
console.error(error);
|
||||
process.exit(1);
|
||||
});
|
||||
} else {
|
||||
conn.once("connected", () => {
|
||||
migrate().catch((error) => {
|
||||
console.error(error);
|
||||
process.exit(1);
|
||||
});
|
||||
});
|
||||
conn.once("error", (error) => {
|
||||
console.error(error);
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user