45 lines
2.3 KiB
TypeScript
45 lines
2.3 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import { getSessionUser, requireSessionUser } from "@/lib/server/session";
|
|
import { apiError, toErrorResponse } from "@/lib/server/errors";
|
|
import { getRequestMeta } from "@/lib/server/request";
|
|
import { acceptInviteLink, getInviteLinkSummaryByToken, getInviteViewerStatus } from "@/lib/server/group-invites";
|
|
import { enforceIpRateLimit } from "@/lib/server/rate-limit";
|
|
|
|
export async function GET(_: Request, context: { params: Promise<{ token: string }> }) {
|
|
const { requestId, ip } = await getRequestMeta();
|
|
try {
|
|
await enforceIpRateLimit({ scope: "invite-links:get:ip", ip, limit: 120 });
|
|
const { token } = await context.params;
|
|
const normalized = String(token || "").trim();
|
|
if (!normalized) apiError("INVITE_NOT_FOUND");
|
|
const link = await getInviteLinkSummaryByToken(normalized);
|
|
if (!link) apiError("INVITE_NOT_FOUND", { tokenLast4: normalized.slice(-4) });
|
|
const user = await getSessionUser();
|
|
if (user) {
|
|
const viewerStatus = await getInviteViewerStatus({ userId: user.id, groupId: link.groupId });
|
|
if (viewerStatus)
|
|
return NextResponse.json({ requestId, request_id: requestId, link: { ...link, viewerStatus } });
|
|
}
|
|
return NextResponse.json({ requestId, request_id: requestId, link });
|
|
} catch (e) {
|
|
const { status, body } = toErrorResponse(e, "GET /api/invite-links/[token]", requestId);
|
|
return NextResponse.json(body, { status });
|
|
}
|
|
}
|
|
|
|
export async function POST(_: Request, context: { params: Promise<{ token: string }> }) {
|
|
const { requestId, ip, userAgent } = await getRequestMeta();
|
|
try {
|
|
await enforceIpRateLimit({ scope: "invite-links:accept:ip", ip, limit: 60 });
|
|
const user = await requireSessionUser();
|
|
const { token } = await context.params;
|
|
const normalized = String(token || "").trim();
|
|
if (!normalized) apiError("INVITE_NOT_FOUND");
|
|
const result = await acceptInviteLink({ userId: user.id, token: normalized, requestId, ip, userAgent });
|
|
return NextResponse.json({ requestId, request_id: requestId, result });
|
|
} catch (e) {
|
|
const { status, body } = toErrorResponse(e, "POST /api/invite-links/[token]", requestId);
|
|
return NextResponse.json(body, { status });
|
|
}
|
|
}
|