118 lines
3.3 KiB
TypeScript
118 lines
3.3 KiB
TypeScript
import { NextRequest } from "next/server";
|
|
import { z } from "zod";
|
|
import { clientIp } from "@/auth/api";
|
|
import { RespondError, applyRespond } from "@/db/respond";
|
|
import { verifyRespondToken } from "@/lib/respond";
|
|
import { rateLimit } from "@/lib/rate-limit";
|
|
|
|
const JSON_HEADERS = { "content-type": "application/json" };
|
|
|
|
const bodySchema = z.object({
|
|
token: z.string().min(1),
|
|
action: z.enum(["confirm", "decline"]),
|
|
});
|
|
|
|
/** Map a refused respond action to its HTTP status. */
|
|
function errorStatus(code: RespondError["code"]): number {
|
|
switch (code) {
|
|
case "already_used":
|
|
case "not_actionable":
|
|
return 409;
|
|
case "unknown_assignment":
|
|
return 404;
|
|
case "wrong_volunteer":
|
|
return 403;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* POST /api/respond — { token, action: "confirm" | "decline" }
|
|
*
|
|
* The no-login confirm/decline endpoint (ARCHITECTURE §5.6). The signed token
|
|
* carries the assignment, volunteer, action and `jti`; the `jti` is consumed
|
|
* exactly once. `GET /respond` renders the page and never mutates, so a link
|
|
* preview cannot accept a shift.
|
|
*/
|
|
export const POST = async (req: NextRequest) => {
|
|
let parsedBody: z.infer<typeof bodySchema>;
|
|
try {
|
|
const body = await req.json().catch(() => null);
|
|
const parsed = bodySchema.safeParse(body);
|
|
if (!parsed.success) {
|
|
return new Response(
|
|
JSON.stringify({ error: "a token and action are required" }),
|
|
{ status: 400, headers: JSON_HEADERS },
|
|
);
|
|
}
|
|
parsedBody = parsed.data;
|
|
} catch {
|
|
return new Response(JSON.stringify({ error: "invalid body" }), {
|
|
status: 400,
|
|
headers: JSON_HEADERS,
|
|
});
|
|
}
|
|
|
|
// Public endpoint: cap attempts per IP so tokens cannot be brute-forced.
|
|
const limit = rateLimit(`respond:${clientIp(req)}`, 30, 60_000);
|
|
if (!limit.ok) {
|
|
return new Response(
|
|
JSON.stringify({
|
|
error: "too many attempts; try again shortly",
|
|
code: "RATE_LIMITED",
|
|
}),
|
|
{
|
|
status: 429,
|
|
headers: {
|
|
...JSON_HEADERS,
|
|
"retry-after": String(limit.retryAfterSeconds),
|
|
},
|
|
},
|
|
);
|
|
}
|
|
|
|
const payload = await verifyRespondToken(parsedBody.token);
|
|
if (!payload) {
|
|
return new Response(
|
|
JSON.stringify({
|
|
error: "this link is invalid or has expired",
|
|
code: "invalid_token",
|
|
}),
|
|
{ status: 400, headers: JSON_HEADERS },
|
|
);
|
|
}
|
|
if (payload.action !== parsedBody.action) {
|
|
return new Response(
|
|
JSON.stringify({
|
|
error: "this link is for a different action",
|
|
code: "action_mismatch",
|
|
}),
|
|
{ status: 400, headers: JSON_HEADERS },
|
|
);
|
|
}
|
|
|
|
try {
|
|
const result = await applyRespond({
|
|
assignmentId: payload.assignmentId,
|
|
userId: payload.userId,
|
|
action: payload.action,
|
|
jti: payload.jti,
|
|
});
|
|
return new Response(JSON.stringify({ ok: true, ...result }), {
|
|
status: 200,
|
|
headers: JSON_HEADERS,
|
|
});
|
|
} catch (e) {
|
|
if (e instanceof RespondError) {
|
|
return new Response(
|
|
JSON.stringify({ error: e.message, code: e.code }),
|
|
{ status: errorStatus(e.code), headers: JSON_HEADERS },
|
|
);
|
|
}
|
|
const message = e instanceof Error ? e.message : "internal error";
|
|
return new Response(JSON.stringify({ error: message }), {
|
|
status: 500,
|
|
headers: JSON_HEADERS,
|
|
});
|
|
}
|
|
};
|