120 lines
3.9 KiB
TypeScript
120 lines
3.9 KiB
TypeScript
"use server";
|
|
|
|
import { revalidatePath } from "next/cache";
|
|
import { headers } from "next/headers";
|
|
|
|
import { AlbumGrantError, albumGrantService } from "@/modules/albums/grants";
|
|
import { requireAdmin } from "@/modules/auth/principal";
|
|
import { emailService } from "@/modules/email/service";
|
|
import { db } from "@/db/client";
|
|
import { editorialAccounts } from "@/db/schema";
|
|
import { eq } from "drizzle-orm";
|
|
|
|
// Returned to the client form so failures surface as inline German text
|
|
// instead of an opaque Next.js error-digest page (a thrown server action
|
|
// becomes a 500 with a digest the user can't read).
|
|
export type AlbumActionResult = { ok: boolean; message: string };
|
|
|
|
const INITIAL_RESULT: AlbumActionResult = { ok: true, message: "" };
|
|
|
|
export async function grantAlbumAction(
|
|
editorialId: string,
|
|
_prevState: AlbumActionResult,
|
|
formData: FormData,
|
|
): Promise<AlbumActionResult> {
|
|
const principal = await getAdminPrincipal();
|
|
const safeEditorialId = requiredId(editorialId);
|
|
const immichAlbumId = requiredId(formData.get("immichAlbumId"));
|
|
try {
|
|
await albumGrantService.grantAlbum({
|
|
editorialId: safeEditorialId,
|
|
immichAlbumId,
|
|
adminUserId: principal.userId,
|
|
});
|
|
} catch (error) {
|
|
return { ok: false, message: albumGrantErrorMessage(error) };
|
|
}
|
|
revalidatePath(`/admin/redaktionen/${safeEditorialId}/alben`);
|
|
return INITIAL_RESULT;
|
|
}
|
|
|
|
export async function revokeAlbumAction(
|
|
editorialId: string,
|
|
_prevState: AlbumActionResult,
|
|
formData: FormData,
|
|
): Promise<AlbumActionResult> {
|
|
const principal = await getAdminPrincipal();
|
|
const safeEditorialId = requiredId(editorialId);
|
|
const immichAlbumId = requiredId(formData.get("immichAlbumId"));
|
|
try {
|
|
await albumGrantService.revokeAlbum({
|
|
editorialId: safeEditorialId,
|
|
immichAlbumId,
|
|
adminUserId: principal.userId,
|
|
});
|
|
} catch (error) {
|
|
return { ok: false, message: albumGrantErrorMessage(error) };
|
|
}
|
|
revalidatePath(`/admin/redaktionen/${safeEditorialId}/alben`);
|
|
return INITIAL_RESULT;
|
|
}
|
|
|
|
export async function sendAlbumNotificationAction(
|
|
editorialId: string,
|
|
_prevState: AlbumActionResult,
|
|
formData: FormData,
|
|
): Promise<AlbumActionResult> {
|
|
await getAdminPrincipal();
|
|
const safeEditorialId = requiredId(editorialId);
|
|
const albumName = formData.get("albumName");
|
|
|
|
if (typeof albumName !== "string" || !albumName) {
|
|
return { ok: false, message: "Ungültiger Albumname." };
|
|
}
|
|
|
|
try {
|
|
const [editorial] = await db
|
|
.select({ loginEmail: editorialAccounts.loginEmail })
|
|
.from(editorialAccounts)
|
|
.where(eq(editorialAccounts.id, safeEditorialId));
|
|
|
|
if (!editorial) {
|
|
return { ok: false, message: "Redaktion nicht gefunden." };
|
|
}
|
|
|
|
await emailService.sendNewAlbumNotification(editorial.loginEmail, albumName);
|
|
return { ok: true, message: "Benachrichtigung erfolgreich gesendet." };
|
|
} catch (error) {
|
|
console.error("Failed to send album notification:", error);
|
|
return { ok: false, message: "E-Mail konnte nicht gesendet werden." };
|
|
}
|
|
}
|
|
|
|
async function getAdminPrincipal() {
|
|
return requireAdmin(new Headers(await headers()));
|
|
}
|
|
|
|
function albumGrantErrorMessage(error: unknown): string {
|
|
if (error instanceof AlbumGrantError) {
|
|
const messages: Record<AlbumGrantError["code"], string> = {
|
|
ALBUM_NOT_ASSIGNABLE:
|
|
"Das Album konnte nicht gefunden werden oder lässt sich nicht zuweisen.",
|
|
IMMICH_UNAVAILABLE:
|
|
"Immich ist momentan nicht erreichbar. Bitte später erneut versuchen.",
|
|
EDITORIAL_NOT_FOUND: "Redaktion nicht gefunden.",
|
|
INVALID_ALBUM_GRANT_INPUT: "Ungültige Albumkennung.",
|
|
};
|
|
return messages[error.code] ?? "Die Zuweisung ist fehlgeschlagen.";
|
|
}
|
|
return "Die Zuweisung ist fehlgeschlagen.";
|
|
}
|
|
|
|
function requiredId(value: unknown): string {
|
|
if (
|
|
typeof value !== "string" ||
|
|
!/^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(value)
|
|
) {
|
|
throw new Error("INVALID_ID");
|
|
}
|
|
return value;
|
|
} |