feat: 陪诊师入驻申请与审核链路

- 新增 attendant_application 表与 5 个接口:提交/我的申请/审核列表/
  材料大图/审批,/mine 注册在 /:id 之前
- 提交侧按员工档案岗位分支:在职陪诊师与管理员及其他岗位 409 拒绝,
  转岗由管理员后台显式操作;停用陪诊师可重新申请
- 审批两步走不依赖事务:条件更新抢占 pending,ensureEmployee 单条
  条件 upsert 幂等建档(只放行无档案或已有 attendant 档案),
  失败靠 approvedEmployeeId 为空续跑
- 岗位冲突时申请单条件回滚为 pending,不卡死在"已通过但未建档"
- employee.userId 加 partial unique 索引并移除 employeeNo 默认值,
  附迁移与存量校验脚本;员工管理端绑定账号查重与 userIdSource 标记
- 证件照复用 parseImage(单张 3MB),入驻前缀放宽请求体上限;
  索引创建失败改为记日志,避免启动即崩
This commit is contained in:
lik
2026-11-08 12:00:32 +08:00
parent 47393adde0
commit 4c2db2b6df
11 changed files with 1357 additions and 13 deletions
+24 -4
View File
@@ -25,16 +25,26 @@ const TrainingSchema = mongoose.Schema(
const EmployeeSchema = mongoose.Schema(
{
// 工号(业务雇佣标识)
employeeNo: { type: String, default: "", unique: true, sparse: true, comment: "工号" },
// 不设默认值:唯一索引是 sparse 的,只跳过"字段缺失"的文档;
// 若默认写成空串,多条无工号的档案会共享 "" 而互相撞唯一索引
employeeNo: { type: String, unique: true, sparse: true, comment: "工号" },
// 登录账号(关联用户体系)
// 一个 user 只允许绑定一条员工档案(partial unique 索引兜底,见文件末尾)。
// userIdSource 记录绑定来源:manual=管理员在后台手动绑定,application=入驻审批自动绑定
userId: {
type: mongoose.Schema.Types.ObjectId,
ref: "user",
index: true,
comment: "关联用户ID",
},
userIdSource: {
type: String,
enum: ["manual", "application", ""],
default: "",
comment: "userId 绑定来源:后台手动 / 入驻审批",
},
// 所属机构
orgId: {
type: mongoose.Schema.Types.ObjectId,
@@ -249,10 +259,20 @@ EmployeeSchema.statics.findOnlineAttendants = async function (options = {}) {
// ==================== 索引定义 ====================
EmployeeSchema.index({ "profile.name": 1 });
// 注意:employeeNo(unique+sparse)与 userId 的索引在字段定义处声明,勿在此重复声明
// 注意:employeeNo(unique+sparse)在字段定义处声明,勿在此重复声明
EmployeeSchema.index({ orgId: 1, role: 1, status: 1 });
EmployeeSchema.index({ role: 1, status: 1 });
EmployeeSchema.index({ "profile.phone": 1 });
EmployeeSchema.index({ status: 1 });
export { EmployeeSchema };
// 一个账号只能绑一条员工档案。
// partial 而非 sparse:历史档案可能存在 userId:null 的显式空值,sparse 挡不住多条 null;
// 上索引前先跑 scripts/check_employee_binding.js 确认无存量重复
EmployeeSchema.index(
{ userId: 1 },
{ unique: true, partialFilterExpression: { userId: { $type: "objectId" } } }
);
// genPinyin/genPinyinFL 一并导出:handler 层走 findOneAndUpdate 时绕过了 pre("save"),
// 需要自行同步拼音字段
export { EmployeeSchema, genPinyin, genPinyinFL };