Compare commits
4
Commits
d748034835
...
b160102f12
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b160102f12 | ||
|
|
78138a417b | ||
|
|
d60a160180 | ||
|
|
67f2962574 |
@@ -253,6 +253,9 @@ class HandlerAttendantApplication {
|
||||
$setOnInsert: {
|
||||
employeeNo: HandlerAttendantApplication.generateEmployeeNo(),
|
||||
"meta.createtime": Date.now(),
|
||||
// 入驻通过只给实习身份:可查看订单但不能接单,由管理员人工转正。
|
||||
// 只在新建时写入,停用档案重新激活保留原有阶段,避免转正后被降回实习
|
||||
"attendantInfo.stage": "intern",
|
||||
},
|
||||
},
|
||||
{ new: true, upsert: true }
|
||||
|
||||
+209
-10
@@ -1,5 +1,6 @@
|
||||
import mongoose from "mongoose";
|
||||
import { DBModel } from "../models/index.js";
|
||||
import { MY_EMPLOYEE_SELECT } from "../models/schema/employee.js";
|
||||
import ResponseUtil from "../utils/responseUtil.js";
|
||||
|
||||
/**
|
||||
@@ -30,6 +31,32 @@ const MIN_PUBLIC_REVIEW_COUNT = 5;
|
||||
class HandlerEmployee {
|
||||
// 公示列表单页上限:避免一次请求把整张表拖出来
|
||||
static MAX_PAGE_SIZE = 50;
|
||||
|
||||
// 陪诊师阶段与等级:等级只做展示/排序,权限只看 stage
|
||||
static STAGES = ["intern", "certified"];
|
||||
static GRADES = ["bronze", "silver", "gold", "diamond"];
|
||||
|
||||
// 公示口径:实习陪诊师不对外展示(不能被用户选择)。
|
||||
// stage 用 $ne 而非 $eq:存量档案没有该字段,一律视为正式
|
||||
static publicAttendantFilter() {
|
||||
return {
|
||||
role: "attendant",
|
||||
status: "active",
|
||||
"attendantInfo.stage": { $ne: "intern" },
|
||||
};
|
||||
}
|
||||
|
||||
// 阶段/等级归一化:存量档案缺字段时按 certified + bronze 处理
|
||||
static normalizeAttendantLevel(info = {}) {
|
||||
const stage = HandlerEmployee.STAGES.includes(info.stage) ? info.stage : "certified";
|
||||
const grade = HandlerEmployee.GRADES.includes(info.grade) ? info.grade : "bronze";
|
||||
const rawStar = Number(info.star);
|
||||
const star = grade === "diamond" && Number.isFinite(rawStar)
|
||||
? Math.min(Math.max(Math.trunc(rawStar), 0), 5)
|
||||
: 0;
|
||||
return { stage, grade, star };
|
||||
}
|
||||
|
||||
// 姓名脱敏:只留姓 + 「陪诊师」,不给全名,降低私下联系(跳单)的可能
|
||||
static maskName(name) {
|
||||
const trimmed = String(name || "").trim();
|
||||
@@ -97,6 +124,7 @@ class HandlerEmployee {
|
||||
specialties: info.specialties || [],
|
||||
serviceAreas: info.serviceAreas || [],
|
||||
languages: info.languages || [],
|
||||
// 累计接单数由陪诊师接单 / 管理员指派成功时回写,可用于公示排序
|
||||
orderCount: info.orderCount || 0,
|
||||
introduction: employee.introduction || "",
|
||||
};
|
||||
@@ -112,12 +140,9 @@ class HandlerEmployee {
|
||||
const rawSize = parseInt(ctx.request.query.pageSize) || 50;
|
||||
const pageSize = Math.min(Math.max(1, rawSize), HandlerEmployee.MAX_PAGE_SIZE);
|
||||
|
||||
// 只取在职陪诊员。
|
||||
// 排序不按 attendantInfo.rating(无可信来源),改按服务年限 / 累计接单数
|
||||
const list = await DBModel.Employee.find({
|
||||
role: "attendant",
|
||||
status: "active",
|
||||
})
|
||||
// 只取在职且已转正的陪诊员,实习不对外公示。
|
||||
// 排序:服务年限优先,累计接单数次之;rating 无写入点,不参与排序
|
||||
const list = await DBModel.Employee.find(HandlerEmployee.publicAttendantFilter())
|
||||
.sort({ "work.workYears": -1, "attendantInfo.orderCount": -1 })
|
||||
.skip((page - 1) * pageSize)
|
||||
.limit(pageSize)
|
||||
@@ -148,8 +173,7 @@ class HandlerEmployee {
|
||||
|
||||
const employee = await DBModel.Employee.findOne({
|
||||
_id: id,
|
||||
role: "attendant",
|
||||
status: "active",
|
||||
...HandlerEmployee.publicAttendantFilter(),
|
||||
}).exec();
|
||||
|
||||
if (!employee) {
|
||||
@@ -176,6 +200,63 @@ class HandlerEmployee {
|
||||
}
|
||||
}
|
||||
|
||||
// 共享候选名单:所有在职陪诊师(含实习),供接单陪诊师把订单共享出去。
|
||||
// 与公示接口不同:这里不做姓名脱敏,也不排除实习,但必须本人是在职陪诊师。
|
||||
async getShareableAttendants(ctx) {
|
||||
try {
|
||||
const userId = ctx.state.user?._id;
|
||||
if (!userId) return ResponseUtil.unauthorized(ctx, "登录已失效");
|
||||
|
||||
const me = await DBModel.Employee.findOne({ userId, role: "attendant", status: "active" })
|
||||
.select("_id")
|
||||
.lean()
|
||||
.exec();
|
||||
if (!me) return ResponseUtil.forbidden(ctx, "仅在职陪诊师可查看共享名单");
|
||||
|
||||
const list = await DBModel.Employee.find({ role: "attendant", status: "active" })
|
||||
.select("profile.name attendantInfo.stage attendantInfo.grade attendantInfo.star")
|
||||
.lean()
|
||||
.exec();
|
||||
|
||||
const gradeOrder = { diamond: 0, gold: 1, silver: 2, bronze: 3 };
|
||||
const gradeLabels = { diamond: "钻石", gold: "黄金", silver: "白银", bronze: "青铜" };
|
||||
const attendants = list
|
||||
.filter((item) => String(item._id) !== String(me._id))
|
||||
.map((item) => {
|
||||
const level = HandlerEmployee.normalizeAttendantLevel(item.attendantInfo);
|
||||
return {
|
||||
employeeId: String(item._id),
|
||||
name: item.profile?.name || "",
|
||||
stage: level.stage,
|
||||
grade: level.grade,
|
||||
star: level.star,
|
||||
stageText: level.stage === "intern" ? "实习陪诊师" : "正式陪诊师",
|
||||
gradeText: `${gradeLabels[level.grade]}${level.grade === "diamond" && level.star
|
||||
? ` ${"★".repeat(level.star)}`
|
||||
: ""}`,
|
||||
};
|
||||
})
|
||||
.sort((a, b) => {
|
||||
const stageDiff = (a.stage === "intern" ? 1 : 0) - (b.stage === "intern" ? 1 : 0);
|
||||
if (stageDiff) return stageDiff;
|
||||
return (gradeOrder[a.grade] ?? 9) - (gradeOrder[b.grade] ?? 9);
|
||||
});
|
||||
|
||||
return ResponseUtil.success(ctx, { attendants }, "查询成功");
|
||||
} catch (err) {
|
||||
return ResponseUtil.internalError(ctx, err);
|
||||
}
|
||||
}
|
||||
|
||||
// 阶段/等级归一化后随档案返回,小程序据此展示身份文案与只读提示。
|
||||
// canAcceptOrder 只由 stage 决定,等级不参与鉴权
|
||||
static decorateMyEmployee(employee) {
|
||||
const level = HandlerEmployee.normalizeAttendantLevel(employee.attendantInfo);
|
||||
employee.attendantInfo = { ...(employee.attendantInfo || {}), ...level };
|
||||
employee.canAcceptOrder = level.stage === "certified";
|
||||
return employee;
|
||||
}
|
||||
|
||||
// 当前登录账号的员工档案:登录即可访问,供管理端判定陪诊员身份。
|
||||
// 只返回员工档案本身,不重复做管理员判定(管理员身份由管理端按登录响应的 apps 判定)
|
||||
async getMyEmployee(ctx) {
|
||||
@@ -186,12 +267,63 @@ class HandlerEmployee {
|
||||
}
|
||||
|
||||
const employee = await DBModel.Employee.findOne({ userId })
|
||||
.select("employeeNo role status profile.name profile.sex profile.phone profile.avatar")
|
||||
.select(MY_EMPLOYEE_SELECT)
|
||||
.lean()
|
||||
.exec();
|
||||
|
||||
// 无员工档案不是错误:管理员账号通常没有 Employee 记录
|
||||
return ResponseUtil.success(ctx, { employee: employee || null }, "查询成功");
|
||||
if (!employee) {
|
||||
return ResponseUtil.success(ctx, { employee: null }, "查询成功");
|
||||
}
|
||||
|
||||
return ResponseUtil.success(
|
||||
ctx,
|
||||
{ employee: HandlerEmployee.decorateMyEmployee(employee) },
|
||||
"查询成功"
|
||||
);
|
||||
} catch (err) {
|
||||
return ResponseUtil.internalError(ctx, err);
|
||||
}
|
||||
}
|
||||
|
||||
// 首页接单开关落库:只允许在职且已转正的陪诊师切换
|
||||
async updateMyOnlineStatus(ctx) {
|
||||
try {
|
||||
const userId = ctx.state.user?._id;
|
||||
if (!userId) {
|
||||
return ResponseUtil.unauthorized(ctx, "登录已失效");
|
||||
}
|
||||
|
||||
const isOnline = ctx.request.body?.isOnline;
|
||||
if (typeof isOnline !== "boolean") {
|
||||
return ResponseUtil.badRequest(ctx, "接单状态必须为布尔值");
|
||||
}
|
||||
|
||||
const employee = await DBModel.Employee.findOne({
|
||||
userId,
|
||||
role: "attendant",
|
||||
status: "active",
|
||||
})
|
||||
.select("attendantInfo.stage")
|
||||
.lean()
|
||||
.exec();
|
||||
if (!employee) {
|
||||
return ResponseUtil.forbidden(ctx, "仅在职陪诊师可切换接单状态");
|
||||
}
|
||||
if (HandlerEmployee.normalizeAttendantLevel(employee.attendantInfo).stage === "intern") {
|
||||
return ResponseUtil.forbidden(ctx, "实习陪诊师暂不能接单");
|
||||
}
|
||||
|
||||
const updated = await DBModel.Employee.setOnlineByUserId(userId, isOnline);
|
||||
if (!updated) {
|
||||
return ResponseUtil.forbidden(ctx, "仅在职陪诊师可切换接单状态");
|
||||
}
|
||||
|
||||
return ResponseUtil.success(
|
||||
ctx,
|
||||
{ employee: HandlerEmployee.decorateMyEmployee(updated) },
|
||||
isOnline ? "已开启接单" : "已暂停接单"
|
||||
);
|
||||
} catch (err) {
|
||||
return ResponseUtil.internalError(ctx, err);
|
||||
}
|
||||
@@ -413,6 +545,73 @@ class HandlerEmployee {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 陪诊师阶段/等级调整:实习转正、青铜~钻石调级、钻石设星
|
||||
*
|
||||
* 独立入口而不是并入通用 PUT:等级只做展示与排序、不参与鉴权,
|
||||
* 单独收口可避免误改档案其他字段
|
||||
*/
|
||||
async updateEmployeeLevel(ctx) {
|
||||
try {
|
||||
const { id } = ctx.params;
|
||||
if (!mongoose.Types.ObjectId.isValid(id)) {
|
||||
return ResponseUtil.badRequest(ctx, "员工ID不合法");
|
||||
}
|
||||
|
||||
const body = ctx.request.body || {};
|
||||
const flat = {};
|
||||
|
||||
if (body.stage !== undefined) {
|
||||
if (!HandlerEmployee.STAGES.includes(body.stage)) {
|
||||
return ResponseUtil.badRequest(ctx, "陪诊师阶段取值不正确");
|
||||
}
|
||||
flat["attendantInfo.stage"] = body.stage;
|
||||
}
|
||||
if (body.grade !== undefined) {
|
||||
if (!HandlerEmployee.GRADES.includes(body.grade)) {
|
||||
return ResponseUtil.badRequest(ctx, "陪诊师等级取值不正确");
|
||||
}
|
||||
flat["attendantInfo.grade"] = body.grade;
|
||||
}
|
||||
if (body.star !== undefined) {
|
||||
const star = Number(body.star);
|
||||
if (!Number.isInteger(star) || star < 0 || star > 5) {
|
||||
return ResponseUtil.badRequest(ctx, "星级必须是 0-5 的整数");
|
||||
}
|
||||
flat["attendantInfo.star"] = star;
|
||||
}
|
||||
if (!Object.keys(flat).length) {
|
||||
return ResponseUtil.badRequest(ctx, "没有可更新的字段");
|
||||
}
|
||||
|
||||
const employee = await DBModel.Employee.findById(id)
|
||||
.select("role attendantInfo")
|
||||
.lean()
|
||||
.exec();
|
||||
if (!employee) {
|
||||
return ResponseUtil.notFound(ctx, "员工不存在");
|
||||
}
|
||||
if (employee.role !== "attendant") {
|
||||
return ResponseUtil.badRequest(ctx, "只有陪诊师可设置阶段与等级");
|
||||
}
|
||||
|
||||
const current = HandlerEmployee.normalizeAttendantLevel(employee.attendantInfo);
|
||||
const nextGrade = flat["attendantInfo.grade"] || current.grade;
|
||||
if (flat["attendantInfo.star"] !== undefined && nextGrade !== "diamond") {
|
||||
return ResponseUtil.badRequest(ctx, "只有钻石等级可以设置星级");
|
||||
}
|
||||
// 离开钻石时星级归零,避免展示残留的星星
|
||||
if (flat["attendantInfo.grade"] && current.grade === "diamond" && nextGrade !== "diamond") {
|
||||
flat["attendantInfo.star"] = 0;
|
||||
}
|
||||
|
||||
const updated = await DBModel.Employee.updateEmployee(id, flat);
|
||||
return ResponseUtil.success(ctx, { employee: updated }, "等级已更新");
|
||||
} catch (err) {
|
||||
return ResponseUtil.internalError(ctx, err);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入校验
|
||||
*
|
||||
|
||||
+423
-9
@@ -1,3 +1,4 @@
|
||||
import mongoose from "mongoose";
|
||||
import { DBModel } from "../models/index.js";
|
||||
import services from "../resource/services.js";
|
||||
import ResponseUtil from "../utils/responseUtil.js";
|
||||
@@ -131,6 +132,166 @@ class HandlerEscortRecord {
|
||||
&& String(record.attendant.id) === String(user?._id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取登录账号的在职陪诊师档案;非在职陪诊师返回 null
|
||||
* @param {ObjectId} userId 登录用户ID
|
||||
*/
|
||||
static async loadAttendant(userId) {
|
||||
if (!userId) return null;
|
||||
const employee = await DBModel.Employee.findOne({ userId })
|
||||
.select("employeeNo userId role status profile.name profile.sex profile.phone attendantInfo")
|
||||
.lean()
|
||||
.exec();
|
||||
if (!employee || employee.role !== "attendant" || employee.status !== "active") return null;
|
||||
return employee;
|
||||
}
|
||||
|
||||
// 阶段归一化:存量档案缺字段时按正式陪诊师处理
|
||||
static attendantStage(employee) {
|
||||
return employee?.attendantInfo?.stage === "intern" ? "intern" : "certified";
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析用户下单时选择的陪诊师。
|
||||
*
|
||||
* 客户端只允许传 employeeId;姓名、电话等快照字段一律以服务端档案为准,
|
||||
* 防止用户伪造陪诊师信息。未选择时返回 { employee: null },订单进入公共订单池。
|
||||
* @param {Object} input 客户端提交的 attendant
|
||||
* @returns {Promise<{ employee?: Object, snapshot?: Object, error?: string }>}
|
||||
*/
|
||||
static async resolveSelectedAttendant(input) {
|
||||
const employeeId = input?.employeeId;
|
||||
if (!employeeId) return { employee: null };
|
||||
|
||||
if (!mongoose.Types.ObjectId.isValid(employeeId)) {
|
||||
return { error: "所选陪诊师ID不合法" };
|
||||
}
|
||||
|
||||
const employee = await DBModel.Employee.findOne({
|
||||
_id: employeeId,
|
||||
role: "attendant",
|
||||
status: "active",
|
||||
}).lean().exec();
|
||||
if (!employee) return { error: "所选陪诊师不存在或已不在职" };
|
||||
// 与用户选择、管理端指派的口径一致:实习陪诊师不可被指定
|
||||
if (HandlerEscortRecord.attendantStage(employee) === "intern") {
|
||||
return { error: "实习陪诊师暂不能被指定" };
|
||||
}
|
||||
if (!employee.userId) {
|
||||
return { error: "所选陪诊师暂不可接单,请选择其他陪诊师" };
|
||||
}
|
||||
|
||||
const profile = employee.profile || {};
|
||||
return {
|
||||
employee,
|
||||
snapshot: {
|
||||
id: employee.userId,
|
||||
name: profile.name || "",
|
||||
sex: ["male", "female"].includes(profile.sex) ? profile.sex : "none",
|
||||
mobile: profile.phone || "",
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 回写陪诊师累计接单数。
|
||||
*
|
||||
* 只在订单成功归属到陪诊师之后调用;写失败不回滚已完成的接单,
|
||||
* 只记日志,避免陪诊师明明接到了单却被接口报成失败。
|
||||
* @param {ObjectId} employeeId 员工档案ID(不是登录 userId)
|
||||
*/
|
||||
static async bumpAttendantOrderCount(employeeId) {
|
||||
if (!employeeId) return;
|
||||
try {
|
||||
await DBModel.Employee.incrementOrderCount(employeeId);
|
||||
} catch (err) {
|
||||
logger.error(`[escort] 回写陪诊师接单数失败 employeeId=${employeeId}: ${err?.message || err}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 陪诊师能否查看订单详情:未分配订单、本人已接订单、被共享的订单
|
||||
* 实习陪诊师同样可以看到未分配订单,但接单会被 acceptRecord 拦截
|
||||
*/
|
||||
static canAttendantViewRecord(record, employee, userId) {
|
||||
if (!record || !employee) return false;
|
||||
const employeeId = String(employee._id);
|
||||
const sharedWith = Array.isArray(record.attendant?.sharedWith) ? record.attendant.sharedWith : [];
|
||||
if (sharedWith.some((id) => String(id) === employeeId)) return true;
|
||||
if (!record.attendant?.id) return true;
|
||||
return String(record.attendant.id) === String(userId);
|
||||
}
|
||||
|
||||
// 手机号脱敏:保留前三后四,避免未接单前泄露完整联系方式
|
||||
static maskMobile(mobile) {
|
||||
const text = String(mobile || "");
|
||||
if (!text) return "";
|
||||
if (text.length < 7) return "****";
|
||||
return `${text.slice(0, 3)}****${text.slice(-4)}`;
|
||||
}
|
||||
|
||||
// 姓名脱敏:未接单前只暴露姓
|
||||
static maskName(name) {
|
||||
const text = String(name || "");
|
||||
return text ? `${text.slice(0, 1)}**` : "";
|
||||
}
|
||||
|
||||
// 共享对象入参:兼容 employeeId 与 employeeIds,去重并过滤非法 ObjectId
|
||||
static parseShareEmployeeIds(body = {}) {
|
||||
const raw = body.employeeIds !== undefined ? body.employeeIds : body.employeeId;
|
||||
const list = Array.isArray(raw) ? raw : [raw];
|
||||
const ids = list
|
||||
.filter((id) => typeof id === "string" && /^[0-9a-fA-F]{24}$/.test(id))
|
||||
.map((id) => String(id));
|
||||
return [...new Set(ids)];
|
||||
}
|
||||
|
||||
/**
|
||||
* 陪诊师视角补充展示字段:
|
||||
* - attendantAssignedToMe / attendantSharedToMe / isPoolOrder 供前端决定按钮
|
||||
* - 实习陪诊师看未分配订单时脱敏手机号、详细地址、病历号等
|
||||
* @param {Object} record 已转成普通对象的订单
|
||||
* @param {Object} employee 当前陪诊师档案
|
||||
*/
|
||||
static decorateAttendantRecord(record, employee) {
|
||||
if (!record) return record;
|
||||
const employeeId = String(employee?._id || "");
|
||||
const sharedWith = Array.isArray(record.attendant?.sharedWith)
|
||||
? record.attendant.sharedWith.map((id) => String(id))
|
||||
: [];
|
||||
const assignedToMe = Boolean(record.attendant?.id)
|
||||
&& String(record.attendant.id) === String(employee?.userId || "");
|
||||
const isPoolOrder = !record.attendant?.id;
|
||||
|
||||
record.attendantAssignedToMe = assignedToMe;
|
||||
record.attendantSharedToMe = employeeId ? sharedWith.includes(employeeId) : false;
|
||||
record.attendantSharedEmployeeIds = sharedWith;
|
||||
record.isPoolOrder = isPoolOrder;
|
||||
// 共享名单是内部协作信息,不整体回传
|
||||
if (record.attendant) delete record.attendant.sharedWith;
|
||||
|
||||
if (HandlerEscortRecord.attendantStage(employee) === "intern" && isPoolOrder) {
|
||||
record.desensitized = true;
|
||||
if (record.patient) {
|
||||
record.patient.name = HandlerEscortRecord.maskName(record.patient.name);
|
||||
record.patient.mobile = HandlerEscortRecord.maskMobile(record.patient.mobile);
|
||||
}
|
||||
if (record.hospital) {
|
||||
if (record.hospital.address) record.hospital.address = "接单后可见";
|
||||
if (record.hospital.medicalRecordNo) record.hospital.medicalRecordNo = "接单后可见";
|
||||
}
|
||||
if (record.shippingAddress) {
|
||||
record.shippingAddress.recipientName = HandlerEscortRecord.maskName(record.shippingAddress.recipientName);
|
||||
record.shippingAddress.mobile = HandlerEscortRecord.maskMobile(record.shippingAddress.mobile);
|
||||
if (record.shippingAddress.address) record.shippingAddress.address = "接单后可见";
|
||||
}
|
||||
if (record.inquiry?.medicalHistory) record.inquiry.medicalHistory = "接单后可见";
|
||||
// 诉求是用户自由文本,可能夹带联系方式,一并脱敏
|
||||
if (record.inquiry?.demand) record.inquiry.demand = "接单后可见";
|
||||
}
|
||||
return record;
|
||||
}
|
||||
|
||||
/**
|
||||
* 输出给前端的订单视图:追加支付窗口终点,供列表/详情判断「支付已超时」。
|
||||
* 只派生展示字段,不写库。
|
||||
@@ -312,7 +473,25 @@ class HandlerEscortRecord {
|
||||
const effectiveUserId = mode === "self"
|
||||
? loginUserId
|
||||
: (mode === "all" ? userId : undefined);
|
||||
const attendantId = mode === "attendant" ? loginUserId : undefined;
|
||||
let attendantId;
|
||||
let attendantVisibility;
|
||||
let attendantEmployee = null;
|
||||
let isInternAttendant = false;
|
||||
if (mode === "attendant") {
|
||||
attendantEmployee = await HandlerEscortRecord.loadAttendant(loginUserId);
|
||||
if (!attendantEmployee) {
|
||||
return ResponseUtil.forbidden(ctx, "仅在职陪诊师可查看订单");
|
||||
}
|
||||
const isIntern = HandlerEscortRecord.attendantStage(attendantEmployee) === "intern";
|
||||
isInternAttendant = isIntern;
|
||||
// 可见范围由身份决定,前端传什么都会被忽略:
|
||||
// 实习 = 未分配 + 被共享;正式 = 未分配 + 本人已接 + 被共享
|
||||
attendantVisibility = {
|
||||
includeUnassigned: true,
|
||||
userId: isIntern ? undefined : loginUserId,
|
||||
employeeId: attendantEmployee._id,
|
||||
};
|
||||
}
|
||||
|
||||
// status解析成数组
|
||||
let statusArray = null;
|
||||
@@ -360,16 +539,25 @@ class HandlerEscortRecord {
|
||||
includeCancelled: ["1", "true"].includes(String(includeCancelled)),
|
||||
userId: effectiveUserId,
|
||||
attendantId,
|
||||
attendantVisibility,
|
||||
appointmentDate,
|
||||
healthProfileId,
|
||||
paymentStatus: paymentStatusArray,
|
||||
payChannel,
|
||||
startDate,
|
||||
endDate,
|
||||
keyword,
|
||||
// 实习看池订单只有脱敏展示,keyword 仍按原文匹配手机号/姓名会形成
|
||||
// 「确认某手机号存在订单」的侧信道,直接忽略其搜索词
|
||||
keyword: mode === "attendant" && isInternAttendant ? undefined : keyword,
|
||||
});
|
||||
|
||||
const records = result.records.map((record) => HandlerEscortRecord.withPaymentWindow(record));
|
||||
const records = result.records.map((record) => {
|
||||
const plain = HandlerEscortRecord.withPaymentWindow(record);
|
||||
// 陪诊员列表:补充接单/共享标记,实习看未分配订单时脱敏
|
||||
return mode === "attendant"
|
||||
? HandlerEscortRecord.decorateAttendantRecord(plain, attendantEmployee)
|
||||
: plain;
|
||||
});
|
||||
return ResponseUtil.success(ctx, { ...result, records, page, pageSize }, "查询成功");
|
||||
} catch (err) {
|
||||
return ResponseUtil.internalError(ctx, err);
|
||||
@@ -388,12 +576,223 @@ class HandlerEscortRecord {
|
||||
if (!record) {
|
||||
return ResponseUtil.error(ctx, "陪诊记录不存在", null, 404);
|
||||
}
|
||||
// 归属校验:非本人订单按无权限处理,避免凭 id 遍历他人订单
|
||||
if (!HandlerEscortRecord.canAccess(ctx.state.user, record)) {
|
||||
return ResponseUtil.forbidden(ctx, "无权访问该订单");
|
||||
const user = ctx.state.user;
|
||||
const isOwner = String(record.userId) === String(user?._id);
|
||||
let employee = null;
|
||||
if (!HandlerEscortRecord.isAdmin(user) && !isOwner) {
|
||||
// 陪诊师:本人已接、被共享或未分配订单可查看,其余按无权限处理
|
||||
employee = await HandlerEscortRecord.loadAttendant(user?._id);
|
||||
if (!HandlerEscortRecord.canAccess(user, record)
|
||||
&& !HandlerEscortRecord.canAttendantViewRecord(record, employee, user?._id)) {
|
||||
return ResponseUtil.forbidden(ctx, "无权访问该订单");
|
||||
}
|
||||
}
|
||||
|
||||
return ResponseUtil.success(ctx, { record: HandlerEscortRecord.withPaymentWindow(record) }, "查询成功");
|
||||
const plain = HandlerEscortRecord.withPaymentWindow(record);
|
||||
if (employee) HandlerEscortRecord.decorateAttendantRecord(plain, employee);
|
||||
return ResponseUtil.success(ctx, { record: plain }, "查询成功");
|
||||
} catch (err) {
|
||||
return ResponseUtil.internalError(ctx, err);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 接单:正式陪诊师把未分配订单接到自己名下。
|
||||
* 与支付状态无关;条件更新保证并发下只有一人成功。
|
||||
*/
|
||||
async acceptRecord(ctx) {
|
||||
try {
|
||||
const { id } = ctx.params;
|
||||
if (!id) return ResponseUtil.badRequest(ctx, "缺少记录ID");
|
||||
|
||||
const userId = ctx.state.user?._id;
|
||||
const employee = await HandlerEscortRecord.loadAttendant(userId);
|
||||
if (!employee) return ResponseUtil.forbidden(ctx, "仅在职陪诊师可接单");
|
||||
if (HandlerEscortRecord.attendantStage(employee) === "intern") {
|
||||
return ResponseUtil.forbidden(ctx, "实习陪诊师暂不能接单");
|
||||
}
|
||||
|
||||
const record = await DBModel.EscortRecord.findById(id);
|
||||
if (!record || record.deletedAt) return ResponseUtil.notFound(ctx, "陪诊记录不存在");
|
||||
// 取消中/已取消的订单不进入接单:取消一旦被确认,刚接单的陪诊师会直接失去订单
|
||||
if (record.cancelStatus === "cancelled" || record.status === "cancelled") {
|
||||
return ResponseUtil.error(ctx, "订单已取消,无法接单", null, 409);
|
||||
}
|
||||
if (record.cancelStatus === "requested") {
|
||||
return ResponseUtil.error(ctx, "订单取消申请确认中,暂不能接单", null, 409);
|
||||
}
|
||||
if (record.attendant?.id) {
|
||||
return ResponseUtil.error(ctx, "订单已被其他陪诊师接单", null, 409);
|
||||
}
|
||||
|
||||
const profile = employee.profile || {};
|
||||
const updated = await DBModel.EscortRecord.acceptRecord(id, {
|
||||
id: userId,
|
||||
name: profile.name || "",
|
||||
sex: ["male", "female"].includes(profile.sex) ? profile.sex : "none",
|
||||
mobile: profile.phone || "",
|
||||
});
|
||||
// 并发下另一个陪诊师先接走:条件更新返回 null
|
||||
if (!updated) {
|
||||
return ResponseUtil.error(ctx, "订单已被其他陪诊师接单", null, 409);
|
||||
}
|
||||
|
||||
await HandlerEscortRecord.bumpAttendantOrderCount(employee._id);
|
||||
|
||||
const plain = HandlerEscortRecord.withPaymentWindow(updated);
|
||||
HandlerEscortRecord.decorateAttendantRecord(plain, employee);
|
||||
return ResponseUtil.success(ctx, { record: plain }, "接单成功");
|
||||
} catch (err) {
|
||||
return ResponseUtil.internalError(ctx, err);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 管理员指派:把未分配订单指派给在职正式陪诊师。
|
||||
* 与陪诊师抢单共用条件更新,保证并发下只有一次归属成功。
|
||||
*/
|
||||
async assignRecord(ctx) {
|
||||
try {
|
||||
const { id } = ctx.params;
|
||||
if (!id) return ResponseUtil.badRequest(ctx, "缺少记录ID");
|
||||
if (!mongoose.Types.ObjectId.isValid(id)) {
|
||||
return ResponseUtil.badRequest(ctx, "记录ID不合法");
|
||||
}
|
||||
|
||||
const employeeId = String(ctx.request.body?.employeeId || "");
|
||||
if (!mongoose.Types.ObjectId.isValid(employeeId)) {
|
||||
return ResponseUtil.badRequest(ctx, "请选择陪诊师");
|
||||
}
|
||||
|
||||
const employee = await DBModel.Employee.findOne({
|
||||
_id: employeeId,
|
||||
role: "attendant",
|
||||
status: "active",
|
||||
}).lean().exec();
|
||||
if (!employee) {
|
||||
return ResponseUtil.badRequest(ctx, "陪诊师不存在或已不在职");
|
||||
}
|
||||
// 实习陪诊师不可被指派,与用户选择、系统派单的口径一致
|
||||
if (HandlerEscortRecord.attendantStage(employee) === "intern") {
|
||||
return ResponseUtil.badRequest(ctx, "实习陪诊师暂不能被指派");
|
||||
}
|
||||
// 指派写入的是登录 userId,未绑定账号的档案指派后无法在小程序看到订单
|
||||
if (!employee.userId) {
|
||||
return ResponseUtil.badRequest(ctx, "该陪诊师未绑定登录账号,无法指派");
|
||||
}
|
||||
|
||||
const record = await DBModel.EscortRecord.findById(id);
|
||||
if (!record || record.deletedAt) return ResponseUtil.notFound(ctx, "陪诊记录不存在");
|
||||
if (record.cancelStatus === "cancelled" || record.status === "cancelled") {
|
||||
return ResponseUtil.error(ctx, "订单已取消,无法指派", null, 409);
|
||||
}
|
||||
if (record.cancelStatus === "requested") {
|
||||
return ResponseUtil.error(ctx, "订单取消申请确认中,暂不能指派", null, 409);
|
||||
}
|
||||
if (record.attendant?.id) {
|
||||
return ResponseUtil.error(ctx, "订单已分配陪诊师", null, 409);
|
||||
}
|
||||
|
||||
const profile = employee.profile || {};
|
||||
const updated = await DBModel.EscortRecord.acceptRecord(id, {
|
||||
id: employee.userId,
|
||||
name: profile.name || "",
|
||||
sex: ["male", "female"].includes(profile.sex) ? profile.sex : "none",
|
||||
mobile: profile.phone || "",
|
||||
});
|
||||
// 并发下已被陪诊师接走或另一管理员先指派
|
||||
if (!updated) {
|
||||
return ResponseUtil.error(ctx, "订单已分配陪诊师", null, 409);
|
||||
}
|
||||
|
||||
await HandlerEscortRecord.bumpAttendantOrderCount(employee._id);
|
||||
|
||||
return ResponseUtil.success(
|
||||
ctx,
|
||||
{ record: HandlerEscortRecord.withPaymentWindow(updated) },
|
||||
"指派成功"
|
||||
);
|
||||
} catch (err) {
|
||||
return ResponseUtil.internalError(ctx, err);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 共享订单:仅接单陪诊师本人可操作,共享对象为所有在职陪诊师(含实习)
|
||||
*/
|
||||
async shareRecord(ctx) {
|
||||
try {
|
||||
const { id } = ctx.params;
|
||||
if (!id) return ResponseUtil.badRequest(ctx, "缺少记录ID");
|
||||
|
||||
const userId = ctx.state.user?._id;
|
||||
const employee = await HandlerEscortRecord.loadAttendant(userId);
|
||||
if (!employee) return ResponseUtil.forbidden(ctx, "仅在职陪诊师可共享订单");
|
||||
|
||||
const record = await DBModel.EscortRecord.findById(id);
|
||||
if (!record) return ResponseUtil.notFound(ctx, "陪诊记录不存在");
|
||||
if (record.cancelStatus === "cancelled" || record.status === "cancelled") {
|
||||
return ResponseUtil.error(ctx, "订单已取消,无法共享", null, 409);
|
||||
}
|
||||
if (String(record.attendant?.id || "") !== String(userId)) {
|
||||
return ResponseUtil.forbidden(ctx, "只有接单陪诊师可以共享该订单");
|
||||
}
|
||||
|
||||
const requested = HandlerEscortRecord.parseShareEmployeeIds(ctx.request.body || {});
|
||||
if (requested.length === 0) return ResponseUtil.badRequest(ctx, "请选择要共享的陪诊师");
|
||||
|
||||
// 共享对象必须是在职陪诊师;不允许共享给自己
|
||||
const targets = await DBModel.Employee.find({
|
||||
_id: { $in: requested },
|
||||
role: "attendant",
|
||||
status: "active",
|
||||
}).select("_id").lean().exec();
|
||||
const employeeIds = targets
|
||||
.map((item) => String(item._id))
|
||||
.filter((employeeId) => employeeId !== String(employee._id));
|
||||
if (employeeIds.length === 0) {
|
||||
return ResponseUtil.badRequest(ctx, "共享对象不存在或不是在职陪诊师");
|
||||
}
|
||||
|
||||
const updated = await DBModel.EscortRecord.addSharedWith(id, userId, employeeIds);
|
||||
if (!updated) return ResponseUtil.forbidden(ctx, "只有接单陪诊师可以共享该订单");
|
||||
|
||||
const plain = HandlerEscortRecord.withPaymentWindow(updated);
|
||||
HandlerEscortRecord.decorateAttendantRecord(plain, employee);
|
||||
return ResponseUtil.success(ctx, { record: plain }, "共享成功");
|
||||
} catch (err) {
|
||||
return ResponseUtil.internalError(ctx, err);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消共享:仅接单陪诊师本人可操作
|
||||
*/
|
||||
async unshareRecord(ctx) {
|
||||
try {
|
||||
const { id } = ctx.params;
|
||||
if (!id) return ResponseUtil.badRequest(ctx, "缺少记录ID");
|
||||
|
||||
const userId = ctx.state.user?._id;
|
||||
const employee = await HandlerEscortRecord.loadAttendant(userId);
|
||||
if (!employee) return ResponseUtil.forbidden(ctx, "仅在职陪诊师可操作共享");
|
||||
|
||||
// 与 shareRecord 对齐:订单不存在按 404,已取消不允许再操作共享名单
|
||||
const record = await DBModel.EscortRecord.findById(id);
|
||||
if (!record) return ResponseUtil.notFound(ctx, "陪诊记录不存在");
|
||||
if (record.cancelStatus === "cancelled" || record.status === "cancelled") {
|
||||
return ResponseUtil.error(ctx, "订单已取消,无法操作共享", null, 409);
|
||||
}
|
||||
|
||||
const employeeIds = HandlerEscortRecord.parseShareEmployeeIds(ctx.request.body || {});
|
||||
if (employeeIds.length === 0) return ResponseUtil.badRequest(ctx, "请选择要取消共享的陪诊师");
|
||||
|
||||
const updated = await DBModel.EscortRecord.removeSharedWith(id, userId, employeeIds);
|
||||
if (!updated) return ResponseUtil.forbidden(ctx, "只有接单陪诊师可以取消共享");
|
||||
|
||||
const plain = HandlerEscortRecord.withPaymentWindow(updated);
|
||||
HandlerEscortRecord.decorateAttendantRecord(plain, employee);
|
||||
return ResponseUtil.success(ctx, { record: plain }, "已取消共享");
|
||||
} catch (err) {
|
||||
return ResponseUtil.internalError(ctx, err);
|
||||
}
|
||||
@@ -453,8 +852,19 @@ class HandlerEscortRecord {
|
||||
const addressResult = HandlerEscortRecord.buildShippingAddress(body.shippingAddress, profile);
|
||||
|
||||
const payload = HandlerEscortRecord.pickFields(body);
|
||||
// 陪诊员分配由平台决定:用户下单时提交的 attendant 一律丢弃,防止自选/伪造陪诊员
|
||||
if (!isAdmin) delete payload.attendant;
|
||||
// 用户可在下单时指定陪诊师:只认 employeeId,快照字段由服务端回填。
|
||||
// 未指定则清空,订单进入公共订单池等待抢单或管理端指派。
|
||||
let selectedEmployee = null;
|
||||
if (!isAdmin) {
|
||||
const selected = await HandlerEscortRecord.resolveSelectedAttendant(body.attendant);
|
||||
if (selected.error) return ResponseUtil.badRequest(ctx, selected.error);
|
||||
if (selected.employee) {
|
||||
selectedEmployee = selected.employee;
|
||||
payload.attendant = { ...selected.snapshot, sharedWith: [] };
|
||||
} else {
|
||||
delete payload.attendant;
|
||||
}
|
||||
}
|
||||
// 归属以档案为准:管理员传入的 userId 一律忽略,无归属档案留空
|
||||
payload.userId = orderUserId;
|
||||
// healthProfileId 以校验通过的档案为准,覆盖客户端原值
|
||||
@@ -513,6 +923,10 @@ class HandlerEscortRecord {
|
||||
|
||||
try {
|
||||
const newRecord = await DBModel.EscortRecord.createRecord(payload);
|
||||
// 用户指定陪诊师即视为接单成功,累计接单数按同一口径回写
|
||||
if (selectedEmployee) {
|
||||
await HandlerEscortRecord.bumpAttendantOrderCount(selectedEmployee._id);
|
||||
}
|
||||
return ResponseUtil.success(ctx, { record: newRecord }, "创建成功");
|
||||
} catch (err) {
|
||||
// 并发重复提交由唯一索引兜底,后到的请求返回已创建订单。
|
||||
|
||||
@@ -3,6 +3,10 @@
|
||||
import mongoose from "mongoose";
|
||||
import { pinyin } from "pinyin-pro";
|
||||
|
||||
// /employee/me 与接单开关返回的公共字段
|
||||
const MY_EMPLOYEE_SELECT =
|
||||
"employeeNo role status profile.name profile.sex profile.phone profile.avatar attendantInfo";
|
||||
|
||||
/**
|
||||
* 培训记录子文档
|
||||
*
|
||||
@@ -99,13 +103,31 @@ const EmployeeSchema = mongoose.Schema(
|
||||
// 陪诊员特有信息
|
||||
attendantInfo: {
|
||||
serviceAreas: { type: [String], default: [], comment: "服务区域/城市列表" },
|
||||
// 陪诊师阶段:intern=实习(只可查看,不能接单/被指派),certified=正式。
|
||||
// 存量档案没有该字段,读取时一律按 certified 处理(见 handler/employee.js)
|
||||
stage: {
|
||||
type: String,
|
||||
enum: ["intern", "certified"],
|
||||
default: "certified",
|
||||
comment: "陪诊师阶段:实习/正式",
|
||||
},
|
||||
// 等级仅用于展示与排序,不参与权限判定;默认青铜
|
||||
grade: {
|
||||
type: String,
|
||||
enum: ["bronze", "silver", "gold", "diamond"],
|
||||
default: "bronze",
|
||||
comment: "陪诊师等级:青铜/白银/黄金/钻石",
|
||||
},
|
||||
// 星级仅在钻石等级下有效(0-5),其他等级由接口归零
|
||||
star: { type: Number, default: 0, min: 0, max: 5, comment: "钻石等级下的星级" },
|
||||
specialties: { type: [String], default: [], comment: "擅长科室/领域" },
|
||||
languages: { type: [String], default: ["普通话"], comment: "掌握语言" },
|
||||
// 注意:本字段全项目没有任何写入点(恒为默认 5.0),不可作为评级来源。
|
||||
// 对外评级一律由 employee_review 真实评价聚合得出(employee_review.js),
|
||||
// 达阈值才展示,详见 .trae/documents/陪诊师公示与指定陪诊师.md
|
||||
// 达阈值才展示,详见 doc/architecture-overview.html 第 10 节「陪诊师成长与资金闭环」
|
||||
rating: { type: Number, default: 5.0, min: 1, max: 5, comment: "评分(无写入点,勿用于公示)" },
|
||||
orderCount: { type: Number, default: 0, comment: "接单数量" },
|
||||
// 累计接单数:陪诊师接单 / 管理员指派成功时 +1;取消不扣减
|
||||
orderCount: { type: Number, default: 0, comment: "累计接单数量" },
|
||||
isOnline: { type: Boolean, default: false, comment: "是否在线" },
|
||||
},
|
||||
|
||||
@@ -239,8 +261,8 @@ EmployeeSchema.statics.updateEmployee = async function (id, update) {
|
||||
/**
|
||||
* 查询在线陪诊员列表
|
||||
*
|
||||
* 排序口径与 handler/employee.js 的公示列表保持一致:服务年限优先、累计服务次数次之。
|
||||
* 曾按 attendantInfo.rating 排,但该字段没有任何写入点(恒为 5.0),等于没排序
|
||||
* 排序口径与 handler/employee.js 的公示列表保持一致:服务年限优先,累计接单数次之。
|
||||
* rating 仍无写入点,不参与排序。
|
||||
*/
|
||||
EmployeeSchema.statics.findOnlineAttendants = async function (options = {}) {
|
||||
const { page = 1, pageSize = 20 } = options;
|
||||
@@ -256,6 +278,40 @@ EmployeeSchema.statics.findOnlineAttendants = async function (options = {}) {
|
||||
.exec();
|
||||
};
|
||||
|
||||
/**
|
||||
* 累计接单数原子加一(接单 / 管理员指派成功后调用)
|
||||
*/
|
||||
EmployeeSchema.statics.incrementOrderCount = async function (employeeId) {
|
||||
if (!employeeId) return null;
|
||||
return await this.updateOne(
|
||||
{ _id: employeeId },
|
||||
{
|
||||
$inc: { "attendantInfo.orderCount": 1 },
|
||||
$set: { "meta.updatetime": Date.now() },
|
||||
}
|
||||
).exec();
|
||||
};
|
||||
|
||||
/**
|
||||
* 按登录账号更新接单开关(仅在职陪诊师)
|
||||
*/
|
||||
EmployeeSchema.statics.setOnlineByUserId = async function (userId, isOnline) {
|
||||
if (!userId) return null;
|
||||
return await this.findOneAndUpdate(
|
||||
{ userId, role: "attendant", status: "active" },
|
||||
{
|
||||
$set: {
|
||||
"attendantInfo.isOnline": Boolean(isOnline),
|
||||
"meta.updatetime": Date.now(),
|
||||
},
|
||||
},
|
||||
{ new: true }
|
||||
)
|
||||
.select(MY_EMPLOYEE_SELECT)
|
||||
.lean()
|
||||
.exec();
|
||||
};
|
||||
|
||||
// ==================== 索引定义 ====================
|
||||
|
||||
EmployeeSchema.index({ "profile.name": 1 });
|
||||
@@ -274,5 +330,5 @@ EmployeeSchema.index(
|
||||
);
|
||||
|
||||
// genPinyin/genPinyinFL 一并导出:handler 层走 findOneAndUpdate 时绕过了 pre("save"),
|
||||
// 需要自行同步拼音字段
|
||||
export { EmployeeSchema, genPinyin, genPinyinFL };
|
||||
// 需要自行同步拼音字段。MY_EMPLOYEE_SELECT 供 handler 复用,避免字段清单两处漂移
|
||||
export { EmployeeSchema, genPinyin, genPinyinFL, MY_EMPLOYEE_SELECT };
|
||||
|
||||
@@ -10,7 +10,7 @@ import mongoose from "mongoose";
|
||||
* 但表本身按「员工」建,不绑死陪诊师——后期护士 / 医生等角色接评价时直接复用,
|
||||
* 不必再建一张平行的表(这也是它叫 employee_review 而不是 attendant_review 的原因)。
|
||||
*
|
||||
* 设计前提(见 .trae/documents/陪诊师公示与指定陪诊师.md):
|
||||
* 设计前提(见 doc/architecture-overview.html 第 07/10 节):
|
||||
* - 一次服务最多产生一条评价,故 recordId 建唯一索引,从数据层断掉刷分
|
||||
* - 评级不由 employee.attendantInfo.rating 提供——那个字段没有任何写入点
|
||||
* - 只有 status 为 published 的评价计入评分,运营隐藏掉的不参与
|
||||
|
||||
@@ -89,6 +89,13 @@ const EscortRecordSchema = mongoose.Schema(
|
||||
name: { type: String, default: "", comment: "陪诊员姓名" },
|
||||
sex: { type: String, enum: ["none", "male", "female"], comment: "陪诊员性别" },
|
||||
mobile: { type: String, default: "", comment: "陪诊员联系电话" },
|
||||
// 接单陪诊师把订单共享给其他陪诊师:存 employee._id(不是登录 userId),
|
||||
// 这样展示共享对象时不依赖账号绑定关系
|
||||
sharedWith: [{
|
||||
type: mongoose.Schema.Types.ObjectId,
|
||||
ref: "employee",
|
||||
comment: "共享给其他陪诊师(employee._id)",
|
||||
}],
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -245,6 +252,7 @@ const EscortRecordSchema = mongoose.Schema(
|
||||
EscortRecordSchema.statics.buildRecordFilter = function (options = {}) {
|
||||
const {
|
||||
status, cancelStatus, userId, appointmentDate, healthProfileId, attendantId,
|
||||
attendantVisibility,
|
||||
includeDeleted = false, includeCancelled = false,
|
||||
paymentStatus, payChannel, startDate, endDate, keyword,
|
||||
} = options;
|
||||
@@ -279,6 +287,19 @@ EscortRecordSchema.statics.buildRecordFilter = function (options = {}) {
|
||||
if (userId) filter.userId = userId;
|
||||
// 陪诊员列表:只查 assign 到本人的订单
|
||||
if (attendantId) filter["attendant.id"] = attendantId;
|
||||
// 陪诊师订单池:按身份合并可见范围,scope 由服务端计算,不接受前端传入。
|
||||
// - includeUnassigned:所有在职陪诊师都能看到未分配订单
|
||||
// - userId:正式陪诊师还能看到自己已接的订单(实习不能接单,故不传)
|
||||
// - employeeId:被共享的订单
|
||||
if (attendantVisibility) {
|
||||
const or = [];
|
||||
if (attendantVisibility.includeUnassigned) or.push({ "attendant.id": null });
|
||||
if (attendantVisibility.userId) or.push({ "attendant.id": attendantVisibility.userId });
|
||||
if (attendantVisibility.employeeId) {
|
||||
or.push({ "attendant.sharedWith": attendantVisibility.employeeId });
|
||||
}
|
||||
if (or.length) andConditions.push({ $or: or });
|
||||
}
|
||||
if (healthProfileId) filter.healthProfileId = healthProfileId;
|
||||
|
||||
// 支付状态 / 渠道:管理端按收款情况对账时使用
|
||||
@@ -456,6 +477,53 @@ EscortRecordSchema.statics.findByIdempotencyKey = async function (userId, idempo
|
||||
return await this.findOne({ userId, idempotencyKey, deletedAt: null }).exec();
|
||||
};
|
||||
|
||||
// 接单:只有「未分配」的订单能被接走,条件更新保证并发下只有一个陪诊师成功。
|
||||
// 与支付状态无关:订单是否付款不影响接单;取消中(requested)与已取消订单不可接。
|
||||
EscortRecordSchema.statics.acceptRecord = async function (id, attendant) {
|
||||
return await this.findOneAndUpdate(
|
||||
{
|
||||
_id: id,
|
||||
deletedAt: null,
|
||||
"attendant.id": null,
|
||||
cancelStatus: { $nin: ["cancelled", "requested"] },
|
||||
status: { $ne: "cancelled" },
|
||||
},
|
||||
{
|
||||
$set: {
|
||||
"attendant.id": attendant.id,
|
||||
"attendant.name": attendant.name || "",
|
||||
"attendant.sex": attendant.sex || "none",
|
||||
"attendant.mobile": attendant.mobile || "",
|
||||
"meta.updatetime": Date.now(),
|
||||
},
|
||||
},
|
||||
{ new: true, runValidators: true }
|
||||
);
|
||||
};
|
||||
|
||||
// 共享 / 取消共享:只有当前接单陪诊师本人能操作,已取消订单不再变动共享名单
|
||||
EscortRecordSchema.statics.addSharedWith = async function (id, attendantId, employeeIds) {
|
||||
return await this.findOneAndUpdate(
|
||||
{ _id: id, deletedAt: null, "attendant.id": attendantId, cancelStatus: { $ne: "cancelled" }, status: { $ne: "cancelled" } },
|
||||
{
|
||||
$addToSet: { "attendant.sharedWith": { $each: employeeIds } },
|
||||
$set: { "meta.updatetime": Date.now() },
|
||||
},
|
||||
{ new: true, runValidators: true }
|
||||
);
|
||||
};
|
||||
|
||||
EscortRecordSchema.statics.removeSharedWith = async function (id, attendantId, employeeIds) {
|
||||
return await this.findOneAndUpdate(
|
||||
{ _id: id, deletedAt: null, "attendant.id": attendantId, cancelStatus: { $ne: "cancelled" }, status: { $ne: "cancelled" } },
|
||||
{
|
||||
$pull: { "attendant.sharedWith": { $in: employeeIds } },
|
||||
$set: { "meta.updatetime": Date.now() },
|
||||
},
|
||||
{ new: true, runValidators: true }
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* 根据ID查找陪诊记录
|
||||
*
|
||||
|
||||
@@ -107,6 +107,12 @@ function registerRoutes(app) {
|
||||
router.post("/escort-record/:id/refund/manual", requireAuth, requireAdmin, handlerPayment.completeManualRefund.bind(handlerPayment));
|
||||
|
||||
router.get("/escort-record/:id", requireAuth, handlerEscortRecord.getRecordById.bind(handlerEscortRecord));
|
||||
// 陪诊师接单 / 共享:身份与可见范围全部由服务端判定
|
||||
router.post("/escort-record/:id/accept", requireAuth, updateLimit, handlerEscortRecord.acceptRecord.bind(handlerEscortRecord));
|
||||
// 管理端指派:仅未分配订单,目标须在职正式陪诊师,条件更新防并发
|
||||
router.post("/escort-record/:id/assign", requireAuth, requireAdmin, updateLimit, handlerEscortRecord.assignRecord.bind(handlerEscortRecord));
|
||||
router.post("/escort-record/:id/share", requireAuth, updateLimit, handlerEscortRecord.shareRecord.bind(handlerEscortRecord));
|
||||
router.post("/escort-record/:id/unshare", requireAuth, updateLimit, handlerEscortRecord.unshareRecord.bind(handlerEscortRecord));
|
||||
router.post("/escort-record", requireAuth, createLimit, handlerEscortRecord.createRecord.bind(handlerEscortRecord));
|
||||
router.put("/escort-record/:id", requireAuth, updateLimit, handlerEscortRecord.updateRecord.bind(handlerEscortRecord));
|
||||
router.patch("/escort-record/:id/status", requireAuth, updateLimit, handlerEscortRecord.updateStatus.bind(handlerEscortRecord));
|
||||
@@ -137,7 +143,11 @@ function registerRoutes(app) {
|
||||
|
||||
// 当前账号的员工档案:登录即可访问,供管理端判定陪诊员身份。
|
||||
// 必须注册在 /employee 管理端路由之前,避免被更宽的路由抢占
|
||||
// 共享订单时的陪诊师候选:在职陪诊师(含实习)可访问,最多看到姓名与等级
|
||||
router.get("/employee/shareable-attendants", requireAuth, handlerEmployee.getShareableAttendants.bind(handlerEmployee));
|
||||
router.get("/employee/me", requireAuth, handlerEmployee.getMyEmployee.bind(handlerEmployee));
|
||||
// 首页接单开关落库:仅在职正式陪诊师可切换
|
||||
router.patch("/employee/me/online", requireAuth, updateLimit, handlerEmployee.updateMyOnlineStatus.bind(handlerEmployee));
|
||||
|
||||
// 员工(陪诊师)管理:管理端 CRUD,必须管理员。
|
||||
// 与下方公开的 /attendant 公示接口分离:这里返回真实姓名等内部字段
|
||||
@@ -145,6 +155,8 @@ function registerRoutes(app) {
|
||||
router.post("/employee", requireAuth, requireAdmin, createLimit, handlerEmployee.createEmployee.bind(handlerEmployee));
|
||||
router.put("/employee/:id", requireAuth, requireAdmin, updateLimit, handlerEmployee.updateEmployee.bind(handlerEmployee));
|
||||
router.patch("/employee/:id/status", requireAuth, requireAdmin, updateLimit, handlerEmployee.updateEmployeeStatus.bind(handlerEmployee));
|
||||
// 实习转正 / 调级 / 钻石设星:等级只做展示与排序,权限仍只看 stage
|
||||
router.patch("/employee/:id/level", requireAuth, requireAdmin, updateLimit, handlerEmployee.updateEmployeeLevel.bind(handlerEmployee));
|
||||
|
||||
// 陪诊师入驻申请:提交与查询本人申请只要求登录;审核相关必须管理员。
|
||||
// /mine 必须注册在 /:id 之前,否则会被当成 id
|
||||
|
||||
@@ -300,6 +300,155 @@ test("审批遇岗位冲突时申请单回滚为 pending,不卡死在 approved
|
||||
});
|
||||
});
|
||||
|
||||
test("入驻审批新建档案只写实习阶段,重新激活不改动已有阶段", async () => {
|
||||
let capturedUpdate = null;
|
||||
const stub = {
|
||||
findOne: () => ({
|
||||
select: () => ({ lean: () => ({ exec: async () => ({ role: "attendant" }) }) }),
|
||||
}),
|
||||
findOneAndUpdate: (filter, update) => {
|
||||
capturedUpdate = update;
|
||||
return { exec: async () => ({ _id: OWNER_ID }) };
|
||||
},
|
||||
};
|
||||
|
||||
await withModel("Employee", stub, async () => {
|
||||
await HandlerAttendantApplication.ensureEmployee({
|
||||
userId: OWNER_ID,
|
||||
applicant: { name: "张三" },
|
||||
materials: {},
|
||||
});
|
||||
|
||||
// 只在插入时给实习身份;$set 不能带 stage,否则重新审批会把已转正的人降回实习
|
||||
assert.equal(capturedUpdate.$setOnInsert["attendantInfo.stage"], "intern");
|
||||
assert.equal(capturedUpdate.$set["attendantInfo.stage"], undefined);
|
||||
});
|
||||
});
|
||||
|
||||
test("公示口径排除实习陪诊师,存量无阶段字段仍视为正式", () => {
|
||||
const filter = HandlerEmployee.publicAttendantFilter();
|
||||
assert.equal(filter.role, "attendant");
|
||||
assert.equal(filter.status, "active");
|
||||
// $ne 对缺失字段同样成立,历史档案不会被误排除
|
||||
assert.deepEqual(filter["attendantInfo.stage"], { $ne: "intern" });
|
||||
});
|
||||
|
||||
test("/employee/me 对存量档案按正式陪诊师归一化并允许接单", async () => {
|
||||
const stub = {
|
||||
findOne: () => ({
|
||||
select: () => ({
|
||||
lean: () => ({ exec: async () => ({ _id: OWNER_ID, role: "attendant", status: "active" }) }),
|
||||
}),
|
||||
}),
|
||||
};
|
||||
|
||||
await withModel("Employee", stub, async () => {
|
||||
const ctx = createCtx({ user: { _id: OWNER_ID } });
|
||||
await new HandlerEmployee().getMyEmployee(ctx);
|
||||
|
||||
assert.equal(ctx.body.code, 0);
|
||||
assert.equal(ctx.body.data.employee.attendantInfo.stage, "certified");
|
||||
assert.equal(ctx.body.data.employee.attendantInfo.grade, "bronze");
|
||||
assert.equal(ctx.body.data.employee.canAcceptOrder, true);
|
||||
});
|
||||
});
|
||||
|
||||
test("/employee/me 实习陪诊师不可接单,非钻石等级星级归零", async () => {
|
||||
const stub = {
|
||||
findOne: () => ({
|
||||
select: () => ({
|
||||
lean: () => ({
|
||||
exec: async () => ({
|
||||
_id: OWNER_ID,
|
||||
role: "attendant",
|
||||
status: "active",
|
||||
attendantInfo: { stage: "intern", grade: "silver", star: 3 },
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
};
|
||||
|
||||
await withModel("Employee", stub, async () => {
|
||||
const ctx = createCtx({ user: { _id: OWNER_ID } });
|
||||
await new HandlerEmployee().getMyEmployee(ctx);
|
||||
|
||||
assert.equal(ctx.body.code, 0);
|
||||
assert.equal(ctx.body.data.employee.attendantInfo.stage, "intern");
|
||||
assert.equal(ctx.body.data.employee.attendantInfo.star, 0);
|
||||
assert.equal(ctx.body.data.employee.canAcceptOrder, false);
|
||||
});
|
||||
});
|
||||
|
||||
/** Employee.findById 链式 stub + updateEmployee 记录 */
|
||||
function levelStub(employee) {
|
||||
const calls = { update: null };
|
||||
return {
|
||||
calls,
|
||||
findById: () => ({
|
||||
select: () => ({ lean: () => ({ exec: async () => employee }) }),
|
||||
}),
|
||||
updateEmployee: async (id, flat) => {
|
||||
calls.update = flat;
|
||||
return { _id: id };
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
test("管理员可把实习陪诊师转正并调级", async () => {
|
||||
const stub = levelStub({ role: "attendant", attendantInfo: { stage: "intern", grade: "bronze" } });
|
||||
|
||||
await withModel("Employee", stub, async () => {
|
||||
const ctx = createCtx({
|
||||
params: { id: OWNER_ID },
|
||||
body: { stage: "certified", grade: "silver" },
|
||||
});
|
||||
await new HandlerEmployee().updateEmployeeLevel(ctx);
|
||||
|
||||
assert.equal(ctx.body.code, 0);
|
||||
assert.deepEqual(stub.calls.update, {
|
||||
"attendantInfo.stage": "certified",
|
||||
"attendantInfo.grade": "silver",
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
test("非钻石等级不能设置星级", async () => {
|
||||
const stub = levelStub({ role: "attendant", attendantInfo: { grade: "gold" } });
|
||||
|
||||
await withModel("Employee", stub, async () => {
|
||||
const ctx = createCtx({ params: { id: OWNER_ID }, body: { star: 3 } });
|
||||
await new HandlerEmployee().updateEmployeeLevel(ctx);
|
||||
|
||||
assert.equal(ctx.body.code, 400);
|
||||
assert.equal(stub.calls.update, null);
|
||||
});
|
||||
});
|
||||
|
||||
test("离开钻石等级时星级自动归零", async () => {
|
||||
const stub = levelStub({ role: "attendant", attendantInfo: { grade: "diamond", star: 4 } });
|
||||
|
||||
await withModel("Employee", stub, async () => {
|
||||
const ctx = createCtx({ params: { id: OWNER_ID }, body: { grade: "gold" } });
|
||||
await new HandlerEmployee().updateEmployeeLevel(ctx);
|
||||
|
||||
assert.equal(ctx.body.code, 0);
|
||||
assert.equal(stub.calls.update["attendantInfo.star"], 0);
|
||||
});
|
||||
});
|
||||
|
||||
test("非陪诊师岗位不能设置阶段与等级", async () => {
|
||||
const stub = levelStub({ role: "nurse" });
|
||||
|
||||
await withModel("Employee", stub, async () => {
|
||||
const ctx = createCtx({ params: { id: OWNER_ID }, body: { grade: "gold" } });
|
||||
await new HandlerEmployee().updateEmployeeLevel(ctx);
|
||||
|
||||
assert.equal(ctx.body.code, 400);
|
||||
assert.equal(stub.calls.update, null);
|
||||
});
|
||||
});
|
||||
|
||||
test("身份证被其他岗位档案占用时,查重文案按实际岗位提示", async () => {
|
||||
const employeeStub = employeeFindOneStub((filter) => {
|
||||
// 按账号查:本账号无档案;按身份证查重:命中护士档案
|
||||
|
||||
@@ -0,0 +1,275 @@
|
||||
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 { HandlerEmployee } from "../handler/employee.js";
|
||||
|
||||
const RECORD_ID = "64b000000000000000000010";
|
||||
const OWNER_ID = "64b000000000000000000001";
|
||||
const ADMIN_ID = "64b000000000000000000009";
|
||||
const EMPLOYEE_ID = "64b000000000000000000033";
|
||||
const EMPLOYEE_USER_ID = "64b000000000000000000003";
|
||||
const INTERN_EMPLOYEE_ID = "64b000000000000000000022";
|
||||
|
||||
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: "北京市东城区" },
|
||||
schedule: { date: new Date("2026-12-01T00:00:00.000Z"), startTime: "08:00", endTime: "17:00" },
|
||||
payment: { totalFee: 500, paidFee: 0, status: "unpaid" },
|
||||
attendant: { id: null, name: "", sharedWith: [] },
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
function buildEmployee(overrides = {}) {
|
||||
return {
|
||||
_id: EMPLOYEE_ID,
|
||||
userId: EMPLOYEE_USER_ID,
|
||||
role: "attendant",
|
||||
status: "active",
|
||||
profile: { name: "李四", sex: "male", phone: "13700137000" },
|
||||
attendantInfo: { stage: "certified", grade: "bronze", isOnline: false, orderCount: 3 },
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
function createCtx({ body = {}, user = { _id: ADMIN_ID }, params = { id: RECORD_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 runAssign({ employee = buildEmployee(), record = buildRecord(), updated } = {}) {
|
||||
const calls = { accepted: null, bumped: null };
|
||||
const employeeStub = employeeFindOneStub(employee, {
|
||||
incrementOrderCount: async (id) => {
|
||||
calls.bumped = id;
|
||||
},
|
||||
});
|
||||
const recordStub = {
|
||||
findById: async () => record,
|
||||
acceptRecord: async (id, attendant) => {
|
||||
calls.accepted = attendant;
|
||||
return updated === undefined
|
||||
? { ...record, attendant: { id: attendant.id, name: attendant.name, sharedWith: [] } }
|
||||
: updated;
|
||||
},
|
||||
};
|
||||
|
||||
const ctx = createCtx({ body: { employeeId: EMPLOYEE_ID } });
|
||||
await withModel("Employee", employeeStub, () =>
|
||||
withModel("EscortRecord", recordStub, () => new HandlerEscortRecord().assignRecord(ctx))
|
||||
);
|
||||
return { ctx, calls };
|
||||
}
|
||||
|
||||
test("管理员指派成功:写入陪诊师快照并累加接单数", async () => {
|
||||
const { ctx, calls } = await runAssign();
|
||||
|
||||
assert.equal(ctx.body.code, 0);
|
||||
assert.equal(calls.accepted.id, EMPLOYEE_USER_ID);
|
||||
assert.equal(calls.accepted.name, "李四");
|
||||
assert.equal(calls.accepted.sex, "male");
|
||||
assert.equal(calls.accepted.mobile, "13700137000");
|
||||
assert.equal(calls.bumped, EMPLOYEE_ID);
|
||||
});
|
||||
|
||||
test("指派参数校验:非法记录 ID 或陪诊师 ID 返回 400", async () => {
|
||||
const badRecordCtx = createCtx({ body: { employeeId: EMPLOYEE_ID }, params: { id: "bad-id" } });
|
||||
await new HandlerEscortRecord().assignRecord(badRecordCtx);
|
||||
assert.equal(badRecordCtx.body.code, 400);
|
||||
|
||||
const badEmployeeCtx = createCtx({ body: { employeeId: "bad-id" } });
|
||||
await new HandlerEscortRecord().assignRecord(badEmployeeCtx);
|
||||
assert.equal(badEmployeeCtx.body.code, 400);
|
||||
});
|
||||
|
||||
test("订单已分配时指派返回 409,不重复写库也不累加", async () => {
|
||||
const { ctx, calls } = await runAssign({
|
||||
record: buildRecord({ attendant: { id: OWNER_ID, name: "张三", sharedWith: [] } }),
|
||||
});
|
||||
|
||||
assert.equal(ctx.body.code, 409);
|
||||
assert.equal(calls.accepted, null);
|
||||
assert.equal(calls.bumped, null);
|
||||
});
|
||||
|
||||
test("并发下订单已被接走:条件更新失败返回 409 且不累加", async () => {
|
||||
const { ctx, calls } = await runAssign({ updated: null });
|
||||
|
||||
assert.equal(ctx.body.code, 409);
|
||||
assert.notEqual(calls.accepted, null);
|
||||
assert.equal(calls.bumped, null);
|
||||
});
|
||||
|
||||
test("实习陪诊师不能被指派", async () => {
|
||||
const { ctx, calls } = await runAssign({
|
||||
employee: buildEmployee({
|
||||
_id: INTERN_EMPLOYEE_ID,
|
||||
attendantInfo: { stage: "intern", grade: "bronze" },
|
||||
}),
|
||||
});
|
||||
|
||||
assert.equal(ctx.body.code, 400);
|
||||
assert.equal(calls.accepted, null);
|
||||
assert.equal(calls.bumped, null);
|
||||
});
|
||||
|
||||
test("非在职陪诊师不能被指派", async () => {
|
||||
const { ctx, calls } = await runAssign({ employee: null });
|
||||
|
||||
assert.equal(ctx.body.code, 400);
|
||||
assert.equal(calls.accepted, null);
|
||||
});
|
||||
|
||||
test("未绑定登录账号的陪诊师不能被指派", async () => {
|
||||
const { ctx, calls } = await runAssign({
|
||||
employee: buildEmployee({ userId: null }),
|
||||
});
|
||||
|
||||
assert.equal(ctx.body.code, 400);
|
||||
assert.equal(calls.accepted, null);
|
||||
});
|
||||
|
||||
test("取消中或已取消订单不能指派", async () => {
|
||||
const { ctx: requestedCtx } = await runAssign({
|
||||
record: buildRecord({ cancelStatus: "requested" }),
|
||||
});
|
||||
assert.equal(requestedCtx.body.code, 409);
|
||||
|
||||
const { ctx: cancelledCtx } = await runAssign({
|
||||
record: buildRecord({ status: "cancelled" }),
|
||||
});
|
||||
assert.equal(cancelledCtx.body.code, 409);
|
||||
});
|
||||
|
||||
test("接单开关:正式陪诊师切换后落库并返回最新状态", async () => {
|
||||
let saved = null;
|
||||
const employee = buildEmployee();
|
||||
const stub = {
|
||||
findOne: () => ({
|
||||
select: () => ({
|
||||
lean: () => ({ exec: async () => ({ attendantInfo: employee.attendantInfo }) }),
|
||||
}),
|
||||
}),
|
||||
setOnlineByUserId: async (userId, isOnline) => {
|
||||
saved = { userId, isOnline };
|
||||
return { ...employee, attendantInfo: { ...employee.attendantInfo, isOnline } };
|
||||
},
|
||||
};
|
||||
|
||||
const ctx = createCtx({ body: { isOnline: true } });
|
||||
await withModel("Employee", stub, () => new HandlerEmployee().updateMyOnlineStatus(ctx));
|
||||
|
||||
assert.equal(ctx.body.code, 0);
|
||||
assert.deepEqual(saved, { userId: ADMIN_ID, isOnline: true });
|
||||
assert.equal(ctx.body.data.employee.attendantInfo.isOnline, true);
|
||||
assert.equal(ctx.body.data.employee.canAcceptOrder, true);
|
||||
});
|
||||
|
||||
test("接单开关:实习陪诊师返回 403 且不写库", async () => {
|
||||
let called = false;
|
||||
const stub = {
|
||||
findOne: () => ({
|
||||
select: () => ({
|
||||
lean: () => ({ exec: async () => ({ attendantInfo: { stage: "intern" } }) }),
|
||||
}),
|
||||
}),
|
||||
setOnlineByUserId: async () => {
|
||||
called = true;
|
||||
return null;
|
||||
},
|
||||
};
|
||||
|
||||
const ctx = createCtx({ body: { isOnline: true } });
|
||||
await withModel("Employee", stub, () => new HandlerEmployee().updateMyOnlineStatus(ctx));
|
||||
|
||||
assert.equal(ctx.body.code, 403);
|
||||
assert.equal(called, false);
|
||||
});
|
||||
|
||||
test("接单开关:非陪诊师账号返回 403,参数非布尔返回 400", async () => {
|
||||
const noEmployeeStub = {
|
||||
findOne: () => ({
|
||||
select: () => ({
|
||||
lean: () => ({ exec: async () => null }),
|
||||
}),
|
||||
}),
|
||||
};
|
||||
const forbiddenCtx = createCtx({ body: { isOnline: true } });
|
||||
await withModel("Employee", noEmployeeStub, () =>
|
||||
new HandlerEmployee().updateMyOnlineStatus(forbiddenCtx)
|
||||
);
|
||||
assert.equal(forbiddenCtx.body.code, 403);
|
||||
|
||||
const badRequestCtx = createCtx({ body: { isOnline: "yes" } });
|
||||
await new HandlerEmployee().updateMyOnlineStatus(badRequestCtx);
|
||||
assert.equal(badRequestCtx.body.code, 400);
|
||||
});
|
||||
|
||||
test("公示列表输出累计接单并按服务年限、接单数排序", async () => {
|
||||
let capturedSort = null;
|
||||
const employee = {
|
||||
_id: EMPLOYEE_ID,
|
||||
profile: { name: "李四" },
|
||||
work: { workYears: 5 },
|
||||
attendantInfo: { orderCount: 12, specialties: ["陪诊"] },
|
||||
};
|
||||
const stub = {
|
||||
find: () => ({
|
||||
sort: (sort) => {
|
||||
capturedSort = sort;
|
||||
return {
|
||||
skip: () => ({
|
||||
limit: () => ({ exec: async () => [employee] }),
|
||||
}),
|
||||
};
|
||||
},
|
||||
}),
|
||||
};
|
||||
|
||||
const ctx = createCtx({ user: null });
|
||||
await withModel("Employee", stub, () => new HandlerEmployee().getAttendants(ctx));
|
||||
|
||||
assert.equal(ctx.body.code, 0);
|
||||
assert.equal(ctx.body.data.attendants[0].orderCount, 12);
|
||||
assert.deepEqual(capturedSort, {
|
||||
"work.workYears": -1,
|
||||
"attendantInfo.orderCount": -1,
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,411 @@
|
||||
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);
|
||||
})
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,167 @@
|
||||
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) {
|
||||
return {
|
||||
request: { query: {}, body },
|
||||
params: {},
|
||||
state: { user: { _id: USER_ID } },
|
||||
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() } = {}) {
|
||||
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));
|
||||
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("用户不指定陪诊师:订单进入公共池,不写陪诊师、不累加接单数", 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);
|
||||
});
|
||||
Reference in New Issue
Block a user