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
@@ -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