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
+37 -3
View File
@@ -301,6 +301,19 @@ class HandlerEmployee {
}
}
// userId 绑定查重:一个账号只能绑一条员工档案(与 employee.userId 的
// partial unique 索引同一规则,应用层先挡可给出可读提示而非 500)
static async assertUserIdAvailable(userId, excludeId = null) {
if (!userId || typeof userId !== "string" || !mongoose.Types.ObjectId.isValid(userId)) {
return null;
}
const filter = { userId: new mongoose.Types.ObjectId(userId) };
if (excludeId) filter._id = { $ne: new mongoose.Types.ObjectId(excludeId) };
const existing = await DBModel.Employee.findOne(filter).select("_id profile.name").lean().exec();
if (!existing) return null;
return `该账号已绑定员工「${existing.profile?.name || existing._id}」,一个账号只能关联一条员工档案`;
}
async createEmployee(ctx) {
try {
const flat = HandlerEmployee.pickFields(ctx.request.body);
@@ -310,17 +323,23 @@ class HandlerEmployee {
return ResponseUtil.badRequest(ctx, invalid);
}
const conflict = await HandlerEmployee.assertUserIdAvailable(flat.userId);
if (conflict) {
return ResponseUtil.error(ctx, conflict, null, 409);
}
const data = {};
for (const [path, value] of Object.entries(flat)) {
HandlerEmployee.setPath(data, path, value);
}
if (data.userId) data.userIdSource = "manual";
const employee = await DBModel.Employee.createEmployee(data);
return ResponseUtil.success(ctx, { employee }, "创建成功");
} catch (err) {
// employeeNo 唯一索引冲突:给出可读提示,而不是「服务器繁忙」
// employeeNo / userId 唯一索引冲突:给出可读提示,而不是「服务器繁忙」
if (err?.code === 11000) {
return ResponseUtil.badRequest(ctx, "工号已存在");
return ResponseUtil.badRequest(ctx, "工号已存在,或该账号已绑定其他员工档案");
}
return ResponseUtil.internalError(ctx, err);
}
@@ -343,6 +362,21 @@ class HandlerEmployee {
return ResponseUtil.badRequest(ctx, invalid);
}
const conflict = await HandlerEmployee.assertUserIdAvailable(flat.userId, id);
if (conflict) {
return ResponseUtil.error(ctx, conflict, null, 409);
}
// 换了绑定账号 = 人工改绑,来源改写为 manual(可与入驻自动绑定区分)
if (flat.userId) {
flat.userIdSource = "manual";
} else if (flat.userId === "") {
// 解绑:userId 是 ObjectId 路径,写空串会触发 CastError,
// 必须转成 null 才能真正解绑(partial unique 索引不约束 null)
flat.userId = null;
flat.userIdSource = "";
}
const employee = await DBModel.Employee.updateEmployee(id, flat);
if (!employee) {
return ResponseUtil.notFound(ctx, "员工不存在");
@@ -350,7 +384,7 @@ class HandlerEmployee {
return ResponseUtil.success(ctx, { employee }, "更新成功");
} catch (err) {
if (err?.code === 11000) {
return ResponseUtil.badRequest(ctx, "工号已存在");
return ResponseUtil.badRequest(ctx, "工号已存在,或该账号已绑定其他员工档案");
}
return ResponseUtil.internalError(ctx, err);
}