182 lines
5.1 KiB
JavaScript
182 lines
5.1 KiB
JavaScript
import { DBModel } from "../models/index.js";
|
|
import ResponseUtil from "../utils/responseUtil.js";
|
|
|
|
class HandlerEscortRecord {
|
|
constructor() {
|
|
}
|
|
|
|
// 白名单:只允许写入 schema 定义的顶层字段(兼容 'patient.name' 等点号路径)
|
|
static RECORD_FIELDS = [
|
|
"userId", "healthProfileId", "patient", "escort",
|
|
"attendant", "hospital", "schedule", "payment", "notes", "status",
|
|
];
|
|
|
|
static pickFields(body) {
|
|
const picked = {};
|
|
for (const key of Object.keys(body || {})) {
|
|
const allowed = HandlerEscortRecord.RECORD_FIELDS.some(
|
|
(f) => key === f || key.startsWith(f + ".")
|
|
);
|
|
if (allowed) {
|
|
picked[key] = body[key];
|
|
}
|
|
}
|
|
return picked;
|
|
}
|
|
|
|
// 按条件查询记录(管理端,可查全部)
|
|
async getRecords(ctx) {
|
|
return this._listRecords(ctx, false);
|
|
}
|
|
|
|
// 查询"我的"记录:强制以登录用户身份查询,忽略 query 中的 userId
|
|
async getMyRecords(ctx) {
|
|
return this._listRecords(ctx, true);
|
|
}
|
|
|
|
async _listRecords(ctx, forceSelf) {
|
|
try {
|
|
const { page = 1, pageSize = 20, status, userId, appointmentDate } = ctx.request.query;
|
|
const effectiveUserId = forceSelf ? ctx.state.user?._id : userId;
|
|
|
|
// status解析成数组
|
|
let statusArray = null;
|
|
if (status && status.length > 0) {
|
|
statusArray = status.split(',')
|
|
}
|
|
|
|
const records = await DBModel.EscortRecord.findRecords({
|
|
page: parseInt(page),
|
|
pageSize: parseInt(pageSize),
|
|
status: statusArray,
|
|
userId: effectiveUserId,
|
|
appointmentDate,
|
|
});
|
|
|
|
return ResponseUtil.success(ctx, { records }, "查询成功");
|
|
} catch (err) {
|
|
return ResponseUtil.internalError(ctx, err.message);
|
|
}
|
|
}
|
|
|
|
async getAttendantRecords(ctx) {
|
|
try {
|
|
// 只允许查询当前登录陪诊员自己的记录
|
|
const attendantId = ctx.state.user?._id;
|
|
if (!attendantId) {
|
|
return ResponseUtil.badRequest(ctx, "缺少陪诊员ID");
|
|
}
|
|
|
|
const { page = 1, pageSize = 20, status } = ctx.request.query;
|
|
const records = await DBModel.EscortRecord.findRecordsByAttendant(attendantId, {
|
|
page: parseInt(page),
|
|
pageSize: parseInt(pageSize),
|
|
status,
|
|
});
|
|
|
|
return ResponseUtil.success(ctx, { records }, "查询成功");
|
|
} catch (err) {
|
|
return ResponseUtil.internalError(ctx, err.message);
|
|
}
|
|
}
|
|
|
|
async getRecordById(ctx) {
|
|
try {
|
|
const { id } = ctx.params;
|
|
if (!id) {
|
|
return ResponseUtil.badRequest(ctx, "缺少记录ID");
|
|
}
|
|
|
|
const record = await DBModel.EscortRecord.findById(id);
|
|
if (!record) {
|
|
return ResponseUtil.error(ctx, "陪诊记录不存在", null, 404);
|
|
}
|
|
|
|
return ResponseUtil.success(ctx, { record }, "查询成功");
|
|
} catch (err) {
|
|
return ResponseUtil.internalError(ctx, err.message);
|
|
}
|
|
}
|
|
|
|
async createRecord(ctx) {
|
|
try {
|
|
const record = ctx.request.body;
|
|
if (!record.userId) {
|
|
return ResponseUtil.badRequest(ctx, "缺少患者信息");
|
|
}
|
|
|
|
const newRecord = await DBModel.EscortRecord.createRecord(HandlerEscortRecord.pickFields(record));
|
|
return ResponseUtil.success(ctx, { record: newRecord }, "创建成功");
|
|
} catch (err) {
|
|
return ResponseUtil.internalError(ctx, err.message);
|
|
}
|
|
}
|
|
|
|
async updateRecord(ctx) {
|
|
try {
|
|
const { id } = ctx.params;
|
|
const update = ctx.request.body;
|
|
|
|
if (!id) {
|
|
return ResponseUtil.badRequest(ctx, "缺少记录ID");
|
|
}
|
|
|
|
const updatedRecord = await DBModel.EscortRecord.updateRecord(id, HandlerEscortRecord.pickFields(update));
|
|
if (!updatedRecord) {
|
|
return ResponseUtil.error(ctx, "陪诊记录不存在", null, 404);
|
|
}
|
|
|
|
return ResponseUtil.success(ctx, { record: updatedRecord }, "更新成功");
|
|
} catch (err) {
|
|
return ResponseUtil.internalError(ctx, err.message);
|
|
}
|
|
}
|
|
|
|
async updateStatus(ctx) {
|
|
try {
|
|
const { id } = ctx.params;
|
|
const { status } = ctx.request.body;
|
|
|
|
if (!id) {
|
|
return ResponseUtil.badRequest(ctx, "缺少记录ID");
|
|
}
|
|
if (!status) {
|
|
return ResponseUtil.badRequest(ctx, "缺少状态值");
|
|
}
|
|
|
|
const updatedRecord = await DBModel.EscortRecord.updateRecord(id, {
|
|
"status": status,
|
|
});
|
|
|
|
if (!updatedRecord) {
|
|
return ResponseUtil.error(ctx, "陪诊记录不存在", null, 404);
|
|
}
|
|
|
|
return ResponseUtil.success(ctx, { record: updatedRecord }, "状态更新成功");
|
|
} catch (err) {
|
|
return ResponseUtil.internalError(ctx, err.message);
|
|
}
|
|
}
|
|
|
|
async deleteRecord(ctx) {
|
|
try {
|
|
const { id } = ctx.params;
|
|
|
|
if (!id) {
|
|
return ResponseUtil.badRequest(ctx, "缺少记录ID");
|
|
}
|
|
|
|
const deletedRecord = await DBModel.EscortRecord.deleteRecord(id);
|
|
|
|
if (!deletedRecord) {
|
|
return ResponseUtil.error(ctx, "陪诊记录不存在", null, 404);
|
|
}
|
|
|
|
return ResponseUtil.success(ctx, { record: deletedRecord }, "删除成功");
|
|
} catch (err) {
|
|
return ResponseUtil.internalError(ctx, err.message);
|
|
}
|
|
}
|
|
}
|
|
|
|
export { HandlerEscortRecord }; |