diff --git a/handler/attendant_application.js b/handler/attendant_application.js index 31b7882..87d6216 100644 --- a/handler/attendant_application.js +++ b/handler/attendant_application.js @@ -253,6 +253,9 @@ class HandlerAttendantApplication { $setOnInsert: { employeeNo: HandlerAttendantApplication.generateEmployeeNo(), "meta.createtime": Date.now(), + // 入驻通过只给实习身份:可查看订单但不能接单,由管理员人工转正。 + // 只在新建时写入,停用档案重新激活保留原有阶段,避免转正后被降回实习 + "attendantInfo.stage": "intern", }, }, { new: true, upsert: true } diff --git a/handler/employee.js b/handler/employee.js index 3692c8d..0a6a412 100644 --- a/handler/employee.js +++ b/handler/employee.js @@ -30,6 +30,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,7 +123,8 @@ class HandlerEmployee { specialties: info.specialties || [], serviceAreas: info.serviceAreas || [], languages: info.languages || [], - orderCount: info.orderCount || 0, + // 注:GB/T 4.2.1.5 要求公示的「累计服务次数」暂缺——orderCount 全项目无写入点(恒 0), + // 展示恒 0 反而误导,已从公示输出摘除;待 plan.md B8 补写入点后恢复 introduction: employee.introduction || "", }; } @@ -112,13 +139,10 @@ 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", - }) - .sort({ "work.workYears": -1, "attendantInfo.orderCount": -1 }) + // 只取在职且已转正的陪诊员,实习不对外公示。 + // 排序不按 attendantInfo.rating / orderCount(均无可信来源),暂只按服务年限 + const list = await DBModel.Employee.find(HandlerEmployee.publicAttendantFilter()) + .sort({ "work.workYears": -1 }) .skip((page - 1) * pageSize) .limit(pageSize) .exec(); @@ -148,8 +172,7 @@ class HandlerEmployee { const employee = await DBModel.Employee.findOne({ _id: id, - role: "attendant", - status: "active", + ...HandlerEmployee.publicAttendantFilter(), }).exec(); if (!employee) { @@ -186,12 +209,22 @@ class HandlerEmployee { } const employee = await DBModel.Employee.findOne({ userId }) - .select("employeeNo role status profile.name profile.sex profile.phone profile.avatar") + .select("employeeNo role status profile.name profile.sex profile.phone profile.avatar attendantInfo") .lean() .exec(); // 无员工档案不是错误:管理员账号通常没有 Employee 记录 - return ResponseUtil.success(ctx, { employee: employee || null }, "查询成功"); + if (!employee) { + return ResponseUtil.success(ctx, { employee: null }, "查询成功"); + } + + // 阶段/等级归一化后随档案返回,小程序据此展示身份文案与只读提示。 + // canAcceptOrder 只由 stage 决定,等级不参与鉴权 + const level = HandlerEmployee.normalizeAttendantLevel(employee.attendantInfo); + employee.attendantInfo = { ...(employee.attendantInfo || {}), ...level }; + employee.canAcceptOrder = level.stage === "certified"; + + return ResponseUtil.success(ctx, { employee }, "查询成功"); } catch (err) { return ResponseUtil.internalError(ctx, err); } @@ -413,6 +446,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); + } + } + /** * 写入校验 * diff --git a/models/schema/employee.js b/models/schema/employee.js index f9b50b6..6e29d7e 100644 --- a/models/schema/employee.js +++ b/models/schema/employee.js @@ -99,13 +99,30 @@ 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: "接单数量" }, + orderCount: { type: Number, default: 0, comment: "接单数量(无写入点恒 0,公示/后台展示已摘除,勿用于排序)" }, isOnline: { type: Boolean, default: false, comment: "是否在线" }, }, @@ -239,8 +256,8 @@ EmployeeSchema.statics.updateEmployee = async function (id, update) { /** * 查询在线陪诊员列表 * - * 排序口径与 handler/employee.js 的公示列表保持一致:服务年限优先、累计服务次数次之。 - * 曾按 attendantInfo.rating 排,但该字段没有任何写入点(恒为 5.0),等于没排序 + * 排序口径与 handler/employee.js 的公示列表保持一致:只按服务年限。 + * 曾按 attendantInfo.rating / orderCount 排,但两者均无写入点,等于没排序 */ EmployeeSchema.statics.findOnlineAttendants = async function (options = {}) { const { page = 1, pageSize = 20 } = options; @@ -250,7 +267,7 @@ EmployeeSchema.statics.findOnlineAttendants = async function (options = {}) { status: "active", "attendantInfo.isOnline": true, }) - .sort({ "work.workYears": -1, "attendantInfo.orderCount": -1 }) + .sort({ "work.workYears": -1 }) .skip(skip) .limit(pageSize) .exec(); diff --git a/routes/index.js b/routes/index.js index 8ba8e10..ec1b4b5 100644 --- a/routes/index.js +++ b/routes/index.js @@ -145,6 +145,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 diff --git a/test/attendant_application.test.js b/test/attendant_application.test.js index a4f58e6..d243180 100644 --- a/test/attendant_application.test.js +++ b/test/attendant_application.test.js @@ -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) => { // 按账号查:本账号无档案;按身份证查重:命中护士档案