Add notes module
This commit is contained in:
@@ -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",
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user