- 新增 attendant_application 表与 5 个接口:提交/我的申请/审核列表/ 材料大图/审批,/mine 注册在 /:id 之前 - 提交侧按员工档案岗位分支:在职陪诊师与管理员及其他岗位 409 拒绝, 转岗由管理员后台显式操作;停用陪诊师可重新申请 - 审批两步走不依赖事务:条件更新抢占 pending,ensureEmployee 单条 条件 upsert 幂等建档(只放行无档案或已有 attendant 档案), 失败靠 approvedEmployeeId 为空续跑 - 岗位冲突时申请单条件回滚为 pending,不卡死在"已通过但未建档" - employee.userId 加 partial unique 索引并移除 employeeNo 默认值, 附迁移与存量校验脚本;员工管理端绑定账号查重与 userIdSource 标记 - 证件照复用 parseImage(单张 3MB),入驻前缀放宽请求体上限; 索引创建失败改为记日志,避免启动即崩
81 lines
3.6 KiB
JavaScript
81 lines
3.6 KiB
JavaScript
'use strict';
|
||
|
||
import mongoose from 'mongoose';
|
||
import { EscortRecordSchema } from "./schema/escort_record.js"
|
||
import { HealthProfileSchema } from "./schema/health_profile.js"
|
||
import { OrganizationSchema } from "./schema/organization.js"
|
||
import { HospitalSchema } from "./schema/hospital.js"
|
||
import { EmployeeSchema } from "./schema/employee.js"
|
||
import { EmployeeReviewSchema } from "./schema/employee_review.js"
|
||
import { TrainingApplicationSchema } from "./schema/training_application.js"
|
||
import { AttendantApplicationSchema } from "./schema/attendant_application.js"
|
||
import { PaymentRecordSchema } from "./schema/payment_record.js"
|
||
import { RefundRecordSchema } from "./schema/refund_record.js"
|
||
import config from '../config.js';
|
||
import logger from '../utils/logger.js';
|
||
|
||
class MongoDBSchema {
|
||
constructor() {
|
||
this.dbConnection = null;
|
||
this.EscortRecord = null;
|
||
this.Organization = null;
|
||
this.Hospital = null;
|
||
this.Employee = null;
|
||
this.HealthProfile = null;
|
||
this.EmployeeReview = null;
|
||
this.TrainingApplication = null;
|
||
this.AttendantApplication = null;
|
||
this.PaymentRecord = null;
|
||
this.RefundRecord = null;
|
||
}
|
||
|
||
init() {
|
||
mongoose.set("debug", config.mongodb.debug);
|
||
|
||
this.dbConnection = mongoose.createConnection(config.mongodb.str, config.mongodb.option);
|
||
this.dbConnection.on("error", () => {
|
||
logger.error("...mongodb connect error ...");
|
||
});
|
||
this.dbConnection.on("connected", async () => {
|
||
logger.info(`Mongodb: ${config.mongodb.host}/${config.mongodb.option.dbName} connected`);
|
||
});
|
||
this.dbConnection.on("disconnected", () =>
|
||
logger.warn(`Mongodb: ${config.mongodb.host}/${config.mongodb.option.dbName} disconnected`)
|
||
);
|
||
this.dbConnection.on("reconnected", () =>
|
||
logger.info(`Mongodb: ${config.mongodb.host}/${config.mongodb.option.dbName} reconnected`)
|
||
);
|
||
this.dbConnection.on("disconnecting", () =>
|
||
logger.warn(`Mongodb: ${config.mongodb.host}/${config.mongodb.option.dbName} disconnecting`)
|
||
);
|
||
this.dbConnection.on("close", () =>
|
||
logger.warn(`Mongodb: ${config.mongodb.host}/${config.mongodb.option.dbName} closed`)
|
||
);
|
||
|
||
this.EscortRecord = this.dbConnection.model('escort_record', EscortRecordSchema)
|
||
this.HealthProfile = this.dbConnection.model('health_profile', HealthProfileSchema)
|
||
this.Organization = this.dbConnection.model('organization', OrganizationSchema)
|
||
this.Hospital = this.dbConnection.model('hospital', HospitalSchema)
|
||
this.Employee = this.dbConnection.model('employee', EmployeeSchema)
|
||
this.EmployeeReview = this.dbConnection.model('employee_review', EmployeeReviewSchema)
|
||
this.TrainingApplication = this.dbConnection.model('training_application', TrainingApplicationSchema)
|
||
this.AttendantApplication = this.dbConnection.model('attendant_application', AttendantApplicationSchema)
|
||
this.PaymentRecord = this.dbConnection.model('payment_record', PaymentRecordSchema)
|
||
this.RefundRecord = this.dbConnection.model('refund_record', RefundRecordSchema)
|
||
|
||
// 索引创建失败(如唯一索引遇到存量重复)时 mongoose 会 emit('error'),
|
||
// 而 EventEmitter 对没有监听器的 'error' 事件会直接抛未捕获异常,
|
||
// 导致整个服务启动即崩。这里统一监听并记日志,把问题留给索引脚本处理
|
||
for (const model of Object.values(this)) {
|
||
if (model && typeof model.on === "function" && model.schema) {
|
||
model.on("error", (err) => {
|
||
logger.error(`[mongo] ${model.modelName} 索引/初始化失败: ${err?.message || err}`);
|
||
});
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
const DBModel = new MongoDBSchema()
|
||
export { DBModel };
|