push to maple-street
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
import { NextRequest } from "next/server";
|
||||
import { z } from "zod";
|
||||
import { gateOrUser, gateSession } from "@/auth/api";
|
||||
import { hasPermission } from "@/auth/permissions";
|
||||
import {
|
||||
createAnnouncement,
|
||||
deleteAnnouncement,
|
||||
listAnnouncements,
|
||||
updateAnnouncement,
|
||||
} from "@/db/announcements";
|
||||
import { errorResponse, jsonResponse } from "@/lib/http";
|
||||
|
||||
const createSchema = z.object({
|
||||
title: z.string().trim().min(1).max(200),
|
||||
body: z.string().max(20_000).optional(),
|
||||
published: z.boolean().optional(),
|
||||
});
|
||||
|
||||
const patchSchema = z.object({
|
||||
id: z.string().min(1),
|
||||
title: z.string().trim().min(1).max(200).optional(),
|
||||
body: z.string().max(20_000).optional(),
|
||||
published: z.boolean().optional(),
|
||||
});
|
||||
|
||||
/**
|
||||
* Announcements. Any signed-in user reads published posts; authors
|
||||
* (`content.author` = admin/coordinator) also see drafts and can write.
|
||||
*
|
||||
* GET -> { announcements }
|
||||
* POST -> { announcement }
|
||||
* PATCH -> { announcement } edit or publish/unpublish
|
||||
* DELETE ?id= -> { ok }
|
||||
*/
|
||||
export const GET = async () => {
|
||||
const gate = await gateSession();
|
||||
if (gate instanceof Response) return gate;
|
||||
|
||||
const canAuthor = hasPermission(gate.systemRole, "content.author");
|
||||
return jsonResponse({
|
||||
announcements: await listAnnouncements({
|
||||
includeUnpublished: canAuthor,
|
||||
}),
|
||||
});
|
||||
};
|
||||
|
||||
export const POST = async (req: NextRequest) => {
|
||||
const gate = await gateOrUser("content.author");
|
||||
if (gate instanceof Response) return gate;
|
||||
|
||||
const parsed = createSchema.safeParse(await req.json().catch(() => null));
|
||||
if (!parsed.success) {
|
||||
return errorResponse(400, parsed.error.issues[0]?.message ?? "invalid post");
|
||||
}
|
||||
|
||||
const announcement = await createAnnouncement({
|
||||
title: parsed.data.title,
|
||||
body: parsed.data.body ?? "",
|
||||
published: parsed.data.published ?? false,
|
||||
createdBy: gate.id,
|
||||
});
|
||||
return jsonResponse({ announcement }, 201);
|
||||
};
|
||||
|
||||
export const PATCH = async (req: NextRequest) => {
|
||||
const gate = await gateOrUser("content.author");
|
||||
if (gate instanceof Response) return gate;
|
||||
|
||||
const parsed = patchSchema.safeParse(await req.json().catch(() => null));
|
||||
if (!parsed.success) {
|
||||
return errorResponse(400, parsed.error.issues[0]?.message ?? "invalid post");
|
||||
}
|
||||
|
||||
const { id, ...patch } = parsed.data;
|
||||
const announcement = await updateAnnouncement(id, patch);
|
||||
if (!announcement) return errorResponse(404, "unknown announcement");
|
||||
return jsonResponse({ announcement });
|
||||
};
|
||||
|
||||
export const DELETE = async (req: NextRequest) => {
|
||||
const gate = await gateOrUser("content.author");
|
||||
if (gate instanceof Response) return gate;
|
||||
|
||||
const id = req.nextUrl.searchParams.get("id");
|
||||
if (!id) return errorResponse(400, "an id is required");
|
||||
|
||||
const deleted = await deleteAnnouncement(id);
|
||||
if (!deleted) return errorResponse(404, "unknown announcement");
|
||||
return jsonResponse({ ok: true });
|
||||
};
|
||||
Reference in New Issue
Block a user