feat: cooking history/gallery, unit conversion, nutrition diary, pantry scan, digest cron, nutrition-targeted meal plans
Six M-sized items from HANDOFF.md's new-features backlog: - Profile tabs: cooking-history stats (total cooked, last-cooked, streak) and a "cooked it" photo gallery, both owner-only - Display-time unit conversion (metric<->imperial) for recipe ingredients, respecting each user's unitPref; original value always shown alongside the conversion - Nutrition daily diary: per-day macro totals computed from cooking history x recipe nutritionData, compared against user goals - Pantry scan: real barcode lookup (zxing + Open Food Facts, no API key) with an AI-vision fallback for unbarcoded items, always confirm-before- insert, both paths tier/rate-limited like other AI features - Weekly digest email: new followers/comments/ratings + trending recipes, sent via a new `cron` Docker stage (alpine+crond+curl) and `digest-cron` compose service hitting a bearer-token-protected internal route - Meal-plan generation can now target a user's nutrition goals as a prompt-level nudge (recipes are AI-invented, not DB-sourced, so this can't be a hard macro constraint) Caught a real deploy-breaking issue while adding the cron stage: appending it after `runner` silently changed the Dockerfile's default build target, and `web`'s compose config didn't pin one — fixed by pinning `target: runner` explicitly. Verified with typecheck, lint, and three separate `docker build --target` runs (runner/cron/migrator) plus `docker compose config` validation. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -3,7 +3,7 @@ import { headers } from "next/headers";
|
||||
import Link from "next/link";
|
||||
import { ChevronLeft, ChevronRight, ShoppingCart, Printer } from "lucide-react";
|
||||
import { auth } from "@/lib/auth/server";
|
||||
import { db, mealPlans, mealPlanMembers, recipes, eq, and, desc } from "@epicure/db";
|
||||
import { db, mealPlans, mealPlanMembers, recipes, userNutritionGoals, eq, and, desc } from "@epicure/db";
|
||||
import { buttonVariants } from "@/components/ui/button";
|
||||
import { MealPlanner } from "@/components/meal-plan/meal-planner";
|
||||
import { ShareMealPlanButton } from "@/components/meal-plan/share-meal-plan-button";
|
||||
@@ -52,7 +52,7 @@ export default async function MealPlanPage({
|
||||
const sunday = addWeeks(monday, 1);
|
||||
sunday.setDate(sunday.getDate() - 1);
|
||||
|
||||
const [plan, userRecipes, sharedMemberships] = await Promise.all([
|
||||
const [plan, userRecipes, sharedMemberships, nutritionGoals] = await Promise.all([
|
||||
db.query.mealPlans.findFirst({
|
||||
where: and(eq(mealPlans.userId, session.user.id), eq(mealPlans.weekStart, weekStart)),
|
||||
with: {
|
||||
@@ -70,8 +70,16 @@ export default async function MealPlanPage({
|
||||
where: eq(mealPlanMembers.userId, session.user.id),
|
||||
with: { mealPlan: { with: { user: true } } },
|
||||
}),
|
||||
db.query.userNutritionGoals.findFirst({
|
||||
where: eq(userNutritionGoals.userId, session.user.id),
|
||||
}),
|
||||
]);
|
||||
|
||||
const hasNutritionGoals = !!(
|
||||
nutritionGoals &&
|
||||
(nutritionGoals.caloriesKcal || nutritionGoals.proteinG || nutritionGoals.carbsG || nutritionGoals.fatG)
|
||||
);
|
||||
|
||||
const entries = (plan?.entries ?? []).map((e) => ({
|
||||
id: e.id,
|
||||
day: e.day,
|
||||
@@ -114,7 +122,7 @@ export default async function MealPlanPage({
|
||||
</div>
|
||||
|
||||
<WeeklyNutritionBar weekStart={weekStart} />
|
||||
<MealPlanner weekStart={weekStart} initialEntries={entries} userRecipes={userRecipes} />
|
||||
<MealPlanner weekStart={weekStart} initialEntries={entries} userRecipes={userRecipes} hasNutritionGoals={hasNutritionGoals} />
|
||||
|
||||
{sharedMemberships.length > 0 && (
|
||||
<div className="space-y-3">
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
import type { Metadata } from "next";
|
||||
import { headers } from "next/headers";
|
||||
import { auth } from "@/lib/auth/server";
|
||||
import { NutritionDiary } from "@/components/nutrition/nutrition-diary";
|
||||
import { getMessages } from "@/lib/i18n/server";
|
||||
|
||||
export const metadata: Metadata = {};
|
||||
|
||||
export default async function NutritionDiaryPage() {
|
||||
const session = await auth.api.getSession({ headers: await headers() });
|
||||
if (!session) return null;
|
||||
const m = getMessages((session.user as { locale?: string }).locale);
|
||||
|
||||
return (
|
||||
<div className="space-y-8">
|
||||
<div>
|
||||
<h1 className="text-2xl font-bold tracking-tight">{m.nutritionDiary.title}</h1>
|
||||
<p className="text-muted-foreground mt-1">{m.nutritionDiary.subtitle}</p>
|
||||
</div>
|
||||
<NutritionDiary />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -51,7 +51,7 @@ export default async function PantryPage() {
|
||||
<div className="space-y-6">
|
||||
<PantryPageHeader items={mappedItems} />
|
||||
<ExpiringSoonSuggestions suggestions={suggestions} />
|
||||
<PantryManager initialItems={mappedItems} />
|
||||
<PantryManager key={mappedItems.map((i) => i.id).join(",")} initialItems={mappedItems} />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -30,10 +30,13 @@ export default async function CookPage({ params }: Params) {
|
||||
|
||||
if (!recipe || recipe.steps.length === 0) notFound();
|
||||
|
||||
const unitPref = (session.user as { unitPref?: string }).unitPref === "imperial" ? "imperial" : "metric";
|
||||
|
||||
return (
|
||||
<CookingMode
|
||||
recipeId={id}
|
||||
recipeTitle={recipe.title}
|
||||
unitPref={unitPref}
|
||||
steps={recipe.steps.map((s) => ({
|
||||
id: s.id,
|
||||
instruction: s.instruction,
|
||||
|
||||
@@ -58,6 +58,7 @@ export default async function RecipePage({ params }: Params) {
|
||||
const m = getMessages((session.user as { locale?: string }).locale);
|
||||
const VISIBILITY_LABEL = m.recipe.visibility;
|
||||
const DIETARY_LABELS = m.recipe.dietary;
|
||||
const unitPref = (session.user as { unitPref?: string }).unitPref === "imperial" ? "imperial" : "metric";
|
||||
|
||||
const [recipe, ratingData, favoriteData, myRating, forkedFrom, myNote] = await Promise.all([
|
||||
db.query.recipes.findFirst({
|
||||
@@ -346,6 +347,7 @@ export default async function RecipePage({ params }: Params) {
|
||||
<ServingScaler
|
||||
baseServings={recipe.baseServings}
|
||||
recipeTitle={recipe.title}
|
||||
unitPref={unitPref}
|
||||
ingredients={recipe.ingredients.map((ing) => ({
|
||||
id: ing.id,
|
||||
rawName: ing.rawName,
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
import type { Metadata } from "next";
|
||||
import Link from "next/link";
|
||||
import { headers } from "next/headers";
|
||||
import { auth } from "@/lib/auth/server";
|
||||
import { db, userNutritionGoals, eq } from "@epicure/db";
|
||||
import { NutritionGoalsForm } from "@/components/nutrition/nutrition-goals-form";
|
||||
import { getMessages } from "@/lib/i18n/server";
|
||||
import { buttonVariants } from "@/components/ui/button";
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
export const metadata: Metadata = {};
|
||||
|
||||
@@ -38,6 +41,15 @@ export default async function NutritionPage() {
|
||||
}
|
||||
/>
|
||||
</section>
|
||||
<section className="rounded-xl border p-6 space-y-3">
|
||||
<div>
|
||||
<h2 className="font-semibold text-lg">{m.nutritionDiary.title}</h2>
|
||||
<p className="text-sm text-muted-foreground mt-1">{m.nutritionDiary.subtitle}</p>
|
||||
</div>
|
||||
<Link href="/nutrition" className={cn(buttonVariants({ variant: "outline", size: "sm" }))}>
|
||||
{m.settings.nutritionGoals.viewDiaryCta}
|
||||
</Link>
|
||||
</section>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -9,10 +9,13 @@ import {
|
||||
recipes,
|
||||
userFollows,
|
||||
userBlocks,
|
||||
cookingHistory,
|
||||
ratings,
|
||||
eq,
|
||||
and,
|
||||
desc,
|
||||
count,
|
||||
isNotNull,
|
||||
} from "@epicure/db";
|
||||
import { Avatar, AvatarImage, AvatarFallback } from "@/components/ui/avatar";
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
@@ -20,14 +23,32 @@ import { FollowButton } from "@/components/social/follow-button";
|
||||
import { BlockButton } from "@/components/social/block-button";
|
||||
import { MessageButton } from "@/components/social/message-button";
|
||||
import { getPublicUrl } from "@/lib/storage";
|
||||
import { ProfileTabs, type HistoryData, type PhotosData } from "@/components/profile/profile-tabs";
|
||||
|
||||
const PAGE_SIZE = 24;
|
||||
|
||||
type Params = {
|
||||
params: Promise<{ username: string }>;
|
||||
searchParams: Promise<{ page?: string }>;
|
||||
searchParams: Promise<{ page?: string; tab?: string; ppage?: string }>;
|
||||
};
|
||||
|
||||
/** Consecutive-day streak (UTC calendar days) ending today or yesterday. */
|
||||
function computeStreak(dates: Date[]): number {
|
||||
const daySet = new Set(dates.map((d) => d.toISOString().slice(0, 10)));
|
||||
const cursor = new Date();
|
||||
cursor.setUTCHours(0, 0, 0, 0);
|
||||
if (!daySet.has(cursor.toISOString().slice(0, 10))) {
|
||||
cursor.setUTCDate(cursor.getUTCDate() - 1);
|
||||
if (!daySet.has(cursor.toISOString().slice(0, 10))) return 0;
|
||||
}
|
||||
let streak = 0;
|
||||
while (daySet.has(cursor.toISOString().slice(0, 10))) {
|
||||
streak++;
|
||||
cursor.setUTCDate(cursor.getUTCDate() - 1);
|
||||
}
|
||||
return streak;
|
||||
}
|
||||
|
||||
export async function generateMetadata({ params }: Params) {
|
||||
const { username } = await params;
|
||||
const user = await db.query.users.findFirst({ where: eq(users.username, username) });
|
||||
@@ -36,9 +57,12 @@ export async function generateMetadata({ params }: Params) {
|
||||
|
||||
export default async function UserProfilePage({ params, searchParams }: Params) {
|
||||
const { username } = await params;
|
||||
const { page: pageParam } = await searchParams;
|
||||
const { page: pageParam, tab: tabParam, ppage: ppageParam } = await searchParams;
|
||||
const page = Math.max(1, parseInt(pageParam ?? "1", 10) || 1);
|
||||
const offset = (page - 1) * PAGE_SIZE;
|
||||
const photoPage = Math.max(1, parseInt(ppageParam ?? "1", 10) || 1);
|
||||
const photoOffset = (photoPage - 1) * PAGE_SIZE;
|
||||
const activeTab = tabParam === "history" || tabParam === "photos" ? tabParam : "recipes";
|
||||
|
||||
const session = await auth.api.getSession({ headers: await headers() });
|
||||
|
||||
@@ -96,6 +120,89 @@ export default async function UserProfilePage({ params, searchParams }: Params)
|
||||
isBlocked = !!blockRow;
|
||||
}
|
||||
|
||||
// Cooking history and "cooked it" photo gallery are only meaningful (and visible) to the
|
||||
// profile owner: history reveals private behavioral/timing patterns, and photos may be
|
||||
// attached to reviews of recipes that aren't visible to other viewers (private recipes),
|
||||
// so surfacing them to non-owners could leak the existence of content they can't access.
|
||||
let historyData: HistoryData | null = null;
|
||||
let photosData: PhotosData | null = null;
|
||||
|
||||
if (isOwnProfile) {
|
||||
const [totalCookedRow, cookedDatesRows, lastCookedRow, recentEntries, totalPhotosRow, photoRows] =
|
||||
await Promise.all([
|
||||
db.select({ count: count() }).from(cookingHistory).where(eq(cookingHistory.userId, user.id)),
|
||||
db.query.cookingHistory.findMany({
|
||||
where: eq(cookingHistory.userId, user.id),
|
||||
columns: { cookedAt: true },
|
||||
orderBy: desc(cookingHistory.cookedAt),
|
||||
limit: 3650,
|
||||
}),
|
||||
db.query.cookingHistory.findFirst({
|
||||
where: eq(cookingHistory.userId, user.id),
|
||||
orderBy: desc(cookingHistory.cookedAt),
|
||||
with: { recipe: { columns: { id: true, title: true } } },
|
||||
}),
|
||||
db.query.cookingHistory.findMany({
|
||||
where: eq(cookingHistory.userId, user.id),
|
||||
orderBy: desc(cookingHistory.cookedAt),
|
||||
limit: 20,
|
||||
with: { recipe: { columns: { id: true, title: true } } },
|
||||
}),
|
||||
db
|
||||
.select({ count: count() })
|
||||
.from(ratings)
|
||||
.where(and(eq(ratings.userId, user.id), isNotNull(ratings.photoKey))),
|
||||
db.query.ratings.findMany({
|
||||
where: and(eq(ratings.userId, user.id), isNotNull(ratings.photoKey)),
|
||||
orderBy: desc(ratings.createdAt),
|
||||
limit: PAGE_SIZE,
|
||||
offset: photoOffset,
|
||||
with: { recipe: { columns: { id: true, title: true } } },
|
||||
}),
|
||||
]);
|
||||
|
||||
const totalCooked = totalCookedRow[0]?.count ?? 0;
|
||||
const streak = computeStreak(cookedDatesRows.map((r) => r.cookedAt));
|
||||
const totalPhotos = totalPhotosRow[0]?.count ?? 0;
|
||||
|
||||
historyData = {
|
||||
totalCooked,
|
||||
streak,
|
||||
lastCooked:
|
||||
lastCookedRow && lastCookedRow.recipe
|
||||
? {
|
||||
recipeId: lastCookedRow.recipe.id,
|
||||
recipeTitle: lastCookedRow.recipe.title,
|
||||
cookedAt: lastCookedRow.cookedAt.toISOString(),
|
||||
}
|
||||
: null,
|
||||
recentEntries: recentEntries
|
||||
.filter((e) => e.recipe)
|
||||
.map((e) => ({
|
||||
id: e.id,
|
||||
recipeId: e.recipe!.id,
|
||||
recipeTitle: e.recipe!.title,
|
||||
cookedAt: e.cookedAt.toISOString(),
|
||||
servings: e.servings,
|
||||
})),
|
||||
};
|
||||
|
||||
photosData = {
|
||||
items: photoRows
|
||||
.filter((r) => r.recipe && r.photoKey)
|
||||
.map((r) => ({
|
||||
id: r.id,
|
||||
recipeId: r.recipe!.id,
|
||||
recipeTitle: r.recipe!.title,
|
||||
photoKey: r.photoKey!,
|
||||
createdAt: r.createdAt.toISOString(),
|
||||
})),
|
||||
page: photoPage,
|
||||
totalPages: Math.max(1, Math.ceil(totalPhotos / PAGE_SIZE)),
|
||||
total: totalPhotos,
|
||||
};
|
||||
}
|
||||
|
||||
const initials = user.name
|
||||
.split(" ")
|
||||
.slice(0, 2)
|
||||
@@ -103,6 +210,72 @@ export default async function UserProfilePage({ params, searchParams }: Params)
|
||||
.join("")
|
||||
.toUpperCase();
|
||||
|
||||
const recipesSection =
|
||||
publicRecipes.length > 0 ? (
|
||||
<div className="space-y-4">
|
||||
<h2 className="text-xl font-semibold">Recipes</h2>
|
||||
<div className="grid grid-cols-2 sm:grid-cols-3 md:grid-cols-4 gap-4">
|
||||
{publicRecipes.map((recipe) => {
|
||||
const cover = recipe.photos[0];
|
||||
return (
|
||||
<Link
|
||||
key={recipe.id}
|
||||
href={`/recipes/${recipe.id}`}
|
||||
className="group block rounded-xl overflow-hidden border bg-card hover:shadow-md transition-shadow"
|
||||
>
|
||||
<div className="relative aspect-square bg-muted overflow-hidden">
|
||||
{cover ? (
|
||||
<Image
|
||||
src={getPublicUrl(cover.storageKey)}
|
||||
alt={recipe.title}
|
||||
fill
|
||||
sizes="(max-width: 640px) 50vw, (max-width: 768px) 33vw, 25vw"
|
||||
className="object-cover group-hover:scale-105 transition-transform duration-200"
|
||||
/>
|
||||
) : (
|
||||
<div className="w-full h-full flex items-center justify-center text-muted-foreground text-3xl">
|
||||
🍽️
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<div className="p-2">
|
||||
<p className="text-sm font-medium leading-tight line-clamp-2">{recipe.title}</p>
|
||||
</div>
|
||||
</Link>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
|
||||
{totalPages > 1 && (
|
||||
<div className="flex items-center justify-center gap-2 pt-2">
|
||||
{page > 1 && (
|
||||
<Link
|
||||
href={`/u/${username}${page - 1 > 1 ? `?page=${page - 1}` : ""}`}
|
||||
className="rounded-md border px-3 py-1.5 text-sm hover:bg-accent"
|
||||
>
|
||||
Previous
|
||||
</Link>
|
||||
)}
|
||||
<span className="text-sm text-muted-foreground px-2">
|
||||
Page {page} of {totalPages}
|
||||
</span>
|
||||
{page < totalPages && (
|
||||
<Link
|
||||
href={`/u/${username}?page=${page + 1}`}
|
||||
className="rounded-md border px-3 py-1.5 text-sm hover:bg-accent"
|
||||
>
|
||||
Next
|
||||
</Link>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
) : (
|
||||
<div className="text-center py-16 text-muted-foreground">
|
||||
<p className="text-lg">No public recipes yet.</p>
|
||||
</div>
|
||||
);
|
||||
|
||||
return (
|
||||
<div className="max-w-4xl mx-auto space-y-10">
|
||||
{/* Profile header */}
|
||||
@@ -149,70 +322,17 @@ export default async function UserProfilePage({ params, searchParams }: Params)
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Recipe grid */}
|
||||
{publicRecipes.length > 0 ? (
|
||||
<div className="space-y-4">
|
||||
<h2 className="text-xl font-semibold">Recipes</h2>
|
||||
<div className="grid grid-cols-2 sm:grid-cols-3 md:grid-cols-4 gap-4">
|
||||
{publicRecipes.map((recipe) => {
|
||||
const cover = recipe.photos[0];
|
||||
return (
|
||||
<Link
|
||||
key={recipe.id}
|
||||
href={`/recipes/${recipe.id}`}
|
||||
className="group block rounded-xl overflow-hidden border bg-card hover:shadow-md transition-shadow"
|
||||
>
|
||||
<div className="relative aspect-square bg-muted overflow-hidden">
|
||||
{cover ? (
|
||||
<Image
|
||||
src={getPublicUrl(cover.storageKey)}
|
||||
alt={recipe.title}
|
||||
fill
|
||||
sizes="(max-width: 640px) 50vw, (max-width: 768px) 33vw, 25vw"
|
||||
className="object-cover group-hover:scale-105 transition-transform duration-200"
|
||||
/>
|
||||
) : (
|
||||
<div className="w-full h-full flex items-center justify-center text-muted-foreground text-3xl">
|
||||
🍽️
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<div className="p-2">
|
||||
<p className="text-sm font-medium leading-tight line-clamp-2">{recipe.title}</p>
|
||||
</div>
|
||||
</Link>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
|
||||
{totalPages > 1 && (
|
||||
<div className="flex items-center justify-center gap-2 pt-2">
|
||||
{page > 1 && (
|
||||
<Link
|
||||
href={`/u/${username}${page - 1 > 1 ? `?page=${page - 1}` : ""}`}
|
||||
className="rounded-md border px-3 py-1.5 text-sm hover:bg-accent"
|
||||
>
|
||||
Previous
|
||||
</Link>
|
||||
)}
|
||||
<span className="text-sm text-muted-foreground px-2">
|
||||
Page {page} of {totalPages}
|
||||
</span>
|
||||
{page < totalPages && (
|
||||
<Link
|
||||
href={`/u/${username}?page=${page + 1}`}
|
||||
className="rounded-md border px-3 py-1.5 text-sm hover:bg-accent"
|
||||
>
|
||||
Next
|
||||
</Link>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
{/* Recipes / cooking history / photos */}
|
||||
{isOwnProfile && historyData && photosData ? (
|
||||
<ProfileTabs
|
||||
username={username}
|
||||
defaultTab={activeTab}
|
||||
recipesContent={recipesSection}
|
||||
history={historyData}
|
||||
photos={photosData}
|
||||
/>
|
||||
) : (
|
||||
<div className="text-center py-16 text-muted-foreground">
|
||||
<p className="text-lg">No public recipes yet.</p>
|
||||
</div>
|
||||
recipesSection
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
|
||||
@@ -0,0 +1,131 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import crypto from "node:crypto";
|
||||
import {
|
||||
db,
|
||||
users,
|
||||
recipes,
|
||||
comments,
|
||||
ratings,
|
||||
userFollows,
|
||||
favorites,
|
||||
eq,
|
||||
and,
|
||||
gte,
|
||||
desc,
|
||||
count,
|
||||
sql,
|
||||
} from "@epicure/db";
|
||||
import { sendEmail, weeklyDigestHtml } from "@/lib/email";
|
||||
|
||||
// Internal cron endpoint — triggered by the `digest-cron` container on a weekly
|
||||
// schedule (see docker/compose.prod.yml). Not part of the public API surface;
|
||||
// protected by a shared secret rather than user auth.
|
||||
//
|
||||
// Computes, for every user: new followers / new comments / new ratings on
|
||||
// their recipes in the last 7 days, plus a site-wide top-3 trending list, and
|
||||
// emails a summary. Sends to all users (all users have a non-null email) —
|
||||
// there's no per-user opt-out preference yet; out of scope for this pass.
|
||||
|
||||
const CHUNK_SIZE = 20;
|
||||
|
||||
function isAuthorized(req: NextRequest): boolean {
|
||||
const secret = process.env["CRON_SECRET"];
|
||||
if (!secret) return false;
|
||||
|
||||
const header = req.headers.get("authorization");
|
||||
if (!header?.startsWith("Bearer ")) return false;
|
||||
const provided = header.slice("Bearer ".length);
|
||||
|
||||
const a = Buffer.from(provided);
|
||||
const b = Buffer.from(secret);
|
||||
if (a.length !== b.length) return false;
|
||||
return crypto.timingSafeEqual(a, b);
|
||||
}
|
||||
|
||||
function chunk<T>(arr: T[], size: number): T[][] {
|
||||
const out: T[][] = [];
|
||||
for (let i = 0; i < arr.length; i += size) out.push(arr.slice(i, i + size));
|
||||
return out;
|
||||
}
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
if (!isAuthorized(req)) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
const weekAgo = new Date(Date.now() - 7 * 24 * 60 * 60 * 1000);
|
||||
const baseUrl = process.env["BETTER_AUTH_URL"] ?? "http://localhost:3000";
|
||||
|
||||
const [allUsers, followerRows, commentRows, ratingRows, trending] = await Promise.all([
|
||||
db.select({ id: users.id, email: users.email }).from(users),
|
||||
db
|
||||
.select({ userId: userFollows.followingId, n: count() })
|
||||
.from(userFollows)
|
||||
.where(gte(userFollows.createdAt, weekAgo))
|
||||
.groupBy(userFollows.followingId),
|
||||
db
|
||||
.select({ userId: recipes.authorId, n: count() })
|
||||
.from(comments)
|
||||
.innerJoin(recipes, eq(comments.recipeId, recipes.id))
|
||||
.where(gte(comments.createdAt, weekAgo))
|
||||
.groupBy(recipes.authorId),
|
||||
db
|
||||
.select({ userId: recipes.authorId, n: count() })
|
||||
.from(ratings)
|
||||
.innerJoin(recipes, eq(ratings.recipeId, recipes.id))
|
||||
.where(gte(ratings.createdAt, weekAgo))
|
||||
.groupBy(recipes.authorId),
|
||||
db
|
||||
.select({
|
||||
id: recipes.id,
|
||||
title: recipes.title,
|
||||
favoriteCount: sql<number>`cast(count(${favorites.recipeId}) as int)`,
|
||||
})
|
||||
.from(recipes)
|
||||
.leftJoin(
|
||||
favorites,
|
||||
and(eq(favorites.recipeId, recipes.id), gte(favorites.createdAt, weekAgo))
|
||||
)
|
||||
.where(eq(recipes.visibility, "public"))
|
||||
.groupBy(recipes.id)
|
||||
.orderBy(desc(sql`count(${favorites.recipeId})`), desc(recipes.createdAt))
|
||||
.limit(3),
|
||||
]);
|
||||
|
||||
const followerMap = new Map(followerRows.map((r) => [r.userId, r.n]));
|
||||
const commentMap = new Map(commentRows.map((r) => [r.userId, r.n]));
|
||||
const ratingMap = new Map(ratingRows.map((r) => [r.userId, r.n]));
|
||||
const trendingList = trending.map((r) => ({ id: r.id, title: r.title }));
|
||||
|
||||
let sent = 0;
|
||||
let failed = 0;
|
||||
|
||||
for (const batch of chunk(allUsers, CHUNK_SIZE)) {
|
||||
const results = await Promise.allSettled(
|
||||
batch.map((user) => {
|
||||
const newFollowers = followerMap.get(user.id) ?? 0;
|
||||
const newComments = commentMap.get(user.id) ?? 0;
|
||||
const newRatings = ratingMap.get(user.id) ?? 0;
|
||||
|
||||
return sendEmail({
|
||||
to: user.email,
|
||||
subject: "Your weekly digest — Epicure",
|
||||
html: weeklyDigestHtml({
|
||||
newFollowers,
|
||||
newComments,
|
||||
newRatings,
|
||||
trending: trendingList,
|
||||
baseUrl,
|
||||
}),
|
||||
});
|
||||
})
|
||||
);
|
||||
|
||||
for (const r of results) {
|
||||
if (r.status === "fulfilled") sent++;
|
||||
else failed++;
|
||||
}
|
||||
}
|
||||
|
||||
return NextResponse.json({ ok: true, totalUsers: allUsers.length, sent, failed });
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { z } from "zod";
|
||||
import { db, recipes, recipeIngredients, recipeSteps, mealPlans, mealPlanEntries, pantryItems, eq, and } from "@epicure/db";
|
||||
import { db, recipes, recipeIngredients, recipeSteps, mealPlans, mealPlanEntries, pantryItems, userNutritionGoals, eq, and } from "@epicure/db";
|
||||
import { requireSession } from "@/lib/api-auth";
|
||||
import { applyRateLimit } from "@/lib/rate-limit";
|
||||
import { getDefaultProviderWithKey } from "@/lib/ai/resolve-user-key";
|
||||
@@ -19,6 +19,7 @@ const Schema = z.object({
|
||||
usePantry: z.boolean().default(false),
|
||||
pantryMode: z.boolean().default(false),
|
||||
difficulty: z.enum(["easy", "medium", "hard"]).optional(),
|
||||
targetNutritionGoals: z.boolean().default(false),
|
||||
});
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
@@ -56,6 +57,24 @@ export async function POST(req: NextRequest) {
|
||||
pantryItemNames = pantry.map((p) => p.rawName);
|
||||
}
|
||||
|
||||
// Optionally fetch the user's nutrition goals to nudge the AI toward them.
|
||||
// Silently ignored (no-op) if the user hasn't set any goals — no need to
|
||||
// fail the whole generation over a missing preference.
|
||||
let nutritionGoals: { caloriesKcal?: number | null; proteinG?: number | null; carbsG?: number | null; fatG?: number | null } | undefined;
|
||||
if (parsed.data.targetNutritionGoals) {
|
||||
const goals = await db.query.userNutritionGoals.findFirst({
|
||||
where: eq(userNutritionGoals.userId, userId),
|
||||
});
|
||||
if (goals && (goals.caloriesKcal || goals.proteinG || goals.carbsG || goals.fatG)) {
|
||||
nutritionGoals = {
|
||||
caloriesKcal: goals.caloriesKcal,
|
||||
proteinG: goals.proteinG,
|
||||
carbsG: goals.carbsG,
|
||||
fatG: goals.fatG,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
const result = await withAiQuota(userId, session!.user.tier as "free" | "pro", () =>
|
||||
generateMealPlan(
|
||||
{
|
||||
@@ -65,6 +84,7 @@ export async function POST(req: NextRequest) {
|
||||
days: parsed.data.days,
|
||||
pantryMode: parsed.data.pantryMode,
|
||||
difficulty: parsed.data.difficulty,
|
||||
nutritionGoals,
|
||||
},
|
||||
{ ...config, userContext: privateBio ?? undefined },
|
||||
locale
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { z } from "zod";
|
||||
import { requireSession } from "@/lib/api-auth";
|
||||
import { applyRateLimit } from "@/lib/rate-limit";
|
||||
|
||||
const Schema = z.object({
|
||||
barcode: z.string().trim().min(4).max(32).regex(/^[0-9]+$/, "Barcode must be numeric"),
|
||||
});
|
||||
|
||||
type OffProduct = {
|
||||
product_name?: string;
|
||||
product_name_en?: string;
|
||||
generic_name?: string;
|
||||
quantity?: string;
|
||||
product_quantity?: string;
|
||||
product_quantity_unit?: string;
|
||||
brands?: string;
|
||||
};
|
||||
|
||||
type OffResponse = {
|
||||
status: number;
|
||||
product?: OffProduct;
|
||||
};
|
||||
|
||||
/** Very rough unit guess from Open Food Facts' free-text `quantity` field (e.g. "500 g", "1 L"). */
|
||||
function extractUnit(quantity: string | undefined): string | undefined {
|
||||
if (!quantity) return undefined;
|
||||
const match = /([a-zA-Z]+)\s*$/.exec(quantity.trim());
|
||||
return match?.[1]?.toLowerCase();
|
||||
}
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
const { session, response } = await requireSession();
|
||||
if (response) return response;
|
||||
|
||||
const body = await req.json() as unknown;
|
||||
const parsed = Schema.safeParse(body);
|
||||
if (!parsed.success) return NextResponse.json({ error: "Validation error" }, { status: 400 });
|
||||
|
||||
const limited = await applyRateLimit(`rl:pantry-scan-barcode:${session!.user.id}`, 20, 60);
|
||||
if (limited) return limited;
|
||||
|
||||
const { barcode } = parsed.data;
|
||||
|
||||
let offResponse: Response;
|
||||
try {
|
||||
offResponse = await fetch(
|
||||
`https://world.openfoodfacts.org/api/v2/product/${encodeURIComponent(barcode)}.json`,
|
||||
{
|
||||
headers: { "User-Agent": "Epicure/1.0 (pantry-scan)" },
|
||||
signal: AbortSignal.timeout(8000),
|
||||
}
|
||||
);
|
||||
} catch {
|
||||
return NextResponse.json({ error: "Barcode lookup service unavailable" }, { status: 502 });
|
||||
}
|
||||
|
||||
if (!offResponse.ok) {
|
||||
return NextResponse.json({ error: "Barcode lookup service unavailable" }, { status: 502 });
|
||||
}
|
||||
|
||||
const data = await offResponse.json() as OffResponse;
|
||||
|
||||
if (data.status !== 1 || !data.product) {
|
||||
return NextResponse.json({ found: false });
|
||||
}
|
||||
|
||||
const product = data.product;
|
||||
const rawName = product.product_name_en?.trim() || product.product_name?.trim() || product.generic_name?.trim();
|
||||
|
||||
if (!rawName) {
|
||||
return NextResponse.json({ found: false });
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
found: true,
|
||||
rawName: product.brands ? `${rawName} (${product.brands.split(",")[0]?.trim()})` : rawName,
|
||||
quantity: product.product_quantity,
|
||||
unit: extractUnit(product.product_quantity_unit) ?? extractUnit(product.quantity),
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { z } from "zod";
|
||||
import { requireSession } from "@/lib/api-auth";
|
||||
import { applyRateLimit } from "@/lib/rate-limit";
|
||||
import { withAiQuota, resolveAiConfigOrError } from "@/lib/ai/ai-error";
|
||||
import { getModelConfigForUseCase } from "@/lib/ai/resolve-user-key";
|
||||
import { scanPantryPhoto } from "@/lib/ai/features/scan-pantry-photo";
|
||||
|
||||
const Schema = z.object({
|
||||
imageBase64: z.string().max(14_000_000),
|
||||
mimeType: z.enum(["image/jpeg", "image/png", "image/webp"]),
|
||||
});
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
const { session, response } = await requireSession();
|
||||
if (response) return response;
|
||||
|
||||
const body = await req.json() as unknown;
|
||||
const parsed = Schema.safeParse(body);
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json({ error: "Validation error", issues: parsed.error.issues }, { status: 400 });
|
||||
}
|
||||
|
||||
const userId = session!.user.id;
|
||||
|
||||
const limited = await applyRateLimit(`rl:ai:${userId}`, 10, 60);
|
||||
if (limited) return limited;
|
||||
|
||||
const configResult = await resolveAiConfigOrError(() => getModelConfigForUseCase(userId, "vision"));
|
||||
if (!configResult.ok) return configResult.response;
|
||||
const aiConfig = configResult.data;
|
||||
|
||||
// Fall back to vision-capable defaults if no explicit model configured
|
||||
if (!aiConfig.model) {
|
||||
if (aiConfig.provider === "openai") aiConfig.model = "gpt-4o";
|
||||
else if (aiConfig.provider === "anthropic") aiConfig.model = "claude-sonnet-4-6";
|
||||
}
|
||||
|
||||
const result = await withAiQuota(userId, session!.user.tier as "free" | "pro", () =>
|
||||
scanPantryPhoto(parsed.data.imageBase64, parsed.data.mimeType, aiConfig)
|
||||
);
|
||||
if (!result.ok) return result.response;
|
||||
|
||||
return NextResponse.json(result.data);
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { db, cookingHistory, recipes, userNutritionGoals, eq, and, gte, lt } from "@epicure/db";
|
||||
import { requireSession } from "@/lib/api-auth";
|
||||
|
||||
function isValidDate(value: string): boolean {
|
||||
return /^\d{4}-\d{2}-\d{2}$/.test(value) && !isNaN(new Date(`${value}T00:00:00.000Z`).getTime());
|
||||
}
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
const { session, response } = await requireSession();
|
||||
if (response) return response;
|
||||
const userId = session!.user.id;
|
||||
|
||||
const dateParam = req.nextUrl.searchParams.get("date");
|
||||
const date = dateParam && isValidDate(dateParam) ? dateParam : new Date().toISOString().slice(0, 10);
|
||||
|
||||
const dayStart = new Date(`${date}T00:00:00.000Z`);
|
||||
const dayEnd = new Date(dayStart.getTime() + 24 * 60 * 60 * 1000);
|
||||
|
||||
const rows = await db
|
||||
.select({
|
||||
id: cookingHistory.id,
|
||||
recipeId: cookingHistory.recipeId,
|
||||
servings: cookingHistory.servings,
|
||||
cookedAt: cookingHistory.cookedAt,
|
||||
recipeTitle: recipes.title,
|
||||
baseServings: recipes.baseServings,
|
||||
nutritionData: recipes.nutritionData,
|
||||
})
|
||||
.from(cookingHistory)
|
||||
.leftJoin(recipes, eq(cookingHistory.recipeId, recipes.id))
|
||||
.where(
|
||||
and(
|
||||
eq(cookingHistory.userId, userId),
|
||||
gte(cookingHistory.cookedAt, dayStart),
|
||||
lt(cookingHistory.cookedAt, dayEnd)
|
||||
)
|
||||
)
|
||||
.orderBy(cookingHistory.cookedAt);
|
||||
|
||||
const totals = { calories: 0, proteinG: 0, carbsG: 0, fatG: 0, fiberG: 0, sodiumMg: 0 };
|
||||
const entries: {
|
||||
id: string;
|
||||
recipeId: string;
|
||||
title: string;
|
||||
servings: number;
|
||||
cookedAt: string;
|
||||
nutritionKnown: boolean;
|
||||
}[] = [];
|
||||
let unknownCount = 0;
|
||||
|
||||
for (const row of rows) {
|
||||
const servings = row.servings ?? row.baseServings ?? 1;
|
||||
const perServing = row.nutritionData?.perServing;
|
||||
const nutritionKnown = !!perServing;
|
||||
|
||||
if (perServing) {
|
||||
totals.calories += perServing.calories * servings;
|
||||
totals.proteinG += perServing.proteinG * servings;
|
||||
totals.carbsG += perServing.carbsG * servings;
|
||||
totals.fatG += perServing.fatG * servings;
|
||||
totals.fiberG += perServing.fiberG * servings;
|
||||
totals.sodiumMg += perServing.sodiumMg * servings;
|
||||
} else {
|
||||
unknownCount += 1;
|
||||
}
|
||||
|
||||
entries.push({
|
||||
id: row.id,
|
||||
recipeId: row.recipeId,
|
||||
title: row.recipeTitle ?? "Unknown recipe",
|
||||
servings,
|
||||
cookedAt: row.cookedAt.toISOString(),
|
||||
nutritionKnown,
|
||||
});
|
||||
}
|
||||
|
||||
for (const key of Object.keys(totals) as (keyof typeof totals)[]) {
|
||||
totals[key] = Math.round(totals[key]);
|
||||
}
|
||||
|
||||
const goalsRow = await db.query.userNutritionGoals.findFirst({
|
||||
where: eq(userNutritionGoals.userId, userId),
|
||||
});
|
||||
|
||||
const goals = goalsRow
|
||||
? {
|
||||
caloriesKcal: goalsRow.caloriesKcal,
|
||||
proteinG: goalsRow.proteinG,
|
||||
carbsG: goalsRow.carbsG,
|
||||
fatG: goalsRow.fatG,
|
||||
}
|
||||
: null;
|
||||
|
||||
const coverage = {
|
||||
calories: goals?.caloriesKcal ? Math.round((totals.calories / goals.caloriesKcal) * 100) : 0,
|
||||
protein: goals?.proteinG ? Math.round((totals.proteinG / goals.proteinG) * 100) : 0,
|
||||
carbs: goals?.carbsG ? Math.round((totals.carbsG / goals.carbsG) * 100) : 0,
|
||||
fat: goals?.fatG ? Math.round((totals.fatG / goals.fatG) * 100) : 0,
|
||||
};
|
||||
|
||||
return NextResponse.json({ date, totals, goals, coverage, entries, unknownCount });
|
||||
}
|
||||
@@ -3,7 +3,7 @@ import { headers } from "next/headers";
|
||||
import { auth } from "@/lib/auth/server";
|
||||
import { db, recipes, eq, and } from "@epicure/db";
|
||||
import { PrintTrigger } from "@/components/recipe/print-trigger";
|
||||
import { hasQuantity } from "@/lib/fractions";
|
||||
import { formatIngredientQuantity } from "@/lib/unit-conversion";
|
||||
import { getMessages, formatMessage } from "@/lib/i18n/server";
|
||||
|
||||
type Params = { params: Promise<{ id: string }> };
|
||||
@@ -14,6 +14,7 @@ export default async function RecipePrintPage({ params }: Params) {
|
||||
if (!session) return null;
|
||||
|
||||
const m = getMessages((session.user as { locale?: string }).locale);
|
||||
const unitPref = (session.user as { unitPref?: string }).unitPref === "imperial" ? "imperial" : "metric";
|
||||
|
||||
const recipe = await db.query.recipes.findFirst({
|
||||
where: and(eq(recipes.id, id), eq(recipes.authorId, session.user.id)),
|
||||
@@ -103,7 +104,7 @@ export default async function RecipePrintPage({ params }: Params) {
|
||||
{recipe.ingredients.map((ing) => (
|
||||
<li key={ing.id}>
|
||||
<span className="qty">
|
||||
{[hasQuantity(ing.quantity) ? ing.quantity : null, ing.unit].filter(Boolean).join(" ")}
|
||||
{formatIngredientQuantity(ing.quantity, ing.unit, unitPref)}
|
||||
</span>
|
||||
{ing.rawName}
|
||||
{ing.note && <span style={{ color: "#888" }}> ({ing.note})</span>}
|
||||
|
||||
@@ -3,7 +3,7 @@ import { headers } from "next/headers";
|
||||
import { auth } from "@/lib/auth/server";
|
||||
import { db, collections, eq, and, or } from "@epicure/db";
|
||||
import { PrintTrigger } from "@/components/recipe/print-trigger";
|
||||
import { hasQuantity } from "@/lib/fractions";
|
||||
import { formatIngredientQuantity } from "@/lib/unit-conversion";
|
||||
import { getMessages, formatMessage } from "@/lib/i18n/server";
|
||||
|
||||
type Params = { params: Promise<{ id: string }> };
|
||||
@@ -14,6 +14,7 @@ export default async function CollectionPrintPage({ params }: Params) {
|
||||
if (!session) return null;
|
||||
|
||||
const m = getMessages((session.user as { locale?: string }).locale);
|
||||
const unitPref = (session.user as { unitPref?: string }).unitPref === "imperial" ? "imperial" : "metric";
|
||||
|
||||
const col = await db.query.collections.findFirst({
|
||||
where: and(
|
||||
@@ -113,7 +114,7 @@ export default async function CollectionPrintPage({ params }: Params) {
|
||||
{recipe.ingredients.map((ing) => (
|
||||
<li key={ing.id}>
|
||||
<span className="qty">
|
||||
{[hasQuantity(ing.quantity) ? ing.quantity : null, ing.unit].filter(Boolean).join(" ")}
|
||||
{formatIngredientQuantity(ing.quantity, ing.unit, unitPref)}
|
||||
</span>
|
||||
{ing.rawName}
|
||||
{ing.note && <span style={{ color: "#888" }}> ({ing.note})</span>}
|
||||
|
||||
@@ -40,6 +40,7 @@ export default async function PublicRecipePage({ params }: Params) {
|
||||
const session = await auth.api.getSession({ headers: await headers() }).catch(() => null);
|
||||
|
||||
const m = getMessages((session?.user as { locale?: string } | undefined)?.locale);
|
||||
const unitPref = (session?.user as { unitPref?: string } | undefined)?.unitPref === "imperial" ? "imperial" : "metric";
|
||||
|
||||
const recipe = await db.query.recipes.findFirst({
|
||||
where: (r, { and, eq, ne }) => and(eq(r.id, id), ne(r.visibility, "private")),
|
||||
@@ -137,6 +138,7 @@ export default async function PublicRecipePage({ params }: Params) {
|
||||
<h2 className="text-xl font-semibold">{m.recipe.ingredients}</h2>
|
||||
<ServingScaler
|
||||
baseServings={recipe.baseServings}
|
||||
unitPref={unitPref}
|
||||
ingredients={recipe.ingredients.map((ing) => ({
|
||||
id: ing.id, rawName: ing.rawName, quantity: ing.quantity, unit: ing.unit, note: ing.note, order: ing.order,
|
||||
}))}
|
||||
|
||||
Reference in New Issue
Block a user