|
| 1 | +export type MorningRoutineItem = { |
| 2 | + id: string; |
| 3 | + label: string; |
| 4 | + minutes: number; |
| 5 | +}; |
| 6 | + |
| 7 | +type RoutineRow = { |
| 8 | + routine_json: string; |
| 9 | +}; |
| 10 | + |
| 11 | +const DEFAULT_MORNING_ROUTINE: MorningRoutineItem[] = [ |
| 12 | + { id: "prepare", label: "身支度", minutes: 20 }, |
| 13 | + { id: "breakfast", label: "朝食", minutes: 15 }, |
| 14 | +]; |
| 15 | + |
| 16 | +function createRoutineItemId(): string { |
| 17 | + if ( |
| 18 | + typeof crypto !== "undefined" && |
| 19 | + typeof crypto.randomUUID === "function" |
| 20 | + ) { |
| 21 | + return crypto.randomUUID(); |
| 22 | + } |
| 23 | + return `${Date.now()}-${Math.random().toString(36).slice(2, 10)}`; |
| 24 | +} |
| 25 | + |
| 26 | +function clampMinutes(value: number): number { |
| 27 | + if (!Number.isFinite(value)) { |
| 28 | + return 0; |
| 29 | + } |
| 30 | + return Math.min(180, Math.max(0, Math.trunc(value))); |
| 31 | +} |
| 32 | + |
| 33 | +function cloneItems(items: MorningRoutineItem[]): MorningRoutineItem[] { |
| 34 | + return items.map((item) => ({ ...item })); |
| 35 | +} |
| 36 | + |
| 37 | +function normalizeRoutineItems(value: unknown): MorningRoutineItem[] | null { |
| 38 | + if (!Array.isArray(value)) { |
| 39 | + return null; |
| 40 | + } |
| 41 | + |
| 42 | + if (value.length === 0 || value.length > 20) { |
| 43 | + return null; |
| 44 | + } |
| 45 | + |
| 46 | + const normalized = value |
| 47 | + .map((item) => { |
| 48 | + if (!item || typeof item !== "object") { |
| 49 | + return null; |
| 50 | + } |
| 51 | + |
| 52 | + const candidate = item as { |
| 53 | + id?: unknown; |
| 54 | + label?: unknown; |
| 55 | + minutes?: unknown; |
| 56 | + }; |
| 57 | + |
| 58 | + const label = |
| 59 | + typeof candidate.label === "string" ? candidate.label.trim() : ""; |
| 60 | + if (label.length === 0 || label.length > 40) { |
| 61 | + return null; |
| 62 | + } |
| 63 | + |
| 64 | + const minutesRaw = |
| 65 | + typeof candidate.minutes === "number" |
| 66 | + ? candidate.minutes |
| 67 | + : typeof candidate.minutes === "string" |
| 68 | + ? Number(candidate.minutes) |
| 69 | + : Number.NaN; |
| 70 | + const minutes = Number.isFinite(minutesRaw) |
| 71 | + ? clampMinutes(minutesRaw) |
| 72 | + : 0; |
| 73 | + |
| 74 | + const id = |
| 75 | + typeof candidate.id === "string" && candidate.id.trim().length > 0 |
| 76 | + ? candidate.id.trim() |
| 77 | + : createRoutineItemId(); |
| 78 | + |
| 79 | + return { id, label, minutes } satisfies MorningRoutineItem; |
| 80 | + }) |
| 81 | + .filter((item): item is MorningRoutineItem => item !== null); |
| 82 | + |
| 83 | + if (normalized.length === 0) { |
| 84 | + return null; |
| 85 | + } |
| 86 | + |
| 87 | + return normalized; |
| 88 | +} |
| 89 | + |
| 90 | +function parseRoutineJson(json: string): MorningRoutineItem[] | null { |
| 91 | + try { |
| 92 | + return normalizeRoutineItems(JSON.parse(json)); |
| 93 | + } catch { |
| 94 | + return null; |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +export function validateMorningRoutineItems( |
| 99 | + value: unknown, |
| 100 | +): MorningRoutineItem[] | null { |
| 101 | + return normalizeRoutineItems(value); |
| 102 | +} |
| 103 | + |
| 104 | +export async function getMorningRoutine( |
| 105 | + db: D1Database, |
| 106 | + userId: string, |
| 107 | +): Promise<MorningRoutineItem[]> { |
| 108 | + const row = await db |
| 109 | + .prepare( |
| 110 | + ` |
| 111 | + select routine_json |
| 112 | + from morning_routine_settings |
| 113 | + where user_id = ? |
| 114 | + limit 1 |
| 115 | + `, |
| 116 | + ) |
| 117 | + .bind(userId) |
| 118 | + .first<RoutineRow>(); |
| 119 | + |
| 120 | + if (!row?.routine_json) { |
| 121 | + return cloneItems(DEFAULT_MORNING_ROUTINE); |
| 122 | + } |
| 123 | + |
| 124 | + const parsed = parseRoutineJson(row.routine_json); |
| 125 | + if (!parsed) { |
| 126 | + return cloneItems(DEFAULT_MORNING_ROUTINE); |
| 127 | + } |
| 128 | + |
| 129 | + return parsed; |
| 130 | +} |
| 131 | + |
| 132 | +export async function saveMorningRoutine( |
| 133 | + db: D1Database, |
| 134 | + userId: string, |
| 135 | + items: MorningRoutineItem[], |
| 136 | +): Promise<MorningRoutineItem[]> { |
| 137 | + const normalized = normalizeRoutineItems(items); |
| 138 | + if (!normalized) { |
| 139 | + throw new Error("Invalid morning routine items."); |
| 140 | + } |
| 141 | + |
| 142 | + const now = new Date().toISOString(); |
| 143 | + await db |
| 144 | + .prepare( |
| 145 | + ` |
| 146 | + insert into morning_routine_settings ( |
| 147 | + user_id, |
| 148 | + routine_json, |
| 149 | + created_at, |
| 150 | + updated_at |
| 151 | + ) |
| 152 | + values (?, ?, ?, ?) |
| 153 | + on conflict(user_id) do update set |
| 154 | + routine_json = excluded.routine_json, |
| 155 | + updated_at = excluded.updated_at |
| 156 | + `, |
| 157 | + ) |
| 158 | + .bind(userId, JSON.stringify(normalized), now, now) |
| 159 | + .run(); |
| 160 | + |
| 161 | + return normalized; |
| 162 | +} |
0 commit comments