Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 2577e33c50 | |||
| b904418628 |
@@ -0,0 +1,45 @@
|
|||||||
|
import type { MiddlewareHandler } from "hono";
|
||||||
|
import { getDb, impersonationAuditLogs } from "@groombook/db";
|
||||||
|
import type { PortalEnv } from "./portalSession.js";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Server-side audit logging middleware for portal routes.
|
||||||
|
* Applied after validatePortalSession in the middleware chain.
|
||||||
|
*
|
||||||
|
* After the route handler completes (await next()), inserts an audit log entry
|
||||||
|
* into impersonationAuditLogs:
|
||||||
|
* - sessionId: from c.get("portalSessionId")
|
||||||
|
* - action: "{METHOD} {routePath}" (e.g., "GET /portal/appointments")
|
||||||
|
* - pageVisited: c.req.path
|
||||||
|
* - metadata: { method, statusCode: c.res.status }
|
||||||
|
*
|
||||||
|
* Log entries are written for both success and error responses.
|
||||||
|
* Does NOT throw if audit logging fails — errors are logged but the user's
|
||||||
|
* request is not affected.
|
||||||
|
*/
|
||||||
|
export const portalAudit: MiddlewareHandler<PortalEnv> = async (c, next) => {
|
||||||
|
await next();
|
||||||
|
|
||||||
|
const sessionId = c.get("portalSessionId");
|
||||||
|
if (!sessionId) return;
|
||||||
|
|
||||||
|
const method = c.req.method;
|
||||||
|
const routePath = c.req.path;
|
||||||
|
const pageVisited = c.req.path;
|
||||||
|
const statusCode = c.res.status;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const db = getDb();
|
||||||
|
await db
|
||||||
|
.insert(impersonationAuditLogs)
|
||||||
|
.values({
|
||||||
|
sessionId,
|
||||||
|
action: `${method} ${routePath}`,
|
||||||
|
pageVisited,
|
||||||
|
metadata: { method, statusCode },
|
||||||
|
})
|
||||||
|
.returning();
|
||||||
|
} catch (err) {
|
||||||
|
console.error("[portalAudit] Failed to write audit log:", err);
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
import type { MiddlewareHandler } from "hono";
|
||||||
|
import { and, eq, getDb, impersonationSessions } from "@groombook/db";
|
||||||
|
|
||||||
|
export interface PortalEnv {
|
||||||
|
Variables: {
|
||||||
|
portalClientId: string;
|
||||||
|
portalSessionId: string;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validates the X-Impersonation-Session-Id header against the impersonationSessions table.
|
||||||
|
* Must be applied to all portal routes.
|
||||||
|
*
|
||||||
|
* Reads x-session-id from request headers, queries impersonationSessions for a row where
|
||||||
|
* id = sessionId AND status = 'active', and checks session.expiresAt > new Date().
|
||||||
|
* Returns 401 if session is invalid/missing/expired.
|
||||||
|
* On success, sets c.set("portalClientId", session.clientId) and c.set("portalSessionId", session.id).
|
||||||
|
*/
|
||||||
|
export const validatePortalSession: MiddlewareHandler<PortalEnv> = async (c, next) => {
|
||||||
|
const sessionId = c.req.header("X-Impersonation-Session-Id");
|
||||||
|
if (!sessionId) {
|
||||||
|
return c.json({ error: "Unauthorized" }, 401);
|
||||||
|
}
|
||||||
|
|
||||||
|
const db = getDb();
|
||||||
|
const [session] = await db
|
||||||
|
.select()
|
||||||
|
.from(impersonationSessions)
|
||||||
|
.where(and(eq(impersonationSessions.id, sessionId), eq(impersonationSessions.status, "active")))
|
||||||
|
.limit(1);
|
||||||
|
|
||||||
|
if (!session || session.expiresAt <= new Date()) {
|
||||||
|
return c.json({ error: "Unauthorized" }, 401);
|
||||||
|
}
|
||||||
|
|
||||||
|
c.set("portalClientId", session.clientId);
|
||||||
|
c.set("portalSessionId", session.id);
|
||||||
|
await next();
|
||||||
|
};
|
||||||
@@ -338,44 +338,35 @@ async function sendConfirmationEmail(
|
|||||||
db: ReturnType<typeof getDb>,
|
db: ReturnType<typeof getDb>,
|
||||||
appt: typeof appointments.$inferSelect
|
appt: typeof appointments.$inferSelect
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const [client] = await db
|
const [row] = await db
|
||||||
.select({ name: clients.name, email: clients.email, emailOptOut: clients.emailOptOut })
|
.select({
|
||||||
.from(clients)
|
clientName: clients.name,
|
||||||
.where(eq(clients.id, appt.clientId))
|
clientEmail: clients.email,
|
||||||
|
clientEmailOptOut: clients.emailOptOut,
|
||||||
|
petName: pets.name,
|
||||||
|
serviceName: services.name,
|
||||||
|
groomerName: staff.name,
|
||||||
|
})
|
||||||
|
.from(appointments)
|
||||||
|
.innerJoin(clients, eq(clients.id, appointments.clientId))
|
||||||
|
.innerJoin(pets, eq(pets.id, appointments.petId))
|
||||||
|
.innerJoin(services, eq(services.id, appointments.serviceId))
|
||||||
|
.leftJoin(staff, eq(staff.id, appointments.staffId))
|
||||||
|
.where(eq(appointments.id, appt.id))
|
||||||
.limit(1);
|
.limit(1);
|
||||||
|
|
||||||
if (!client || !client.email || client.emailOptOut) return;
|
if (!row) return;
|
||||||
|
const { clientName, clientEmail, clientEmailOptOut, petName, serviceName, groomerName } = row;
|
||||||
|
|
||||||
const [pet] = await db
|
if (!clientEmail || clientEmailOptOut) return;
|
||||||
.select({ name: pets.name })
|
if (!petName || !serviceName) return;
|
||||||
.from(pets)
|
|
||||||
.where(eq(pets.id, appt.petId))
|
|
||||||
.limit(1);
|
|
||||||
|
|
||||||
const [service] = await db
|
|
||||||
.select({ name: services.name })
|
|
||||||
.from(services)
|
|
||||||
.where(eq(services.id, appt.serviceId))
|
|
||||||
.limit(1);
|
|
||||||
|
|
||||||
let groomerName: string | null = null;
|
|
||||||
if (appt.staffId) {
|
|
||||||
const [groomer] = await db
|
|
||||||
.select({ name: staff.name })
|
|
||||||
.from(staff)
|
|
||||||
.where(eq(staff.id, appt.staffId))
|
|
||||||
.limit(1);
|
|
||||||
groomerName = groomer?.name ?? null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!pet || !service) return;
|
|
||||||
|
|
||||||
const sent = await sendEmail(
|
const sent = await sendEmail(
|
||||||
buildConfirmationEmail(client.email, {
|
buildConfirmationEmail(clientEmail, {
|
||||||
clientName: client.name,
|
clientName,
|
||||||
petName: pet.name,
|
petName,
|
||||||
serviceName: service.name,
|
serviceName,
|
||||||
groomerName,
|
groomerName: groomerName ?? null,
|
||||||
startTime: appt.startTime,
|
startTime: appt.startTime,
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|||||||
+23
-122
@@ -1,33 +1,22 @@
|
|||||||
import { Hono } from "hono";
|
import { Hono } from "hono";
|
||||||
import { zValidator } from "@hono/zod-validator";
|
import { zValidator } from "@hono/zod-validator";
|
||||||
import { z } from "zod/v3";
|
import { z } from "zod/v3";
|
||||||
import { and, eq, inArray } from "@groombook/db";
|
import { eq, inArray } from "@groombook/db";
|
||||||
import { getDb, appointments, impersonationSessions, waitlistEntries, clients, pets, services, staff, invoices, invoiceLineItems } from "@groombook/db";
|
import { getDb, appointments, impersonationSessions, waitlistEntries, clients, pets, services, staff, invoices, invoiceLineItems } from "@groombook/db";
|
||||||
import type { AppEnv } from "../middleware/rbac.js";
|
import { validatePortalSession } from "../middleware/portalSession.js";
|
||||||
|
import { portalAudit } from "../middleware/portalAudit.js";
|
||||||
|
import type { PortalEnv } from "../middleware/portalSession.js";
|
||||||
|
|
||||||
export const portalRouter = new Hono<AppEnv>();
|
export const portalRouter = new Hono<PortalEnv>();
|
||||||
|
|
||||||
// ─── Session helper ───────────────────────────────────────────────────────────
|
// Apply middleware to all portal routes
|
||||||
|
portalRouter.use("/*", validatePortalSession, portalAudit);
|
||||||
async function getClientIdFromSession(sessionId: string | null | undefined): Promise<string | null> {
|
|
||||||
if (!sessionId) return null;
|
|
||||||
const db = getDb();
|
|
||||||
const [session] = await db
|
|
||||||
.select()
|
|
||||||
.from(impersonationSessions)
|
|
||||||
.where(and(eq(impersonationSessions.id, sessionId), eq(impersonationSessions.status, "active")))
|
|
||||||
.limit(1);
|
|
||||||
if (!session || session.expiresAt <= new Date()) return null;
|
|
||||||
return session.clientId;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ─── GET routes ──────────────────────────────────────────────────────────────
|
// ─── GET routes ──────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
portalRouter.get("/me", async (c) => {
|
portalRouter.get("/me", async (c) => {
|
||||||
const db = getDb();
|
const db = getDb();
|
||||||
const sessionId = c.req.header("X-Impersonation-Session-Id");
|
const clientId = c.get("portalClientId");
|
||||||
const clientId = await getClientIdFromSession(sessionId);
|
|
||||||
if (!clientId) return c.json({ error: "Unauthorized" }, 401);
|
|
||||||
|
|
||||||
const [client] = await db.select().from(clients).where(eq(clients.id, clientId)).limit(1);
|
const [client] = await db.select().from(clients).where(eq(clients.id, clientId)).limit(1);
|
||||||
if (!client) return c.json({ error: "Not found" }, 404);
|
if (!client) return c.json({ error: "Not found" }, 404);
|
||||||
@@ -49,9 +38,7 @@ portalRouter.get("/services", async (c) => {
|
|||||||
|
|
||||||
portalRouter.get("/appointments", async (c) => {
|
portalRouter.get("/appointments", async (c) => {
|
||||||
const db = getDb();
|
const db = getDb();
|
||||||
const sessionId = c.req.header("X-Impersonation-Session-Id");
|
const clientId = c.get("portalClientId");
|
||||||
const clientId = await getClientIdFromSession(sessionId);
|
|
||||||
if (!clientId) return c.json({ error: "Unauthorized" }, 401);
|
|
||||||
|
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
const allAppts = await db
|
const allAppts = await db
|
||||||
@@ -101,9 +88,7 @@ portalRouter.get("/appointments", async (c) => {
|
|||||||
|
|
||||||
portalRouter.get("/pets", async (c) => {
|
portalRouter.get("/pets", async (c) => {
|
||||||
const db = getDb();
|
const db = getDb();
|
||||||
const sessionId = c.req.header("X-Impersonation-Session-Id");
|
const clientId = c.get("portalClientId");
|
||||||
const clientId = await getClientIdFromSession(sessionId);
|
|
||||||
if (!clientId) return c.json({ error: "Unauthorized" }, 401);
|
|
||||||
|
|
||||||
const clientPets = await db.select().from(pets).where(eq(pets.clientId, clientId));
|
const clientPets = await db.select().from(pets).where(eq(pets.clientId, clientId));
|
||||||
return c.json(clientPets.map(p => ({ id: p.id, name: p.name, breed: p.breed, weightKg: p.weightKg, dateOfBirth: p.dateOfBirth, photoKey: p.photoKey, groomingNotes: p.groomingNotes })));
|
return c.json(clientPets.map(p => ({ id: p.id, name: p.name, breed: p.breed, weightKg: p.weightKg, dateOfBirth: p.dateOfBirth, photoKey: p.photoKey, groomingNotes: p.groomingNotes })));
|
||||||
@@ -111,9 +96,7 @@ portalRouter.get("/pets", async (c) => {
|
|||||||
|
|
||||||
portalRouter.get("/invoices", async (c) => {
|
portalRouter.get("/invoices", async (c) => {
|
||||||
const db = getDb();
|
const db = getDb();
|
||||||
const sessionId = c.req.header("X-Impersonation-Session-Id");
|
const clientId = c.get("portalClientId");
|
||||||
const clientId = await getClientIdFromSession(sessionId);
|
|
||||||
if (!clientId) return c.json({ error: "Unauthorized" }, 401);
|
|
||||||
|
|
||||||
const clientInvoices = await db.select().from(invoices).where(eq(invoices.clientId, clientId));
|
const clientInvoices = await db.select().from(invoices).where(eq(invoices.clientId, clientId));
|
||||||
const invoiceIds = clientInvoices.map(i => i.id);
|
const invoiceIds = clientInvoices.map(i => i.id);
|
||||||
@@ -148,12 +131,7 @@ portalRouter.patch(
|
|||||||
const db = getDb();
|
const db = getDb();
|
||||||
const id = c.req.param("id");
|
const id = c.req.param("id");
|
||||||
const body = c.req.valid("json");
|
const body = c.req.valid("json");
|
||||||
|
const clientId = c.get("portalClientId");
|
||||||
const sessionId = c.req.header("X-Impersonation-Session-Id");
|
|
||||||
const clientId = await getClientIdFromSession(sessionId);
|
|
||||||
if (!clientId) {
|
|
||||||
return c.json({ error: "Unauthorized" }, 401);
|
|
||||||
}
|
|
||||||
|
|
||||||
const [appt] = await db
|
const [appt] = await db
|
||||||
.select()
|
.select()
|
||||||
@@ -196,12 +174,7 @@ portalRouter.patch(
|
|||||||
portalRouter.post("/appointments/:id/confirm", async (c) => {
|
portalRouter.post("/appointments/:id/confirm", async (c) => {
|
||||||
const db = getDb();
|
const db = getDb();
|
||||||
const id = c.req.param("id");
|
const id = c.req.param("id");
|
||||||
|
const clientId = c.get("portalClientId");
|
||||||
const sessionId = c.req.header("X-Impersonation-Session-Id");
|
|
||||||
const clientId = await getClientIdFromSession(sessionId);
|
|
||||||
if (!clientId) {
|
|
||||||
return c.json({ error: "Unauthorized" }, 401);
|
|
||||||
}
|
|
||||||
|
|
||||||
const [appt] = await db
|
const [appt] = await db
|
||||||
.select()
|
.select()
|
||||||
@@ -250,12 +223,7 @@ portalRouter.post("/appointments/:id/confirm", async (c) => {
|
|||||||
portalRouter.post("/appointments/:id/cancel", async (c) => {
|
portalRouter.post("/appointments/:id/cancel", async (c) => {
|
||||||
const db = getDb();
|
const db = getDb();
|
||||||
const id = c.req.param("id");
|
const id = c.req.param("id");
|
||||||
|
const clientId = c.get("portalClientId");
|
||||||
const sessionId = c.req.header("X-Impersonation-Session-Id");
|
|
||||||
const clientId = await getClientIdFromSession(sessionId);
|
|
||||||
if (!clientId) {
|
|
||||||
return c.json({ error: "Unauthorized" }, 401);
|
|
||||||
}
|
|
||||||
|
|
||||||
const [appt] = await db
|
const [appt] = await db
|
||||||
.select()
|
.select()
|
||||||
@@ -319,28 +287,7 @@ portalRouter.post(
|
|||||||
async (c) => {
|
async (c) => {
|
||||||
const db = getDb();
|
const db = getDb();
|
||||||
const body = c.req.valid("json");
|
const body = c.req.valid("json");
|
||||||
const sessionId = c.req.header("X-Impersonation-Session-Id");
|
const clientId = c.get("portalClientId");
|
||||||
|
|
||||||
let clientId: string | null = null;
|
|
||||||
if (sessionId) {
|
|
||||||
const [session] = await db
|
|
||||||
.select()
|
|
||||||
.from(impersonationSessions)
|
|
||||||
.where(
|
|
||||||
and(
|
|
||||||
eq(impersonationSessions.id, sessionId),
|
|
||||||
eq(impersonationSessions.status, "active")
|
|
||||||
)
|
|
||||||
)
|
|
||||||
.limit(1);
|
|
||||||
if (session && session.expiresAt > new Date()) {
|
|
||||||
clientId = session.clientId;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!clientId) {
|
|
||||||
return c.json({ error: "Unauthorized" }, 401);
|
|
||||||
}
|
|
||||||
|
|
||||||
const [entry] = await db
|
const [entry] = await db
|
||||||
.insert(waitlistEntries)
|
.insert(waitlistEntries)
|
||||||
@@ -364,26 +311,7 @@ portalRouter.patch(
|
|||||||
const db = getDb();
|
const db = getDb();
|
||||||
const id = c.req.param("id");
|
const id = c.req.param("id");
|
||||||
const body = c.req.valid("json");
|
const body = c.req.valid("json");
|
||||||
const sessionId = c.req.header("X-Impersonation-Session-Id");
|
const clientId = c.get("portalClientId");
|
||||||
|
|
||||||
if (!sessionId) {
|
|
||||||
return c.json({ error: "Unauthorized" }, 401);
|
|
||||||
}
|
|
||||||
|
|
||||||
const [session] = await db
|
|
||||||
.select()
|
|
||||||
.from(impersonationSessions)
|
|
||||||
.where(
|
|
||||||
and(
|
|
||||||
eq(impersonationSessions.id, sessionId),
|
|
||||||
eq(impersonationSessions.status, "active")
|
|
||||||
)
|
|
||||||
)
|
|
||||||
.limit(1);
|
|
||||||
|
|
||||||
if (!session || session.expiresAt <= new Date()) {
|
|
||||||
return c.json({ error: "Unauthorized" }, 401);
|
|
||||||
}
|
|
||||||
|
|
||||||
const [existing] = await db
|
const [existing] = await db
|
||||||
.select()
|
.select()
|
||||||
@@ -392,7 +320,7 @@ portalRouter.patch(
|
|||||||
.limit(1);
|
.limit(1);
|
||||||
|
|
||||||
if (!existing) return c.json({ error: "Not found" }, 404);
|
if (!existing) return c.json({ error: "Not found" }, 404);
|
||||||
if (existing.clientId !== session.clientId) {
|
if (existing.clientId !== clientId) {
|
||||||
return c.json({ error: "Forbidden" }, 403);
|
return c.json({ error: "Forbidden" }, 403);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -414,26 +342,7 @@ portalRouter.patch(
|
|||||||
portalRouter.delete("/waitlist/:id", async (c) => {
|
portalRouter.delete("/waitlist/:id", async (c) => {
|
||||||
const db = getDb();
|
const db = getDb();
|
||||||
const id = c.req.param("id");
|
const id = c.req.param("id");
|
||||||
const sessionId = c.req.header("X-Impersonation-Session-Id");
|
const clientId = c.get("portalClientId");
|
||||||
|
|
||||||
if (!sessionId) {
|
|
||||||
return c.json({ error: "Unauthorized" }, 401);
|
|
||||||
}
|
|
||||||
|
|
||||||
const [session] = await db
|
|
||||||
.select()
|
|
||||||
.from(impersonationSessions)
|
|
||||||
.where(
|
|
||||||
and(
|
|
||||||
eq(impersonationSessions.id, sessionId),
|
|
||||||
eq(impersonationSessions.status, "active")
|
|
||||||
)
|
|
||||||
)
|
|
||||||
.limit(1);
|
|
||||||
|
|
||||||
if (!session || session.expiresAt <= new Date()) {
|
|
||||||
return c.json({ error: "Unauthorized" }, 401);
|
|
||||||
}
|
|
||||||
|
|
||||||
const [entry] = await db
|
const [entry] = await db
|
||||||
.select()
|
.select()
|
||||||
@@ -442,7 +351,7 @@ portalRouter.delete("/waitlist/:id", async (c) => {
|
|||||||
.limit(1);
|
.limit(1);
|
||||||
|
|
||||||
if (!entry) return c.json({ error: "Not found" }, 404);
|
if (!entry) return c.json({ error: "Not found" }, 404);
|
||||||
if (entry.clientId !== session.clientId) {
|
if (entry.clientId !== clientId) {
|
||||||
return c.json({ error: "Forbidden" }, 403);
|
return c.json({ error: "Forbidden" }, 403);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -475,9 +384,7 @@ portalRouter.post(
|
|||||||
async (c) => {
|
async (c) => {
|
||||||
const db = getDb();
|
const db = getDb();
|
||||||
const body = c.req.valid("json");
|
const body = c.req.valid("json");
|
||||||
const sessionId = c.req.header("X-Impersonation-Session-Id");
|
const clientId = c.get("portalClientId");
|
||||||
const clientId = await getClientIdFromSession(sessionId);
|
|
||||||
if (!clientId) return c.json({ error: "Unauthorized" }, 401);
|
|
||||||
|
|
||||||
const invoiceRows = await db
|
const invoiceRows = await db
|
||||||
.select()
|
.select()
|
||||||
@@ -514,9 +421,7 @@ portalRouter.post(
|
|||||||
);
|
);
|
||||||
|
|
||||||
portalRouter.get("/payment-methods", async (c) => {
|
portalRouter.get("/payment-methods", async (c) => {
|
||||||
const sessionId = c.req.header("X-Impersonation-Session-Id");
|
const clientId = c.get("portalClientId");
|
||||||
const clientId = await getClientIdFromSession(sessionId);
|
|
||||||
if (!clientId) return c.json({ error: "Unauthorized" }, 401);
|
|
||||||
|
|
||||||
const methods = await listPaymentMethods(clientId);
|
const methods = await listPaymentMethods(clientId);
|
||||||
if (methods === null) return c.json({ error: "Payment service unavailable" }, 503);
|
if (methods === null) return c.json({ error: "Payment service unavailable" }, 503);
|
||||||
@@ -524,9 +429,7 @@ portalRouter.get("/payment-methods", async (c) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
portalRouter.post("/payment-methods", async (c) => {
|
portalRouter.post("/payment-methods", async (c) => {
|
||||||
const sessionId = c.req.header("X-Impersonation-Session-Id");
|
const clientId = c.get("portalClientId");
|
||||||
const clientId = await getClientIdFromSession(sessionId);
|
|
||||||
if (!clientId) return c.json({ error: "Unauthorized" }, 401);
|
|
||||||
|
|
||||||
const stripePublishableKey = process.env.STRIPE_PUBLISHABLE_KEY ?? "";
|
const stripePublishableKey = process.env.STRIPE_PUBLISHABLE_KEY ?? "";
|
||||||
const customerId = await getOrCreateStripeCustomer(clientId);
|
const customerId = await getOrCreateStripeCustomer(clientId);
|
||||||
@@ -539,9 +442,7 @@ portalRouter.post("/payment-methods", async (c) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
portalRouter.delete("/payment-methods/:id", async (c) => {
|
portalRouter.delete("/payment-methods/:id", async (c) => {
|
||||||
const sessionId = c.req.header("X-Impersonation-Session-Id");
|
const clientId = c.get("portalClientId");
|
||||||
const clientId = await getClientIdFromSession(sessionId);
|
|
||||||
if (!clientId) return c.json({ error: "Unauthorized" }, 401);
|
|
||||||
|
|
||||||
const paymentMethodId = c.req.param("id");
|
const paymentMethodId = c.req.param("id");
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user