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:
@@ -0,0 +1,166 @@
|
||||
"use strict";
|
||||
|
||||
import mongoose from "mongoose";
|
||||
|
||||
/**
|
||||
* AttendantApplication Schema
|
||||
* 陪诊师入驻申请
|
||||
*
|
||||
* 与 employee 分离存放:employee 只保存审核通过的真实员工,
|
||||
* 申请单独立成表,驳回原因与历史提交都能留存,也不会让"申请中"的人
|
||||
* 混进员工列表和陪诊师公示。
|
||||
*
|
||||
* 材料图片以 data URL 入参,服务端限制单张 3MB(见 handler 的 IMAGE_MAX_BYTES);
|
||||
* 驳回后重新提交且未重传照片时,会复制上一份的 data URL 到新申请文档,
|
||||
* 保留审计痕迹的代价是每次重提交图片存储翻倍,需由留存/清理策略覆盖。
|
||||
* 审核通过后只把结构化字段(健康证到期日等)同步到 employee,
|
||||
* 原图留在本表做审计,不复制进员工表。
|
||||
*/
|
||||
const ImageSchema = mongoose.Schema(
|
||||
{
|
||||
data: { type: String, required: true, comment: "data URL" },
|
||||
mime: { type: String, enum: ["image/jpeg", "image/png"], required: true, comment: "图片类型" },
|
||||
},
|
||||
{ _id: false }
|
||||
);
|
||||
|
||||
const HealthCertificateSchema = mongoose.Schema(
|
||||
{
|
||||
data: { type: String, required: true, comment: "健康证照片 data URL" },
|
||||
mime: { type: String, enum: ["image/jpeg", "image/png"], required: true, comment: "图片类型" },
|
||||
expireDate: { type: Date, required: true, comment: "健康证有效期至" },
|
||||
},
|
||||
{ _id: false }
|
||||
);
|
||||
|
||||
const AttendantApplicationSchema = mongoose.Schema(
|
||||
{
|
||||
userId: {
|
||||
type: mongoose.Schema.Types.ObjectId,
|
||||
ref: "user",
|
||||
required: true,
|
||||
comment: "申请用户ID",
|
||||
},
|
||||
|
||||
applicant: {
|
||||
name: { type: String, required: true, trim: true, maxlength: 20, comment: "姓名" },
|
||||
mobile: {
|
||||
type: String,
|
||||
required: true,
|
||||
trim: true,
|
||||
match: /^1[3-9]\d{9}$/,
|
||||
comment: "手机号",
|
||||
},
|
||||
idNumber: {
|
||||
type: String,
|
||||
required: true,
|
||||
trim: true,
|
||||
uppercase: true,
|
||||
match: /^\d{17}[\dX]$/,
|
||||
comment: "身份证号",
|
||||
},
|
||||
sex: { type: String, enum: ["male", "female", ""], default: "", comment: "性别" },
|
||||
serviceAreas: { type: [String], default: [], comment: "服务区域/城市列表" },
|
||||
introduction: { type: String, default: "", maxlength: 200, comment: "个人简介" },
|
||||
},
|
||||
|
||||
materials: {
|
||||
qualification: { type: ImageSchema, required: true, comment: "资质证书照片" },
|
||||
healthCertificate: { type: HealthCertificateSchema, required: true, comment: "健康证明" },
|
||||
},
|
||||
|
||||
/**
|
||||
* pending 待审核 / approved 已通过 / rejected 已驳回
|
||||
* v1 不提供用户主动撤回,避免出现没有入口的空状态
|
||||
*/
|
||||
status: {
|
||||
type: String,
|
||||
enum: ["pending", "approved", "rejected"],
|
||||
default: "pending",
|
||||
comment: "审核状态",
|
||||
},
|
||||
|
||||
review: {
|
||||
reviewerId: { type: mongoose.Schema.Types.ObjectId, comment: "审核人用户ID" },
|
||||
reviewedAt: { type: Number, comment: "审核时间" },
|
||||
rejectReason: { type: String, default: "", maxlength: 200, comment: "驳回原因" },
|
||||
},
|
||||
|
||||
// 通过后回填,用于第二步失败时的续跑判断(先改状态,再幂等建档)
|
||||
approvedEmployeeId: {
|
||||
type: mongoose.Schema.Types.ObjectId,
|
||||
default: null,
|
||||
comment: "审核通过后生成的员工ID",
|
||||
},
|
||||
|
||||
meta: {
|
||||
createtime: { type: Number, default: () => Date.now(), comment: "创建时间" },
|
||||
updatetime: { type: Number, default: () => Date.now(), comment: "更新时间" },
|
||||
},
|
||||
},
|
||||
{
|
||||
minimize: false,
|
||||
strict: true,
|
||||
collection: "attendant_application",
|
||||
timestamps: false,
|
||||
}
|
||||
);
|
||||
|
||||
AttendantApplicationSchema.statics.createApplication = async function (data) {
|
||||
data.meta = { createtime: Date.now(), updatetime: Date.now() };
|
||||
const doc = new this(data);
|
||||
return await doc.save();
|
||||
};
|
||||
|
||||
AttendantApplicationSchema.statics.findLatestByUser = async function (userId) {
|
||||
return await this.findOne({ userId }).sort({ "meta.createtime": -1 }).exec();
|
||||
};
|
||||
|
||||
AttendantApplicationSchema.statics.findPendingByUser = async function (userId) {
|
||||
return await this.findOne({ userId, status: "pending" }).exec();
|
||||
};
|
||||
|
||||
/**
|
||||
* 管理端审核列表
|
||||
* 默认不返回 materials:一遍列表带几十张 data URL,响应体会直接失控
|
||||
*/
|
||||
AttendantApplicationSchema.statics.findApplicationPage = async function (options = {}) {
|
||||
const { page = 1, pageSize = 20, status = "pending", keyword } = options;
|
||||
const filter = {};
|
||||
if (status) filter.status = status;
|
||||
if (keyword && String(keyword).trim()) {
|
||||
const safe = String(keyword).trim().replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
||||
const reg = new RegExp(safe, "i");
|
||||
filter.$or = [{ "applicant.name": reg }, { "applicant.mobile": reg }];
|
||||
}
|
||||
|
||||
const skip = (page - 1) * pageSize;
|
||||
const [applications, total] = await Promise.all([
|
||||
this.find(filter)
|
||||
.select("-materials -applicant.idNumber")
|
||||
.sort({ "meta.createtime": -1 })
|
||||
.skip(skip)
|
||||
.limit(pageSize)
|
||||
.lean()
|
||||
.exec(),
|
||||
this.countDocuments(filter).exec(),
|
||||
]);
|
||||
|
||||
return { applications, total };
|
||||
};
|
||||
|
||||
/**
|
||||
* 同一用户同时只允许一条待审核申请
|
||||
*
|
||||
* 只靠 handler 先查后插挡不住并发双击,这里用部分唯一索引兜底;
|
||||
* 已通过/已驳回的历史申请不受约束,用户可以重新提交。
|
||||
*/
|
||||
AttendantApplicationSchema.index(
|
||||
{ userId: 1 },
|
||||
{ unique: true, partialFilterExpression: { status: "pending" } }
|
||||
);
|
||||
// 查"我的最新申请"与审核列表各一条复合索引,不再单独给 userId / status 建单字段索引
|
||||
AttendantApplicationSchema.index({ userId: 1, "meta.createtime": -1 });
|
||||
AttendantApplicationSchema.index({ status: 1, "meta.createtime": -1 });
|
||||
|
||||
export { AttendantApplicationSchema };
|
||||
@@ -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 };
|
||||
|
||||
Reference in New Issue
Block a user