wip: checkpoint host working tree
Login-page gate, notification outbox dispatcher and cron, assignment proposals/responses, coverage and training-guide updates, plus the generated training PDFs. Checkpointed from the host before merging the invite-link fix.
This commit is contained in:
@@ -8,11 +8,13 @@ const bodySchema = z.object({ action: z.enum(["confirm", "decline"]) });
|
||||
function errorStatus(code: RespondError["code"]): number {
|
||||
switch (code) {
|
||||
case "unknown_assignment":
|
||||
case "unknown_instance":
|
||||
return 404;
|
||||
case "wrong_volunteer":
|
||||
return 403;
|
||||
case "not_actionable":
|
||||
case "already_used":
|
||||
case "invalid_alternative":
|
||||
return 409;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ import { NextRequest } from "next/server";
|
||||
import { z } from "zod";
|
||||
import { and, eq, inArray } from "drizzle-orm";
|
||||
import { auth } from "@/auth/config";
|
||||
import { requestOrigin } from "@/auth/magic-link";
|
||||
import { AuthError, requirePermission } from "@/auth/permissions";
|
||||
import type { SystemRole } from "@/auth/roles";
|
||||
import { db } from "@/db";
|
||||
@@ -16,8 +17,10 @@ import {
|
||||
AssignFactError,
|
||||
resolveAssignFacts,
|
||||
} from "@/db/assignments";
|
||||
import { cancelAssignmentNotifications } from "@/db/outbox";
|
||||
import { enqueuePublishNotifications } from "@/db/outbox";
|
||||
import {
|
||||
cancelAssignmentNotifications,
|
||||
enqueueAssignmentProposed,
|
||||
} from "@/db/outbox";
|
||||
import { enqueueRemindersForInstance } from "@/db/reminders";
|
||||
import { classifyConflicts, hardConflicts } from "@/domain/conflicts";
|
||||
import { uuidv7 } from "@/lib/uuid";
|
||||
@@ -195,6 +198,8 @@ export const POST = async (req: NextRequest) => {
|
||||
.limit(1);
|
||||
|
||||
const assignmentId = uuidv7();
|
||||
const now = new Date();
|
||||
const appOrigin = requestOrigin(req);
|
||||
await db.transaction(async (tx) => {
|
||||
await tx.insert(assignments).values({
|
||||
id: assignmentId,
|
||||
@@ -203,7 +208,7 @@ export const POST = async (req: NextRequest) => {
|
||||
status: "proposed",
|
||||
source: "manual",
|
||||
assignedBy: actorId,
|
||||
assignedAt: new Date(),
|
||||
assignedAt: now,
|
||||
});
|
||||
await tx
|
||||
.update(slots)
|
||||
@@ -224,12 +229,12 @@ export const POST = async (req: NextRequest) => {
|
||||
}),
|
||||
);
|
||||
|
||||
// A published week's mail is written at publish time; a seat added later
|
||||
// would otherwise never be announced. Both enqueues are idempotent on
|
||||
// dedupe_key, so existing rows are untouched.
|
||||
// The proposed email always goes out, whether the week is draft or
|
||||
// published: the volunteer has to accept, decline, or propose another
|
||||
// Sunday. On a published week only the reminders need topping up.
|
||||
await enqueueAssignmentProposed(tx, assignmentId, { now, appOrigin });
|
||||
if (instance?.status === "published") {
|
||||
await enqueuePublishNotifications(tx, instance.id, { now: new Date() });
|
||||
await enqueueRemindersForInstance(tx, instance.id, { now: new Date() });
|
||||
await enqueueRemindersForInstance(tx, instance.id, { now, appOrigin });
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ import {
|
||||
requestOrigin,
|
||||
} from "@/auth/magic-link";
|
||||
import { rateLimit } from "@/lib/rate-limit";
|
||||
import { hasPasswordProtectedAdmin } from "@/db/users";
|
||||
|
||||
const JSON_HEADERS = { "content-type": "application/json" };
|
||||
const bodySchema = z.object({ email: z.string().email() });
|
||||
@@ -59,11 +60,29 @@ export const POST = async (req: NextRequest) => {
|
||||
);
|
||||
}
|
||||
|
||||
// Bootstrap only. Once an admin has a real password, a public request for
|
||||
// that admin's email must not mint a session link. New-account and recovery
|
||||
// links are still issued by signed-in admins from the People panel.
|
||||
if (await hasPasswordProtectedAdmin()) {
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
error:
|
||||
"Use your username and password to sign in. Contact an admin if you need help.",
|
||||
}),
|
||||
{ status: 403, headers: JSON_HEADERS },
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await requestMagicLink(parsed.data.email, {
|
||||
origin: requestOrigin(req),
|
||||
});
|
||||
return new Response(JSON.stringify(result), {
|
||||
// Never expose account metadata to an unauthenticated caller. The dev
|
||||
// transport may still return the URL for local bootstrap/tests; production
|
||||
// turns that transport off and the login UI never renders a URL anyway.
|
||||
const payload: { ok: true; url?: string } = { ok: true };
|
||||
if ("url" in result && result.url) payload.url = result.url;
|
||||
return new Response(JSON.stringify(payload), {
|
||||
status: 200,
|
||||
headers: JSON_HEADERS,
|
||||
});
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
import { NextRequest } from "next/server";
|
||||
import { z } from "zod";
|
||||
import { clientIp } from "@/auth/api";
|
||||
import { RespondError, applyAlternativeProposal } 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),
|
||||
instanceId: z.string().min(1),
|
||||
note: z.string().max(500).optional(),
|
||||
});
|
||||
|
||||
/** Map a refused proposal to its HTTP status. */
|
||||
function errorStatus(code: RespondError["code"]): number {
|
||||
switch (code) {
|
||||
case "unknown_assignment":
|
||||
case "unknown_instance":
|
||||
return 404;
|
||||
case "wrong_volunteer":
|
||||
return 403;
|
||||
case "not_actionable":
|
||||
case "already_used":
|
||||
case "invalid_alternative":
|
||||
return 409;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* POST /api/respond/alternative — { token, instanceId, note? }
|
||||
*
|
||||
* The no-login "propose another Sunday" endpoint. The token must be an
|
||||
* `alternative` link; its `jti` is consumed exactly once, so the same email
|
||||
* link cannot file two proposals. The assignment itself is left live for a
|
||||
* coordinator to reassign.
|
||||
*/
|
||||
export const POST = async (req: NextRequest) => {
|
||||
const parsed = bodySchema.safeParse(await req.json().catch(() => null));
|
||||
if (!parsed.success) {
|
||||
return new Response(
|
||||
JSON.stringify({ error: "a token and service date are required" }),
|
||||
{ status: 400, headers: JSON_HEADERS },
|
||||
);
|
||||
}
|
||||
|
||||
const limit = rateLimit(`respond-alternative:${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(parsed.data.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 !== "alternative") {
|
||||
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 applyAlternativeProposal({
|
||||
assignmentId: payload.assignmentId,
|
||||
userId: payload.userId,
|
||||
jti: payload.jti,
|
||||
proposedInstanceId: parsed.data.instanceId,
|
||||
note: parsed.data.note ?? null,
|
||||
});
|
||||
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 },
|
||||
);
|
||||
}
|
||||
console.error("[respond-alternative] ERROR", e);
|
||||
return new Response(JSON.stringify({ error: "failed to save the proposal" }), {
|
||||
status: 500,
|
||||
headers: JSON_HEADERS,
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -17,8 +17,10 @@ function errorStatus(code: RespondError["code"]): number {
|
||||
switch (code) {
|
||||
case "already_used":
|
||||
case "not_actionable":
|
||||
case "invalid_alternative":
|
||||
return 409;
|
||||
case "unknown_assignment":
|
||||
case "unknown_instance":
|
||||
return 404;
|
||||
case "wrong_volunteer":
|
||||
return 403;
|
||||
|
||||
Reference in New Issue
Block a user