Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 690cb2ff74 | |||
| 21127b38ab | |||
| f5f189e0a4 | |||
| 43c5402592 | |||
| 02bae889b4 |
@@ -1,6 +1,7 @@
|
||||
name: Update Test Badges
|
||||
|
||||
on:
|
||||
workflow_dispatch:
|
||||
push:
|
||||
branches: [main]
|
||||
paths:
|
||||
@@ -47,8 +48,9 @@ jobs:
|
||||
run: |
|
||||
OUTPUT=$(npm run test:run 2>&1) || true
|
||||
echo "$OUTPUT"
|
||||
# Extract "Tests X passed" from output
|
||||
PASSED=$(echo "$OUTPUT" | grep -oP 'Tests\s+\K\d+(?=\s+passed)' | tail -1)
|
||||
# Strip ANSI escape codes, then extract "Tests X passed" from output
|
||||
CLEAN=$(echo "$OUTPUT" | sed 's/\x1b\[[0-9;]*m//g')
|
||||
PASSED=$(echo "$CLEAN" | grep -oP 'Tests\s+\K\d+(?=\s+passed)' | tail -1)
|
||||
echo "count=$PASSED" >> $GITHUB_OUTPUT
|
||||
|
||||
- name: Run frontend tests and capture count
|
||||
@@ -60,8 +62,9 @@ jobs:
|
||||
run: |
|
||||
OUTPUT=$(npm run test:run 2>&1) || true
|
||||
echo "$OUTPUT"
|
||||
# Extract "Tests X passed" from output
|
||||
PASSED=$(echo "$OUTPUT" | grep -oP 'Tests\s+\K\d+(?=\s+passed)' | tail -1)
|
||||
# Strip ANSI escape codes, then extract "Tests X passed" from output
|
||||
CLEAN=$(echo "$OUTPUT" | sed 's/\x1b\[[0-9;]*m//g')
|
||||
PASSED=$(echo "$CLEAN" | grep -oP 'Tests\s+\K\d+(?=\s+passed)' | tail -1)
|
||||
echo "count=$PASSED" >> $GITHUB_OUTPUT
|
||||
|
||||
- name: Update README badges
|
||||
|
||||
@@ -5,6 +5,7 @@ import { type Client, createClient } from "@libsql/client";
|
||||
import dotenv from "dotenv";
|
||||
import { drizzle } from "drizzle-orm/libsql";
|
||||
import { migrate } from "drizzle-orm/libsql/migrator";
|
||||
import { parseIntakesJson, parseLocalDateTime } from "../utils/scheduler-utils.js";
|
||||
|
||||
dotenv.config({ path: process.env.DOTENV_PATH || ".env" });
|
||||
|
||||
@@ -158,6 +159,165 @@ export async function ensureDefaultUser(client: Client, authEnabled: boolean): P
|
||||
}
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// Startup repair: fix orphaned dose tracking IDs from past schedule changes
|
||||
// =============================================================================
|
||||
|
||||
const MS_PER_DAY = 86_400_000;
|
||||
|
||||
/**
|
||||
* Repair dose IDs that have a trailing hyphen caused by a frontend bug where
|
||||
* `[].toString()` produced an empty string, resulting in IDs like "5-0-1729123200000-"
|
||||
* instead of "5-0-1729123200000". This strips trailing hyphens from all dose IDs.
|
||||
*
|
||||
* This function is idempotent - safe to run on every startup.
|
||||
*/
|
||||
export async function repairTrailingHyphenDoseIds(client: Client): Promise<{ repaired: number; errors: string[] }> {
|
||||
const errors: string[] = [];
|
||||
let repaired = 0;
|
||||
|
||||
try {
|
||||
const result = await client.execute(
|
||||
"UPDATE dose_tracking SET dose_id = RTRIM(dose_id, '-') WHERE dose_id LIKE '%-'"
|
||||
);
|
||||
repaired = result.rowsAffected;
|
||||
} catch (e: any) {
|
||||
errors.push(`Trailing-hyphen repair failed: ${e.message}`);
|
||||
}
|
||||
|
||||
return { repaired, errors };
|
||||
}
|
||||
|
||||
/**
|
||||
* Repair orphaned dose tracking IDs that no longer match the current intake schedule.
|
||||
* This fixes dose IDs that became invalid when a medication's schedule was changed
|
||||
* BEFORE the on-edit migration (PR #103) was introduced.
|
||||
*
|
||||
* For each medication, generates all valid schedule dateOnlyMs values from each intake's
|
||||
* start date up to today, then checks all dose_tracking entries. Any dose whose timestamp
|
||||
* doesn't match a valid schedule date is remapped to the nearest valid date.
|
||||
*
|
||||
* This function is idempotent - safe to run on every startup.
|
||||
*/
|
||||
export async function repairOrphanedDoseIds(client: Client): Promise<{ repaired: number; errors: string[] }> {
|
||||
const errors: string[] = [];
|
||||
let repaired = 0;
|
||||
|
||||
try {
|
||||
// Get all medications
|
||||
const medsResult = await client.execute(
|
||||
"SELECT id, intakes_json, usage_json, every_json, start_json, intake_reminders_enabled FROM medications"
|
||||
);
|
||||
|
||||
if (medsResult.rows.length === 0) return { repaired, errors };
|
||||
|
||||
// Get all dose tracking entries
|
||||
const dosesResult = await client.execute("SELECT id, dose_id FROM dose_tracking");
|
||||
if (dosesResult.rows.length === 0) return { repaired, errors };
|
||||
|
||||
// Build a map of medId → dose entries for quick lookup
|
||||
const dosesByMed = new Map<number, Array<{ id: number; doseId: string }>>();
|
||||
for (const row of dosesResult.rows) {
|
||||
const doseId = row.dose_id as string;
|
||||
const parts = doseId.split("-");
|
||||
if (parts.length < 3) continue;
|
||||
const medId = parseInt(parts[0], 10);
|
||||
if (Number.isNaN(medId)) continue;
|
||||
if (!dosesByMed.has(medId)) dosesByMed.set(medId, []);
|
||||
dosesByMed.get(medId)!.push({ id: row.id as number, doseId });
|
||||
}
|
||||
|
||||
const now = new Date();
|
||||
const today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
|
||||
|
||||
for (const med of medsResult.rows) {
|
||||
const medId = med.id as number;
|
||||
const medDoses = dosesByMed.get(medId);
|
||||
if (!medDoses || medDoses.length === 0) continue;
|
||||
|
||||
// Parse intakes
|
||||
const intakes = parseIntakesJson(
|
||||
med.intakes_json as string | null,
|
||||
{
|
||||
usageJson: (med.usage_json as string) || "[]",
|
||||
everyJson: (med.every_json as string) || "[]",
|
||||
startJson: (med.start_json as string) || "[]",
|
||||
},
|
||||
(med.intake_reminders_enabled as number) === 1
|
||||
);
|
||||
|
||||
if (intakes.length === 0) continue;
|
||||
|
||||
// For each intake index, build the set of valid dateOnlyMs values
|
||||
const validDatesByIntake = new Map<number, Set<number>>();
|
||||
for (let idx = 0; idx < intakes.length; idx++) {
|
||||
const intake = intakes[idx];
|
||||
const start = parseLocalDateTime(intake.start);
|
||||
const every = intake.every;
|
||||
if (every <= 0 || Number.isNaN(start.getTime())) continue;
|
||||
|
||||
const validDates = new Set<number>();
|
||||
for (let d = new Date(start); d <= today; d.setDate(d.getDate() + every)) {
|
||||
validDates.add(new Date(d.getFullYear(), d.getMonth(), d.getDate()).getTime());
|
||||
}
|
||||
validDatesByIntake.set(idx, validDates);
|
||||
}
|
||||
|
||||
// Check each dose entry
|
||||
for (const dose of medDoses) {
|
||||
const parts = dose.doseId.split("-");
|
||||
if (parts.length < 3) continue;
|
||||
|
||||
const intakeIdx = parseInt(parts[1], 10);
|
||||
const dateOnlyMs = parseInt(parts[2], 10);
|
||||
if (Number.isNaN(intakeIdx) || Number.isNaN(dateOnlyMs)) continue;
|
||||
|
||||
const validDates = validDatesByIntake.get(intakeIdx);
|
||||
if (!validDates) continue; // Unknown intake index - skip
|
||||
|
||||
// Check if this dose's timestamp is valid
|
||||
if (validDates.has(dateOnlyMs)) continue; // Already valid - nothing to do
|
||||
|
||||
// Orphaned dose - find the nearest valid schedule date
|
||||
const intake = intakes[intakeIdx];
|
||||
if (!intake) continue;
|
||||
|
||||
const halfInterval = (intake.every * MS_PER_DAY) / 2;
|
||||
let bestMatch: number | null = null;
|
||||
let bestDist = Infinity;
|
||||
|
||||
for (const validDate of validDates) {
|
||||
const dist = Math.abs(validDate - dateOnlyMs);
|
||||
if (dist < bestDist && dist <= halfInterval) {
|
||||
bestDist = dist;
|
||||
bestMatch = validDate;
|
||||
}
|
||||
}
|
||||
|
||||
if (bestMatch !== null) {
|
||||
// Rebuild dose ID with new timestamp, preserving person suffix
|
||||
const personSuffix = parts.length > 3 ? `-${parts.slice(3).join("-")}` : "";
|
||||
const newDoseId = `${medId}-${intakeIdx}-${bestMatch}${personSuffix}`;
|
||||
|
||||
try {
|
||||
await client.execute({
|
||||
sql: "UPDATE dose_tracking SET dose_id = ? WHERE id = ?",
|
||||
args: [newDoseId, dose.id],
|
||||
});
|
||||
repaired++;
|
||||
} catch (e: any) {
|
||||
errors.push(`Failed to repair dose ${dose.id}: ${e.message}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e: any) {
|
||||
errors.push(`Repair failed: ${e.message}`);
|
||||
}
|
||||
|
||||
return { repaired, errors };
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// Database initialization (runs on import)
|
||||
// =============================================================================
|
||||
@@ -218,6 +378,24 @@ async function runMigrations() {
|
||||
}
|
||||
console.log(`[DB] Tables verified/created`);
|
||||
|
||||
// Repair dose IDs with trailing hyphens (from frontend takenBy bug)
|
||||
const trailingResult = await repairTrailingHyphenDoseIds(client);
|
||||
if (trailingResult.repaired > 0) {
|
||||
console.log(`[DB] Repaired ${trailingResult.repaired} dose IDs with trailing hyphens`);
|
||||
}
|
||||
if (trailingResult.errors.length > 0) {
|
||||
trailingResult.errors.forEach((err) => console.error(`[DB] Trailing-hyphen repair error:`, err));
|
||||
}
|
||||
|
||||
// Repair orphaned dose tracking IDs from past schedule changes
|
||||
const repairResult = await repairOrphanedDoseIds(client);
|
||||
if (repairResult.repaired > 0) {
|
||||
console.log(`[DB] Repaired ${repairResult.repaired} orphaned dose tracking IDs`);
|
||||
}
|
||||
if (repairResult.errors.length > 0) {
|
||||
repairResult.errors.forEach((err) => console.error(`[DB] Dose repair error:`, err));
|
||||
}
|
||||
|
||||
// If auth is disabled, ensure a default user exists (ID=1)
|
||||
const authEnabled = process.env.AUTH_ENABLED === "true";
|
||||
const created = await ensureDefaultUser(client, authEnabled);
|
||||
|
||||
@@ -312,29 +312,122 @@ export async function medicationRoutes(app: FastifyInstance) {
|
||||
|
||||
if (!result.length) return reply.notFound();
|
||||
|
||||
// Clean up dose tracking entries that are before the earliest start date
|
||||
// This ensures consistency when the user changes the start date
|
||||
const earliestStart = Math.min(...intakes.map((b) => parseLocalDateTime(b.start).getTime()));
|
||||
if (!Number.isNaN(earliestStart)) {
|
||||
// Get all dose tracking entries for this medication and filter out invalid ones
|
||||
const allDoses = await db
|
||||
.select()
|
||||
.from(doseTracking)
|
||||
.where(and(eq(doseTracking.userId, userId), like(doseTracking.doseId, `${idNum}-%`)));
|
||||
// ---------------------------------------------------------------
|
||||
// Migrate dose tracking IDs when intake schedule changes
|
||||
// ---------------------------------------------------------------
|
||||
// Parse old intakes from the existing medication row
|
||||
const oldIntakes = parseIntakesJson(
|
||||
existing.intakesJson,
|
||||
{ usageJson: existing.usageJson, everyJson: existing.everyJson, startJson: existing.startJson },
|
||||
existing.intakeRemindersEnabled
|
||||
);
|
||||
|
||||
// Find doses with timestamps before the earliest start date
|
||||
const dosesToDelete = allDoses.filter((dose) => {
|
||||
const parts = dose.doseId.split("-");
|
||||
if (parts.length >= 3) {
|
||||
const timestamp = parseInt(parts[2], 10);
|
||||
return !Number.isNaN(timestamp) && timestamp < earliestStart;
|
||||
// Get all dose tracking entries for this medication
|
||||
const allDoses = await db
|
||||
.select()
|
||||
.from(doseTracking)
|
||||
.where(and(eq(doseTracking.userId, userId), like(doseTracking.doseId, `${idNum}-%`)));
|
||||
|
||||
if (allDoses.length > 0) {
|
||||
// Build migration map: for each intake index, map old dateOnlyMs → new dateOnlyMs
|
||||
const now = new Date();
|
||||
const migrationEnd = new Date(now.getFullYear(), now.getMonth(), now.getDate());
|
||||
const MS_PER_DAY = 86_400_000;
|
||||
|
||||
for (let idx = 0; idx < Math.max(oldIntakes.length, intakes.length); idx++) {
|
||||
const oldIntake = oldIntakes[idx];
|
||||
const newIntake = intakes[idx];
|
||||
|
||||
// Skip if this intake index doesn't exist in both old and new
|
||||
if (!oldIntake || !newIntake) continue;
|
||||
|
||||
const oldStart = parseLocalDateTime(oldIntake.start);
|
||||
const newStart = parseLocalDateTime(newIntake.start);
|
||||
const oldEvery = oldIntake.every;
|
||||
const newEvery = newIntake.every;
|
||||
|
||||
// Check if start date or interval changed (time-of-day changes don't matter for dateOnlyMs)
|
||||
const oldStartDateOnly = new Date(oldStart.getFullYear(), oldStart.getMonth(), oldStart.getDate()).getTime();
|
||||
const newStartDateOnly = new Date(newStart.getFullYear(), newStart.getMonth(), newStart.getDate()).getTime();
|
||||
|
||||
if (oldStartDateOnly === newStartDateOnly && oldEvery === newEvery) {
|
||||
continue; // No schedule change that affects dose IDs
|
||||
}
|
||||
return false;
|
||||
});
|
||||
|
||||
// Delete invalid doses
|
||||
for (const dose of dosesToDelete) {
|
||||
await db.delete(doseTracking).where(eq(doseTracking.id, dose.id));
|
||||
// Build set of new valid dateOnlyMs values for this intake
|
||||
const newDates = new Set<number>();
|
||||
for (let d = new Date(newStart); d <= migrationEnd; d.setDate(d.getDate() + newEvery)) {
|
||||
newDates.add(new Date(d.getFullYear(), d.getMonth(), d.getDate()).getTime());
|
||||
}
|
||||
|
||||
// Build set of old dateOnlyMs values with mapping to nearest new date
|
||||
const oldToNewMap = new Map<number, number>();
|
||||
for (let d = new Date(oldStart); d <= migrationEnd; d.setDate(d.getDate() + oldEvery)) {
|
||||
const oldDateMs = new Date(d.getFullYear(), d.getMonth(), d.getDate()).getTime();
|
||||
// Find the closest new date within ±(newEvery/2) days
|
||||
const halfInterval = (newEvery * MS_PER_DAY) / 2;
|
||||
let bestMatch: number | null = null;
|
||||
let bestDist = Infinity;
|
||||
for (const newDateMs of newDates) {
|
||||
const dist = Math.abs(newDateMs - oldDateMs);
|
||||
if (dist < bestDist && dist <= halfInterval) {
|
||||
bestDist = dist;
|
||||
bestMatch = newDateMs;
|
||||
}
|
||||
}
|
||||
if (bestMatch !== null && bestMatch !== oldDateMs) {
|
||||
oldToNewMap.set(oldDateMs, bestMatch);
|
||||
// Remove matched new date to prevent double-mapping
|
||||
newDates.delete(bestMatch);
|
||||
}
|
||||
}
|
||||
|
||||
// Apply migrations to dose tracking entries
|
||||
if (oldToNewMap.size > 0) {
|
||||
const prefix = `${idNum}-${idx}-`;
|
||||
const dosesToMigrate = allDoses.filter((d) => d.doseId.startsWith(prefix));
|
||||
|
||||
for (const dose of dosesToMigrate) {
|
||||
const parts = dose.doseId.split("-");
|
||||
if (parts.length >= 3) {
|
||||
const oldTimestamp = parseInt(parts[2], 10);
|
||||
const newTimestamp = oldToNewMap.get(oldTimestamp);
|
||||
if (newTimestamp !== undefined) {
|
||||
// Replace the timestamp in the dose ID, keeping any person suffix
|
||||
const newDoseId = `${idNum}-${idx}-${newTimestamp}${parts.length > 3 ? `-${parts.slice(3).join("-")}` : ""}`;
|
||||
await db.update(doseTracking).set({ doseId: newDoseId }).where(eq(doseTracking.id, dose.id));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Also clean up dose tracking entries before the earliest new start date
|
||||
const earliestStartDate = intakes.reduce((min, b) => {
|
||||
const d = parseLocalDateTime(b.start);
|
||||
// Use date-only (midnight) to match dose ID format
|
||||
const dateOnly = new Date(d.getFullYear(), d.getMonth(), d.getDate()).getTime();
|
||||
return dateOnly < min ? dateOnly : min;
|
||||
}, Infinity);
|
||||
if (!Number.isNaN(earliestStartDate)) {
|
||||
// Re-fetch after possible migrations
|
||||
const updatedDoses = await db
|
||||
.select()
|
||||
.from(doseTracking)
|
||||
.where(and(eq(doseTracking.userId, userId), like(doseTracking.doseId, `${idNum}-%`)));
|
||||
|
||||
const dosesToDelete = updatedDoses.filter((dose) => {
|
||||
const parts = dose.doseId.split("-");
|
||||
if (parts.length >= 3) {
|
||||
const timestamp = parseInt(parts[2], 10);
|
||||
return !Number.isNaN(timestamp) && timestamp < earliestStartDate;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
|
||||
for (const dose of dosesToDelete) {
|
||||
await db.delete(doseTracking).where(eq(doseTracking.id, dose.id));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -13,6 +13,8 @@ import {
|
||||
ensureDataDirectory,
|
||||
ensureDefaultUser,
|
||||
getDbPaths,
|
||||
repairOrphanedDoseIds,
|
||||
repairTrailingHyphenDoseIds,
|
||||
runAlterMigrations,
|
||||
runDrizzleMigrations,
|
||||
} from "../db/client.js";
|
||||
@@ -620,4 +622,373 @@ describe("Database Client", () => {
|
||||
expect(users.rows).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe("repairOrphanedDoseIds", () => {
|
||||
let client: ReturnType<typeof createClient>;
|
||||
|
||||
beforeEach(async () => {
|
||||
client = createClient({ url: ":memory:" });
|
||||
const db = drizzle(client);
|
||||
await migrate(db, { migrationsFolder });
|
||||
// Create a test user
|
||||
await client.execute("INSERT INTO users (id, username, auth_provider) VALUES (1, 'testuser', 'local')");
|
||||
});
|
||||
|
||||
it("should return 0 repairs when no data exists", async () => {
|
||||
const result = await repairOrphanedDoseIds(client);
|
||||
expect(result.repaired).toBe(0);
|
||||
expect(result.errors).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("should not modify dose IDs that already match the current schedule", async () => {
|
||||
// Create weekly medication starting Oct 17 (Friday)
|
||||
const intakes = JSON.stringify([
|
||||
{ usage: 1, every: 7, start: "2025-10-17T08:00:00", takenBy: null, intakeRemindersEnabled: false },
|
||||
]);
|
||||
await client.execute({
|
||||
sql: `INSERT INTO medications (id, user_id, name, intakes_json, usage_json, every_json, start_json)
|
||||
VALUES (1, 1, 'Weekly Med', ?, '[1]', '[7]', '["2025-10-17T08:00:00"]')`,
|
||||
args: [intakes],
|
||||
});
|
||||
|
||||
// Insert dose IDs that match the schedule (Fridays)
|
||||
const fri17 = new Date(2025, 9, 17).getTime();
|
||||
const fri24 = new Date(2025, 9, 24).getTime();
|
||||
await client.execute({
|
||||
sql: "INSERT INTO dose_tracking (user_id, dose_id) VALUES (1, ?)",
|
||||
args: [`1-0-${fri17}`],
|
||||
});
|
||||
await client.execute({
|
||||
sql: "INSERT INTO dose_tracking (user_id, dose_id) VALUES (1, ?)",
|
||||
args: [`1-0-${fri24}`],
|
||||
});
|
||||
|
||||
const result = await repairOrphanedDoseIds(client);
|
||||
expect(result.repaired).toBe(0);
|
||||
|
||||
// Verify IDs unchanged
|
||||
const doses = await client.execute("SELECT dose_id FROM dose_tracking ORDER BY dose_id");
|
||||
expect(doses.rows[0].dose_id).toBe(`1-0-${fri17}`);
|
||||
expect(doses.rows[1].dose_id).toBe(`1-0-${fri24}`);
|
||||
});
|
||||
|
||||
it("should repair orphaned dose IDs when schedule shifted by 1 day", async () => {
|
||||
// Current schedule: Saturdays (Oct 18)
|
||||
const intakes = JSON.stringify([
|
||||
{ usage: 1, every: 7, start: "2025-10-18T08:00:00", takenBy: null, intakeRemindersEnabled: false },
|
||||
]);
|
||||
await client.execute({
|
||||
sql: `INSERT INTO medications (id, user_id, name, intakes_json, usage_json, every_json, start_json)
|
||||
VALUES (1, 1, 'Weekly Med', ?, '[1]', '[7]', '["2025-10-18T08:00:00"]')`,
|
||||
args: [intakes],
|
||||
});
|
||||
|
||||
// Insert orphaned dose IDs from OLD schedule (Fridays)
|
||||
const fri17 = new Date(2025, 9, 17).getTime();
|
||||
const fri24 = new Date(2025, 9, 24).getTime();
|
||||
const fri31 = new Date(2025, 9, 31).getTime();
|
||||
await client.execute({
|
||||
sql: "INSERT INTO dose_tracking (user_id, dose_id) VALUES (1, ?)",
|
||||
args: [`1-0-${fri17}`],
|
||||
});
|
||||
await client.execute({
|
||||
sql: "INSERT INTO dose_tracking (user_id, dose_id) VALUES (1, ?)",
|
||||
args: [`1-0-${fri24}`],
|
||||
});
|
||||
await client.execute({
|
||||
sql: "INSERT INTO dose_tracking (user_id, dose_id) VALUES (1, ?)",
|
||||
args: [`1-0-${fri31}`],
|
||||
});
|
||||
|
||||
const result = await repairOrphanedDoseIds(client);
|
||||
expect(result.repaired).toBe(3);
|
||||
expect(result.errors).toHaveLength(0);
|
||||
|
||||
// Verify dose IDs are now Saturdays
|
||||
const sat18 = new Date(2025, 9, 18).getTime();
|
||||
const sat25 = new Date(2025, 9, 25).getTime();
|
||||
const nov1 = new Date(2025, 10, 1).getTime();
|
||||
|
||||
const doses = await client.execute("SELECT dose_id FROM dose_tracking ORDER BY dose_id");
|
||||
const ids = doses.rows.map((r) => r.dose_id);
|
||||
expect(ids).toContain(`1-0-${sat18}`);
|
||||
expect(ids).toContain(`1-0-${sat25}`);
|
||||
expect(ids).toContain(`1-0-${nov1}`);
|
||||
});
|
||||
|
||||
it("should preserve person suffix when repairing dose IDs", async () => {
|
||||
// Current schedule: Saturdays
|
||||
const intakes = JSON.stringify([
|
||||
{ usage: 1, every: 7, start: "2025-10-18T08:00:00", takenBy: "Alice", intakeRemindersEnabled: false },
|
||||
]);
|
||||
await client.execute({
|
||||
sql: `INSERT INTO medications (id, user_id, name, intakes_json, usage_json, every_json, start_json)
|
||||
VALUES (1, 1, 'Person Med', ?, '[1]', '[7]', '["2025-10-18T08:00:00"]')`,
|
||||
args: [intakes],
|
||||
});
|
||||
|
||||
// Orphaned dose with person suffix (from old Friday schedule)
|
||||
const fri17 = new Date(2025, 9, 17).getTime();
|
||||
await client.execute({
|
||||
sql: "INSERT INTO dose_tracking (user_id, dose_id) VALUES (1, ?)",
|
||||
args: [`1-0-${fri17}-Alice`],
|
||||
});
|
||||
|
||||
const result = await repairOrphanedDoseIds(client);
|
||||
expect(result.repaired).toBe(1);
|
||||
|
||||
// Verify person suffix preserved
|
||||
const sat18 = new Date(2025, 9, 18).getTime();
|
||||
const doses = await client.execute("SELECT dose_id FROM dose_tracking");
|
||||
expect(doses.rows[0].dose_id).toBe(`1-0-${sat18}-Alice`);
|
||||
});
|
||||
|
||||
it("should not repair doses that are too far from any valid schedule date", async () => {
|
||||
// Current schedule: biweekly (every 14 days) starting Oct 18
|
||||
// halfInterval = 7 days, so doses more than 7 days from any valid date won't match
|
||||
const intakes = JSON.stringify([
|
||||
{ usage: 1, every: 14, start: "2025-10-18T08:00:00", takenBy: null, intakeRemindersEnabled: false },
|
||||
]);
|
||||
await client.execute({
|
||||
sql: `INSERT INTO medications (id, user_id, name, intakes_json, usage_json, every_json, start_json)
|
||||
VALUES (1, 1, 'Biweekly Med', ?, '[1]', '[14]', '["2025-10-18T08:00:00"]')`,
|
||||
args: [intakes],
|
||||
});
|
||||
|
||||
// Insert dose on Oct 27 (9 days away from Oct 18, 4 days away from Nov 1)
|
||||
// halfInterval = 7 days. Oct 27 is 9 days from Oct 18 (too far) and 4 days from Nov 1 (within range)
|
||||
// Actually use Oct 26 which is 8 days from both (Oct 18 and Nov 1) - exactly at halfInterval + 1
|
||||
// Wait: biweekly = Oct 18, Nov 1. Oct 26 is 8 days from Oct 18, 6 days from Nov 1 → 6 < 7, matches Nov 1
|
||||
// Use Oct 25: 7 days from Oct 18, 7 days from Nov 1 → exactly at boundary. Use Oct 25 and check.
|
||||
// The condition is dist <= halfInterval, so 7 <= 7 is true. Need dist > 7.
|
||||
// Use a 28-day schedule instead: Oct 18, Nov 15. Midpoint is Nov 1-2. Nov 2 is 15 days from Oct 18, 13 from Nov 15. Both > 14. No match.
|
||||
const intakes28 = JSON.stringify([
|
||||
{ usage: 1, every: 28, start: "2025-10-18T08:00:00", takenBy: null, intakeRemindersEnabled: false },
|
||||
]);
|
||||
await client.execute({
|
||||
sql: `UPDATE medications SET intakes_json = ?, every_json = '[28]' WHERE id = 1`,
|
||||
args: [intakes28],
|
||||
});
|
||||
|
||||
// Insert dose on Nov 2 (15 days from Oct 18, 13 days from Nov 15)
|
||||
// halfInterval = 14 days. Both 15 > 14 and 13 < 14, so Nov 2 actually WOULD map to Nov 15.
|
||||
// Use Nov 4: 17 days from Oct 18, 11 days from Nov 15 → 11 < 14, maps to Nov 15.
|
||||
// For a 28-day interval, halfInterval = 14. A date must be > 14 days from ALL schedule dates.
|
||||
// Between Oct 18 and Nov 15 (28 days), the only date > 14 from both is impossible.
|
||||
// So lets use a gap: Oct 18 is the only past date for a monthly schedule.
|
||||
// If we pick a date before Oct 18, like Oct 1 (17 days before Oct 18) → 17 > 14 → no match!
|
||||
const oct1 = new Date(2025, 9, 1).getTime();
|
||||
await client.execute({
|
||||
sql: "INSERT INTO dose_tracking (user_id, dose_id) VALUES (1, ?)",
|
||||
args: [`1-0-${oct1}`],
|
||||
});
|
||||
|
||||
const result = await repairOrphanedDoseIds(client);
|
||||
expect(result.repaired).toBe(0);
|
||||
|
||||
// Dose should remain unchanged
|
||||
const doses = await client.execute("SELECT dose_id FROM dose_tracking");
|
||||
expect(doses.rows[0].dose_id).toBe(`1-0-${oct1}`);
|
||||
});
|
||||
|
||||
it("should be idempotent - running twice produces same result", async () => {
|
||||
// Current schedule: Saturdays
|
||||
const intakes = JSON.stringify([
|
||||
{ usage: 1, every: 7, start: "2025-10-18T08:00:00", takenBy: null, intakeRemindersEnabled: false },
|
||||
]);
|
||||
await client.execute({
|
||||
sql: `INSERT INTO medications (id, user_id, name, intakes_json, usage_json, every_json, start_json)
|
||||
VALUES (1, 1, 'Weekly Med', ?, '[1]', '[7]', '["2025-10-18T08:00:00"]')`,
|
||||
args: [intakes],
|
||||
});
|
||||
|
||||
// Insert orphaned dose from Friday
|
||||
const fri17 = new Date(2025, 9, 17).getTime();
|
||||
await client.execute({
|
||||
sql: "INSERT INTO dose_tracking (user_id, dose_id) VALUES (1, ?)",
|
||||
args: [`1-0-${fri17}`],
|
||||
});
|
||||
|
||||
// First run
|
||||
const result1 = await repairOrphanedDoseIds(client);
|
||||
expect(result1.repaired).toBe(1);
|
||||
|
||||
// Second run - should find 0 repairs (already fixed)
|
||||
const result2 = await repairOrphanedDoseIds(client);
|
||||
expect(result2.repaired).toBe(0);
|
||||
|
||||
// Verify final state
|
||||
const sat18 = new Date(2025, 9, 18).getTime();
|
||||
const doses = await client.execute("SELECT dose_id FROM dose_tracking");
|
||||
expect(doses.rows).toHaveLength(1);
|
||||
expect(doses.rows[0].dose_id).toBe(`1-0-${sat18}`);
|
||||
});
|
||||
|
||||
it("should handle multiple medications independently", async () => {
|
||||
// Med 1: weekly Saturdays
|
||||
const intakes1 = JSON.stringify([
|
||||
{ usage: 1, every: 7, start: "2025-10-18T08:00:00", takenBy: null, intakeRemindersEnabled: false },
|
||||
]);
|
||||
await client.execute({
|
||||
sql: `INSERT INTO medications (id, user_id, name, intakes_json, usage_json, every_json, start_json)
|
||||
VALUES (1, 1, 'Med 1', ?, '[1]', '[7]', '["2025-10-18T08:00:00"]')`,
|
||||
args: [intakes1],
|
||||
});
|
||||
|
||||
// Med 2: daily starting Oct 20 (valid IDs, no repair needed)
|
||||
const intakes2 = JSON.stringify([
|
||||
{ usage: 1, every: 1, start: "2025-10-20T08:00:00", takenBy: null, intakeRemindersEnabled: false },
|
||||
]);
|
||||
await client.execute({
|
||||
sql: `INSERT INTO medications (id, user_id, name, intakes_json, usage_json, every_json, start_json)
|
||||
VALUES (2, 1, 'Med 2', ?, '[1]', '[1]', '["2025-10-20T08:00:00"]')`,
|
||||
args: [intakes2],
|
||||
});
|
||||
|
||||
// Med 1: orphaned Friday dose
|
||||
const fri17 = new Date(2025, 9, 17).getTime();
|
||||
await client.execute({
|
||||
sql: "INSERT INTO dose_tracking (user_id, dose_id) VALUES (1, ?)",
|
||||
args: [`1-0-${fri17}`],
|
||||
});
|
||||
|
||||
// Med 2: valid daily dose
|
||||
const oct20 = new Date(2025, 9, 20).getTime();
|
||||
await client.execute({
|
||||
sql: "INSERT INTO dose_tracking (user_id, dose_id) VALUES (1, ?)",
|
||||
args: [`2-0-${oct20}`],
|
||||
});
|
||||
|
||||
const result = await repairOrphanedDoseIds(client);
|
||||
expect(result.repaired).toBe(1); // Only med 1 dose repaired
|
||||
|
||||
// Med 2 dose should be unchanged
|
||||
const med2Doses = await client.execute("SELECT dose_id FROM dose_tracking WHERE dose_id LIKE '2-%'");
|
||||
expect(med2Doses.rows[0].dose_id).toBe(`2-0-${oct20}`);
|
||||
});
|
||||
|
||||
it("should handle legacy format (no intakes_json, uses usage/every/start arrays)", async () => {
|
||||
// Medication with only legacy fields (intakes_json is '[]')
|
||||
await client.execute({
|
||||
sql: `INSERT INTO medications (id, user_id, name, intakes_json, usage_json, every_json, start_json)
|
||||
VALUES (1, 1, 'Legacy Med', '[]', '[1]', '[7]', '["2025-10-18T08:00:00"]')`,
|
||||
args: [],
|
||||
});
|
||||
|
||||
// Orphaned Friday dose
|
||||
const fri17 = new Date(2025, 9, 17).getTime();
|
||||
await client.execute({
|
||||
sql: "INSERT INTO dose_tracking (user_id, dose_id) VALUES (1, ?)",
|
||||
args: [`1-0-${fri17}`],
|
||||
});
|
||||
|
||||
const result = await repairOrphanedDoseIds(client);
|
||||
expect(result.repaired).toBe(1);
|
||||
|
||||
// Verify mapped to Saturday
|
||||
const sat18 = new Date(2025, 9, 18).getTime();
|
||||
const doses = await client.execute("SELECT dose_id FROM dose_tracking");
|
||||
expect(doses.rows[0].dose_id).toBe(`1-0-${sat18}`);
|
||||
});
|
||||
});
|
||||
|
||||
describe("repairTrailingHyphenDoseIds", () => {
|
||||
let client: ReturnType<typeof createClient>;
|
||||
|
||||
beforeEach(async () => {
|
||||
client = createClient({ url: ":memory:" });
|
||||
const db = drizzle(client);
|
||||
await migrate(db, { migrationsFolder });
|
||||
await client.execute("INSERT INTO users (id, username, auth_provider) VALUES (1, 'testuser', 'local')");
|
||||
});
|
||||
|
||||
it("should return 0 repairs when no dose IDs have trailing hyphens", async () => {
|
||||
const ts = new Date(2025, 9, 17).getTime();
|
||||
await client.execute({
|
||||
sql: "INSERT INTO dose_tracking (user_id, dose_id) VALUES (1, ?)",
|
||||
args: [`1-0-${ts}`],
|
||||
});
|
||||
|
||||
const result = await repairTrailingHyphenDoseIds(client);
|
||||
expect(result.repaired).toBe(0);
|
||||
expect(result.errors).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("should strip trailing hyphen from dose IDs", async () => {
|
||||
const ts = new Date(2025, 9, 17).getTime();
|
||||
await client.execute({
|
||||
sql: "INSERT INTO dose_tracking (user_id, dose_id) VALUES (1, ?)",
|
||||
args: [`1-0-${ts}-`],
|
||||
});
|
||||
|
||||
const result = await repairTrailingHyphenDoseIds(client);
|
||||
expect(result.repaired).toBe(1);
|
||||
expect(result.errors).toHaveLength(0);
|
||||
|
||||
const doses = await client.execute("SELECT dose_id FROM dose_tracking");
|
||||
expect(doses.rows[0].dose_id).toBe(`1-0-${ts}`);
|
||||
});
|
||||
|
||||
it("should not modify dose IDs with valid person suffixes", async () => {
|
||||
const ts = new Date(2025, 9, 17).getTime();
|
||||
await client.execute({
|
||||
sql: "INSERT INTO dose_tracking (user_id, dose_id) VALUES (1, ?)",
|
||||
args: [`1-0-${ts}-Alice`],
|
||||
});
|
||||
|
||||
const result = await repairTrailingHyphenDoseIds(client);
|
||||
expect(result.repaired).toBe(0);
|
||||
|
||||
const doses = await client.execute("SELECT dose_id FROM dose_tracking");
|
||||
expect(doses.rows[0].dose_id).toBe(`1-0-${ts}-Alice`);
|
||||
});
|
||||
|
||||
it("should handle multiple trailing hyphens", async () => {
|
||||
const ts = new Date(2025, 9, 17).getTime();
|
||||
await client.execute({
|
||||
sql: "INSERT INTO dose_tracking (user_id, dose_id) VALUES (1, ?)",
|
||||
args: [`1-0-${ts}--`],
|
||||
});
|
||||
|
||||
const result = await repairTrailingHyphenDoseIds(client);
|
||||
expect(result.repaired).toBe(1);
|
||||
|
||||
const doses = await client.execute("SELECT dose_id FROM dose_tracking");
|
||||
expect(doses.rows[0].dose_id).toBe(`1-0-${ts}`);
|
||||
});
|
||||
|
||||
it("should repair multiple affected rows at once", async () => {
|
||||
const ts1 = new Date(2025, 9, 17).getTime();
|
||||
const ts2 = new Date(2025, 9, 24).getTime();
|
||||
const ts3 = new Date(2025, 9, 31).getTime();
|
||||
await client.execute({
|
||||
sql: "INSERT INTO dose_tracking (user_id, dose_id) VALUES (1, ?), (1, ?), (1, ?)",
|
||||
args: [`1-0-${ts1}-`, `2-0-${ts2}-`, `1-0-${ts3}`],
|
||||
});
|
||||
|
||||
const result = await repairTrailingHyphenDoseIds(client);
|
||||
expect(result.repaired).toBe(2); // Only 2 had trailing hyphens
|
||||
expect(result.errors).toHaveLength(0);
|
||||
|
||||
const doses = await client.execute("SELECT dose_id FROM dose_tracking ORDER BY dose_id");
|
||||
const ids = doses.rows.map((r) => r.dose_id);
|
||||
expect(ids).toContain(`1-0-${ts1}`);
|
||||
expect(ids).toContain(`2-0-${ts2}`);
|
||||
expect(ids).toContain(`1-0-${ts3}`);
|
||||
});
|
||||
|
||||
it("should be idempotent - running twice has no effect the second time", async () => {
|
||||
const ts = new Date(2025, 9, 17).getTime();
|
||||
await client.execute({
|
||||
sql: "INSERT INTO dose_tracking (user_id, dose_id) VALUES (1, ?)",
|
||||
args: [`1-0-${ts}-`],
|
||||
});
|
||||
|
||||
const result1 = await repairTrailingHyphenDoseIds(client);
|
||||
expect(result1.repaired).toBe(1);
|
||||
|
||||
const result2 = await repairTrailingHyphenDoseIds(client);
|
||||
expect(result2.repaired).toBe(0);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -365,6 +365,196 @@ describe("Integration Tests", () => {
|
||||
});
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Dose ID Migration on Schedule Changes
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
describe("Dose ID migration when schedule changes", () => {
|
||||
it("should migrate dose IDs when weekly start day changes", async () => {
|
||||
// Create a weekly medication starting Friday Oct 17
|
||||
const createRes = await app.inject({
|
||||
method: "POST",
|
||||
url: "/medications",
|
||||
payload: {
|
||||
name: "Weekly Med",
|
||||
blisters: [{ usage: 1, every: 7, start: "2025-10-17T08:00:00" }],
|
||||
},
|
||||
});
|
||||
const medId = createRes.json().id;
|
||||
|
||||
// Mark doses for Fridays (Oct 17, Oct 24, Oct 31)
|
||||
const fri17 = new Date(2025, 9, 17).getTime(); // Oct 17
|
||||
const fri24 = new Date(2025, 9, 24).getTime(); // Oct 24
|
||||
const fri31 = new Date(2025, 9, 31).getTime(); // Oct 31
|
||||
|
||||
for (const ts of [fri17, fri24, fri31]) {
|
||||
await app.inject({
|
||||
method: "POST",
|
||||
url: "/doses/taken",
|
||||
payload: { doseId: `${medId}-0-${ts}` },
|
||||
});
|
||||
}
|
||||
|
||||
// Verify 3 doses exist
|
||||
const before = await testClient.execute({
|
||||
sql: `SELECT COUNT(*) as count FROM dose_tracking WHERE dose_id LIKE ?`,
|
||||
args: [`${medId}-%`],
|
||||
});
|
||||
expect(before.rows[0].count).toBe(3);
|
||||
|
||||
// Change start to Saturday Oct 18 (shifts all future and past IDs)
|
||||
await app.inject({
|
||||
method: "PUT",
|
||||
url: `/medications/${medId}`,
|
||||
payload: {
|
||||
name: "Weekly Med",
|
||||
blisters: [{ usage: 1, every: 7, start: "2025-10-18T08:00:00" }],
|
||||
},
|
||||
});
|
||||
|
||||
// Doses should be migrated to Saturday dates
|
||||
const sat18 = new Date(2025, 9, 18).getTime(); // Oct 18
|
||||
const sat25 = new Date(2025, 9, 25).getTime(); // Oct 25
|
||||
const nov1 = new Date(2025, 10, 1).getTime(); // Nov 1
|
||||
|
||||
const after = await testClient.execute({
|
||||
sql: `SELECT dose_id FROM dose_tracking WHERE dose_id LIKE ? ORDER BY dose_id`,
|
||||
args: [`${medId}-%`],
|
||||
});
|
||||
expect(after.rows.length).toBe(3);
|
||||
const ids = after.rows.map((r: { dose_id: string }) => r.dose_id);
|
||||
expect(ids).toContain(`${medId}-0-${sat18}`);
|
||||
expect(ids).toContain(`${medId}-0-${sat25}`);
|
||||
expect(ids).toContain(`${medId}-0-${nov1}`);
|
||||
});
|
||||
|
||||
it("should migrate dose IDs with person suffix when schedule changes", async () => {
|
||||
// Create weekly medication with takenBy person
|
||||
const createRes = await app.inject({
|
||||
method: "POST",
|
||||
url: "/medications",
|
||||
payload: {
|
||||
name: "Person Med",
|
||||
intakes: [{ usage: 1, every: 7, start: "2025-10-17T08:00:00", takenBy: "Alice" }],
|
||||
},
|
||||
});
|
||||
const medId = createRes.json().id;
|
||||
|
||||
// Mark dose with person suffix
|
||||
const fri17 = new Date(2025, 9, 17).getTime();
|
||||
await app.inject({
|
||||
method: "POST",
|
||||
url: "/doses/taken",
|
||||
payload: { doseId: `${medId}-0-${fri17}-Alice` },
|
||||
});
|
||||
|
||||
// Change start day
|
||||
await app.inject({
|
||||
method: "PUT",
|
||||
url: `/medications/${medId}`,
|
||||
payload: {
|
||||
name: "Person Med",
|
||||
intakes: [{ usage: 1, every: 7, start: "2025-10-18T08:00:00", takenBy: "Alice" }],
|
||||
},
|
||||
});
|
||||
|
||||
// Dose should be migrated with person suffix preserved
|
||||
const sat18 = new Date(2025, 9, 18).getTime();
|
||||
const after = await testClient.execute({
|
||||
sql: `SELECT dose_id FROM dose_tracking WHERE dose_id LIKE ?`,
|
||||
args: [`${medId}-%`],
|
||||
});
|
||||
expect(after.rows.length).toBe(1);
|
||||
expect(after.rows[0].dose_id).toBe(`${medId}-0-${sat18}-Alice`);
|
||||
});
|
||||
|
||||
it("should not migrate dose IDs when only time-of-day changes", async () => {
|
||||
// Create daily medication at 08:00
|
||||
const createRes = await app.inject({
|
||||
method: "POST",
|
||||
url: "/medications",
|
||||
payload: {
|
||||
name: "Daily Med",
|
||||
blisters: [{ usage: 1, every: 1, start: "2025-10-17T08:00:00" }],
|
||||
},
|
||||
});
|
||||
const medId = createRes.json().id;
|
||||
|
||||
// Mark dose
|
||||
const oct17 = new Date(2025, 9, 17).getTime();
|
||||
await app.inject({
|
||||
method: "POST",
|
||||
url: "/doses/taken",
|
||||
payload: { doseId: `${medId}-0-${oct17}` },
|
||||
});
|
||||
|
||||
// Change only time from 08:00 to 20:00 (same date)
|
||||
await app.inject({
|
||||
method: "PUT",
|
||||
url: `/medications/${medId}`,
|
||||
payload: {
|
||||
name: "Daily Med",
|
||||
blisters: [{ usage: 1, every: 1, start: "2025-10-17T20:00:00" }],
|
||||
},
|
||||
});
|
||||
|
||||
// Dose ID should remain unchanged (dateOnlyMs is the same)
|
||||
const after = await testClient.execute({
|
||||
sql: `SELECT dose_id FROM dose_tracking WHERE dose_id LIKE ?`,
|
||||
args: [`${medId}-%`],
|
||||
});
|
||||
expect(after.rows.length).toBe(1);
|
||||
expect(after.rows[0].dose_id).toBe(`${medId}-0-${oct17}`);
|
||||
});
|
||||
|
||||
it("should migrate dose IDs when interval changes from daily to every-other-day", async () => {
|
||||
// Create daily medication starting Oct 17
|
||||
const createRes = await app.inject({
|
||||
method: "POST",
|
||||
url: "/medications",
|
||||
payload: {
|
||||
name: "Interval Med",
|
||||
blisters: [{ usage: 1, every: 1, start: "2025-10-17T08:00:00" }],
|
||||
},
|
||||
});
|
||||
const medId = createRes.json().id;
|
||||
|
||||
// Mark doses for Oct 17, 18, 19
|
||||
const oct17 = new Date(2025, 9, 17).getTime();
|
||||
const oct18 = new Date(2025, 9, 18).getTime();
|
||||
const oct19 = new Date(2025, 9, 19).getTime();
|
||||
|
||||
for (const ts of [oct17, oct18, oct19]) {
|
||||
await app.inject({
|
||||
method: "POST",
|
||||
url: "/doses/taken",
|
||||
payload: { doseId: `${medId}-0-${ts}` },
|
||||
});
|
||||
}
|
||||
|
||||
// Change to every 2 days (Oct 17, 19, 21, ...)
|
||||
await app.inject({
|
||||
method: "PUT",
|
||||
url: `/medications/${medId}`,
|
||||
payload: {
|
||||
name: "Interval Med",
|
||||
blisters: [{ usage: 1, every: 2, start: "2025-10-17T08:00:00" }],
|
||||
},
|
||||
});
|
||||
|
||||
// Oct 17 stays (matches), Oct 18 → Oct 19 (nearest), Oct 19 → no match (already used)
|
||||
// Actually: Oct 17 is exact match (no migration needed), Oct 18 maps to Oct 19 (within 1 day = half of 2),
|
||||
// Oct 19 was the original schedule date but the new schedule also has Oct 19,
|
||||
// which was already taken by Oct 18's migration
|
||||
const after = await testClient.execute({
|
||||
sql: `SELECT dose_id FROM dose_tracking WHERE dose_id LIKE ? ORDER BY dose_id`,
|
||||
args: [`${medId}-%`],
|
||||
});
|
||||
// We should have at least the doses that could be mapped
|
||||
expect(after.rows.length).toBeGreaterThanOrEqual(2);
|
||||
});
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Share Link + Dose Tracking Integration
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@@ -4,7 +4,7 @@ import { useAuth } from "../components/Auth";
|
||||
import { useAppContext } from "../context";
|
||||
import type { Coverage } from "../types";
|
||||
import { formatNumber, getExpiryClass, getSystemLocale } from "../utils/formatters";
|
||||
import { getStockStatus } from "../utils/schedule";
|
||||
import { expandDoseIds, getStockStatus } from "../utils/schedule";
|
||||
|
||||
// Helper for user-specific localStorage keys
|
||||
function userStorageKey(userId: number | undefined, key: string): string {
|
||||
@@ -490,11 +490,7 @@ export function DashboardPage() {
|
||||
{pastDays.length > 0 &&
|
||||
(() => {
|
||||
const missedCount = missedPastDoseIds.length;
|
||||
const totalPastDoses = pastDays.flatMap((d) =>
|
||||
d.meds.flatMap((m) =>
|
||||
m.doses.flatMap((dose) => (dose.takenBy ? [`${dose.id}-${dose.takenBy}`] : [dose.id]))
|
||||
)
|
||||
);
|
||||
const totalPastDoses = pastDays.flatMap((d) => d.meds.flatMap((m) => expandDoseIds(m.doses)));
|
||||
return (
|
||||
<div className="past-days-header">
|
||||
<div
|
||||
@@ -541,7 +537,10 @@ export function DashboardPage() {
|
||||
{showPastDays &&
|
||||
pastDays.map((day) => {
|
||||
const allDoseIds = day.meds.flatMap((item) =>
|
||||
item.doses.flatMap((d) => (d.takenBy ? [`${d.id}-${d.takenBy}`] : [d.id]))
|
||||
item.doses.flatMap((d) => {
|
||||
const takenByArray = Array.isArray(d.takenBy) ? d.takenBy : [];
|
||||
return takenByArray.length > 0 ? takenByArray.map((p) => `${d.id}-${p}`) : [d.id];
|
||||
})
|
||||
);
|
||||
const allDayTaken =
|
||||
allDoseIds.length > 0 && allDoseIds.every((id) => takenDoses.has(id) || dismissedDoses.has(id));
|
||||
@@ -586,7 +585,7 @@ export function DashboardPage() {
|
||||
const med = meds.find((m) => m.name === item.medName);
|
||||
const medCov = coverageByMed[item.medName];
|
||||
const isEmpty = medCov ? medCov.medsLeft <= 0 : false;
|
||||
const itemDoseIds = item.doses.flatMap((d) => (d.takenBy ? [`${d.id}-${d.takenBy}`] : [d.id]));
|
||||
const itemDoseIds = expandDoseIds(item.doses);
|
||||
const allTaken = itemDoseIds.every((id) => takenDoses.has(id));
|
||||
return (
|
||||
<div key={`${day.dateStr}-${item.medName}`} className={`time-row ${allTaken ? "taken" : ""}`}>
|
||||
@@ -676,9 +675,7 @@ export function DashboardPage() {
|
||||
{todayDay &&
|
||||
(() => {
|
||||
const day = todayDay;
|
||||
const allDoseIds = day.meds.flatMap((item) =>
|
||||
item.doses.flatMap((d) => (d.takenBy ? [`${d.id}-${d.takenBy}`] : [d.id]))
|
||||
);
|
||||
const allDoseIds = day.meds.flatMap((item) => expandDoseIds(item.doses));
|
||||
const allDayTaken = allDoseIds.length > 0 && allDoseIds.every((id) => takenDoses.has(id));
|
||||
const takenCount = allDoseIds.filter((id) => takenDoses.has(id)).length;
|
||||
|
||||
@@ -737,7 +734,7 @@ export function DashboardPage() {
|
||||
: medCoverage
|
||||
? getStockStatus(medCoverage.daysLeft, medCoverage.medsLeft, stockThresholds)
|
||||
: null;
|
||||
const itemDoseIds = item.doses.flatMap((d) => (d.takenBy ? [`${d.id}-${d.takenBy}`] : [d.id]));
|
||||
const itemDoseIds = expandDoseIds(item.doses);
|
||||
const allTaken = itemDoseIds.every((id) => takenDoses.has(id));
|
||||
return (
|
||||
<div key={`${day.dateStr}-${item.medName}`} className={`time-row ${allTaken ? "taken" : ""}`}>
|
||||
@@ -769,7 +766,7 @@ export function DashboardPage() {
|
||||
<div className="doses-col">
|
||||
{item.doses.map((dose) => {
|
||||
const isOverdue = dose.when < Date.now();
|
||||
const people = dose.takenBy ? [dose.takenBy] : [null];
|
||||
const people = dose.takenBy.length > 0 ? dose.takenBy : [null];
|
||||
const allTaken = people.every((person) => takenDoses.has(getDoseId(dose.id, person)));
|
||||
return (
|
||||
<div
|
||||
@@ -866,9 +863,7 @@ export function DashboardPage() {
|
||||
{/* Future days */}
|
||||
{showFutureDays &&
|
||||
futureDays.map((day) => {
|
||||
const allDoseIds = day.meds.flatMap((item) =>
|
||||
item.doses.flatMap((d) => (d.takenBy ? [`${d.id}-${d.takenBy}`] : [d.id]))
|
||||
);
|
||||
const allDoseIds = day.meds.flatMap((item) => expandDoseIds(item.doses));
|
||||
const allDayTaken = allDoseIds.length > 0 && allDoseIds.every((id) => takenDoses.has(id));
|
||||
const takenCount = allDoseIds.filter((id) => takenDoses.has(id)).length;
|
||||
|
||||
@@ -926,7 +921,7 @@ export function DashboardPage() {
|
||||
: medCoverage
|
||||
? getStockStatus(medCoverage.daysLeft, medCoverage.medsLeft, stockThresholds)
|
||||
: null;
|
||||
const itemDoseIds = item.doses.flatMap((d) => (d.takenBy ? [`${d.id}-${d.takenBy}`] : [d.id]));
|
||||
const itemDoseIds = expandDoseIds(item.doses);
|
||||
const allTaken = itemDoseIds.every((id) => takenDoses.has(id));
|
||||
return (
|
||||
<div key={`${day.dateStr}-${item.medName}`} className={`time-row ${allTaken ? "taken" : ""}`}>
|
||||
|
||||
@@ -3,6 +3,7 @@ import { MedicationAvatar } from "../components";
|
||||
import { useAuth } from "../components/Auth";
|
||||
import { useAppContext } from "../context";
|
||||
import type { Coverage } from "../types";
|
||||
import { expandDoseIds } from "../utils/schedule";
|
||||
|
||||
// Helper for user-specific localStorage keys
|
||||
function userStorageKey(userId: number | undefined, key: string): string {
|
||||
@@ -125,12 +126,7 @@ export function SchedulePage() {
|
||||
{/* Past days (when expanded) */}
|
||||
{showPastDays &&
|
||||
pastDays.map((day) => {
|
||||
const allDoseIds = day.meds.flatMap((item) =>
|
||||
item.doses.flatMap((d) => {
|
||||
const takenByArray = Array.isArray(d.takenBy) ? d.takenBy : [];
|
||||
return takenByArray.length > 0 ? takenByArray.map((p) => `${d.id}-${p}`) : [d.id];
|
||||
})
|
||||
);
|
||||
const allDoseIds = day.meds.flatMap((item) => expandDoseIds(item.doses));
|
||||
const allDayTaken = allDoseIds.length > 0 && allDoseIds.every((id) => takenDoses.has(id));
|
||||
const takenCount = allDoseIds.filter((id) => takenDoses.has(id)).length;
|
||||
const isManuallyExpanded = manuallyExpandedDays.has(day.dateStr);
|
||||
@@ -172,10 +168,7 @@ export function SchedulePage() {
|
||||
const med = meds.find((m) => m.name === item.medName);
|
||||
const medCov = coverageByMed[item.medName];
|
||||
const isEmpty = medCov ? medCov.medsLeft <= 0 : false;
|
||||
const itemDoseIds = item.doses.flatMap((d) => {
|
||||
const takenByArray = Array.isArray(d.takenBy) ? d.takenBy : [];
|
||||
return takenByArray.length > 0 ? takenByArray.map((p) => `${d.id}-${p}`) : [d.id];
|
||||
});
|
||||
const itemDoseIds = expandDoseIds(item.doses);
|
||||
const allTaken = itemDoseIds.every((id) => takenDoses.has(id));
|
||||
return (
|
||||
<div key={`${day.dateStr}-${item.medName}`} className={`time-row ${allTaken ? "taken" : ""}`}>
|
||||
@@ -277,10 +270,7 @@ export function SchedulePage() {
|
||||
: medCoverage
|
||||
? getStockStatus(medCoverage.daysLeft, medCoverage.medsLeft, settings)
|
||||
: null;
|
||||
const itemDoseIds = item.doses.flatMap((d) => {
|
||||
const takenByArray = Array.isArray(d.takenBy) ? d.takenBy : [];
|
||||
return takenByArray.length > 0 ? takenByArray.map((p) => `${d.id}-${p}`) : [d.id];
|
||||
});
|
||||
const itemDoseIds = expandDoseIds(item.doses);
|
||||
const allTaken = itemDoseIds.every((id) => takenDoses.has(id));
|
||||
return (
|
||||
<div key={`${day.dateStr}-${item.medName}`} className={`time-row ${allTaken ? "taken" : ""}`}>
|
||||
|
||||
@@ -4,6 +4,7 @@ import {
|
||||
buildSchedulePreview,
|
||||
calculateCoverage,
|
||||
computeMissedPastDoseIds,
|
||||
expandDoseIds,
|
||||
getNextReminderForMed,
|
||||
getReminderStatusText,
|
||||
getStockStatus,
|
||||
@@ -1147,3 +1148,52 @@ function groupEventsIntoPastDays(
|
||||
meds: Array.from(medMap.entries()).map(([medName, doses]) => ({ medName, doses })),
|
||||
}));
|
||||
}
|
||||
|
||||
describe("expandDoseIds", () => {
|
||||
it("returns base IDs when takenBy is empty array", () => {
|
||||
const doses = [
|
||||
{ id: "1-0-1729123200000", takenBy: [] as string[] },
|
||||
{ id: "2-0-1729123200000", takenBy: [] as string[] },
|
||||
];
|
||||
const result = expandDoseIds(doses);
|
||||
expect(result).toEqual(["1-0-1729123200000", "2-0-1729123200000"]);
|
||||
});
|
||||
|
||||
it("returns person-suffixed IDs when takenBy has entries", () => {
|
||||
const doses = [{ id: "1-0-1729123200000", takenBy: ["Alice"] }];
|
||||
const result = expandDoseIds(doses);
|
||||
expect(result).toEqual(["1-0-1729123200000-Alice"]);
|
||||
});
|
||||
|
||||
it("returns multiple IDs for multiple takenBy entries", () => {
|
||||
const doses = [{ id: "1-0-1729123200000", takenBy: ["Alice", "Bob"] }];
|
||||
const result = expandDoseIds(doses);
|
||||
expect(result).toEqual(["1-0-1729123200000-Alice", "1-0-1729123200000-Bob"]);
|
||||
});
|
||||
|
||||
it("handles mix of empty and non-empty takenBy", () => {
|
||||
const doses = [
|
||||
{ id: "1-0-1729123200000", takenBy: ["Alice"] },
|
||||
{ id: "2-0-1729123200000", takenBy: [] as string[] },
|
||||
{ id: "3-0-1729123200000", takenBy: ["Bob", "Carol"] },
|
||||
];
|
||||
const result = expandDoseIds(doses);
|
||||
expect(result).toEqual([
|
||||
"1-0-1729123200000-Alice",
|
||||
"2-0-1729123200000",
|
||||
"3-0-1729123200000-Bob",
|
||||
"3-0-1729123200000-Carol",
|
||||
]);
|
||||
});
|
||||
|
||||
it("returns empty array for empty doses", () => {
|
||||
expect(expandDoseIds([])).toEqual([]);
|
||||
});
|
||||
|
||||
it("handles non-array takenBy gracefully", () => {
|
||||
// In case of unexpected data, treat non-array as empty
|
||||
const doses = [{ id: "1-0-1729123200000", takenBy: null as unknown as string[] }];
|
||||
const result = expandDoseIds(doses);
|
||||
expect(result).toEqual(["1-0-1729123200000"]);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -446,6 +446,18 @@ export function isDoseDismissed(doseId: string, dismissedUntilDate: string | und
|
||||
* @param dismissedDoses - Set of dose IDs individually dismissed
|
||||
* @returns Array of dose IDs that are missed
|
||||
*/
|
||||
/**
|
||||
* Compute the full set of dose IDs for a list of doses, correctly handling
|
||||
* per-intake takenBy arrays. Empty arrays produce base IDs (no suffix),
|
||||
* non-empty arrays produce one ID per person with a `-person` suffix.
|
||||
*/
|
||||
export function expandDoseIds(doses: ReadonlyArray<{ id: string; takenBy: string[] }>): string[] {
|
||||
return doses.flatMap((d) => {
|
||||
const takenByArray = Array.isArray(d.takenBy) ? d.takenBy : [];
|
||||
return takenByArray.length > 0 ? takenByArray.map((p) => `${d.id}-${p}`) : [d.id];
|
||||
});
|
||||
}
|
||||
|
||||
export function computeMissedPastDoseIds(
|
||||
pastDays: ReadonlyArray<{
|
||||
meds: ReadonlyArray<{
|
||||
|
||||
Reference in New Issue
Block a user