"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( { // 证件材料允许后补:照片与有效期都可能缺失,是否成对由 handler 校验 data: { type: String, comment: "健康证照片 data URL" }, mime: { type: String, enum: ["image/jpeg", "image/png"], comment: "图片类型" }, expireDate: { type: Date, 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, 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, comment: "资质证书照片" }, healthCertificate: { type: HealthCertificateSchema, 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 };