diff --git a/src/app/admin/redaktionen/[id]/actions.ts b/src/app/admin/redaktionen/[id]/actions.ts index 0589617..51fd5b9 100644 --- a/src/app/admin/redaktionen/[id]/actions.ts +++ b/src/app/admin/redaktionen/[id]/actions.ts @@ -55,6 +55,16 @@ export async function archiveEditorialAction(editorialId: string): Promise redirect("/admin/redaktionen"); } +export async function unarchiveEditorialAction(editorialId: string): Promise { + 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 { await requireAdmin(new Headers(await headers())); await editorialService.hardDeleteEditorial(editorialId); diff --git a/src/modules/editorials/admin-detail.tsx b/src/modules/editorials/admin-detail.tsx index 2715c16..7e87291 100644 --- a/src/modules/editorials/admin-detail.tsx +++ b/src/modules/editorials/admin-detail.tsx @@ -2,7 +2,7 @@ import Link from "next/link"; import { Card, CardContent, CardHeader, CardTitle, CardDescription, CardFooter } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; 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 { ResetUsageCard } from "./ResetUsageCard"; import { ApproveForm } from "./ApproveForm"; @@ -33,8 +33,27 @@ export function EditorialDetailContent({ {editorial.archivedAt && ( -
- Diese Redaktion wurde am {new Date(editorial.archivedAt).toLocaleDateString('de-DE')} archiviert. +
+
+ Diese Redaktion wurde am {new Date(editorial.archivedAt).toLocaleDateString('de-DE')} archiviert. +
+ + + Archivierung aufheben + + Reaktiviert den Zugang und stellt die zum Archivierungszeitpunkt zugewiesenen + Alben wieder her. Alben mit passender Auto-Regel werden ohnehin automatisch + neu zugeteilt. + + + +
+ +
+
+
)} diff --git a/src/modules/editorials/service.test.ts b/src/modules/editorials/service.test.ts index 169e5d9..a3befc9 100644 --- a/src/modules/editorials/service.test.ts +++ b/src/modules/editorials/service.test.ts @@ -14,6 +14,7 @@ function createRepository(): EditorialServiceRepository & { transitionStatus: ReturnType; claimApprovalNotice: ReturnType; archiveEditorial: ReturnType; + unarchiveEditorial: ReturnType; resetUsageData: ReturnType; } { return { @@ -28,6 +29,7 @@ function createRepository(): EditorialServiceRepository & { transitionStatus: vi.fn().mockResolvedValue(undefined), claimApprovalNotice: vi.fn().mockResolvedValue(true), archiveEditorial: vi.fn().mockResolvedValue(undefined), + unarchiveEditorial: vi.fn().mockResolvedValue({ restoredGrants: 0 }), hardDeleteEditorial: vi.fn().mockResolvedValue(undefined), 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 }); 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", + }); +}); diff --git a/src/modules/editorials/service.ts b/src/modules/editorials/service.ts index 09b6d5e..f8f10a2 100644 --- a/src/modules/editorials/service.ts +++ b/src/modules/editorials/service.ts @@ -57,6 +57,10 @@ export type EditorialServiceRepository = { adminUserId: string; }): Promise; archiveEditorial(input: { editorialId: string; adminUserId: string }): Promise; + unarchiveEditorial(input: { + editorialId: string; + adminUserId: string; + }): Promise<{ restoredGrants: number }>; hardDeleteEditorial(editorialId: string): Promise; resetUsageData(input: { editorialId: string; @@ -85,7 +89,8 @@ export class EditorialServiceError extends Error { | "EMAIL_NOT_VERIFIED" | "ALBUM_GRANT_REQUIRED" | "APPROVAL_NOTICE_DELIVERY_UNKNOWN" - | "INVALID_ACCOUNT_TRANSITION", + | "INVALID_ACCOUNT_TRANSITION" + | "EDITORIAL_NOT_ARCHIVED", ) { super(code); } @@ -168,6 +173,10 @@ export function createEditorialService({ await repository.archiveEditorial(input); }, + async unarchiveEditorial(input: { editorialId: string; adminUserId: string }) { + return repository.unarchiveEditorial(input); + }, + async hardDeleteEditorial(editorialId: string): Promise { 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) { await db.transaction(async (transaction) => { await lockEditorialForMutation(transaction, editorialId);