Add notes module

This commit is contained in:
ginnoir
2026-05-06 04:10:50 -05:00
parent 016d01cc25
commit 5b434702ba
19 changed files with 775 additions and 24 deletions
+142
View File
@@ -0,0 +1,142 @@
"use server";
import { and, eq } from "drizzle-orm";
import { revalidatePath } from "next/cache";
import { z } from "zod";
import { db } from "@/lib/db";
import { getCurrentSession } from "@/lib/session";
import { reminders } from "@/modules/_core/schema";
import { notes } from "../schema";
import { canAccessNote, getNote } from "./queries";
const noteInput = z.object({
title: z.string().trim().min(1).max(200),
body: z.string().max(20000).default(""),
pinned: z.boolean().default(false),
remindAt: z.coerce.date().nullable().optional(),
});
const updateNoteInput = z.object({
id: z.string().uuid(),
title: noteInput.shape.title.optional(),
body: z.string().max(20000).optional(),
pinned: z.boolean().optional(),
remindAt: z.coerce.date().nullable().optional(),
});
export async function createNote(input: z.input<typeof noteInput>) {
const parsed = noteInput.parse(input);
const { household, user } = await getCurrentSession();
const [note] = await db.transaction(async (tx) => {
const [created] = await tx
.insert(notes)
.values({
householdId: household.id,
authorId: user.id,
title: parsed.title,
body: parsed.body,
pinned: parsed.pinned,
remindAt: parsed.remindAt ?? null,
})
.returning();
if (!created) throw new Error("Note was not created");
await syncNoteReminder(tx, {
householdId: household.id,
noteId: created.id,
remindAt: created.remindAt,
});
return [created];
});
revalidatePath("/notes");
return note;
}
export async function updateNote(input: z.input<typeof updateNoteInput>) {
const parsed = updateNoteInput.parse(input);
const { household } = await getCurrentSession();
await assertCanAccessNote(parsed.id, household.id);
const [note] = await db.transaction(async (tx) => {
const [updated] = await tx
.update(notes)
.set({
title: parsed.title,
body: parsed.body,
pinned: parsed.pinned,
remindAt: parsed.remindAt === undefined ? undefined : parsed.remindAt,
updatedAt: new Date(),
})
.where(eq(notes.id, parsed.id))
.returning();
if (!updated) throw new Error("Note was not updated");
if (parsed.remindAt !== undefined) {
await syncNoteReminder(tx, {
householdId: household.id,
noteId: updated.id,
remindAt: updated.remindAt,
});
}
return [updated];
});
revalidatePath("/notes");
revalidatePath(`/notes/${parsed.id}`);
return note;
}
export async function setNotePinned(input: { id: string; pinned: boolean }) {
const parsed = z.object({ id: z.string().uuid(), pinned: z.boolean() }).parse(input);
const { household } = await getCurrentSession();
await assertCanAccessNote(parsed.id, household.id);
await db
.update(notes)
.set({ pinned: parsed.pinned, updatedAt: new Date() })
.where(eq(notes.id, parsed.id));
revalidatePath("/notes");
revalidatePath(`/notes/${parsed.id}`);
return getNote(parsed.id);
}
export async function deleteNote(input: { id: string }) {
const parsed = z.object({ id: z.string().uuid() }).parse(input);
const { household } = await getCurrentSession();
await assertCanAccessNote(parsed.id, household.id);
await db.transaction(async (tx) => {
await tx
.delete(reminders)
.where(and(eq(reminders.entityType, "notes.note"), eq(reminders.entityId, parsed.id)));
await tx.delete(notes).where(eq(notes.id, parsed.id));
});
revalidatePath("/notes");
}
async function assertCanAccessNote(noteId: string, householdId: string) {
if (!(await canAccessNote(noteId, householdId))) throw new Error("Forbidden");
}
async function syncNoteReminder(
tx: Parameters<Parameters<typeof db.transaction>[0]>[0],
input: { householdId: string; noteId: string; remindAt: Date | null },
) {
await tx
.delete(reminders)
.where(and(eq(reminders.entityType, "notes.note"), eq(reminders.entityId, input.noteId)));
if (!input.remindAt) return;
await tx.insert(reminders).values({
householdId: input.householdId,
entityType: "notes.note",
entityId: input.noteId,
fireAt: input.remindAt,
channel: "in_app",
});
}
+112
View File
@@ -0,0 +1,112 @@
"use server";
import { and, desc, eq, or, sql } from "drizzle-orm";
import { z } from "zod";
import { db } from "@/lib/db";
import { getCurrentSession } from "@/lib/session";
import { notes } from "../schema";
export type NoteDto = {
id: string;
householdId: string;
authorId: string;
title: string;
body: string;
pinned: boolean;
remindAt: string | null;
createdAt: string;
updatedAt: string;
};
export async function listNotes(): Promise<NoteDto[]> {
const { household } = await getCurrentSession();
const rows = await db
.select()
.from(notes)
.where(eq(notes.householdId, household.id))
.orderBy(desc(notes.pinned), desc(notes.updatedAt));
return rows.map(toNoteDto);
}
export async function getNote(id: string): Promise<NoteDto> {
const parsed = z.string().uuid().parse(id);
const { household } = await getCurrentSession();
const [note] = await db
.select()
.from(notes)
.where(and(eq(notes.id, parsed), eq(notes.householdId, household.id)))
.limit(1);
if (!note) throw new Error("Note not found");
return toNoteDto(note);
}
export async function canAccessNote(noteId: string, householdId: string) {
const [note] = await db
.select({ id: notes.id })
.from(notes)
.where(and(eq(notes.id, noteId), eq(notes.householdId, householdId)))
.limit(1);
return !!note;
}
export async function searchNotes(query: string, householdId: string) {
const rows = await db
.select({
id: notes.id,
title: notes.title,
body: notes.body,
})
.from(notes)
.where(
and(
eq(notes.householdId, householdId),
or(sql`${notes.title} ilike ${`%${query}%`}`, sql`${notes.body} ilike ${`%${query}%`}`),
),
)
.limit(10);
return rows.map((row) => ({
id: row.id,
title: row.title,
url: `/notes/${row.id}`,
excerpt: row.body.slice(0, 160),
}));
}
export async function listWidgetNotes(input: { filter: "pinned" | "all"; limit?: number }) {
const parsed = z
.object({
filter: z.enum(["pinned", "all"]),
limit: z.number().int().min(1).max(50).optional(),
})
.parse(input);
const { household } = await getCurrentSession();
const conditions = [eq(notes.householdId, household.id)];
if (parsed.filter === "pinned") conditions.push(eq(notes.pinned, true));
const rows = await db
.select()
.from(notes)
.where(and(...conditions))
.orderBy(desc(notes.pinned), desc(notes.updatedAt))
.limit(parsed.limit ?? 10);
return rows.map(toNoteDto);
}
function toNoteDto(note: typeof notes.$inferSelect): NoteDto {
return {
id: note.id,
householdId: note.householdId,
authorId: note.authorId,
title: note.title,
body: note.body,
pinned: note.pinned,
remindAt: note.remindAt?.toISOString() ?? null,
createdAt: note.createdAt.toISOString(),
updatedAt: note.updatedAt.toISOString(),
};
}