fix: 管理员判定统一口径,订单字段按状态收敛并校验档案归属
新增 utils/permissions.js 供路由、订单 handler 与 WS 共用;用户端可改字段改为按订单状态的白名单,进行中仅可改就诊人备注;下单丢弃客户端 attendant;改档校验档案归属并刷新就诊人快照。
This commit is contained in:
+76
-17
@@ -1,6 +1,7 @@
|
||||
import { DBModel } from "../models/index.js";
|
||||
import services from "../resource/services.js";
|
||||
import ResponseUtil from "../utils/responseUtil.js";
|
||||
import { isAdmin as isAdminUser } from "../utils/permissions.js";
|
||||
|
||||
class HandlerEscortRecord {
|
||||
constructor() {
|
||||
@@ -13,10 +14,20 @@ class HandlerEscortRecord {
|
||||
"shippingAddress",
|
||||
];
|
||||
|
||||
// 用户端可自助修改的字段:费用、状态、陪诊员分配等一律由平台控制
|
||||
static OWNER_UPDATE_FIELDS = [
|
||||
"patient", "healthProfileId", "hospital", "schedule", "notes",
|
||||
// 用户自助可改字段按订单状态区分(费用、状态、陪诊员分配、档案归属一律由平台控制):
|
||||
// - 待支付/已支付:可改医院与排期(改期会写审计时间)
|
||||
// - 进行中:服务已开始,仅可补充就诊人备注,排期与医院不允许用户单方变更
|
||||
// - 终态:全部不可改
|
||||
static OWNER_NOTE_FIELDS = ["notes.patientNote"];
|
||||
static OWNER_BOOKING_FIELDS = [
|
||||
"hospital.province", "hospital.name", "hospital.address", "hospital.department",
|
||||
"hospital.doctor", "hospital.medicalRecordNo", "schedule.date", "schedule.startTime",
|
||||
];
|
||||
static OWNER_EDITABLE_BY_STATUS = {
|
||||
pending: ["notes.patientNote", ...HandlerEscortRecord.OWNER_BOOKING_FIELDS],
|
||||
confirmed: ["notes.patientNote", ...HandlerEscortRecord.OWNER_BOOKING_FIELDS],
|
||||
in_progress: ["notes.patientNote"],
|
||||
};
|
||||
|
||||
// 用户端只能取消;服务完成由平台确认,避免用户端重复写入终态。
|
||||
static OWNER_ALLOWED_STATUS = ["cancelled"];
|
||||
@@ -36,7 +47,31 @@ class HandlerEscortRecord {
|
||||
* @param {Object} user token 解析出的用户
|
||||
*/
|
||||
static isAdmin(user) {
|
||||
return !!(user && user.app && ("wxapp-escort-admin" in user.app));
|
||||
return isAdminUser(user);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户端在当前状态下可自助修改的字段清单(白名单精确匹配,点号路径)
|
||||
* @param {string} status 订单状态
|
||||
* @returns {string[]}
|
||||
*/
|
||||
static ownerEditableFields(status) {
|
||||
return HandlerEscortRecord.OWNER_EDITABLE_BY_STATUS[status] || [];
|
||||
}
|
||||
|
||||
/**
|
||||
* 就诊人快照:下单与更换档案共用,保证订单里的展示字段一致
|
||||
* @param {Object} profile 健康档案文档
|
||||
* @returns {Object}
|
||||
*/
|
||||
static buildPatientSnapshot(profile) {
|
||||
const patient = {
|
||||
name: profile.profile?.name || "",
|
||||
mobile: profile.profile?.mobile || "",
|
||||
birth: profile.profile?.birth || "",
|
||||
};
|
||||
if (["male", "female"].includes(profile.profile?.sex)) patient.sex = profile.profile.sex;
|
||||
return patient;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -50,11 +85,17 @@ class HandlerEscortRecord {
|
||||
return String(record.userId) === String(user?._id);
|
||||
}
|
||||
|
||||
static pickFields(body, allowFields = HandlerEscortRecord.RECORD_FIELDS) {
|
||||
/**
|
||||
* 从入参中挑出白名单字段
|
||||
* @param {Object} body 请求体
|
||||
* @param {string[]} allowFields 允许的字段(支持点号路径)
|
||||
* @param {boolean} exact 是否精确匹配;用户端用 true,防止 notes.patientNote.x 这类嵌套越界
|
||||
*/
|
||||
static pickFields(body, allowFields = HandlerEscortRecord.RECORD_FIELDS, exact = false) {
|
||||
const picked = {};
|
||||
for (const key of Object.keys(body || {})) {
|
||||
const allowed = allowFields.some(
|
||||
(f) => key === f || key.startsWith(f + ".")
|
||||
(f) => key === f || (!exact && key.startsWith(f + "."))
|
||||
);
|
||||
if (allowed) {
|
||||
picked[key] = body[key];
|
||||
@@ -273,14 +314,13 @@ class HandlerEscortRecord {
|
||||
if (addressResult.error) return ResponseUtil.badRequest(ctx, addressResult.error);
|
||||
|
||||
const payload = HandlerEscortRecord.pickFields(body);
|
||||
// 陪诊员分配由平台决定:用户下单时提交的 attendant 一律丢弃,防止自选/伪造陪诊员
|
||||
if (!HandlerEscortRecord.isAdmin(ctx.state.user)) delete payload.attendant;
|
||||
payload.userId = userId;
|
||||
// healthProfileId 以校验通过的档案为准,覆盖客户端原值
|
||||
payload.healthProfileId = profile._id;
|
||||
payload.status = "pending";
|
||||
payload.patient = {
|
||||
name: profile.profile?.name || "",
|
||||
mobile: profile.profile?.mobile || "",
|
||||
birth: profile.profile?.birth || "",
|
||||
};
|
||||
if (["male", "female"].includes(profile.profile?.sex)) payload.patient.sex = profile.profile.sex;
|
||||
payload.patient = HandlerEscortRecord.buildPatientSnapshot(profile);
|
||||
payload.escort = {
|
||||
serviceId: service.id,
|
||||
serviceName: service.title,
|
||||
@@ -331,15 +371,30 @@ class HandlerEscortRecord {
|
||||
}
|
||||
|
||||
const isAdmin = HandlerEscortRecord.isAdmin(ctx.state.user);
|
||||
if (!isAdmin && ["completed", "cancelled"].includes(existing.status)) {
|
||||
// 非管理员的可改字段随订单状态收敛:终态无可改字段,进行中只剩就诊人备注
|
||||
const allowFields = isAdmin
|
||||
? HandlerEscortRecord.RECORD_FIELDS
|
||||
: HandlerEscortRecord.ownerEditableFields(existing.status);
|
||||
if (allowFields.length === 0) {
|
||||
return ResponseUtil.forbidden(ctx, "已结束的订单不可修改");
|
||||
}
|
||||
|
||||
// 非管理员只能改就诊信息类字段,费用/状态/归属/陪诊员分配不可自助修改
|
||||
const allowFields = isAdmin
|
||||
? HandlerEscortRecord.RECORD_FIELDS
|
||||
: HandlerEscortRecord.OWNER_UPDATE_FIELDS;
|
||||
const payload = HandlerEscortRecord.pickFields(update, allowFields);
|
||||
const payload = HandlerEscortRecord.pickFields(update, allowFields, !isAdmin);
|
||||
if (Object.keys(payload).length === 0) {
|
||||
// 例如进行中订单提交医院/排期:字段被状态规则过滤,明确告知而不是静默成功
|
||||
return ResponseUtil.badRequest(ctx, "该订单当前状态不支持修改这些信息");
|
||||
}
|
||||
|
||||
// 档案归属校验:档案必须属于订单所属用户,避免把订单挂到他人档案上;
|
||||
// 通过后同步刷新订单里的就诊人快照
|
||||
if (payload.healthProfileId) {
|
||||
const profile = await DBModel.HealthProfile.findProfileById(payload.healthProfileId);
|
||||
if (!profile || String(profile.userId) !== String(existing.userId)) {
|
||||
return ResponseUtil.badRequest(ctx, "就诊人档案不存在");
|
||||
}
|
||||
payload.patient = HandlerEscortRecord.buildPatientSnapshot(profile);
|
||||
}
|
||||
const service = services.find((item) => item.id === existing.escort?.serviceId);
|
||||
if (!service) return ResponseUtil.badRequest(ctx, "订单关联的服务已下架");
|
||||
|
||||
@@ -352,6 +407,10 @@ class HandlerEscortRecord {
|
||||
for (const key of Object.keys(payload)) {
|
||||
if (key.startsWith("schedule.")) delete payload[key];
|
||||
}
|
||||
// 已支付后的改期留审计时间,便于客服追溯谁在何时改过排期
|
||||
if (existing.status !== "pending") {
|
||||
scheduleResult.schedule.scheduleUpdatedAt = new Date();
|
||||
}
|
||||
payload.schedule = scheduleResult.schedule;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user