Files
api_health/test/attendant_selection.test.js
T
lik cd3ff09a16 fix(escort): 管理员下单也按员工档案回填陪诊师快照
下单指定陪诊师时不再按 isAdmin 跳过解析,避免客户端原始 employeeId 落库、姓名电话为空,导致详情页误显示待分配;补充管理员下单的回归用例。
2026-11-11 21:03:41 +08:00

190 lines
5.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";
const USER_ID = "64b000000000000000000101";
const PROFILE_ID = "64b000000000000000000102";
const EMPLOYEE_ID = "64b000000000000000000103";
const EMPLOYEE_USER_ID = "64b000000000000000000104";
const RECORD_ID = "64b000000000000000000105";
function futureDate(days) {
const date = new Date();
date.setDate(date.getDate() + days);
return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, "0")}-${String(
date.getDate()
).padStart(2, "0")}`;
}
function buildProfile() {
return {
_id: PROFILE_ID,
userId: USER_ID,
profile: { name: "张三", mobile: "13800138000", sex: "male", birth: "1990-01-01" },
};
}
function buildEmployee(overrides = {}) {
return {
_id: EMPLOYEE_ID,
userId: EMPLOYEE_USER_ID,
role: "attendant",
status: "active",
profile: { name: "李四", sex: "female", phone: "13700137000" },
attendantInfo: { stage: "certified", grade: "silver" },
...overrides,
};
}
function buildBody(attendant) {
return {
healthProfileId: PROFILE_ID,
escort: { serviceId: 1, sexRequirement: "none" },
hospital: { province: "北京市", name: "北京协和医院" },
schedule: { date: futureDate(3), startTime: "08:00" },
payment: { payChannel: "offline" },
...(attendant ? { attendant } : {}),
};
}
function createCtx(body, user = { _id: USER_ID }) {
return {
request: { query: {}, body },
params: {},
state: { user },
headers: {},
status: 200,
body: null,
set(name, value) {
this.headers[name] = value;
},
};
}
function withModel(modelName, stub, fn) {
const previous = DBModel[modelName];
DBModel[modelName] = stub;
return Promise.resolve(fn()).finally(() => {
DBModel[modelName] = previous;
});
}
function employeeFindOneStub(employee, overrides = {}) {
const query = { lean: () => ({ exec: async () => employee }) };
return {
findOne: () => ({
...query,
select: () => query,
}),
incrementOrderCount: async () => {},
...overrides,
};
}
async function runCreate({ attendant, employee = buildEmployee(), user } = {}) {
const calls = { payload: null, bumped: null };
const recordStub = {
findByIdempotencyKey: async () => null,
countDocuments: async () => 0,
createRecord: async (payload) => {
calls.payload = payload;
return { _id: RECORD_ID, ...payload };
},
};
const employeeStub = employeeFindOneStub(employee, {
incrementOrderCount: async (id) => {
calls.bumped = id;
},
});
const ctx = createCtx(buildBody(attendant), user);
await withModel("HealthProfile", { findProfileById: async () => buildProfile() }, () =>
withModel("EscortRecord", recordStub, () =>
withModel("Employee", employeeStub, () => new HandlerEscortRecord().createRecord(ctx))
)
);
return { ctx, calls };
}
test("用户下单指定陪诊师:服务端回填快照并累加接单数", async () => {
const { ctx, calls } = await runCreate({
// 客户端伪造的姓名 / 电话必须被服务端档案覆盖
attendant: { employeeId: EMPLOYEE_ID, name: "伪造姓名", mobile: "10000000000" },
});
assert.equal(ctx.body.code, 0);
assert.deepEqual(calls.payload.attendant, {
id: EMPLOYEE_USER_ID,
name: "李四",
sex: "female",
mobile: "13700137000",
sharedWith: [],
});
assert.equal(calls.bumped, EMPLOYEE_ID);
});
test("管理员用用户端下单指定陪诊师:同样回填快照而不是落库 employeeId", async () => {
const admin = {
_id: USER_ID,
apps: [{ appId: "web-escort-admin", roles: ["admin"], status: "active" }],
};
const { ctx, calls } = await runCreate({
user: admin,
attendant: { employeeId: EMPLOYEE_ID },
});
assert.equal(ctx.body.code, 0);
assert.deepEqual(calls.payload.attendant, {
id: EMPLOYEE_USER_ID,
name: "李四",
sex: "female",
mobile: "13700137000",
sharedWith: [],
});
assert.equal(calls.payload.attendant.employeeId, undefined);
assert.equal(calls.bumped, EMPLOYEE_ID);
});
test("用户不指定陪诊师:订单进入公共池,不写陪诊师、不累加接单数", async () => {
const { ctx, calls } = await runCreate({ attendant: null });
assert.equal(ctx.body.code, 0);
assert.equal(calls.payload.attendant, undefined);
assert.equal(calls.bumped, null);
});
test("指定实习陪诊师或不在职陪诊师返回 400 且不创建订单", async () => {
const intern = await runCreate({
attendant: { employeeId: EMPLOYEE_ID },
employee: buildEmployee({ attendantInfo: { stage: "intern" } }),
});
assert.equal(intern.ctx.body.code, 400);
assert.equal(intern.calls.payload, null);
const inactive = await runCreate({
attendant: { employeeId: EMPLOYEE_ID },
employee: null,
});
assert.equal(inactive.ctx.body.code, 400);
assert.equal(inactive.calls.payload, null);
});
test("指定未绑定登录账号的陪诊师返回 400", async () => {
const { ctx, calls } = await runCreate({
attendant: { employeeId: EMPLOYEE_ID },
employee: buildEmployee({ userId: null }),
});
assert.equal(ctx.body.code, 400);
assert.equal(calls.payload, null);
});
test("陪诊师 ID 不合法返回 400", async () => {
const { ctx, calls } = await runCreate({ attendant: { employeeId: "bad-id" } });
assert.equal(ctx.body.code, 400);
assert.equal(calls.payload, null);
});