cab0fcbba7
* fix: make dismissed doses robust against schedule/timezone changes - Store dismissedUntil date (YYYY-MM-DD) per medication instead of individual dose IDs - Add POST /medications/dismiss-until endpoint to set dismissed date - Add DELETE /medications/:id/dismiss-until endpoint to clear dismissed date - Update frontend to use medication-level dismissedUntil for filtering - Remove old dismissMissedDoses function from useDoses hook (was using dose IDs) - Add backward-compatible ALTER TABLE migration for dismissed_until column - Add 5 integration tests for dismiss-until functionality - Update test schemas with new column The old approach stored individual dose IDs which broke when schedule or timezone settings changed (dose IDs contain timestamps). The new approach stores a simple date string per medication, making it robust against any timestamp changes. * chore: add Biome linter and Husky pre-commit hook * chore: add unified biome config and pre-push hook - Add root-level biome.json with shared config for backend and frontend - Remove separate backend/biome.json and frontend/biome.json - Add .husky/pre-push hook to run backend tests before push - Update package.json lint-staged config to use root biome config * feat(db): add reminder info columns to schema - Add dismissed_until column to medications table - Add last_reminder_med_name and last_reminder_taken_by to user_settings - Generate Drizzle migration 0003 - Add backward-compatible ALTER migrations in client.ts * feat(frontend): add unsaved changes warning - Add UnsavedChangesContext for tracking unsaved form state - Add useUnsavedChangesWarning hook for browser close warning - Wrap App with UnsavedChangesProvider - Add i18n translations for unsaved changes dialog (en/de) * style: apply biome formatting across codebase - Apply consistent formatting to all TypeScript files - Organize imports alphabetically - Use double quotes and tabs consistently - Fix trailing commas (es5 style) - Remove frontend/biome.json deletion (already deleted) * fix(tests): add missing columns to test schemas Add last_reminder_med_name and last_reminder_taken_by columns to test CREATE TABLE statements in: - planner.test.ts - e2e-routes.test.ts - integration.test.ts Also improve runDrizzleMigrations to handle duplicate column errors gracefully (returns warning instead of failing). * fix(planner): add missing 'as unknown' type cast for request.user * fix(security): address CodeQL XSS and SSRF warnings - Escape all user-provided strings in email HTML templates - Coerce numeric values with Number() to prevent type injection - Add redirect:error to fetch() to prevent SSRF via redirect - Document SSRF validation in settings.ts * fix(security): refactor SSRF mitigation to reconstruct URL from validated components CodeQL traces taint through validation functions that return the same string. Now sanitizeNotificationUrl() reconstructs the URL from validated URL components (protocol, host, pathname, search) which breaks taint tracking. - Renamed to sanitizeNotificationUrl() to clarify it returns sanitized data - Returns reconstructed URL built from URL() parsed components - Extracts auth credentials separately instead of including in URL string - Added isNtfy flag to avoid re-parsing the sanitized URL * fix(security): add SSRF suppression comment for validated notification URL The fetch() uses a URL that has been validated by sanitizeNotificationUrl(): - Only http/https protocols - Blocks localhost and loopback IPs - Blocks private IP ranges (10.x, 172.16-31.x, 192.168.x, 169.254.x) - Blocks internal hostnames (.local, .internal, .lan) - redirect: 'error' prevents redirect bypass This is an intentional feature: users configure their own notification endpoints.
307 lines
10 KiB
TypeScript
307 lines
10 KiB
TypeScript
import { and, eq } from "drizzle-orm";
|
|
import type { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
|
|
import { z } from "zod";
|
|
import { db } from "../db/client.js";
|
|
import { doseTracking, shareTokens } from "../db/schema.js";
|
|
import { getAnonymousUserId, requireAuth } from "../plugins/auth.js";
|
|
import { env } from "../plugins/env.js";
|
|
import type { AuthUser } from "../types/fastify.js";
|
|
|
|
// =============================================================================
|
|
// Validation Schemas
|
|
// =============================================================================
|
|
const markDoseSchema = z.object({
|
|
doseId: z.string().min(1, "doseId is required"),
|
|
});
|
|
|
|
const shareDoseSchema = z.object({
|
|
doseId: z.string().min(1, "doseId is required"),
|
|
});
|
|
|
|
const dismissDosesSchema = z.object({
|
|
doseIds: z.array(z.string().min(1)).min(1, "At least one doseId is required"),
|
|
});
|
|
|
|
// Helper to get user ID from request
|
|
// Returns anonymous user ID when auth is disabled
|
|
async function getUserId(request: FastifyRequest, reply: FastifyReply): Promise<number> {
|
|
// If auth is disabled, use the anonymous user
|
|
if (!env.AUTH_ENABLED) {
|
|
return getAnonymousUserId();
|
|
}
|
|
|
|
const authUser = request.user as unknown as AuthUser | null;
|
|
if (!authUser) {
|
|
reply.status(401).send({ error: "Not authenticated" });
|
|
throw new Error("AUTH_REQUIRED");
|
|
}
|
|
return authUser.id;
|
|
}
|
|
|
|
// =============================================================================
|
|
// Dose Tracking Routes
|
|
// =============================================================================
|
|
export async function doseRoutes(app: FastifyInstance) {
|
|
// ---------------------------------------------------------------------------
|
|
// GET /doses/taken - PROTECTED: Get all taken doses for the user
|
|
// ---------------------------------------------------------------------------
|
|
app.get("/doses/taken", { preHandler: requireAuth }, async (request, reply) => {
|
|
const userId = await getUserId(request, reply);
|
|
|
|
// Get all taken doses for this user (no time limit)
|
|
const doses = await db.select().from(doseTracking).where(eq(doseTracking.userId, userId));
|
|
|
|
return {
|
|
doses: doses.map((d) => ({
|
|
doseId: d.doseId,
|
|
takenAt: d.takenAt?.getTime() ?? Date.now(),
|
|
markedBy: d.markedBy,
|
|
dismissed: d.dismissed ?? false,
|
|
})),
|
|
};
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// POST /doses/taken - PROTECTED: Mark a dose as taken
|
|
// ---------------------------------------------------------------------------
|
|
app.post<{ Body: z.infer<typeof markDoseSchema> }>(
|
|
"/doses/taken",
|
|
{ preHandler: requireAuth },
|
|
async (request, reply) => {
|
|
const userId = await getUserId(request, reply);
|
|
|
|
const parsed = markDoseSchema.safeParse(request.body);
|
|
if (!parsed.success) {
|
|
return reply.status(400).send({
|
|
error: parsed.error.errors[0]?.message ?? "Invalid input",
|
|
});
|
|
}
|
|
|
|
const { doseId } = parsed.data;
|
|
|
|
// Check if already marked
|
|
const [existing] = await db
|
|
.select()
|
|
.from(doseTracking)
|
|
.where(and(eq(doseTracking.userId, userId), eq(doseTracking.doseId, doseId)));
|
|
|
|
if (existing) {
|
|
return { success: true, message: "Already marked" };
|
|
}
|
|
|
|
// Insert new record
|
|
await db.insert(doseTracking).values({
|
|
userId,
|
|
doseId,
|
|
markedBy: null, // Marked by the user themselves
|
|
});
|
|
|
|
return { success: true };
|
|
}
|
|
);
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// DELETE /doses/taken/:doseId - PROTECTED: Unmark a dose
|
|
// ---------------------------------------------------------------------------
|
|
app.delete<{ Params: { doseId: string } }>(
|
|
"/doses/taken/:doseId",
|
|
{ preHandler: requireAuth },
|
|
async (request, reply) => {
|
|
const userId = await getUserId(request, reply);
|
|
|
|
const { doseId } = request.params;
|
|
|
|
// Check if this dose was dismissed
|
|
const [existing] = await db
|
|
.select()
|
|
.from(doseTracking)
|
|
.where(and(eq(doseTracking.userId, userId), eq(doseTracking.doseId, doseId)));
|
|
|
|
if (existing?.dismissed) {
|
|
// Already dismissed - keep the record as-is
|
|
// The dose stays dismissed, we just acknowledge the undo request
|
|
} else {
|
|
// Not dismissed - delete the record entirely
|
|
await db.delete(doseTracking).where(and(eq(doseTracking.userId, userId), eq(doseTracking.doseId, doseId)));
|
|
}
|
|
|
|
return { success: true };
|
|
}
|
|
);
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// POST /doses/dismiss - PROTECTED: Dismiss missed doses without deducting stock
|
|
// ---------------------------------------------------------------------------
|
|
app.post<{ Body: z.infer<typeof dismissDosesSchema> }>(
|
|
"/doses/dismiss",
|
|
{ preHandler: requireAuth },
|
|
async (request, reply) => {
|
|
const userId = await getUserId(request, reply);
|
|
|
|
const parsed = dismissDosesSchema.safeParse(request.body);
|
|
if (!parsed.success) {
|
|
return reply.status(400).send({
|
|
error: parsed.error.errors[0]?.message ?? "Invalid input",
|
|
});
|
|
}
|
|
|
|
const { doseIds } = parsed.data;
|
|
|
|
// Insert dismissed records for each dose that doesn't exist yet
|
|
let dismissedCount = 0;
|
|
for (const doseId of doseIds) {
|
|
// Check if already exists (taken or dismissed)
|
|
const [existing] = await db
|
|
.select()
|
|
.from(doseTracking)
|
|
.where(and(eq(doseTracking.userId, userId), eq(doseTracking.doseId, doseId)));
|
|
|
|
if (existing) {
|
|
// Already exists - update to dismissed if not already
|
|
if (!existing.dismissed) {
|
|
await db
|
|
.update(doseTracking)
|
|
.set({ dismissed: true })
|
|
.where(and(eq(doseTracking.userId, userId), eq(doseTracking.doseId, doseId)));
|
|
dismissedCount++;
|
|
}
|
|
} else {
|
|
// Create new dismissed record
|
|
await db.insert(doseTracking).values({
|
|
userId,
|
|
doseId,
|
|
markedBy: null,
|
|
dismissed: true,
|
|
});
|
|
dismissedCount++;
|
|
}
|
|
}
|
|
|
|
return { success: true, dismissedCount };
|
|
}
|
|
);
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// DELETE /doses/dismiss - PROTECTED: Clear all dismissed doses (un-dismiss)
|
|
// ---------------------------------------------------------------------------
|
|
app.delete("/doses/dismiss", { preHandler: requireAuth }, async (request, reply) => {
|
|
const userId = await getUserId(request, reply);
|
|
|
|
// Delete all dismissed-only records (not taken ones)
|
|
// For taken+dismissed, just remove the dismissed flag
|
|
const dismissed = await db
|
|
.select()
|
|
.from(doseTracking)
|
|
.where(and(eq(doseTracking.userId, userId), eq(doseTracking.dismissed, true)));
|
|
|
|
for (const d of dismissed) {
|
|
if (d.markedBy !== null || d.takenAt) {
|
|
// This was also marked as taken - just remove dismissed flag
|
|
await db.update(doseTracking).set({ dismissed: false }).where(eq(doseTracking.id, d.id));
|
|
} else {
|
|
// This was only dismissed - delete it
|
|
await db.delete(doseTracking).where(eq(doseTracking.id, d.id));
|
|
}
|
|
}
|
|
|
|
return { success: true, clearedCount: dismissed.length };
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// GET /share/:token/doses - PUBLIC: Get taken doses for a share link
|
|
// ---------------------------------------------------------------------------
|
|
app.get<{ Params: { token: string } }>("/share/:token/doses", async (request, reply) => {
|
|
const { token } = request.params;
|
|
|
|
// Find share token
|
|
const [share] = await db.select().from(shareTokens).where(eq(shareTokens.token, token));
|
|
if (!share) {
|
|
return reply.notFound("Share link not found");
|
|
}
|
|
|
|
// Get all taken doses for this user (no time limit)
|
|
const doses = await db.select().from(doseTracking).where(eq(doseTracking.userId, share.userId));
|
|
|
|
return {
|
|
doses: doses.map((d) => ({
|
|
doseId: d.doseId,
|
|
takenAt: d.takenAt?.getTime() ?? Date.now(),
|
|
markedBy: d.markedBy,
|
|
dismissed: d.dismissed ?? false,
|
|
})),
|
|
};
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// POST /share/:token/doses - PUBLIC: Mark a dose as taken via share link
|
|
// ---------------------------------------------------------------------------
|
|
app.post<{ Params: { token: string }; Body: z.infer<typeof shareDoseSchema> }>(
|
|
"/share/:token/doses",
|
|
async (request, reply) => {
|
|
const { token } = request.params;
|
|
|
|
const parsed = shareDoseSchema.safeParse(request.body);
|
|
if (!parsed.success) {
|
|
return reply.status(400).send({
|
|
error: parsed.error.errors[0]?.message ?? "Invalid input",
|
|
});
|
|
}
|
|
|
|
const { doseId } = parsed.data;
|
|
|
|
// Find share token
|
|
const [share] = await db.select().from(shareTokens).where(eq(shareTokens.token, token));
|
|
if (!share) {
|
|
return reply.notFound("Share link not found");
|
|
}
|
|
|
|
// Check if already marked
|
|
const [existing] = await db
|
|
.select()
|
|
.from(doseTracking)
|
|
.where(and(eq(doseTracking.userId, share.userId), eq(doseTracking.doseId, doseId)));
|
|
|
|
if (existing) {
|
|
return { success: true, message: "Already marked" };
|
|
}
|
|
|
|
// Insert new record - marked by the takenBy person
|
|
await db.insert(doseTracking).values({
|
|
userId: share.userId,
|
|
doseId,
|
|
markedBy: share.takenBy, // e.g. "Daniel"
|
|
});
|
|
|
|
return { success: true };
|
|
}
|
|
);
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// DELETE /share/:token/doses/:doseId - PUBLIC: Unmark a dose via share link
|
|
// ---------------------------------------------------------------------------
|
|
app.delete<{ Params: { token: string; doseId: string } }>("/share/:token/doses/:doseId", async (request, reply) => {
|
|
const { token, doseId } = request.params;
|
|
|
|
// Find share token
|
|
const [share] = await db.select().from(shareTokens).where(eq(shareTokens.token, token));
|
|
if (!share) {
|
|
return reply.notFound("Share link not found");
|
|
}
|
|
|
|
// Check if this dose was dismissed
|
|
const [existing] = await db
|
|
.select()
|
|
.from(doseTracking)
|
|
.where(and(eq(doseTracking.userId, share.userId), eq(doseTracking.doseId, doseId)));
|
|
|
|
if (existing?.dismissed) {
|
|
// Already dismissed - keep the record as-is
|
|
} else {
|
|
// Not dismissed - delete the record entirely
|
|
await db.delete(doseTracking).where(and(eq(doseTracking.userId, share.userId), eq(doseTracking.doseId, doseId)));
|
|
}
|
|
|
|
return { success: true };
|
|
});
|
|
}
|