109 lines
3.0 KiB
TypeScript
109 lines
3.0 KiB
TypeScript
import { and, eq } from "drizzle-orm";
|
|
import { db } from "@/lib/db";
|
|
import type { PublicShareContext, ShareContext } from "@/modules/_core/module";
|
|
import { gardenContainers, gardenPlants } from "../schema";
|
|
|
|
export type ContainerShareData = {
|
|
id: string;
|
|
name: string;
|
|
type: string;
|
|
locationNotes: string | null;
|
|
coverImageUrl: string | null;
|
|
images: string[];
|
|
plants: { id: string; name: string; scientificName: string | null }[];
|
|
};
|
|
|
|
export type PlantShareData = {
|
|
id: string;
|
|
name: string;
|
|
scientificName: string | null;
|
|
primaryImageUrl: string | null;
|
|
images: string[];
|
|
healthStatus: string;
|
|
growthStage: string | null;
|
|
category: string;
|
|
wateringNotes: string | null;
|
|
fertilizingNotes: string | null;
|
|
notes: string | null;
|
|
sunlight: string | null;
|
|
};
|
|
|
|
export async function canSharePlant(id: string, ctx: ShareContext): Promise<boolean> {
|
|
const [plant] = await db
|
|
.select({ id: gardenPlants.id })
|
|
.from(gardenPlants)
|
|
.where(and(eq(gardenPlants.id, id), eq(gardenPlants.householdId, ctx.householdId)))
|
|
.limit(1);
|
|
|
|
return !!plant;
|
|
}
|
|
|
|
export async function canShareContainer(id: string, ctx: ShareContext): Promise<boolean> {
|
|
const [container] = await db
|
|
.select({ id: gardenContainers.id })
|
|
.from(gardenContainers)
|
|
.where(and(eq(gardenContainers.id, id), eq(gardenContainers.householdId, ctx.householdId)))
|
|
.limit(1);
|
|
|
|
return !!container;
|
|
}
|
|
|
|
export async function loadPlantForShare(
|
|
id: string,
|
|
ctx: PublicShareContext,
|
|
): Promise<PlantShareData | null> {
|
|
const [plant] = await db
|
|
.select({
|
|
id: gardenPlants.id,
|
|
name: gardenPlants.name,
|
|
scientificName: gardenPlants.scientificName,
|
|
primaryImageUrl: gardenPlants.primaryImageUrl,
|
|
images: gardenPlants.images,
|
|
healthStatus: gardenPlants.healthStatus,
|
|
growthStage: gardenPlants.growthStage,
|
|
category: gardenPlants.category,
|
|
wateringNotes: gardenPlants.wateringNotes,
|
|
fertilizingNotes: gardenPlants.fertilizingNotes,
|
|
notes: gardenPlants.notes,
|
|
sunlight: gardenPlants.sunlight,
|
|
})
|
|
.from(gardenPlants)
|
|
.where(and(eq(gardenPlants.id, id), eq(gardenPlants.householdId, ctx.householdId)))
|
|
.limit(1);
|
|
|
|
if (!plant) return null;
|
|
return plant;
|
|
}
|
|
|
|
export async function loadContainerForShare(
|
|
id: string,
|
|
ctx: PublicShareContext,
|
|
): Promise<ContainerShareData | null> {
|
|
const [container] = await db
|
|
.select()
|
|
.from(gardenContainers)
|
|
.where(and(eq(gardenContainers.id, id), eq(gardenContainers.householdId, ctx.householdId)))
|
|
.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), eq(gardenPlants.householdId, ctx.householdId)));
|
|
|
|
return {
|
|
id: container.id,
|
|
name: container.name,
|
|
type: container.type,
|
|
locationNotes: container.locationNotes,
|
|
coverImageUrl: container.coverImageUrl,
|
|
images: container.images ?? [],
|
|
plants,
|
|
};
|
|
}
|