fix: album assignment + approval error handling
Album assignment 500'd with IMMICH_UNAVAILABLE because grantAlbum verified the
album via getAlbum, which pulled the full album incl. all assets — large albums
blew the 15s timeout. Request ?withoutAssets=true so Immich returns metadata
only (no consumer reads .assets); make assets optional in the schema.
Album thumbnails 500'd because next/image rejected the ?albumId= query string
(images.localPatterns). Switch the album page to a plain <img>, matching the
editorial catalog (next/image was used in exactly one place).
Overhaul action error handling: grantAlbumAction/revokeAlbumAction/approveEditorialAction
returned a thrown Error, which Next.js turns into an opaque error-digest page —
the German message never reached the admin. They now return {ok,message}; new
client AlbumGrantForm + ApproveForm use useActionState to show the message
inline. Auth (requireAdmin) still throws (guarded by the layout). Update both
action test files to the result contract.
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
1 parent
7aeceee078
commit
089f5f7035
9 files changed
+233
-60
No files matched your search
@@ -3,37 +3,77 @@
|
||||
import { revalidatePath } from "next/cache";
|
||||
import { headers } from "next/headers";
|
||||
|
||||
import { albumGrantService } from "@/modules/albums/grants";
|
||||
import { AlbumGrantError, albumGrantService } from "@/modules/albums/grants";
|
||||
import { requireAdmin } from "@/modules/auth/principal";
|
||||
|
||||
export async function grantAlbumAction(editorialId: string, formData: FormData): Promise<void> {
|
||||
// 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"));
|
||||
await albumGrantService.grantAlbum({
|
||||
editorialId: safeEditorialId,
|
||||
immichAlbumId,
|
||||
adminUserId: principal.userId,
|
||||
});
|
||||
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, formData: FormData): Promise<void> {
|
||||
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"));
|
||||
await albumGrantService.revokeAlbum({
|
||||
editorialId: safeEditorialId,
|
||||
immichAlbumId,
|
||||
adminUserId: principal.userId,
|
||||
});
|
||||
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;
|
||||
}
|
||||
|
||||
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" ||
|
||||
@@ -42,4 +82,4 @@ function requiredId(value: unknown): string {
|
||||
throw new Error("INVALID_ID");
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user