feat: unarchive editorials and restore archived grants

Adds an "Archivierung aufheben" action on the editorial detail page:
clears the archive markers, reactivates suspended accounts and
re-activates exactly the album grants that were revoked by the archive
step (matched via the archive timestamp, skipping albums that received
a fresh active grant in the meantime).
This commit is contained in:
Leon Tonhäuser committed 2026-08-25 01:38:20 +02:00
1 parent c9d3f3d1dc
commit 9308b46d84
4 files changed
+135 -4

No files matched your search

+10
View File
@@ -55,6 +55,16 @@ export async function archiveEditorialAction(editorialId: string): Promise<void>
redirect("/admin/redaktionen"); redirect("/admin/redaktionen");
} }
export async function unarchiveEditorialAction(editorialId: string): Promise<void> {
const admin = await requireAdmin(new Headers(await headers()));
await editorialService.unarchiveEditorial({
editorialId,
adminUserId: admin.userId,
});
revalidatePath(`/admin/redaktionen/${editorialId}`);
revalidatePath("/admin/redaktionen");
}
export async function hardDeleteEditorialAction(editorialId: string): Promise<void> { export async function hardDeleteEditorialAction(editorialId: string): Promise<void> {
await requireAdmin(new Headers(await headers())); await requireAdmin(new Headers(await headers()));
await editorialService.hardDeleteEditorial(editorialId); await editorialService.hardDeleteEditorial(editorialId);
+22 -3
View File
@@ -2,7 +2,7 @@ import Link from "next/link";
import { Card, CardContent, CardHeader, CardTitle, CardDescription, CardFooter } from "@/components/ui/card"; import { Card, CardContent, CardHeader, CardTitle, CardDescription, CardFooter } from "@/components/ui/card";
import { Button } from "@/components/ui/button"; import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input"; import { Input } from "@/components/ui/input";
import { archiveEditorialAction, suspendEditorialAction, reactivateEditorialAction, updateFlatrateAction } from "@/app/admin/redaktionen/[id]/actions"; import { archiveEditorialAction, unarchiveEditorialAction, suspendEditorialAction, reactivateEditorialAction, updateFlatrateAction } from "@/app/admin/redaktionen/[id]/actions";
import { DangerZone } from "./DangerZone"; import { DangerZone } from "./DangerZone";
import { ResetUsageCard } from "./ResetUsageCard"; import { ResetUsageCard } from "./ResetUsageCard";
import { ApproveForm } from "./ApproveForm"; import { ApproveForm } from "./ApproveForm";
@@ -33,8 +33,27 @@ export function EditorialDetailContent({
</div> </div>
{editorial.archivedAt && ( {editorial.archivedAt && (
<div className="bg-destructive/15 text-destructive p-4 rounded-md border border-destructive/30" role="status"> <div className="space-y-4">
Diese Redaktion wurde am {new Date(editorial.archivedAt).toLocaleDateString('de-DE')} archiviert. <div className="bg-destructive/15 text-destructive p-4 rounded-md border border-destructive/30" role="status">
Diese Redaktion wurde am {new Date(editorial.archivedAt).toLocaleDateString('de-DE')} archiviert.
</div>
<Card>
<CardHeader>
<CardTitle>Archivierung aufheben</CardTitle>
<CardDescription>
Reaktiviert den Zugang und stellt die zum Archivierungszeitpunkt zugewiesenen
Alben wieder her. Alben mit passender Auto-Regel werden ohnehin automatisch
neu zugeteilt.
</CardDescription>
</CardHeader>
<CardContent>
<form action={unarchiveEditorialAction.bind(null, editorial.id)}>
<Button type="submit" variant="secondary">
Archivierung aufheben &amp; reaktivieren
</Button>
</form>
</CardContent>
</Card>
</div> </div>
)} )}
+16
View File
@@ -14,6 +14,7 @@ function createRepository(): EditorialServiceRepository & {
transitionStatus: ReturnType<typeof vi.fn>; transitionStatus: ReturnType<typeof vi.fn>;
claimApprovalNotice: ReturnType<typeof vi.fn>; claimApprovalNotice: ReturnType<typeof vi.fn>;
archiveEditorial: ReturnType<typeof vi.fn>; archiveEditorial: ReturnType<typeof vi.fn>;
unarchiveEditorial: ReturnType<typeof vi.fn>;
resetUsageData: ReturnType<typeof vi.fn>; resetUsageData: ReturnType<typeof vi.fn>;
} { } {
return { return {
@@ -28,6 +29,7 @@ function createRepository(): EditorialServiceRepository & {
transitionStatus: vi.fn().mockResolvedValue(undefined), transitionStatus: vi.fn().mockResolvedValue(undefined),
claimApprovalNotice: vi.fn().mockResolvedValue(true), claimApprovalNotice: vi.fn().mockResolvedValue(true),
archiveEditorial: vi.fn().mockResolvedValue(undefined), archiveEditorial: vi.fn().mockResolvedValue(undefined),
unarchiveEditorial: vi.fn().mockResolvedValue({ restoredGrants: 0 }),
hardDeleteEditorial: vi.fn().mockResolvedValue(undefined), hardDeleteEditorial: vi.fn().mockResolvedValue(undefined),
resetUsageData: vi.fn().mockResolvedValue({ deletedUsages: 0, deletedReports: 0 }), resetUsageData: vi.fn().mockResolvedValue({ deletedUsages: 0, deletedReports: 0 }),
}; };
@@ -187,3 +189,17 @@ test("resetUsageData delegates to the repository with the acting admin", async (
).resolves.toEqual({ deletedUsages: 7, deletedReports: 2 }); ).resolves.toEqual({ deletedUsages: 7, deletedReports: 2 });
expect(repository.resetUsageData).toHaveBeenCalledWith({ editorialId: "e1", adminUserId: "a1" }); expect(repository.resetUsageData).toHaveBeenCalledWith({ editorialId: "e1", adminUserId: "a1" });
}); });
test("unarchiveEditorial delegates to the repository with the acting admin", async () => {
const repository = createRepository();
const service = createEditorialService({ repository, mailer: { sendApprovalNotice: vi.fn() } });
repository.unarchiveEditorial.mockResolvedValue({ restoredGrants: 3 });
await expect(
service.unarchiveEditorial({ editorialId: "e1", adminUserId: "a1" }),
).resolves.toEqual({ restoredGrants: 3 });
expect(repository.unarchiveEditorial).toHaveBeenCalledWith({
editorialId: "e1",
adminUserId: "a1",
});
});
+87 -1
View File
@@ -57,6 +57,10 @@ export type EditorialServiceRepository = {
adminUserId: string; adminUserId: string;
}): Promise<void>; }): Promise<void>;
archiveEditorial(input: { editorialId: string; adminUserId: string }): Promise<void>; archiveEditorial(input: { editorialId: string; adminUserId: string }): Promise<void>;
unarchiveEditorial(input: {
editorialId: string;
adminUserId: string;
}): Promise<{ restoredGrants: number }>;
hardDeleteEditorial(editorialId: string): Promise<void>; hardDeleteEditorial(editorialId: string): Promise<void>;
resetUsageData(input: { resetUsageData(input: {
editorialId: string; editorialId: string;
@@ -85,7 +89,8 @@ export class EditorialServiceError extends Error {
| "EMAIL_NOT_VERIFIED" | "EMAIL_NOT_VERIFIED"
| "ALBUM_GRANT_REQUIRED" | "ALBUM_GRANT_REQUIRED"
| "APPROVAL_NOTICE_DELIVERY_UNKNOWN" | "APPROVAL_NOTICE_DELIVERY_UNKNOWN"
| "INVALID_ACCOUNT_TRANSITION", | "INVALID_ACCOUNT_TRANSITION"
| "EDITORIAL_NOT_ARCHIVED",
) { ) {
super(code); super(code);
} }
@@ -168,6 +173,10 @@ export function createEditorialService({
await repository.archiveEditorial(input); await repository.archiveEditorial(input);
}, },
async unarchiveEditorial(input: { editorialId: string; adminUserId: string }) {
return repository.unarchiveEditorial(input);
},
async hardDeleteEditorial(editorialId: string): Promise<void> { async hardDeleteEditorial(editorialId: string): Promise<void> {
await repository.hardDeleteEditorial(editorialId); await repository.hardDeleteEditorial(editorialId);
}, },
@@ -530,6 +539,83 @@ export function createEditorialServiceRepository(
}); });
}, },
/**
* Lifts the archive state: restores login (suspended -> active), clears
* the archive markers and re-activates exactly those album grants that
* were revoked by the archive step (stamped with the archive timestamp).
*/
async unarchiveEditorial({ editorialId, adminUserId }) {
return db.transaction(async (transaction) => {
await lockEditorialForMutation(transaction, editorialId);
const [editorial] = await transaction
.select({
id: editorialAccounts.id,
status: editorialAccounts.status,
archivedAt: editorialAccounts.archivedAt,
})
.from(editorialAccounts)
.where(eq(editorialAccounts.id, editorialId))
.for("update")
.limit(1);
if (!editorial) {
throw new EditorialServiceError("EDITORIAL_NOT_FOUND");
}
if (!editorial.archivedAt) {
throw new EditorialServiceError("EDITORIAL_NOT_ARCHIVED");
}
// archiveEditorial stamped every active grant with revokedAt = changedAt
// (one active row per album is guaranteed by the partial unique index),
// so matching that exact timestamp restores precisely what was lost.
// Albums granted manually in the meantime keep their newer active row.
const restored = await transaction
.update(albumGrants)
.set({ revokedAt: null })
.where(
and(
eq(albumGrants.editorialAccountId, editorialId),
eq(albumGrants.revokedAt, editorial.archivedAt),
sql`not exists (
select 1
from ${albumGrants} other
where other.editorial_account_id = ${albumGrants.editorialAccountId}
and lower(other.immich_album_id) = lower(${albumGrants.immichAlbumId})
and other.revoked_at is null
)`,
),
)
.returning({ id: albumGrants.id });
const changedAt = new Date();
const newStatus = editorial.status === "suspended" ? "active" : editorial.status;
await transaction
.update(editorialAccounts)
.set({
archivedAt: null,
archivedByUserId: null,
status: newStatus,
updatedAt: changedAt,
})
.where(eq(editorialAccounts.id, editorialId));
await transaction.insert(auditEvents).values({
eventType: "editorial_unarchived",
actorUserId: adminUserId,
actorType: "admin",
entityType: "editorial_account",
entityId: editorialId,
previousValues: { status: editorial.status, archived: true },
newValues: { status: newStatus, archived: false, restoredGrants: restored.length },
occurredAt: changedAt,
});
return { restoredGrants: restored.length };
});
},
async hardDeleteEditorial(editorialId: string) { async hardDeleteEditorial(editorialId: string) {
await db.transaction(async (transaction) => { await db.transaction(async (transaction) => {
await lockEditorialForMutation(transaction, editorialId); await lockEditorialForMutation(transaction, editorialId);