mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-08 12:32:43 +00:00
Eight touch-disjoint waves landed on develop before this release: - W-INSIGHTS-HOT + W-RECONCILE-INSIGHTS — extract the timeout-stub persist helper and wire all seven *-status routes through it, closing the recurring ~14 s warm response the iOS client sees on dashboard open once the AI provider has stalled once that day. - W-DELETED-2 — close the remaining three soft-delete reader-tier gaps (export bundle, gamification achievements, doctor-report aggregator) with a five-route integration-test pin. - W-ORG — types consolidation into src/types/analytics.ts and src/types/backups.ts; prompt directory unification. - W-FRONTEND-FACTORY — queryKey factory expansion + ESLint rule lift. - W-IOS-COORD — POST /api/auth/check-user four-branch onboarding discovery, identifier matched verbatim, per-IP rate-limited. - W-SIMPLIFIER — today-intake projection helper extract; unused tx? params dropped; 13 knip-flagged dead exports trimmed. - W-PERF-OPS — v1.4.39 W-SUM legacy-NULL UNION discovery arm retired. - W-PROCESS-DOCS — pg.Pool sizing operator doc; ESLint rule formalisation; knip CI gate staged for v1.4.42 flip. No migration, no env-var change, no API contract break for iOS v0.5.4. The new check-user endpoint is an additive surface. AP-2 .p8 key is installed; SB-5 time-sensitive APNs is live as of this deploy. pnpm test --run green at 4732 passing / 1 skipped. pnpm typecheck, pnpm lint, pnpm knip green.
98 lines
2.7 KiB
TypeScript
98 lines
2.7 KiB
TypeScript
import { prisma } from "@/lib/db";
|
|
import { MEDICATION_CATEGORY_VALUES } from "@/lib/validations/medication";
|
|
|
|
const MEDICATION_CATEGORIES = MEDICATION_CATEGORY_VALUES;
|
|
|
|
export type MedicationCategory = (typeof MEDICATION_CATEGORIES)[number];
|
|
|
|
const DEFAULT_CATEGORY: MedicationCategory = "OTHER";
|
|
|
|
let ensureTablePromise: Promise<void> | null = null;
|
|
|
|
function normalizeCategory(input: unknown): MedicationCategory {
|
|
if (typeof input !== "string") return DEFAULT_CATEGORY;
|
|
return MEDICATION_CATEGORIES.includes(input as MedicationCategory)
|
|
? (input as MedicationCategory)
|
|
: DEFAULT_CATEGORY;
|
|
}
|
|
|
|
async function ensureMedicationCategoryTable() {
|
|
if (!ensureTablePromise) {
|
|
ensureTablePromise = (async () => {
|
|
await prisma.$executeRawUnsafe(`
|
|
CREATE TABLE IF NOT EXISTS medication_categories (
|
|
medication_id TEXT PRIMARY KEY REFERENCES medications(id) ON DELETE CASCADE,
|
|
category TEXT NOT NULL DEFAULT 'OTHER',
|
|
updated_at TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP
|
|
);
|
|
`);
|
|
|
|
await prisma.$executeRawUnsafe(`
|
|
CREATE INDEX IF NOT EXISTS medication_categories_category_idx
|
|
ON medication_categories(category);
|
|
`);
|
|
})().catch((err) => {
|
|
ensureTablePromise = null;
|
|
throw err;
|
|
});
|
|
}
|
|
|
|
await ensureTablePromise;
|
|
}
|
|
|
|
export async function getMedicationCategories(
|
|
medicationIds: string[],
|
|
): Promise<Record<string, MedicationCategory>> {
|
|
if (medicationIds.length === 0) return {};
|
|
await ensureMedicationCategoryTable();
|
|
|
|
const rows = await prisma.$queryRawUnsafe<
|
|
Array<{ medication_id: string; category: string }>
|
|
>(
|
|
`
|
|
SELECT medication_id, category
|
|
FROM medication_categories
|
|
WHERE medication_id = ANY($1::text[])
|
|
`,
|
|
medicationIds,
|
|
);
|
|
|
|
const map: Record<string, MedicationCategory> = {};
|
|
for (const id of medicationIds) {
|
|
map[id] = DEFAULT_CATEGORY;
|
|
}
|
|
for (const row of rows) {
|
|
map[row.medication_id] = normalizeCategory(row.category);
|
|
}
|
|
return map;
|
|
}
|
|
|
|
export async function setMedicationCategory(
|
|
medicationId: string,
|
|
category: unknown,
|
|
) {
|
|
await ensureMedicationCategoryTable();
|
|
const normalized = normalizeCategory(category);
|
|
|
|
await prisma.$executeRawUnsafe(
|
|
`
|
|
INSERT INTO medication_categories (medication_id, category, updated_at)
|
|
VALUES ($1, $2, NOW())
|
|
ON CONFLICT (medication_id)
|
|
DO UPDATE SET category = EXCLUDED.category, updated_at = NOW()
|
|
`,
|
|
medicationId,
|
|
normalized,
|
|
);
|
|
|
|
return normalized;
|
|
}
|
|
|
|
export async function deleteMedicationCategory(medicationId: string) {
|
|
await ensureMedicationCategoryTable();
|
|
await prisma.$executeRawUnsafe(
|
|
`DELETE FROM medication_categories WHERE medication_id = $1`,
|
|
medicationId,
|
|
);
|
|
}
|