feat: garden containers crud (task 71)
- add listContainers/getContainer/searchContainers queries - add createContainer/updateContainer/deleteContainer server actions with logActivity - add ContainerList, ContainerDetail, ContainerForm client components - add /garden, /garden/containers/[id], /garden/containers/new pages - register garden.container entity with share + search in manifest - add sprout icon to NavIcon registry - add garden e2e test spec
This commit is contained in:
@@ -1,3 +1,97 @@
|
||||
"use server";
|
||||
|
||||
// Garden server actions — implemented in tasks 71–74
|
||||
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 { logActivity } from "@/modules/_core/activity";
|
||||
import { gardenContainers } from "../schema";
|
||||
|
||||
const containerInput = z.object({
|
||||
name: z.string().trim().min(1).max(120),
|
||||
type: z.string().trim().min(1).max(40).default("other"),
|
||||
locationNotes: z.string().trim().max(500).nullable().optional(),
|
||||
coverImageUrl: z.string().trim().max(500).nullable().optional(),
|
||||
});
|
||||
|
||||
export async function createContainer(input: z.input<typeof containerInput>) {
|
||||
const parsed = containerInput.parse(input);
|
||||
const { household } = await getCurrentSession();
|
||||
|
||||
const [container] = await db
|
||||
.insert(gardenContainers)
|
||||
.values({
|
||||
householdId: household.id,
|
||||
name: parsed.name,
|
||||
type: parsed.type,
|
||||
locationNotes: parsed.locationNotes ?? null,
|
||||
coverImageUrl: parsed.coverImageUrl ?? null,
|
||||
})
|
||||
.returning();
|
||||
|
||||
if (!container) throw new Error("Container was not created");
|
||||
|
||||
await logActivity({
|
||||
entityType: "garden.container",
|
||||
entityId: container.id,
|
||||
action: "create",
|
||||
payload: { name: container.name },
|
||||
});
|
||||
revalidatePath("/garden");
|
||||
return container;
|
||||
}
|
||||
|
||||
export async function updateContainer(
|
||||
input: { id: string } & Partial<z.input<typeof containerInput>>,
|
||||
) {
|
||||
const parsed = z.object({ id: z.string().uuid() }).and(containerInput.partial()).parse(input);
|
||||
const { household } = await getCurrentSession();
|
||||
await assertCanAccessContainer(parsed.id, household.id);
|
||||
|
||||
await db
|
||||
.update(gardenContainers)
|
||||
.set({
|
||||
name: parsed.name,
|
||||
type: parsed.type,
|
||||
locationNotes:
|
||||
parsed.locationNotes === undefined ? undefined : (parsed.locationNotes ?? null),
|
||||
coverImageUrl:
|
||||
parsed.coverImageUrl === undefined ? undefined : (parsed.coverImageUrl ?? null),
|
||||
updatedAt: new Date(),
|
||||
})
|
||||
.where(eq(gardenContainers.id, parsed.id));
|
||||
|
||||
await logActivity({
|
||||
entityType: "garden.container",
|
||||
entityId: parsed.id,
|
||||
action: "update",
|
||||
payload: { name: parsed.name },
|
||||
});
|
||||
revalidatePath("/garden");
|
||||
revalidatePath(`/garden/containers/${parsed.id}`);
|
||||
}
|
||||
|
||||
export async function deleteContainer(input: { id: string }) {
|
||||
const parsed = z.object({ id: z.string().uuid() }).parse(input);
|
||||
const { household } = await getCurrentSession();
|
||||
await assertCanAccessContainer(parsed.id, household.id);
|
||||
|
||||
await logActivity({
|
||||
entityType: "garden.container",
|
||||
entityId: parsed.id,
|
||||
action: "delete",
|
||||
});
|
||||
await db.delete(gardenContainers).where(eq(gardenContainers.id, parsed.id));
|
||||
revalidatePath("/garden");
|
||||
}
|
||||
|
||||
async function assertCanAccessContainer(id: string, householdId: string) {
|
||||
const [row] = await db
|
||||
.select({ id: gardenContainers.id })
|
||||
.from(gardenContainers)
|
||||
.where(and(eq(gardenContainers.id, id), eq(gardenContainers.householdId, householdId)))
|
||||
.limit(1);
|
||||
|
||||
if (!row) throw new Error("Forbidden");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user