fix: 订单与档案接口收敛错误细节与写入校验

internalError 只把通用文案回给前端、详情进日志;状态迁移规则抽到 utils/record_state.js 单点维护;档案更新开 runValidators 并补姓名/手机号/身高体重校验与每人档案上限;已支付订单改为软删除保留审计。
This commit is contained in:
lik
2026-10-24 22:24:39 +08:00
committed by trae-solo-agent
parent 1774239553
commit f4760a4c5b
6 changed files with 174 additions and 32 deletions
+21 -5
View File
@@ -153,6 +153,12 @@ const EscortRecordSchema = mongoose.Schema(
cancelReason: { type: String, default: "", comment: "订单取消原因" },
cancelledAt: { type: Date, default: null, comment: "订单取消时间" },
/**
* 软删除标记:已支付/进行中/已完成的订单不允许物理删除(费用与服务记录需留存审计),
* 管理员删除时打此标记,列表与详情默认过滤,历史数据不受影响
*/
deletedAt: { type: Date, default: null, comment: "软删除时间" },
/**
* 元数据 - 系统管理信息
*/
@@ -182,9 +188,12 @@ const EscortRecordSchema = mongoose.Schema(
* @returns {Promise<Array>} 陪诊记录列表(按预约日期倒序)
*/
EscortRecordSchema.statics.buildRecordFilter = function (options = {}) {
const { status, userId, appointmentDate, healthProfileId } = options;
const { status, userId, appointmentDate, healthProfileId, includeDeleted = false } = options;
const filter = {};
// 软删除的订单默认不进入列表:数据库里保留审计,界面上视为已删除
if (!includeDeleted) filter.deletedAt = null;
if (status && Array.isArray(status) && status.length > 0) {
filter.status = { $in: status };
}
@@ -281,20 +290,26 @@ EscortRecordSchema.statics.createRecord = async function (record, cb) {
*/
EscortRecordSchema.statics.updateRecord = async function (id, update, cb) {
const $set = { ...update, "meta.updatetime": Date.now() };
return await this.findByIdAndUpdate(id, { $set }, { new: true, runValidators: true }, cb);
// 已软删除的订单不可再被修改
return await this.findOneAndUpdate(
{ _id: id, deletedAt: null },
{ $set },
{ new: true, runValidators: true },
cb
);
};
// 状态迁移必须仍以读取时的状态为条件,避免并发请求重复取消或跨过服务阶段。
EscortRecordSchema.statics.updateRecordStatus = async function (id, fromStatus, update) {
return await this.findOneAndUpdate(
{ _id: id, status: fromStatus },
{ _id: id, status: fromStatus, deletedAt: null },
{ $set: { ...update, "meta.updatetime": Date.now() } },
{ new: true, runValidators: true }
);
};
EscortRecordSchema.statics.findByIdempotencyKey = async function (userId, idempotencyKey) {
return await this.findOne({ userId, idempotencyKey }).exec();
return await this.findOne({ userId, idempotencyKey, deletedAt: null }).exec();
};
/**
@@ -305,7 +320,8 @@ EscortRecordSchema.statics.findByIdempotencyKey = async function (userId, idempo
* @returns {Promise<Object|null>} 陪诊记录,不存在返回null
*/
EscortRecordSchema.statics.findRecordById = async function (id, cb) {
return await this.findById(id).exec(cb);
// 软删除的订单按不存在处理
return await this.findOne({ _id: id, deletedAt: null }).exec(cb);
};
/**
+7 -4
View File
@@ -39,8 +39,9 @@ const HealthProfileSchema = mongoose.Schema(
// 健康信息
health: {
height: { type: Number, default: 0, comment: "身高(cm)" },
weight: { type: Number, default: 0, comment: "体重(kg)" },
// 范围校验:0 表示未填写;非法值(负数、超大值)以前会被静默写入并影响下单展示
height: { type: Number, min: 0, max: 300, default: 0, comment: "身高(cm)" },
weight: { type: Number, min: 0, max: 500, default: 0, comment: "体重(kg)" },
bloodType: { type: String, default: "", comment: "血型" },
remark: { type: String, default: "", comment: "备注" },
},
@@ -165,11 +166,13 @@ HealthProfileSchema.statics.createProfile = async function (data) {
/**
* 根据ID更新健康档案(更新姓名时同步拼音字段)
* runValidators: 更新时同样校验 schema 约束(性别枚举、身高体重范围),
* 否则非法值会绕过校验被静默写入
*/
HealthProfileSchema.statics.updateProfileById = async function (id, update) {
syncPinyinOnUpdate(update);
update["meta.updatetime"] = Date.now();
return await this.findByIdAndUpdate(id, { $set: update }, { new: true }).exec();
return await this.findByIdAndUpdate(id, { $set: update }, { new: true, runValidators: true }).exec();
};
/**
@@ -178,7 +181,7 @@ HealthProfileSchema.statics.updateProfileById = async function (id, update) {
HealthProfileSchema.statics.updateProfile = async function (userId, update) {
syncPinyinOnUpdate(update);
update["meta.updatetime"] = Date.now();
return await this.findOneAndUpdate({ userId }, { $set: update }, { new: true }).exec();
return await this.findOneAndUpdate({ userId }, { $set: update }, { new: true, runValidators: true }).exec();
};
/**