Download tasks/linkedin/environment/reference/clone/app/api/connections/route.ts from TIGER-Lab/WebsiteBench: direct link, hf CLI and curl.
- Browser
- Download file 3.84 kB
-
https://huggingface.co/datasets/TIGER-Lab/WebsiteBench/resolve/main/tasks/linkedin/environment/reference/clone/app/api/connections/route.ts
- Command line
-
hf download hf://datasets/TIGER-Lab/WebsiteBench/tasks/linkedin/environment/reference/clone/app/api/connections/route.ts
-
curl -L -o route.ts https://huggingface.co/datasets/TIGER-Lab/WebsiteBench/resolve/main/tasks/linkedin/environment/reference/clone/app/api/connections/route.ts
3.84 kB
| import { and, eq, ne, or } from "drizzle-orm"; | |
| import { NextRequest, NextResponse } from "next/server"; | |
| import { z } from "zod"; | |
| import { getDb } from "@/db"; | |
| import { connections, notifications, users } from "@/db/schema"; | |
| import { ApiError, apiResponseError, readJsonObject, requireSameOrigin, requireUser } from "@/lib/api"; | |
| import { writeAuditLog } from "@/lib/audit"; | |
| const createConnectionSchema = z.object({ recipientId: z.string().uuid() }); | |
| const updateConnectionSchema = z.object({ | |
| id: z.string().uuid(), | |
| status: z.enum(["accepted", "declined"]), | |
| }); | |
| export async function GET(request: NextRequest) { | |
| try { | |
| const user = await requireUser(request); | |
| const db = getDb(); | |
| const pending = await db.select({ | |
| connection: connections, | |
| requester: { id: users.id, name: users.name, username: users.username, headline: users.headline, avatarUrl: users.avatarUrl }, | |
| }).from(connections) | |
| .innerJoin(users, eq(users.id, connections.requesterId)) | |
| .where(and(eq(connections.recipientId, user.id), eq(connections.status, "pending"))); | |
| const suggestions = await db.select({ | |
| id: users.id, | |
| name: users.name, | |
| username: users.username, | |
| headline: users.headline, | |
| avatarUrl: users.avatarUrl, | |
| location: users.location, | |
| }).from(users).where(ne(users.id, user.id)).limit(12); | |
| return NextResponse.json({ pending, suggestions }); | |
| } catch (error) { | |
| return apiResponseError(error); | |
| } | |
| } | |
| export async function POST(request: NextRequest) { | |
| try { | |
| requireSameOrigin(request); | |
| const user = await requireUser(request); | |
| const payload = createConnectionSchema.parse(await readJsonObject(request)); | |
| if (payload.recipientId === user.id) throw new ApiError(400, "You cannot connect with yourself.", "validation_error"); | |
| const db = getDb(); | |
| const [recipient] = await db.select().from(users).where(eq(users.id, payload.recipientId)).limit(1); | |
| if (!recipient) throw new ApiError(404, "Member not found.", "not_found"); | |
| const [existing] = await db.select().from(connections) | |
| .where(or( | |
| and(eq(connections.requesterId, user.id), eq(connections.recipientId, payload.recipientId)), | |
| and(eq(connections.requesterId, payload.recipientId), eq(connections.recipientId, user.id)), | |
| )) | |
| .limit(1); | |
| if (existing) return NextResponse.json({ connection: existing, existing: true }); | |
| const [connection] = await db.insert(connections).values({ requesterId: user.id, recipientId: payload.recipientId }).returning(); | |
| await db.insert(notifications).values({ | |
| userId: payload.recipientId, | |
| actorId: user.id, | |
| type: "connection", | |
| title: `${user.name} invited you to connect`, | |
| href: "/my-network", | |
| }); | |
| await writeAuditLog({ actorId: user.id, action: "connection.requested", entityType: "connection", entityId: connection.id }); | |
| return NextResponse.json({ connection }, { status: 201 }); | |
| } catch (error) { | |
| return apiResponseError(error); | |
| } | |
| } | |
| export async function PATCH(request: NextRequest) { | |
| try { | |
| requireSameOrigin(request); | |
| const user = await requireUser(request); | |
| const payload = updateConnectionSchema.parse(await readJsonObject(request)); | |
| const [connection] = await getDb().update(connections) | |
| .set({ status: payload.status, respondedAt: new Date() }) | |
| .where(and(eq(connections.id, payload.id), eq(connections.recipientId, user.id), eq(connections.status, "pending"))) | |
| .returning(); | |
| if (!connection) throw new ApiError(404, "Connection invitation not found.", "not_found"); | |
| await writeAuditLog({ actorId: user.id, action: `connection.${payload.status}`, entityType: "connection", entityId: connection.id }); | |
| return NextResponse.json({ connection }); | |
| } catch (error) { | |
| return apiResponseError(error); | |
| } | |
| } | |