fix: tighten sharing and shadcn composition
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
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 = {
|
||||
@@ -27,7 +28,30 @@ export type PlantShareData = {
|
||||
sunlight: string | null;
|
||||
};
|
||||
|
||||
export async function loadPlantForShare(id: string): Promise<PlantShareData | 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,
|
||||
@@ -44,18 +68,21 @@ export async function loadPlantForShare(id: string): Promise<PlantShareData | nu
|
||||
sunlight: gardenPlants.sunlight,
|
||||
})
|
||||
.from(gardenPlants)
|
||||
.where(eq(gardenPlants.id, id))
|
||||
.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): Promise<ContainerShareData | null> {
|
||||
export async function loadContainerForShare(
|
||||
id: string,
|
||||
ctx: PublicShareContext,
|
||||
): Promise<ContainerShareData | null> {
|
||||
const [container] = await db
|
||||
.select()
|
||||
.from(gardenContainers)
|
||||
.where(eq(gardenContainers.id, id))
|
||||
.where(and(eq(gardenContainers.id, id), eq(gardenContainers.householdId, ctx.householdId)))
|
||||
.limit(1);
|
||||
|
||||
if (!container) return null;
|
||||
@@ -67,7 +94,7 @@ export async function loadContainerForShare(id: string): Promise<ContainerShareD
|
||||
scientificName: gardenPlants.scientificName,
|
||||
})
|
||||
.from(gardenPlants)
|
||||
.where(and(eq(gardenPlants.containerId, id)));
|
||||
.where(and(eq(gardenPlants.containerId, id), eq(gardenPlants.householdId, ctx.householdId)));
|
||||
|
||||
return {
|
||||
id: container.id,
|
||||
|
||||
Reference in New Issue
Block a user