320 lines
10 KiB
JavaScript
320 lines
10 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: { 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 = {}, 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) {
|
|
return {
|
|
findOne: () => ({
|
|
select: () => ({
|
|
lean: () => ({ exec: async () => employee }),
|
|
}),
|
|
}),
|
|
};
|
|
}
|
|
|
|
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 } });
|
|
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);
|
|
|
|
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.inquiry.medicalHistory, "接单后可见");
|
|
})
|
|
);
|
|
});
|
|
|
|
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 } });
|
|
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);
|
|
|
|
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;
|
|
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()), () =>
|
|
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);
|
|
})
|
|
);
|
|
});
|
|
|
|
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);
|
|
})
|
|
);
|
|
});
|