fix: 订单与档案接口收敛错误细节与写入校验
internalError 只把通用文案回给前端、详情进日志;状态迁移规则抽到 utils/record_state.js 单点维护;档案更新开 runValidators 并补姓名/手机号/身高体重校验与每人档案上限;已支付订单改为软删除保留审计。
This commit is contained in:
@@ -11,6 +11,12 @@ class HandlerHealthProfile {
|
||||
// 单页最大条数,避免 pageSize 传超大值一次拖库
|
||||
static MAX_PAGE_SIZE = 200;
|
||||
|
||||
// 单个账号可添加的就诊人数量上限
|
||||
static MAX_PROFILES_PER_USER = 20;
|
||||
|
||||
// 手机号:11 位大陆手机号
|
||||
static MOBILE_PATTERN = /^1[3-9]\d{9}$/;
|
||||
|
||||
// 解析分页参数并夹紧上限
|
||||
static parsePaging(query = {}) {
|
||||
const page = Math.max(1, parseInt(query.page) || 1);
|
||||
@@ -19,6 +25,40 @@ class HandlerHealthProfile {
|
||||
return { page, pageSize };
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验就诊人基本信息(只校验请求中出现的字段,未出现的沿用原值)
|
||||
* @param {Object} body 已按白名单挑出的请求体
|
||||
* @param {boolean} requireBase 新增时要求姓名与手机号必填
|
||||
* @returns {string|null} 不合规时返回提示文案
|
||||
*/
|
||||
static validateProfileInput(body, requireBase = false) {
|
||||
const name = body["profile.name"] ?? body.profile?.name;
|
||||
const mobile = body["profile.mobile"] ?? body.profile?.mobile;
|
||||
|
||||
if (requireBase || name !== undefined) {
|
||||
if (!String(name || "").trim()) return "请填写就诊人姓名";
|
||||
}
|
||||
if (requireBase || mobile !== undefined) {
|
||||
const value = String(mobile || "").trim();
|
||||
if (!HandlerHealthProfile.MOBILE_PATTERN.test(value)) return "请填写正确的就诊人手机号";
|
||||
}
|
||||
|
||||
const height = body["health.height"] ?? body.health?.height;
|
||||
if (height !== undefined && (Number(height) < 0 || Number(height) > 300)) {
|
||||
return "身高需在 0-300cm 之间";
|
||||
}
|
||||
const weight = body["health.weight"] ?? body.health?.weight;
|
||||
if (weight !== undefined && (Number(weight) < 0 || Number(weight) > 500)) {
|
||||
return "体重需在 0-500kg 之间";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 按白名单挑选字段(支持 "profile.name" 这类点号路径)
|
||||
* @param {Object} body 请求体
|
||||
* @returns {Object}
|
||||
*/
|
||||
static pickFields(body) {
|
||||
const picked = {};
|
||||
for (const key of Object.keys(body || {})) {
|
||||
@@ -32,6 +72,18 @@ class HandlerHealthProfile {
|
||||
return picked;
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验失败统一转成 400:schema 校验错误直接抛给前端会暴露字段名与内部实现
|
||||
* @param {Object} ctx Koa 上下文
|
||||
* @param {Error} err 捕获到的异常
|
||||
*/
|
||||
static handleWriteError(ctx, err) {
|
||||
if (err?.name === "ValidationError") {
|
||||
return ResponseUtil.badRequest(ctx, "填写内容不合法,请检查身高、体重、性别等字段");
|
||||
}
|
||||
return ResponseUtil.internalError(ctx, err);
|
||||
}
|
||||
|
||||
async getProfiles(ctx) {
|
||||
try {
|
||||
const { userId, name, mobile, sortBy } = ctx.request.query;
|
||||
@@ -99,10 +151,24 @@ class HandlerHealthProfile {
|
||||
const body = HandlerHealthProfile.pickFields(ctx.request.body);
|
||||
body.userId = ctx.state.user?._id;
|
||||
|
||||
const invalid = HandlerHealthProfile.validateProfileInput(body, true);
|
||||
if (invalid) {
|
||||
return ResponseUtil.badRequest(ctx, invalid);
|
||||
}
|
||||
|
||||
// 每账号档案数量上限:防止被当作无限存储使用
|
||||
const count = await DBModel.HealthProfile.countDocuments({ userId: body.userId });
|
||||
if (count >= HandlerHealthProfile.MAX_PROFILES_PER_USER) {
|
||||
return ResponseUtil.badRequest(
|
||||
ctx,
|
||||
`最多可添加 ${HandlerHealthProfile.MAX_PROFILES_PER_USER} 位就诊人`
|
||||
);
|
||||
}
|
||||
|
||||
const newProfile = await DBModel.HealthProfile.createProfile(body);
|
||||
return ResponseUtil.success(ctx, { profile: newProfile }, "创建成功");
|
||||
} catch (err) {
|
||||
return ResponseUtil.internalError(ctx, err.message);
|
||||
return HandlerHealthProfile.handleWriteError(ctx, err);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -124,10 +190,15 @@ class HandlerHealthProfile {
|
||||
const update = HandlerHealthProfile.pickFields(ctx.request.body);
|
||||
delete update.userId; // 不允许通过请求转移档案归属
|
||||
|
||||
const invalid = HandlerHealthProfile.validateProfileInput(update);
|
||||
if (invalid) {
|
||||
return ResponseUtil.badRequest(ctx, invalid);
|
||||
}
|
||||
|
||||
const updatedProfile = await DBModel.HealthProfile.updateProfileById(id, update);
|
||||
return ResponseUtil.success(ctx, { profile: updatedProfile }, "更新成功");
|
||||
} catch (err) {
|
||||
return ResponseUtil.internalError(ctx, err.message);
|
||||
return HandlerHealthProfile.handleWriteError(ctx, err);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user