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");
|
||||
}
|
||||
|
||||
@@ -1 +1,135 @@
|
||||
// Garden query functions — implemented in tasks 71–74
|
||||
import { and, desc, eq, sql } from "drizzle-orm";
|
||||
import { db } from "@/lib/db";
|
||||
import { getCurrentSession } from "@/lib/session";
|
||||
import { gardenContainers, gardenPlants } from "../schema";
|
||||
|
||||
export type ContainerDto = {
|
||||
id: string;
|
||||
householdId: string;
|
||||
name: string;
|
||||
type: string;
|
||||
locationNotes: string | null;
|
||||
coverImageUrl: string | null;
|
||||
plantCount: number;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
};
|
||||
|
||||
export type ContainerDetailDto = ContainerDto & {
|
||||
plants: PlantSummaryDto[];
|
||||
};
|
||||
|
||||
export type PlantSummaryDto = {
|
||||
id: string;
|
||||
name: string;
|
||||
scientificName: string | null;
|
||||
healthStatus: string;
|
||||
primaryImageUrl: string | null;
|
||||
category: string;
|
||||
};
|
||||
|
||||
export async function listContainers(): Promise<ContainerDto[]> {
|
||||
const { household } = await getCurrentSession();
|
||||
|
||||
const rows = await db
|
||||
.select({
|
||||
id: gardenContainers.id,
|
||||
householdId: gardenContainers.householdId,
|
||||
name: gardenContainers.name,
|
||||
type: gardenContainers.type,
|
||||
locationNotes: gardenContainers.locationNotes,
|
||||
coverImageUrl: gardenContainers.coverImageUrl,
|
||||
createdAt: gardenContainers.createdAt,
|
||||
updatedAt: gardenContainers.updatedAt,
|
||||
plantCount: sql<number>`count(${gardenPlants.id})::int`,
|
||||
})
|
||||
.from(gardenContainers)
|
||||
.leftJoin(gardenPlants, eq(gardenPlants.containerId, gardenContainers.id))
|
||||
.where(eq(gardenContainers.householdId, household.id))
|
||||
.groupBy(gardenContainers.id)
|
||||
.orderBy(gardenContainers.name);
|
||||
|
||||
return rows.map((r) => ({
|
||||
...r,
|
||||
plantCount: r.plantCount ?? 0,
|
||||
createdAt: r.createdAt.toISOString(),
|
||||
updatedAt: r.updatedAt.toISOString(),
|
||||
}));
|
||||
}
|
||||
|
||||
export async function getContainer(id: string): Promise<ContainerDetailDto | null> {
|
||||
const { household } = await getCurrentSession();
|
||||
|
||||
const [container] = await db
|
||||
.select({
|
||||
id: gardenContainers.id,
|
||||
householdId: gardenContainers.householdId,
|
||||
name: gardenContainers.name,
|
||||
type: gardenContainers.type,
|
||||
locationNotes: gardenContainers.locationNotes,
|
||||
coverImageUrl: gardenContainers.coverImageUrl,
|
||||
createdAt: gardenContainers.createdAt,
|
||||
updatedAt: gardenContainers.updatedAt,
|
||||
plantCount: sql<number>`(select count(*)::int from garden_plants where container_id = ${gardenContainers.id})`,
|
||||
})
|
||||
.from(gardenContainers)
|
||||
.where(and(eq(gardenContainers.id, id), eq(gardenContainers.householdId, household.id)))
|
||||
.limit(1);
|
||||
|
||||
if (!container) return null;
|
||||
|
||||
const plants = await db
|
||||
.select({
|
||||
id: gardenPlants.id,
|
||||
name: gardenPlants.name,
|
||||
scientificName: gardenPlants.scientificName,
|
||||
healthStatus: gardenPlants.healthStatus,
|
||||
primaryImageUrl: gardenPlants.primaryImageUrl,
|
||||
category: gardenPlants.category,
|
||||
})
|
||||
.from(gardenPlants)
|
||||
.where(eq(gardenPlants.containerId, id))
|
||||
.orderBy(desc(gardenPlants.name));
|
||||
|
||||
return {
|
||||
id: container.id,
|
||||
householdId: container.householdId,
|
||||
name: container.name,
|
||||
type: container.type,
|
||||
locationNotes: container.locationNotes,
|
||||
coverImageUrl: container.coverImageUrl,
|
||||
plantCount: container.plantCount ?? 0,
|
||||
createdAt: container.createdAt.toISOString(),
|
||||
updatedAt: container.updatedAt.toISOString(),
|
||||
plants,
|
||||
};
|
||||
}
|
||||
|
||||
export async function canAccessContainer(id: string, householdId: string): Promise<boolean> {
|
||||
const [row] = await db
|
||||
.select({ id: gardenContainers.id })
|
||||
.from(gardenContainers)
|
||||
.where(and(eq(gardenContainers.id, id), eq(gardenContainers.householdId, householdId)))
|
||||
.limit(1);
|
||||
|
||||
return !!row;
|
||||
}
|
||||
|
||||
export async function searchContainers(query: string, householdId: string) {
|
||||
const rows = await db
|
||||
.select({ id: gardenContainers.id, name: gardenContainers.name })
|
||||
.from(gardenContainers)
|
||||
.where(
|
||||
and(
|
||||
eq(gardenContainers.householdId, householdId),
|
||||
sql`${gardenContainers.name} ilike ${`%${query}%`}`,
|
||||
),
|
||||
)
|
||||
.limit(10);
|
||||
|
||||
return rows.map((r) => ({
|
||||
id: r.id,
|
||||
title: r.name,
|
||||
url: `/garden/containers/${r.id}`,
|
||||
}));
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
import { and, eq } from "drizzle-orm";
|
||||
import { db } from "@/lib/db";
|
||||
import { gardenContainers, gardenPlants } from "../schema";
|
||||
|
||||
export type ContainerShareData = {
|
||||
id: string;
|
||||
name: string;
|
||||
type: string;
|
||||
locationNotes: string | null;
|
||||
coverImageUrl: string | null;
|
||||
plants: { id: string; name: string; scientificName: string | null }[];
|
||||
};
|
||||
|
||||
export async function loadContainerForShare(id: string): Promise<ContainerShareData | null> {
|
||||
const [container] = await db
|
||||
.select()
|
||||
.from(gardenContainers)
|
||||
.where(eq(gardenContainers.id, id))
|
||||
.limit(1);
|
||||
|
||||
if (!container) return null;
|
||||
|
||||
const plants = await db
|
||||
.select({
|
||||
id: gardenPlants.id,
|
||||
name: gardenPlants.name,
|
||||
scientificName: gardenPlants.scientificName,
|
||||
})
|
||||
.from(gardenPlants)
|
||||
.where(and(eq(gardenPlants.containerId, id)));
|
||||
|
||||
return {
|
||||
id: container.id,
|
||||
name: container.name,
|
||||
type: container.type,
|
||||
locationNotes: container.locationNotes,
|
||||
coverImageUrl: container.coverImageUrl,
|
||||
plants,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user