feat: implement remaining TODO.md feature ideas + fix mobile headers

Implements the six previously-unscoped feature ideas plus a mobile
layout fix reported via screenshot:

- Mobile: Recipes/Collections/Pantry/Meal Plan/Shopping Lists headers
  now stack and wrap instead of clipping buttons on narrow viewports.
- Recipe diff/compare view: word/list diff against any past version,
  next to Restore in version history.
- Shared meal plans & shopping lists: new shoppingListMembers/
  mealPlanMembers tables (viewer/editor roles, mirrors
  collectionMembers), share dialogs, membership-checked routes.
- PDF cookbook export: /print/collection/[id] renders a whole
  collection with page breaks, using the existing print-CSS pattern
  instead of adding a PDF rendering dependency.
- Grocery delivery handoff: shopping lists can copy-as-text (works
  today) or send to Instacart once INSTACART_API_KEY is configured
  (stub adapter — real API needs a partner agreement).
- Personalized "For You" feed tab: ranks public recipes by tag/
  dietary overlap with the user's favorited/highly-rated history.
- PWA: added manifest.json + icons on top of the existing service
  worker so the app is installable; cook-mode pages were already
  cached for offline use.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Arnaud
2026-07-02 12:13:00 +02:00
parent d2faf98ac1
commit e5d1080fb9
56 changed files with 6096 additions and 74 deletions
@@ -0,0 +1,104 @@
import { describe, it, expect, vi, beforeEach } from "vitest";
import { NextRequest } from "next/server";
const mockSession = { user: { id: "user-2" } };
vi.mock("@/lib/api-auth", () => ({
requireSession: vi.fn(),
}));
vi.mock("@/lib/webhooks", () => ({
dispatchWebhook: vi.fn(),
}));
const { mockPlanFindFirst, mockMemberFindFirst, mockRecipeFindFirst, mockInsertValues, mockDeleteWhere } = vi.hoisted(() => ({
mockPlanFindFirst: vi.fn(),
mockMemberFindFirst: vi.fn(),
mockRecipeFindFirst: vi.fn(),
mockInsertValues: vi.fn().mockResolvedValue(undefined),
mockDeleteWhere: vi.fn().mockResolvedValue(undefined),
}));
vi.mock("@epicure/db", () => ({
db: {
query: {
mealPlans: { findFirst: mockPlanFindFirst },
mealPlanMembers: { findFirst: mockMemberFindFirst },
recipes: { findFirst: mockRecipeFindFirst },
},
insert: vi.fn(() => ({ values: mockInsertValues })),
delete: vi.fn(() => ({ where: mockDeleteWhere })),
},
mealPlans: { id: "id", userId: "user_id" },
mealPlanMembers: { mealPlanId: "meal_plan_id", userId: "user_id" },
mealPlanEntries: { id: "id", mealPlanId: "meal_plan_id", day: "day", mealType: "meal_type" },
recipes: { id: "id", authorId: "author_id", visibility: "visibility" },
eq: vi.fn((a, b) => ({ a, b, op: "eq" })),
and: vi.fn((...args) => ({ args, op: "and" })),
or: vi.fn((...args) => ({ args, op: "or" })),
ne: vi.fn((a, b) => ({ a, b, op: "ne" })),
}));
const { requireSession } = await import("@/lib/api-auth");
import { POST, DELETE } from "../route";
const ctx = { params: Promise.resolve({ mealPlanId: "plan-1" }) };
function makeRequest(method: string, body?: unknown, search = "") {
return new NextRequest(`http://localhost/api/v1/meal-plans/shared/plan-1/entries${search}`, {
method,
headers: { "Content-Type": "application/json" },
body: body ? JSON.stringify(body) : undefined,
});
}
beforeEach(() => {
vi.clearAllMocks();
vi.mocked(requireSession).mockResolvedValue({ session: mockSession as never, response: null });
mockPlanFindFirst.mockResolvedValue({ id: "plan-1", userId: "user-1" });
});
const validBody = { day: "mon", mealType: "dinner", recipeId: "r-1", servings: 2 };
describe("POST /api/v1/meal-plans/shared/[mealPlanId]/entries", () => {
it("returns 404 when the caller has no access to the plan", async () => {
mockMemberFindFirst.mockResolvedValue(undefined);
const res = await POST(makeRequest("POST", validBody), ctx);
expect(res.status).toBe(404);
});
it("returns 403 for a viewer trying to add an entry", async () => {
mockMemberFindFirst.mockResolvedValue({ role: "viewer" });
const res = await POST(makeRequest("POST", validBody), ctx);
expect(res.status).toBe(403);
});
it("allows an editor to add an entry", async () => {
mockMemberFindFirst.mockResolvedValue({ role: "editor" });
mockRecipeFindFirst.mockResolvedValue({ id: "r-1" });
const res = await POST(makeRequest("POST", validBody), ctx);
expect(res.status).toBe(201);
expect(mockInsertValues).toHaveBeenCalledWith(expect.objectContaining({ mealPlanId: "plan-1", day: "mon" }));
});
it("returns 404 when the recipe isn't accessible to the editor", async () => {
mockMemberFindFirst.mockResolvedValue({ role: "editor" });
mockRecipeFindFirst.mockResolvedValue(undefined);
const res = await POST(makeRequest("POST", validBody), ctx);
expect(res.status).toBe(404);
});
});
describe("DELETE /api/v1/meal-plans/shared/[mealPlanId]/entries", () => {
it("returns 403 for a viewer trying to remove an entry", async () => {
mockMemberFindFirst.mockResolvedValue({ role: "viewer" });
const res = await DELETE(makeRequest("DELETE", undefined, "?entryId=e1"), ctx);
expect(res.status).toBe(403);
});
it("allows an editor to remove an entry", async () => {
mockMemberFindFirst.mockResolvedValue({ role: "editor" });
const res = await DELETE(makeRequest("DELETE", undefined, "?entryId=e1"), ctx);
expect(res.status).toBe(204);
});
});
@@ -0,0 +1,81 @@
import { NextRequest, NextResponse } from "next/server";
import { z } from "zod";
import { db, mealPlanEntries, recipes, eq, and, or, ne } from "@epicure/db";
import { requireSession } from "@/lib/api-auth";
import { getMealPlanAccessById, canWriteMealPlan } from "@/lib/meal-plan-access";
import { dispatchWebhook } from "@/lib/webhooks";
type Params = { params: Promise<{ mealPlanId: string }> };
const Schema = z.object({
day: z.enum(["mon", "tue", "wed", "thu", "fri", "sat", "sun"]),
mealType: z.enum(["breakfast", "lunch", "dinner", "snack"]),
recipeId: z.string().optional(),
servings: z.number().int().min(1).max(100).default(2),
note: z.string().max(500).optional(),
});
export async function POST(req: NextRequest, { params }: Params) {
const { session, response } = await requireSession();
if (response) return response;
const { mealPlanId } = await params;
const access = await getMealPlanAccessById(mealPlanId, session!.user.id);
if (!access) return NextResponse.json({ error: "Not found" }, { status: 404 });
if (!canWriteMealPlan(access.role)) return NextResponse.json({ error: "Forbidden" }, { status: 403 });
const body = await req.json() as unknown;
const parsed = Schema.safeParse(body);
if (!parsed.success) return NextResponse.json({ error: "Validation error" }, { status: 400 });
if (parsed.data.recipeId) {
const recipe = await db.query.recipes.findFirst({
where: and(
eq(recipes.id, parsed.data.recipeId),
or(eq(recipes.authorId, session!.user.id), ne(recipes.visibility, "private"))
),
});
if (!recipe) return NextResponse.json({ error: "Recipe not found" }, { status: 404 });
}
await db.delete(mealPlanEntries).where(
and(
eq(mealPlanEntries.mealPlanId, mealPlanId),
eq(mealPlanEntries.day, parsed.data.day),
eq(mealPlanEntries.mealType, parsed.data.mealType)
)
);
const entryId = crypto.randomUUID();
await db.insert(mealPlanEntries).values({
id: entryId,
mealPlanId,
day: parsed.data.day,
mealType: parsed.data.mealType,
recipeId: parsed.data.recipeId,
servings: parsed.data.servings,
note: parsed.data.note,
});
void dispatchWebhook(access.plan.userId, "meal_plan.updated", { mealPlanId, day: parsed.data.day, mealType: parsed.data.mealType });
return NextResponse.json({ id: entryId }, { status: 201 });
}
export async function DELETE(req: NextRequest, { params }: Params) {
const { session, response } = await requireSession();
if (response) return response;
const { mealPlanId } = await params;
const access = await getMealPlanAccessById(mealPlanId, session!.user.id);
if (!access) return NextResponse.json({ error: "Not found" }, { status: 404 });
if (!canWriteMealPlan(access.role)) return NextResponse.json({ error: "Forbidden" }, { status: 403 });
const entryId = req.nextUrl.searchParams.get("entryId");
if (!entryId) return NextResponse.json({ error: "entryId required" }, { status: 400 });
await db.delete(mealPlanEntries).where(
and(eq(mealPlanEntries.id, entryId), eq(mealPlanEntries.mealPlanId, mealPlanId))
);
return new NextResponse(null, { status: 204 });
}
@@ -0,0 +1,27 @@
import { NextRequest, NextResponse } from "next/server";
import { db, mealPlans, users, eq } from "@epicure/db";
import { requireSession } from "@/lib/api-auth";
import { getMealPlanAccessById } from "@/lib/meal-plan-access";
type Params = { params: Promise<{ mealPlanId: string }> };
export async function GET(_req: NextRequest, { params }: Params) {
const { session, response } = await requireSession();
if (response) return response;
const { mealPlanId } = await params;
const access = await getMealPlanAccessById(mealPlanId, session!.user.id);
if (!access) return NextResponse.json({ error: "Not found" }, { status: 404 });
const plan = await db.query.mealPlans.findFirst({
where: eq(mealPlans.id, mealPlanId),
with: {
entries: { with: { recipe: { with: { photos: true } } } },
},
});
if (!plan) return NextResponse.json({ error: "Not found" }, { status: 404 });
const owner = await db.query.users.findFirst({ where: eq(users.id, plan.userId) });
return NextResponse.json({ ...plan, role: access.role, owner: owner ? { name: owner.name } : null });
}