fix: link album assignment and narrow errors

This commit is contained in:
Leon Tonhäuser committed 2026-08-09 22:06:34 +02:00
1 parent 4af86f6eb9
commit f6e6018cc1
5 files changed
+123 -42

No files matched your search

@@ -0,0 +1,16 @@
import { AlbumGrantError, albumGrantService } from "@/modules/albums/grants";
export async function loadAssignableAlbums(
editorialId: string,
service: Pick<typeof albumGrantService, "listAssignableAlbums"> = albumGrantService,
) {
try {
const albums = await service.listAssignableAlbums(editorialId);
return { albums, unavailable: false };
} catch (error) {
if (error instanceof AlbumGrantError && error.code === "IMMICH_UNAVAILABLE") {
return { albums: [], unavailable: true };
}
throw error;
}
}
@@ -0,0 +1,47 @@
// @vitest-environment jsdom
import { render, screen } from "@testing-library/react";
import { expect, test, vi } from "vitest";
import { EditorialDetailContent } from "@/modules/editorials/admin-detail";
import { AlbumGrantError } from "@/modules/albums/grants";
import { loadAssignableAlbums } from "./page-content";
const editorial = {
id: "00000000-0000-4000-8000-000000000001",
name: "Neue Redaktion",
contactName: "Mara Muster",
loginEmail: "mara@example.com",
status: "pending_approval" as const,
emailVerifiedAt: null,
currentPriceCents: null,
};
test("the editorial detail links admins to its album-assignment page", () => {
render(<EditorialDetailContent editorial={editorial} hasActiveGrant={false} />);
expect(screen.getByRole("link", { name: /albumrechte verwalten/i }).getAttribute("href")).toBe(
"/admin/redaktionen/00000000-0000-4000-8000-000000000001/alben",
);
expect(screen.queryByText(/folgt im nächsten schritt/i)).toBeNull();
});
test("only an explicit Immich-unavailable error renders the unavailable album state", async () => {
const service = {
listAssignableAlbums: vi.fn().mockRejectedValue(new AlbumGrantError("IMMICH_UNAVAILABLE")),
};
await expect(loadAssignableAlbums(editorial.id, service)).resolves.toEqual({
albums: [],
unavailable: true,
});
});
test("database and programming errors propagate instead of being displayed as Immich unavailable", async () => {
const databaseFailure = new Error("database connection lost");
const service = {
listAssignableAlbums: vi.fn().mockRejectedValue(databaseFailure),
};
await expect(loadAssignableAlbums(editorial.id, service)).rejects.toBe(databaseFailure);
});
@@ -8,6 +8,7 @@ import { editorialAccounts } from "@/db/schema";
import { albumGrantService } from "@/modules/albums/grants"; import { albumGrantService } from "@/modules/albums/grants";
import { grantAlbumAction, revokeAlbumAction } from "./actions"; import { grantAlbumAction, revokeAlbumAction } from "./actions";
import { loadAssignableAlbums } from "./page-content";
export default async function EditorialAlbumsPage({ export default async function EditorialAlbumsPage({
params, params,
@@ -22,10 +23,7 @@ export default async function EditorialAlbumsPage({
notFound(); notFound();
} }
const albumResult = await albumGrantService const albumResult = await loadAssignableAlbums(editorial.id, albumGrantService);
.listAssignableAlbums(editorial.id)
.then((albums) => ({ albums, unavailable: false }))
.catch(() => ({ albums: [], unavailable: true }));
if (albumResult.unavailable) { if (albumResult.unavailable) {
return ( return (
+2 -38
View File
@@ -3,6 +3,7 @@ import { notFound } from "next/navigation";
import { db } from "@/db/client"; import { db } from "@/db/client";
import { albumGrants, editorialAccounts } from "@/db/schema"; import { albumGrants, editorialAccounts } from "@/db/schema";
import { EditorialDetailContent } from "@/modules/editorials/admin-detail";
export default async function EditorialDetailPage({ export default async function EditorialDetailPage({
params, params,
@@ -33,42 +34,5 @@ export default async function EditorialDetailPage({
) )
.limit(1); .limit(1);
return ( return <EditorialDetailContent editorial={editorial} hasActiveGrant={Boolean(grant)} />;
<section className="content-section" aria-labelledby="editorial-title">
<p className="eyebrow">Redaktion</p>
<h1 id="editorial-title">{editorial.name}</h1>
<dl>
<dt>Ansprechperson</dt>
<dd>{editorial.contactName}</dd>
<dt>Anmelde-E-Mail</dt>
<dd>{editorial.loginEmail}</dd>
<dt>Status</dt>
<dd>{editorial.status}</dd>
<dt>E-Mail bestätigt</dt>
<dd>{editorial.emailVerifiedAt ? "Ja" : "Nein"}</dd>
<dt>Individueller Nettopreis</dt>
<dd>
{editorial.currentPriceCents === null
? "Noch nicht festgelegt"
: formatCents(editorial.currentPriceCents)}
</dd>
</dl>
<section aria-labelledby="approval-title">
<h2 id="approval-title">Freigabe</h2>
<button type="button" disabled aria-describedby="approval-help">
Redaktion freigeben
</button>
<p id="approval-help">
{grant
? "Die Freigabe bleibt bis zur Verwaltungsoberfläche für Albumrechte deaktiviert."
: "Eine Freigabe ist erst möglich, wenn mindestens ein aktives Albumrecht zugewiesen wurde. Die Verwaltung der Albumrechte folgt im nächsten Schritt."}
</p>
</section>
</section>
);
}
function formatCents(cents: number): string {
return new Intl.NumberFormat("de-DE", { style: "currency", currency: "EUR" }).format(cents / 100);
} }
+56
View File
@@ -0,0 +1,56 @@
import Link from "next/link";
export type EditorialDetail = {
id: string;
name: string;
contactName: string;
loginEmail: string;
status: string;
emailVerifiedAt: Date | null;
currentPriceCents: number | null;
};
export function EditorialDetailContent({
editorial,
hasActiveGrant,
}: Readonly<{ editorial: EditorialDetail; hasActiveGrant: boolean }>) {
return (
<section className="content-section" aria-labelledby="editorial-title">
<p className="eyebrow">Redaktion</p>
<h1 id="editorial-title">{editorial.name}</h1>
<dl>
<dt>Ansprechperson</dt>
<dd>{editorial.contactName}</dd>
<dt>Anmelde-E-Mail</dt>
<dd>{editorial.loginEmail}</dd>
<dt>Status</dt>
<dd>{editorial.status}</dd>
<dt>E-Mail bestätigt</dt>
<dd>{editorial.emailVerifiedAt ? "Ja" : "Nein"}</dd>
<dt>Individueller Nettopreis</dt>
<dd>
{editorial.currentPriceCents === null
? "Noch nicht festgelegt"
: formatCents(editorial.currentPriceCents)}
</dd>
</dl>
<section aria-labelledby="approval-title">
<h2 id="approval-title">Freigabe</h2>
<button type="button" disabled aria-describedby="approval-help">
Redaktion freigeben
</button>
<p id="approval-help">
{hasActiveGrant
? "Mindestens ein Albumrecht ist zugewiesen. Die Freigabe bleibt bis zur weiteren Verwaltungsoberfläche deaktiviert."
: "Eine Freigabe ist erst möglich, wenn mindestens ein aktives Albumrecht zugewiesen wurde."}
</p>
<Link href={`/admin/redaktionen/${editorial.id}/alben`}>Albumrechte verwalten</Link>
</section>
</section>
);
}
function formatCents(cents: number): string {
return new Intl.NumberFormat("de-DE", { style: "currency", currency: "EUR" }).format(cents / 100);
}