mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-04 12:27:28 +01:00
refactor(habits): type frequency end-to-end as HabitFrequency, not string
The frequency column was stored as text without a TypeScript-level
constraint (`text("frequency")`), so Drizzle inferred plain `string`
everywhere it was read. The doc comment "/** daily | weekdays | weekly */"
was the only place that knowledge lived in code form. Consequence:
- HabitRow.tsx had to `as HabitFrequency` when feeding `habit.frequency`
to the inline-edit draft.
- HabitWithStatus / HabitWithHistory / createHabit / isDueToday all
hand-typed it as `string`, losing the union narrowing.
Now `.$type<HabitFrequency>()` on the schema column propagates the
union through Drizzle inference. The query-projection types and
helper signatures use HabitFrequency directly. Default literal
`"daily"` now goes through HABIT_FREQUENCY.DAILY (SSOT). The
HabitRow cast is gone. The two remaining `as HabitFrequency` are on
HTML select `e.target.value`, which is genuinely string→union narrowing
at a system boundary and unavoidable.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
4 files changed
+10
-10
No files matched your search
@@ -1,9 +1,9 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { createHabit, getTodayHabits } from "@/db/queries/habits";
|
||||
import { HABIT_FREQUENCY } from "@/lib/constants/statuses";
|
||||
import { HABIT_FREQUENCY, type HabitFrequency } from "@/lib/constants/statuses";
|
||||
import { readJsonBody, z } from "@/lib/api/route-helpers";
|
||||
|
||||
const FREQUENCIES = Object.values(HABIT_FREQUENCY) as [string, ...string[]];
|
||||
const FREQUENCIES = Object.values(HABIT_FREQUENCY) as [HabitFrequency, ...HabitFrequency[]];
|
||||
|
||||
const CreateHabitBody = z.object({
|
||||
title: z.string().trim().min(1, "title is required"),
|
||||
|
||||
@@ -26,7 +26,7 @@ export function HabitRow({
|
||||
|
||||
const startEdit = () => ie.start({
|
||||
title: habit.title,
|
||||
frequency: habit.frequency as HabitFrequency,
|
||||
frequency: habit.frequency,
|
||||
});
|
||||
|
||||
const commitEdit = () => {
|
||||
|
||||
@@ -2,13 +2,13 @@ import { DEFAULT_USER_ID, HABIT_HISTORY_DAYS } from "@/lib/constants";
|
||||
import { db } from "@/db";
|
||||
import { habits, habitCompletions } from "@/db/schema";
|
||||
import { eq, and, inArray, sql } from "drizzle-orm";
|
||||
import { HABIT_FREQUENCY } from "@/lib/constants/statuses";
|
||||
import { HABIT_FREQUENCY, type HabitFrequency } from "@/lib/constants/statuses";
|
||||
import { toLocalDateStr } from "@/lib/dates";
|
||||
|
||||
const todayDate = () => toLocalDateStr(new Date());
|
||||
|
||||
/** Whether a habit should appear today given its frequency */
|
||||
function isDueToday(frequency: string): boolean {
|
||||
function isDueToday(frequency: HabitFrequency): boolean {
|
||||
const dow = new Date().getDay(); // 0=Sun, 6=Sat
|
||||
if (frequency === HABIT_FREQUENCY.WEEKDAYS) return dow >= 1 && dow <= 5;
|
||||
if (frequency === HABIT_FREQUENCY.WEEKLY) return dow === 1; // Mondays
|
||||
@@ -18,7 +18,7 @@ function isDueToday(frequency: string): boolean {
|
||||
export type HabitWithStatus = {
|
||||
id: string;
|
||||
title: string;
|
||||
frequency: string;
|
||||
frequency: HabitFrequency;
|
||||
sortOrder: number;
|
||||
doneToday: boolean;
|
||||
/** 7-day streak count */
|
||||
@@ -98,7 +98,7 @@ export async function toggleHabitCompletion(habitId: string, done: boolean): Pro
|
||||
}
|
||||
}
|
||||
|
||||
export async function createHabit(title: string, frequency: string): Promise<{ id: string; title: string }> {
|
||||
export async function createHabit(title: string, frequency: HabitFrequency): Promise<{ id: string; title: string }> {
|
||||
const [maxOrder] = await db
|
||||
.select({ max: sql<number>`coalesce(max(${habits.sortOrder}), -1)` })
|
||||
.from(habits)
|
||||
@@ -125,7 +125,7 @@ export async function deleteHabit(id: string): Promise<void> {
|
||||
export type HabitWithHistory = {
|
||||
id: string;
|
||||
title: string;
|
||||
frequency: string;
|
||||
frequency: HabitFrequency;
|
||||
sortOrder: number;
|
||||
active: boolean;
|
||||
createdAt: Date;
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
import { pgTable, uuid, text, date, boolean, integer, timestamp, index, unique } from "drizzle-orm/pg-core";
|
||||
import { users } from "./users";
|
||||
import { HABIT_FREQUENCY, type HabitFrequency } from "@/lib/constants/statuses";
|
||||
|
||||
export const habits = pgTable("habits", {
|
||||
id: uuid("id").primaryKey().defaultRandom(),
|
||||
userId: uuid("user_id").notNull().references(() => users.id),
|
||||
title: text("title").notNull(),
|
||||
/** daily | weekdays | weekly */
|
||||
frequency: text("frequency").notNull().default("daily"),
|
||||
frequency: text("frequency").$type<HabitFrequency>().notNull().default(HABIT_FREQUENCY.DAILY),
|
||||
/** Display order (lower = first) */
|
||||
sortOrder: integer("sort_order").notNull().default(0),
|
||||
active: boolean("active").notNull().default(true),
|
||||
|
||||
Reference in new issue
Block a user