Files
temetro/backend/src/routes/appointments.ts
T
Khalid Abdi 75940313a4 feat: activity audit log written from all resource routes
Add the activity_log table, a best-effort recordActivity() service and a
GET /api/activity feed, and write entries on create/update/delete of
patients, notes, appointments, prescriptions and tasks. The Activity page
now shows the real audit trail (actor, action, patient context, time);
the fabricated signing hashes / approval badges are gone — that vision
stays deferred.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-07 19:46:22 +03:00

108 lines
2.9 KiB
TypeScript

import { Router } from "express";
import { appointmentInputSchema } from "../lib/appointment-validation.js";
import { HttpError } from "../lib/http-error.js";
import {
requireAuth,
requireOrg,
requirePermission,
} from "../middleware/auth.js";
import { recordActivity } from "../services/activity.js";
import * as service from "../services/appointments.js";
export const appointmentsRouter = Router();
// Appointments are clinic-wide records, gated by the caller's role like patients.
appointmentsRouter.use(requireAuth, requireOrg);
appointmentsRouter.get(
"/",
requirePermission({ appointment: ["read"] }),
async (req, res, next) => {
try {
res.json(await service.listAppointments(req.organizationId!));
} catch (err) {
next(err);
}
},
);
appointmentsRouter.post(
"/",
requirePermission({ appointment: ["write"] }),
async (req, res, next) => {
try {
const input = appointmentInputSchema.parse(req.body);
const created = await service.createAppointment(
req.organizationId!,
req.user!.id,
input,
);
await recordActivity({
orgId: req.organizationId!,
actor: { id: req.user!.id, name: req.user!.name },
action: `Scheduled appointment for ${created.name} on ${created.date}`,
entityType: "appointment",
entityId: created.id,
patientName: created.name,
patientFileNumber: created.fileNumber || null,
});
res.status(201).json(created);
} catch (err) {
next(err);
}
},
);
appointmentsRouter.put(
"/:id",
requirePermission({ appointment: ["write"] }),
async (req, res, next) => {
try {
const input = appointmentInputSchema.parse(req.body);
const updated = await service.updateAppointment(
req.organizationId!,
req.params.id as string,
input,
);
if (!updated) throw new HttpError(404, "Appointment not found.");
await recordActivity({
orgId: req.organizationId!,
actor: { id: req.user!.id, name: req.user!.name },
action: `Updated appointment for ${updated.name}`,
entityType: "appointment",
entityId: updated.id,
patientName: updated.name,
patientFileNumber: updated.fileNumber || null,
});
res.json(updated);
} catch (err) {
next(err);
}
},
);
appointmentsRouter.delete(
"/:id",
requirePermission({ appointment: ["delete"] }),
async (req, res, next) => {
try {
const ok = await service.deleteAppointment(
req.organizationId!,
req.params.id as string,
);
if (!ok) throw new HttpError(404, "Appointment not found.");
await recordActivity({
orgId: req.organizationId!,
actor: { id: req.user!.id, name: req.user!.name },
action: "Deleted appointment",
entityType: "appointment",
entityId: req.params.id as string,
});
res.status(204).end();
} catch (err) {
next(err);
}
},
);