Files
api_health/models/schema/training_application.js
T

135 lines
4.3 KiB
JavaScript

"use strict";
import mongoose from "mongoose";
/**
* TrainingApplication Schema
* 2026 年国开培训中心「医疗陪诊(高级)」考位报名。
*
* 证书口径:颁发主体是国家开放大学培训中心,本报名只采集考位信息,
* 不表示职业资格证书或就业承诺。照片与身份证电子版仅用于报名和办证,
* 查询列表时默认不返回,避免在列表接口中反复传输敏感大图。
*/
const TrainingApplicationSchema = mongoose.Schema(
{
userId: {
type: mongoose.Schema.Types.ObjectId,
ref: "user",
required: true,
index: true,
comment: "报名用户ID",
},
applicant: {
name: { type: String, required: true, trim: true, maxlength: 20, comment: "姓名" },
idNumber: {
type: String,
required: true,
trim: true,
uppercase: true,
match: /^\d{17}[\dX]$/,
comment: "身份证号",
},
mobile: { type: String, required: true, trim: true, comment: "手机号" },
education: {
type: String,
required: true,
enum: ["初中及以下", "高中/中专", "大专", "本科", "硕士及以上"],
comment: "学历",
},
},
// 2026 年考位:前端只允许选择 2026-01 至 2026-12
exam: {
year: { type: Number, default: 2026, min: 2026, max: 2026, comment: "考试年份" },
month: { type: Number, required: true, min: 1, max: 12, comment: "考试月份" },
},
// 图片以 data URL 落库;每张原图服务端限制 5MB。
// 列表查询使用 .select('-materials'),只有后续确需办证查看时才单独读取
materials: {
photo: {
data: { type: String, required: true, comment: "2寸蓝底照片 data URL" },
mime: { type: String, enum: ["image/jpeg", "image/png"], required: true },
},
idCard: {
data: { type: String, required: true, comment: "身份证电子版 data URL" },
mime: { type: String, enum: ["image/jpeg", "image/png"], required: true },
},
},
course: {
name: { type: String, default: "医疗陪诊顾问(高级)", comment: "报考项目" },
issuer: { type: String, default: "国家开放大学培训中心", comment: "颁发机构" },
},
status: {
type: String,
enum: ["pending", "contacted", "enrolled", "cancelled"],
default: "pending",
index: true,
comment: "pending 待审核 / contacted 已联系 / enrolled 已入班 / cancelled 已取消",
},
meta: {
createtime: { type: Number, default: () => Date.now(), comment: "创建时间" },
updatetime: { type: Number, default: () => Date.now(), comment: "更新时间" },
},
},
{
minimize: false,
strict: true,
collection: "training_application",
timestamps: false,
}
);
TrainingApplicationSchema.statics.createApplication = async function (data) {
data.meta = { createtime: Date.now(), updatetime: Date.now() };
const doc = new this(data);
return await doc.save();
};
TrainingApplicationSchema.statics.findLatestByUser = async function (userId) {
return await this.findOne({ userId })
.select("-materials")
.sort({ "meta.createtime": -1 })
.exec();
};
// 拦截短时间内的重复提交;已取消的报名不阻挡用户重新报名
TrainingApplicationSchema.statics.findRecentActiveByUser = async function (userId, since) {
return await this.findOne({
userId,
status: { $ne: "cancelled" },
"meta.createtime": { $gte: since },
})
.select("-materials")
.sort({ "meta.createtime": -1 })
.exec();
};
TrainingApplicationSchema.statics.findApplicationPage = async function (options = {}) {
const { page = 1, pageSize = 20, status } = options;
const filter = {};
if (status) filter.status = status;
const skip = (page - 1) * pageSize;
const [applications, total] = await Promise.all([
this.find(filter)
.select("-materials")
.sort({ "meta.createtime": -1 })
.skip(skip)
.limit(pageSize)
.exec(),
this.countDocuments(filter).exec(),
]);
return { applications, total };
};
TrainingApplicationSchema.index({ userId: 1, "meta.createtime": -1 });
TrainingApplicationSchema.index({ status: 1, "meta.createtime": -1 });
export { TrainingApplicationSchema };