feat: add bulk "add to collection" action on recipes page
Select recipes → add to an existing collection or create a new one inline. Fixes duplicate "visibility" i18n key bug found during verification. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { z } from "zod";
|
||||
import { db, collections, collectionRecipes, recipes, eq, and, or, ne } from "@epicure/db";
|
||||
import { db, collections, collectionRecipes, recipes, eq, and, or, ne, inArray } from "@epicure/db";
|
||||
import { requireSession } from "@/lib/api-auth";
|
||||
|
||||
type Params = { params: Promise<{ id: string }> };
|
||||
@@ -35,6 +35,7 @@ export async function PUT(req: NextRequest, { params }: Params) {
|
||||
description: z.string().max(500).optional(),
|
||||
isPublic: z.boolean().optional(),
|
||||
addRecipeId: z.string().optional(),
|
||||
addRecipeIds: z.array(z.string()).max(200).optional(),
|
||||
removeRecipeId: z.string().optional(),
|
||||
}).safeParse(body);
|
||||
if (!parsed.success) return NextResponse.json({ error: "Validation error" }, { status: 400 });
|
||||
@@ -49,15 +50,19 @@ export async function PUT(req: NextRequest, { params }: Params) {
|
||||
}).where(eq(collections.id, id));
|
||||
}
|
||||
|
||||
if (data.addRecipeId) {
|
||||
const recipe = await db.query.recipes.findFirst({
|
||||
const idsToAdd = [...(data.addRecipeId ? [data.addRecipeId] : []), ...(data.addRecipeIds ?? [])];
|
||||
if (idsToAdd.length > 0) {
|
||||
const owned = await db.query.recipes.findMany({
|
||||
where: and(
|
||||
eq(recipes.id, data.addRecipeId),
|
||||
inArray(recipes.id, idsToAdd),
|
||||
or(eq(recipes.authorId, session!.user.id), ne(recipes.visibility, "private"))
|
||||
),
|
||||
columns: { id: true },
|
||||
});
|
||||
if (recipe) {
|
||||
await db.insert(collectionRecipes).values({ collectionId: id, recipeId: data.addRecipeId }).onConflictDoNothing();
|
||||
if (owned.length > 0) {
|
||||
await db.insert(collectionRecipes)
|
||||
.values(owned.map((r) => ({ collectionId: id, recipeId: r.id })))
|
||||
.onConflictDoNothing();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user