入驻申请只收基本信息(身份证选填、材料全可空),服务城市改为单选白名单(北京/上海/天津/西安,默认北京);新增 POST /attendant-application/mine/materials 材料后补与 PATCH /employee/me/service-area 改城市接口;订单池按陪诊师服务城市过滤(本人已接/被共享不限城市),接单与详情二次校验;appId wxapp-escort-admin 更名为 wxapp_companion。
469 lines
15 KiB
JavaScript
469 lines
15 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 RECORD_ID = "64b000000000000000000010";
|
|
const OWNER_ID = "64b000000000000000000001";
|
|
const INTERN_USER_ID = "64b000000000000000000002";
|
|
const INTERN_EMPLOYEE_ID = "64b000000000000000000022";
|
|
const CERT_USER_ID = "64b000000000000000000003";
|
|
const CERT_EMPLOYEE_ID = "64b000000000000000000033";
|
|
const OTHER_USER_ID = "64b000000000000000000004";
|
|
|
|
function buildRecord(overrides = {}) {
|
|
return {
|
|
_id: RECORD_ID,
|
|
userId: OWNER_ID,
|
|
status: "pending",
|
|
cancelStatus: "none",
|
|
patient: { name: "张三", mobile: "13800138000" },
|
|
escort: { serviceId: 1, serviceName: "全天陪诊" },
|
|
hospital: { name: "北京协和医院", address: "北京市东城区", medicalRecordNo: "MR0001" },
|
|
shippingAddress: { recipientName: "张三", mobile: "13900139000", address: "北京市西城区" },
|
|
inquiry: { medicalHistory: "高血压", demand: "陪同检查" },
|
|
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" },
|
|
attendant: { id: null, name: "", sharedWith: [] },
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
function buildEmployee(overrides = {}) {
|
|
return {
|
|
_id: CERT_EMPLOYEE_ID,
|
|
userId: CERT_USER_ID,
|
|
role: "attendant",
|
|
status: "active",
|
|
profile: { name: "李四", sex: "male", phone: "13700137000" },
|
|
attendantInfo: { stage: "certified", grade: "bronze" },
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
function createCtx({ body = {}, query = {}, user, params = { id: RECORD_ID } } = {}) {
|
|
return {
|
|
request: { query, body },
|
|
params,
|
|
state: { user: user ?? { _id: CERT_USER_ID } },
|
|
headers: {},
|
|
status: 200,
|
|
body: null,
|
|
set(name, value) {
|
|
this.headers[name] = value;
|
|
},
|
|
};
|
|
}
|
|
|
|
function employeeFindOneStub(employee, overrides = {}) {
|
|
return {
|
|
findOne: () => ({
|
|
select: () => ({
|
|
lean: () => ({ exec: async () => employee }),
|
|
}),
|
|
}),
|
|
incrementOrderCount: async () => {},
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
function withModel(modelName, stub, fn) {
|
|
const previous = DBModel[modelName];
|
|
DBModel[modelName] = stub;
|
|
return Promise.resolve(fn()).finally(() => {
|
|
DBModel[modelName] = previous;
|
|
});
|
|
}
|
|
|
|
test("实习陪诊师看未分配订单:可见范围含未分配且按 employeeId 查共享", async () => {
|
|
const intern = buildEmployee({
|
|
_id: INTERN_EMPLOYEE_ID,
|
|
userId: INTERN_USER_ID,
|
|
profile: { name: "王五", sex: "female", phone: "13600136000" },
|
|
attendantInfo: { stage: "intern", grade: "bronze" },
|
|
});
|
|
let captured = null;
|
|
const recordStub = {
|
|
findRecordPage: async (options) => {
|
|
captured = options;
|
|
return { records: [buildRecord()], total: 1, counts: {} };
|
|
},
|
|
};
|
|
|
|
await withModel("Employee", employeeFindOneStub(intern), () =>
|
|
withModel("EscortRecord", recordStub, async () => {
|
|
const ctx = createCtx({
|
|
user: { _id: INTERN_USER_ID },
|
|
// 实习传关键词想按手机号搜池订单:应被服务端忽略(防侧信道)
|
|
query: { keyword: "13800138000" },
|
|
});
|
|
await new HandlerEscortRecord().getAttendantRecords(ctx);
|
|
|
|
assert.equal(ctx.body.code, 0);
|
|
assert.equal(captured.attendantVisibility.includeUnassigned, true);
|
|
assert.equal(captured.attendantVisibility.userId, undefined);
|
|
assert.equal(String(captured.attendantVisibility.employeeId), INTERN_EMPLOYEE_ID);
|
|
assert.equal(captured.keyword, undefined);
|
|
|
|
const record = ctx.body.data.records[0];
|
|
assert.equal(record.isPoolOrder, true);
|
|
assert.equal(record.desensitized, true);
|
|
assert.equal(record.patient.name, "张**");
|
|
assert.equal(record.patient.mobile, "138****8000");
|
|
assert.equal(record.hospital.address, "接单后可见");
|
|
assert.equal(record.hospital.medicalRecordNo, "接单后可见");
|
|
assert.equal(record.shippingAddress.recipientName, "张**");
|
|
assert.equal(record.inquiry.medicalHistory, "接单后可见");
|
|
assert.equal(record.inquiry.demand, "接单后可见");
|
|
})
|
|
);
|
|
});
|
|
|
|
test("正式陪诊师可见本人已接与被共享订单,未分配同样在内", async () => {
|
|
let captured = null;
|
|
const record = buildRecord({
|
|
attendant: { id: OTHER_USER_ID, name: "赵六", sharedWith: [CERT_EMPLOYEE_ID] },
|
|
});
|
|
const recordStub = {
|
|
findRecordPage: async (options) => {
|
|
captured = options;
|
|
return { records: [record], total: 1, counts: {} };
|
|
},
|
|
};
|
|
|
|
await withModel("Employee", employeeFindOneStub(buildEmployee()), () =>
|
|
withModel("EscortRecord", recordStub, async () => {
|
|
const ctx = createCtx({
|
|
user: { _id: CERT_USER_ID },
|
|
// 正式陪诊师可见完整信息,keyword 正常生效
|
|
query: { keyword: "协和" },
|
|
});
|
|
await new HandlerEscortRecord().getAttendantRecords(ctx);
|
|
|
|
assert.equal(ctx.body.code, 0);
|
|
assert.equal(captured.attendantVisibility.includeUnassigned, true);
|
|
assert.equal(String(captured.attendantVisibility.userId), CERT_USER_ID);
|
|
assert.equal(captured.keyword, "协和");
|
|
|
|
const item = ctx.body.data.records[0];
|
|
assert.equal(item.attendantSharedToMe, true);
|
|
assert.deepEqual(item.attendantSharedEmployeeIds, [CERT_EMPLOYEE_ID]);
|
|
assert.equal(item.attendant.sharedWith, undefined);
|
|
assert.equal(item.desensitized, undefined);
|
|
assert.equal(item.patient.mobile, "13800138000");
|
|
})
|
|
);
|
|
});
|
|
|
|
test("实习陪诊师不能接单", async () => {
|
|
const intern = buildEmployee({
|
|
_id: INTERN_EMPLOYEE_ID,
|
|
userId: INTERN_USER_ID,
|
|
attendantInfo: { stage: "intern" },
|
|
});
|
|
let accepted = 0;
|
|
|
|
await withModel("Employee", employeeFindOneStub(intern), () =>
|
|
withModel("EscortRecord", {
|
|
findById: async () => buildRecord(),
|
|
acceptRecord: async () => {
|
|
accepted += 1;
|
|
return buildRecord();
|
|
},
|
|
}, async () => {
|
|
const ctx = createCtx({ user: { _id: INTERN_USER_ID } });
|
|
await new HandlerEscortRecord().acceptRecord(ctx);
|
|
|
|
assert.equal(ctx.body.code, 403);
|
|
assert.equal(accepted, 0);
|
|
})
|
|
);
|
|
});
|
|
|
|
test("正式陪诊师接单写入陪诊师快照,不校验支付状态", async () => {
|
|
let payload = null;
|
|
let bumped = null;
|
|
const record = buildRecord();
|
|
const recordStub = {
|
|
findById: async () => record,
|
|
acceptRecord: async (id, attendant) => {
|
|
payload = attendant;
|
|
return {
|
|
...record,
|
|
attendant: { id: attendant.id, name: attendant.name, sharedWith: [] },
|
|
};
|
|
},
|
|
};
|
|
|
|
await withModel(
|
|
"Employee",
|
|
employeeFindOneStub(buildEmployee(), {
|
|
incrementOrderCount: async (employeeId) => {
|
|
bumped = employeeId;
|
|
},
|
|
}),
|
|
() =>
|
|
withModel("EscortRecord", recordStub, async () => {
|
|
const ctx = createCtx({ user: { _id: CERT_USER_ID } });
|
|
await new HandlerEscortRecord().acceptRecord(ctx);
|
|
|
|
assert.equal(ctx.body.code, 0);
|
|
assert.equal(payload.id, CERT_USER_ID);
|
|
assert.equal(payload.name, "李四");
|
|
assert.equal(payload.sex, "male");
|
|
assert.equal(payload.mobile, "13700137000");
|
|
assert.equal(ctx.body.data.record.attendantAssignedToMe, true);
|
|
assert.equal(bumped, CERT_EMPLOYEE_ID);
|
|
})
|
|
);
|
|
});
|
|
|
|
test("订单已被接走时返回 409 且不重复写库", async () => {
|
|
let accepted = 0;
|
|
const recordStub = {
|
|
findById: async () => buildRecord({ attendant: { id: OTHER_USER_ID, sharedWith: [] } }),
|
|
acceptRecord: async () => {
|
|
accepted += 1;
|
|
return null;
|
|
},
|
|
};
|
|
|
|
await withModel("Employee", employeeFindOneStub(buildEmployee()), () =>
|
|
withModel("EscortRecord", recordStub, async () => {
|
|
const ctx = createCtx({ user: { _id: CERT_USER_ID } });
|
|
await new HandlerEscortRecord().acceptRecord(ctx);
|
|
|
|
assert.equal(ctx.body.code, 409);
|
|
assert.equal(accepted, 0);
|
|
})
|
|
);
|
|
});
|
|
|
|
test("接单陪诊师可把订单共享给在职陪诊师(含实习)", async () => {
|
|
let sharedIds = null;
|
|
const record = buildRecord({ attendant: { id: CERT_USER_ID, name: "李四", sharedWith: [] } });
|
|
const me = buildEmployee();
|
|
const employeeStub = {
|
|
findOne: () => ({
|
|
select: () => ({ lean: () => ({ exec: async () => me }) }),
|
|
}),
|
|
find: () => ({
|
|
select: () => ({
|
|
lean: () => ({
|
|
exec: async () => [{ _id: INTERN_EMPLOYEE_ID }, { _id: OTHER_USER_ID }],
|
|
}),
|
|
}),
|
|
}),
|
|
};
|
|
const recordStub = {
|
|
findById: async () => record,
|
|
addSharedWith: async (id, attendantId, employeeIds) => {
|
|
sharedIds = { id, attendantId, employeeIds };
|
|
return { ...record, attendant: { ...record.attendant, sharedWith: employeeIds } };
|
|
},
|
|
};
|
|
|
|
await withModel("Employee", employeeStub, () =>
|
|
withModel("EscortRecord", recordStub, async () => {
|
|
const ctx = createCtx({
|
|
user: { _id: CERT_USER_ID },
|
|
body: { employeeIds: [INTERN_EMPLOYEE_ID, OTHER_USER_ID] },
|
|
});
|
|
await new HandlerEscortRecord().shareRecord(ctx);
|
|
|
|
assert.equal(ctx.body.code, 0);
|
|
assert.equal(sharedIds.attendantId, CERT_USER_ID);
|
|
assert.deepEqual(sharedIds.employeeIds, [INTERN_EMPLOYEE_ID, OTHER_USER_ID]);
|
|
assert.equal(ctx.body.data.record.attendantSharedEmployeeIds.length, 2);
|
|
})
|
|
);
|
|
});
|
|
|
|
test("非接单陪诊师不能共享订单", async () => {
|
|
let shared = 0;
|
|
const recordStub = {
|
|
findById: async () => buildRecord({ attendant: { id: OTHER_USER_ID, sharedWith: [] } }),
|
|
addSharedWith: async () => {
|
|
shared += 1;
|
|
return null;
|
|
},
|
|
};
|
|
|
|
await withModel("Employee", employeeFindOneStub(buildEmployee()), () =>
|
|
withModel("EscortRecord", recordStub, async () => {
|
|
const ctx = createCtx({
|
|
user: { _id: CERT_USER_ID },
|
|
body: { employeeIds: [INTERN_EMPLOYEE_ID] },
|
|
});
|
|
await new HandlerEscortRecord().shareRecord(ctx);
|
|
|
|
assert.equal(ctx.body.code, 403);
|
|
assert.equal(shared, 0);
|
|
})
|
|
);
|
|
});
|
|
|
|
test("实习陪诊师可看未分配订单详情,但不能看他人已接且未共享的订单", async () => {
|
|
const intern = buildEmployee({
|
|
_id: INTERN_EMPLOYEE_ID,
|
|
userId: INTERN_USER_ID,
|
|
attendantInfo: { stage: "intern" },
|
|
});
|
|
|
|
await withModel("Employee", employeeFindOneStub(intern), () =>
|
|
withModel("EscortRecord", {
|
|
findRecordById: async () => buildRecord(),
|
|
}, async () => {
|
|
const ctx = createCtx({ user: { _id: INTERN_USER_ID } });
|
|
await new HandlerEscortRecord().getRecordById(ctx);
|
|
|
|
assert.equal(ctx.body.code, 0);
|
|
assert.equal(ctx.body.data.record.desensitized, true);
|
|
assert.equal(ctx.body.data.record.patient.mobile, "138****8000");
|
|
})
|
|
);
|
|
|
|
await withModel("Employee", employeeFindOneStub(intern), () =>
|
|
withModel("EscortRecord", {
|
|
findRecordById: async () => buildRecord({ attendant: { id: OTHER_USER_ID, sharedWith: [] } }),
|
|
}, async () => {
|
|
const ctx = createCtx({ user: { _id: INTERN_USER_ID } });
|
|
await new HandlerEscortRecord().getRecordById(ctx);
|
|
|
|
assert.equal(ctx.body.code, 403);
|
|
})
|
|
);
|
|
});
|
|
|
|
test("取消中/已取消订单不能接单", async () => {
|
|
const cases = [
|
|
{ cancelStatus: "requested", msg: "取消申请确认中" },
|
|
{ cancelStatus: "cancelled", msg: "订单已取消" },
|
|
{ status: "cancelled", msg: "订单已取消" },
|
|
];
|
|
for (const override of cases) {
|
|
let accepted = 0;
|
|
await withModel("Employee", employeeFindOneStub(buildEmployee()), () =>
|
|
withModel("EscortRecord", {
|
|
findById: async () => buildRecord(override),
|
|
acceptRecord: async () => {
|
|
accepted += 1;
|
|
return buildRecord();
|
|
},
|
|
}, async () => {
|
|
const ctx = createCtx({ user: { _id: CERT_USER_ID } });
|
|
await new HandlerEscortRecord().acceptRecord(ctx);
|
|
|
|
assert.equal(ctx.body.code, 409, `场景 ${JSON.stringify(override)} 应返回 409`);
|
|
assert.equal(accepted, 0);
|
|
})
|
|
);
|
|
}
|
|
});
|
|
|
|
test("已取消订单不能共享", async () => {
|
|
let shared = 0;
|
|
const recordStub = {
|
|
findById: async () => buildRecord({
|
|
cancelStatus: "cancelled",
|
|
attendant: { id: CERT_USER_ID, name: "李四", sharedWith: [] },
|
|
}),
|
|
addSharedWith: async () => {
|
|
shared += 1;
|
|
return null;
|
|
},
|
|
};
|
|
|
|
await withModel("Employee", employeeFindOneStub(buildEmployee()), () =>
|
|
withModel("EscortRecord", recordStub, async () => {
|
|
const ctx = createCtx({
|
|
user: { _id: CERT_USER_ID },
|
|
body: { employeeIds: [INTERN_EMPLOYEE_ID] },
|
|
});
|
|
await new HandlerEscortRecord().shareRecord(ctx);
|
|
|
|
assert.equal(ctx.body.code, 409);
|
|
assert.equal(shared, 0);
|
|
})
|
|
);
|
|
});
|
|
|
|
test("取消共享时订单不存在返回 404", async () => {
|
|
await withModel("Employee", employeeFindOneStub(buildEmployee()), () =>
|
|
withModel("EscortRecord", {
|
|
findById: async () => null,
|
|
}, async () => {
|
|
const ctx = createCtx({
|
|
user: { _id: CERT_USER_ID },
|
|
body: { employeeIds: [INTERN_EMPLOYEE_ID] },
|
|
});
|
|
await new HandlerEscortRecord().unshareRecord(ctx);
|
|
|
|
assert.equal(ctx.body.code, 404);
|
|
})
|
|
);
|
|
});
|
|
|
|
test("订单池按服务城市过滤,scope=mine 不带城市条件", async () => {
|
|
const employee = buildEmployee({
|
|
attendantInfo: { stage: "certified", grade: "bronze", serviceAreas: ["上海市"] },
|
|
});
|
|
let captured = null;
|
|
const recordStub = {
|
|
findRecordPage: async (options) => {
|
|
captured = options;
|
|
return { records: [], total: 0, counts: {} };
|
|
},
|
|
};
|
|
|
|
await withModel("Employee", employeeFindOneStub(employee), () =>
|
|
withModel("EscortRecord", recordStub, async () => {
|
|
const poolCtx = createCtx({ user: { _id: CERT_USER_ID }, query: { scope: "pool" } });
|
|
await new HandlerEscortRecord().getAttendantRecords(poolCtx);
|
|
|
|
assert.equal(poolCtx.body.code, 0);
|
|
assert.deepEqual(captured.attendantVisibility.poolCities, ["上海市", "上海"]);
|
|
|
|
const mineCtx = createCtx({ user: { _id: CERT_USER_ID }, query: { scope: "mine" } });
|
|
await new HandlerEscortRecord().getAttendantRecords(mineCtx);
|
|
|
|
assert.equal(mineCtx.body.code, 0);
|
|
// 本人已接订单不受城市限制
|
|
assert.equal(captured.attendantVisibility.poolCities, undefined);
|
|
assert.equal(captured.attendantVisibility.includeUnassigned, undefined);
|
|
})
|
|
);
|
|
});
|
|
|
|
test("服务城市匹配兼容「市」后缀,未配置城市保持全部可见", () => {
|
|
assert.deepEqual(HandlerEscortRecord.cityVariants(["上海市"]), ["上海市", "上海"]);
|
|
assert.equal(
|
|
HandlerEscortRecord.matchesServiceArea(
|
|
{ hospital: { province: "上海市" } },
|
|
{ attendantInfo: { serviceAreas: ["上海"] } }
|
|
),
|
|
true
|
|
);
|
|
assert.equal(
|
|
HandlerEscortRecord.matchesServiceArea(
|
|
{ hospital: { province: "北京市" } },
|
|
{ attendantInfo: { serviceAreas: ["上海市"] } }
|
|
),
|
|
false
|
|
);
|
|
// 存量档案没配城市:保持原行为,不因城市过滤而看不到订单
|
|
assert.equal(
|
|
HandlerEscortRecord.matchesServiceArea(
|
|
{ hospital: { province: "北京市" } },
|
|
{ attendantInfo: {} }
|
|
),
|
|
true
|
|
);
|
|
});
|