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:
1 parent
c9d3f3d1dc
commit
9308b46d84
4 files changed
+135
-4
No files matched your search
@@ -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({
|
||||
</div>
|
||||
|
||||
{editorial.archivedAt && (
|
||||
<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 className="space-y-4">
|
||||
<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 & reaktivieren
|
||||
</Button>
|
||||
</form>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
)}
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ function createRepository(): EditorialServiceRepository & {
|
||||
transitionStatus: ReturnType<typeof vi.fn>;
|
||||
claimApprovalNotice: ReturnType<typeof vi.fn>;
|
||||
archiveEditorial: ReturnType<typeof vi.fn>;
|
||||
unarchiveEditorial: ReturnType<typeof vi.fn>;
|
||||
resetUsageData: ReturnType<typeof vi.fn>;
|
||||
} {
|
||||
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",
|
||||
});
|
||||
});
|
||||
@@ -57,6 +57,10 @@ export type EditorialServiceRepository = {
|
||||
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>;
|
||||
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<void> {
|
||||
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);
|
||||
|
||||
Reference in new issue
Block a user