Files
api_health/test/order_permission.test.js
T
lik 5878520707 feat: 订单可改字段按角色收敛并修复嵌套字段覆盖
- 陪诊员只能写服务备注与就诊小结;订单主人按状态收敛,
  取消中/已取消一律冻结
- 更新前把嵌套子文档摊平成点号路径并与现值合并:
  整块 $set 会抹掉未提交字段,与点号路径混用还会触发 Mongo 路径冲突
- 通用更新接口的状态迁移改走状态机,软删除订单按不存在处理
2026-11-08 11:59:53 +08:00

206 lines
6.5 KiB
JavaScript

import { test } from "node:test";
import assert from "node:assert/strict";
import { DBModel } from "../models/index.js";
import { HandlerEscortRecord } from "../handler/escort_record.js";
import { HandlerHealthProfile } from "../handler/health_profile.js";
const RECORD_ID = "64b000000000000000000010";
const OWNER_ID = "64b000000000000000000001";
const ATTENDANT_ID = "64b000000000000000000002";
const ADMIN_ID = "64b000000000000000000003";
const PAYMENT_ID = "64b000000000000000000020";
const ADMIN_USER = {
_id: ADMIN_ID,
app: { "wxapp-escort-admin": { role: ["admin"] } },
};
function buildRecord(overrides = {}) {
return {
_id: RECORD_ID,
userId: OWNER_ID,
status: "confirmed",
cancelStatus: "none",
escort: { serviceId: 1, serviceName: "全天陪诊" },
hospital: { province: "北京市", name: "北京协和医院" },
schedule: {
date: new Date("2026-12-01T00:00:00.000Z"),
startTime: "08:00",
endTime: "17:00",
duration: 480,
},
payment: {
totalFee: 500,
paidFee: 0,
status: "unpaid",
payChannel: "online_wechat",
paymentRecordId: PAYMENT_ID,
},
notes: { patientNote: "患者原备注", escortNote: "原服务记录", medicalSummary: "" },
attendant: { id: ATTENDANT_ID, name: "李四" },
...overrides,
};
}
function createCtx({ body = {}, user, params = { id: RECORD_ID } } = {}) {
return {
request: { query: {}, body },
params,
state: { user: user ?? { _id: OWNER_ID } },
headers: {},
status: 200,
body: null,
set(name, value) {
this.headers[name] = value;
},
};
}
function createRecordStub(record) {
const calls = { update: null, count: 0 };
return {
calls,
findById: async () => record,
updateRecord: async (id, payload) => {
calls.update = payload;
calls.count += 1;
return { ...record, ...payload };
},
};
}
function withModel(modelName, stub, fn) {
const previous = DBModel[modelName];
DBModel[modelName] = stub;
return Promise.resolve(fn()).finally(() => {
DBModel[modelName] = previous;
});
}
test("陪诊员只能写服务备注,医院与患者备注被过滤", async () => {
const stub = createRecordStub(buildRecord());
await withModel("EscortRecord", stub, async () => {
const ctx = createCtx({
user: { _id: ATTENDANT_ID },
body: {
"hospital.name": "被篡改的医院",
"notes.patientNote": "被篡改的患者备注",
"notes.escortNote": "已完成陪同",
},
});
await new HandlerEscortRecord().updateRecord(ctx);
assert.equal(ctx.body.code, 0);
assert.deepEqual(stub.calls.update, { "notes.escortNote": "已完成陪同" });
});
});
test("陪诊员在待确认订单上不能写入", async () => {
const stub = createRecordStub(buildRecord({ status: "pending" }));
await withModel("EscortRecord", stub, async () => {
const ctx = createCtx({
user: { _id: ATTENDANT_ID },
body: { "notes.escortNote": "提前写" },
});
await new HandlerEscortRecord().updateRecord(ctx);
assert.equal(ctx.body.code, 403);
assert.equal(stub.calls.count, 0);
});
});
test("订单主人仍可修改医院等预约字段", async () => {
const stub = createRecordStub(buildRecord());
await withModel("EscortRecord", stub, async () => {
const ctx = createCtx({ body: { "hospital.name": "北京协和医院东院" } });
await new HandlerEscortRecord().updateRecord(ctx);
assert.equal(ctx.body.code, 0);
assert.equal(stub.calls.update["hospital.name"], "北京协和医院东院");
});
});
test("管理员通用更新不能绕过状态机回退状态", async () => {
const stub = createRecordStub(buildRecord({ status: "completed" }));
await withModel("EscortRecord", stub, async () => {
const ctx = createCtx({ user: ADMIN_USER, body: { status: "in_progress" } });
await new HandlerEscortRecord().updateRecord(ctx);
assert.equal(ctx.body.code, 400);
assert.equal(stub.calls.count, 0);
});
});
test("管理员合法状态流转仍可通过", async () => {
const stub = createRecordStub(buildRecord({ status: "pending" }));
await withModel("EscortRecord", stub, async () => {
const ctx = createCtx({ user: ADMIN_USER, body: { status: "confirmed" } });
await new HandlerEscortRecord().updateRecord(ctx);
assert.equal(ctx.body.code, 0);
assert.equal(stub.calls.update.status, "confirmed");
});
});
test("管理员只提交部分 payment 字段时保留中间支付状态", async () => {
const stub = createRecordStub(buildRecord());
const paymentStub = { findOne: async () => null };
await withModel("EscortRecord", stub, () =>
withModel("PaymentRecord", paymentStub, async () => {
const ctx = createCtx({ user: ADMIN_USER, body: { payment: { totalFee: 600 } } });
await new HandlerEscortRecord().updateRecord(ctx);
assert.equal(ctx.body.code, 0);
assert.equal(stub.calls.update["payment.totalFee"], 600);
assert.equal(stub.calls.update["payment.status"], "unpaid");
assert.equal(stub.calls.update["payment.paymentRecordId"], PAYMENT_ID);
assert.equal(stub.calls.update["payment.payChannel"], "online_wechat");
// 嵌套对象必须摊平,不能与点号路径同时出现在 $set 里
assert.equal(stub.calls.update.payment, undefined);
})
);
});
test("切换支付渠道时嵌套 payment 摊平后不会产生路径冲突", async () => {
const stub = createRecordStub(buildRecord());
const paymentStub = {
findOne: async () => null,
find: async () => [],
};
await withModel("EscortRecord", stub, () =>
withModel("PaymentRecord", paymentStub, async () => {
const ctx = createCtx({ user: ADMIN_USER, body: { payment: { payChannel: "offline" } } });
await new HandlerEscortRecord().updateRecord(ctx);
assert.equal(ctx.body.code, 0);
assert.equal(stub.calls.update.payment, undefined);
assert.equal(stub.calls.update["payment.payChannel"], "offline");
assert.equal(stub.calls.update["payment.paymentRecordId"], null);
})
);
});
test("健康档案更新把嵌套对象摊平成点号路径", () => {
const picked = HandlerHealthProfile.pickFields(
{
profile: { name: "李四", sex: "male" },
health: { height: 170, weight: 60 },
userId: OWNER_ID,
},
{ flatten: true }
);
assert.deepEqual(picked, {
"profile.name": "李四",
"profile.sex": "male",
"health.height": 170,
"health.weight": 60,
userId: OWNER_ID,
});
assert.equal(picked.profile, undefined);
assert.equal(picked.health, undefined);
});