feat: merge cross-album search, auto-rules, album UI redesign, editorial archiving and pdf download into main
This commit is contained in:
commit
e555069ee1
22 files changed
+1110
-138
No files matched your search
@@ -0,0 +1,121 @@
|
||||
import "server-only";
|
||||
|
||||
import { eq, sql } from "drizzle-orm";
|
||||
import type { NodePgDatabase } from "drizzle-orm/node-postgres";
|
||||
|
||||
import { db } from "@/db/client";
|
||||
import { albumAutoRules } from "@/db/schema";
|
||||
import type * as schema from "@/db/schema";
|
||||
import { albumGrantService } from "./grants";
|
||||
import { immichClient } from "@/modules/immich/client";
|
||||
import type { ImmichClient } from "@/modules/immich/types";
|
||||
|
||||
export type AutoRule = {
|
||||
id: string;
|
||||
editorialAccountId: string;
|
||||
keyword: string;
|
||||
createdAt: Date;
|
||||
createdByUserId: string | null;
|
||||
};
|
||||
|
||||
export type AutoRuleRepository = {
|
||||
listRules(editorialId: string): Promise<AutoRule[]>;
|
||||
createRule(input: { editorialId: string; keyword: string; adminUserId: string }): Promise<void>;
|
||||
deleteRule(input: { ruleId: string; adminUserId: string }): Promise<void>;
|
||||
};
|
||||
|
||||
export function createAutoRuleService({
|
||||
repository,
|
||||
grantService,
|
||||
immich,
|
||||
}: {
|
||||
repository: AutoRuleRepository;
|
||||
grantService: Pick<typeof albumGrantService, "getGrantedAlbumIds" | "grantAlbum">;
|
||||
immich: Pick<ImmichClient, "listAlbums">;
|
||||
}) {
|
||||
return {
|
||||
async listRules(editorialId: string): Promise<AutoRule[]> {
|
||||
return repository.listRules(editorialId);
|
||||
},
|
||||
|
||||
async createRule(input: { editorialId: string; keyword: string; adminUserId: string }): Promise<void> {
|
||||
await repository.createRule(input);
|
||||
},
|
||||
|
||||
async deleteRule(input: { ruleId: string; adminUserId: string }): Promise<void> {
|
||||
await repository.deleteRule(input);
|
||||
},
|
||||
|
||||
async applyRules(editorialId: string, adminUserId: string): Promise<{ granted: number }> {
|
||||
const rules = await repository.listRules(editorialId);
|
||||
if (rules.length === 0) {
|
||||
return { granted: 0 };
|
||||
}
|
||||
|
||||
let albums;
|
||||
try {
|
||||
albums = await immich.listAlbums();
|
||||
} catch (error) {
|
||||
return { granted: 0 };
|
||||
}
|
||||
|
||||
const grantedAlbumIds = new Set(await grantService.getGrantedAlbumIds(editorialId));
|
||||
let newlyGranted = 0;
|
||||
|
||||
for (const album of albums) {
|
||||
if (grantedAlbumIds.has(album.id)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const albumNameLower = album.albumName.toLowerCase();
|
||||
const matchesRule = rules.some((rule) => albumNameLower.includes(rule.keyword.toLowerCase()));
|
||||
|
||||
if (matchesRule) {
|
||||
try {
|
||||
await grantService.grantAlbum({ editorialId, immichAlbumId: album.id, adminUserId });
|
||||
newlyGranted++;
|
||||
grantedAlbumIds.add(album.id);
|
||||
} catch (error) {
|
||||
// Ignore individual album grant failures
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return { granted: newlyGranted };
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export function createAutoRuleRepository(
|
||||
database: NodePgDatabase<typeof schema>,
|
||||
): AutoRuleRepository {
|
||||
return {
|
||||
async listRules(editorialId: string) {
|
||||
return database
|
||||
.select()
|
||||
.from(albumAutoRules)
|
||||
.where(eq(albumAutoRules.editorialAccountId, editorialId));
|
||||
},
|
||||
|
||||
async createRule({ editorialId, keyword, adminUserId }) {
|
||||
await database
|
||||
.insert(albumAutoRules)
|
||||
.values({
|
||||
editorialAccountId: editorialId,
|
||||
keyword,
|
||||
createdByUserId: adminUserId,
|
||||
})
|
||||
.onConflictDoNothing();
|
||||
},
|
||||
|
||||
async deleteRule({ ruleId, adminUserId }) {
|
||||
await database.delete(albumAutoRules).where(eq(albumAutoRules.id, ruleId));
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export const autoRuleService = createAutoRuleService({
|
||||
repository: createAutoRuleRepository(db),
|
||||
grantService: albumGrantService,
|
||||
immich: immichClient,
|
||||
});
|
||||
@@ -47,6 +47,7 @@ export function PhotoContactSheet({
|
||||
downloadOptions,
|
||||
page,
|
||||
query,
|
||||
showAlbumName = false,
|
||||
}: Readonly<{
|
||||
albumId?: string;
|
||||
albumName: string;
|
||||
@@ -54,10 +55,11 @@ export function PhotoContactSheet({
|
||||
downloadOptions?: EditorialDownloadOptions;
|
||||
page: ImmichSearchPage;
|
||||
query: string;
|
||||
showAlbumName?: boolean;
|
||||
}>) {
|
||||
return (
|
||||
<>
|
||||
<form aria-label={`Fotos in ${albumName} durchsuchen`} className="photo-search" role="search">
|
||||
<form action={albumId ? `/redaktion/alben/${encodeURIComponent(albumId)}` : '/redaktion/alben'} aria-label={`Fotos in ${albumName} durchsuchen`} className="photo-search" role="search">
|
||||
<label htmlFor="photo-query">Suchbegriff</label>
|
||||
<div className="photo-search-controls">
|
||||
<input defaultValue={query} id="photo-query" name="suche" type="search" />
|
||||
@@ -85,6 +87,9 @@ export function PhotoContactSheet({
|
||||
/>
|
||||
<figcaption className="contact-strip">
|
||||
<span className="contact-strip-name">{asset.originalFileName}</span>
|
||||
{showAlbumName && (asset as any).albums && (asset as any).albums.length > 0 && (
|
||||
<span className="contact-strip-album">{(asset as any).albums[0].id}</span>
|
||||
)}
|
||||
<span>{formatDate(asset.localDateTime ?? asset.fileCreatedAt)}</span>
|
||||
</figcaption>
|
||||
{albumId && downloadOptions ? (
|
||||
|
||||
@@ -45,7 +45,9 @@ export function createAlbumCatalog({ grants, immich }: AlbumCatalogDependencies)
|
||||
input: SearchVisiblePhotosInput,
|
||||
): Promise<ImmichSearchPage> {
|
||||
const grantedAlbumIds = await currentGrantIds(grants, editorialId);
|
||||
const albumIds = authorizedSearchAlbums(grantedAlbumIds, input.requestedAlbumId);
|
||||
const albumIds = input.requestedAlbumId
|
||||
? authorizedSearchAlbums(grantedAlbumIds, input.requestedAlbumId)
|
||||
: grantedAlbumIds;
|
||||
if (albumIds.length === 0) {
|
||||
return emptySearchPage();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
"use client";
|
||||
|
||||
import { useState, useTransition } from "react";
|
||||
|
||||
import { archiveEditorialAction } from "@/app/admin/redaktionen/[id]/actions";
|
||||
|
||||
type DangerZoneProps = {
|
||||
editorialId: string;
|
||||
editorialName: string;
|
||||
};
|
||||
|
||||
export function DangerZone({ editorialId, editorialName }: DangerZoneProps) {
|
||||
const [confirmText, setConfirmText] = useState("");
|
||||
const [isPending, startTransition] = useTransition();
|
||||
const isConfirmed = confirmText === editorialName;
|
||||
|
||||
function handleArchive() {
|
||||
if (!isConfirmed) return;
|
||||
startTransition(async () => {
|
||||
await archiveEditorialAction(editorialId);
|
||||
});
|
||||
}
|
||||
|
||||
return (
|
||||
<section className="danger-zone" aria-labelledby="danger-title">
|
||||
<h2 id="danger-title">Gefahrenzone</h2>
|
||||
<p>Alle Albumrechte werden widerrufen und der Login wird deaktiviert. Abrechnungsdaten bleiben erhalten.</p>
|
||||
<label>
|
||||
<span>Redaktionsname zur Bestätigung eingeben:</span>
|
||||
<input
|
||||
type="text"
|
||||
value={confirmText}
|
||||
onChange={(e) => setConfirmText(e.target.value)}
|
||||
placeholder={editorialName}
|
||||
className="danger-zone-input"
|
||||
disabled={isPending}
|
||||
/>
|
||||
</label>
|
||||
<button
|
||||
type="button"
|
||||
className="action-button action-button--danger"
|
||||
disabled={!isConfirmed || isPending}
|
||||
onClick={handleArchive}
|
||||
>
|
||||
{isPending ? "Wird archiviert…" : "Redaktion archivieren"}
|
||||
</button>
|
||||
</section>
|
||||
);
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
import Link from "next/link";
|
||||
|
||||
import { ApproveForm } from "@/components/admin/approve-form";
|
||||
import { approveEditorialAction } from "@/app/admin/redaktionen/[id]/actions";
|
||||
import { archiveEditorialAction, suspendEditorialAction, reactivateEditorialAction } from "@/app/admin/redaktionen/[id]/actions";
|
||||
import { DangerZone } from "./DangerZone";
|
||||
|
||||
export type EditorialDetail = {
|
||||
id: string;
|
||||
@@ -11,24 +10,23 @@ export type EditorialDetail = {
|
||||
status: string;
|
||||
emailVerifiedAt: Date | null;
|
||||
currentPriceCents: number | null;
|
||||
archivedAt: Date | null;
|
||||
};
|
||||
|
||||
export function EditorialDetailContent({
|
||||
editorial,
|
||||
hasActiveGrant,
|
||||
}: Readonly<{ editorial: EditorialDetail; hasActiveGrant: boolean }>) {
|
||||
const emailVerified = Boolean(editorial.emailVerifiedAt);
|
||||
const canApprove = hasActiveGrant && emailVerified;
|
||||
const approvalHint = !emailVerified
|
||||
? "Eine Freigabe ist erst möglich, nachdem die E-Mail-Adresse bestätigt wurde."
|
||||
: hasActiveGrant
|
||||
? "Mit der Freigabe wird der Nettopreis festgelegt und die Redaktion kann sich anmelden."
|
||||
: "Eine Freigabe ist erst möglich, wenn mindestens ein aktives Albumrecht zugewiesen wurde.";
|
||||
|
||||
return (
|
||||
<section className="content-section" aria-labelledby="editorial-title">
|
||||
<p className="eyebrow">Redaktion</p>
|
||||
<h1 id="editorial-title">{editorial.name}</h1>
|
||||
|
||||
{editorial.archivedAt && (
|
||||
<p className="danger-banner" role="status">
|
||||
Diese Redaktion wurde am {editorial.archivedAt.toLocaleDateString('de-DE')} archiviert.
|
||||
</p>
|
||||
)}
|
||||
<dl>
|
||||
<dt>Ansprechperson</dt>
|
||||
<dd>{editorial.contactName}</dd>
|
||||
@@ -48,19 +46,44 @@ export function EditorialDetailContent({
|
||||
|
||||
<section aria-labelledby="approval-title">
|
||||
<h2 id="approval-title">Freigabe</h2>
|
||||
{editorial.status === "active" ? (
|
||||
<p>Die Redaktion ist freigegeben (Netto {formatCents(editorial.currentPriceCents ?? 0)}).</p>
|
||||
) : (
|
||||
<>
|
||||
<ApproveForm
|
||||
action={approveEditorialAction.bind(null, editorial.id)}
|
||||
canApprove={canApprove}
|
||||
/>
|
||||
<p>{approvalHint}</p>
|
||||
<Link href={`/admin/redaktionen/${editorial.id}/alben`}>Albumrechte verwalten</Link>
|
||||
</>
|
||||
)}
|
||||
<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>
|
||||
{" · "}
|
||||
<Link href={`/admin/redaktionen/${editorial.id}/auto-regeln`}>Auto-Zuweisungsregeln</Link>
|
||||
</section>
|
||||
|
||||
{/* Status Actions */}
|
||||
{!editorial.archivedAt && (
|
||||
<section aria-labelledby="status-actions-title">
|
||||
<h2 id="status-actions-title">Status-Aktionen</h2>
|
||||
{editorial.status === 'active' && (
|
||||
<form action={suspendEditorialAction.bind(null, editorial.id)}>
|
||||
<button type="submit" className="action-button action-button--warning">
|
||||
Zugang sperren
|
||||
</button>
|
||||
</form>
|
||||
)}
|
||||
{editorial.status === 'suspended' && (
|
||||
<form action={reactivateEditorialAction.bind(null, editorial.id)}>
|
||||
<button type="submit" className="action-button">
|
||||
Zugang reaktivieren
|
||||
</button>
|
||||
</form>
|
||||
)}
|
||||
</section>
|
||||
)}
|
||||
|
||||
{/* Danger Zone */}
|
||||
{!editorial.archivedAt && (
|
||||
<DangerZone editorialId={editorial.id} editorialName={editorial.name} />
|
||||
)}
|
||||
</section>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ function createRepository(): EditorialServiceRepository & {
|
||||
replaceBillingAddress: ReturnType<typeof vi.fn>;
|
||||
transitionStatus: ReturnType<typeof vi.fn>;
|
||||
claimApprovalNotice: ReturnType<typeof vi.fn>;
|
||||
archiveEditorial: ReturnType<typeof vi.fn>;
|
||||
} {
|
||||
return {
|
||||
prepareApproval: vi.fn().mockResolvedValue({
|
||||
@@ -25,6 +26,7 @@ function createRepository(): EditorialServiceRepository & {
|
||||
replaceBillingAddress: vi.fn().mockResolvedValue("address-2"),
|
||||
transitionStatus: vi.fn().mockResolvedValue(undefined),
|
||||
claimApprovalNotice: vi.fn().mockResolvedValue(true),
|
||||
archiveEditorial: vi.fn().mockResolvedValue(undefined),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -50,6 +50,7 @@ export type EditorialServiceRepository = {
|
||||
to: AccountStatus;
|
||||
adminUserId: string;
|
||||
}): Promise<void>;
|
||||
archiveEditorial(input: { editorialId: string; adminUserId: string }): Promise<void>;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -151,6 +152,10 @@ export function createEditorialService({
|
||||
}
|
||||
await repository.transitionStatus(input);
|
||||
},
|
||||
|
||||
async archiveEditorial(input: { editorialId: string; adminUserId: string }): Promise<void> {
|
||||
await repository.archiveEditorial(input);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -416,6 +421,95 @@ export function createEditorialServiceRepository(
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
async archiveEditorial({ editorialId, adminUserId }) {
|
||||
await database.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) {
|
||||
return;
|
||||
}
|
||||
|
||||
const activeGrants = await transaction
|
||||
.select({ id: albumGrants.id, immichAlbumId: albumGrants.immichAlbumId })
|
||||
.from(albumGrants)
|
||||
.where(
|
||||
and(
|
||||
eq(albumGrants.editorialAccountId, editorialId),
|
||||
isNull(albumGrants.revokedAt),
|
||||
),
|
||||
)
|
||||
.for("update");
|
||||
|
||||
const changedAt = new Date();
|
||||
const newStatus = editorial.status === "active" ? "suspended" : editorial.status;
|
||||
|
||||
await transaction
|
||||
.update(editorialAccounts)
|
||||
.set({
|
||||
archivedAt: changedAt,
|
||||
archivedByUserId: adminUserId,
|
||||
status: newStatus,
|
||||
updatedAt: changedAt,
|
||||
})
|
||||
.where(eq(editorialAccounts.id, editorialId));
|
||||
|
||||
if (activeGrants.length > 0) {
|
||||
const grantIds = activeGrants.map((g) => g.id);
|
||||
await transaction
|
||||
.update(albumGrants)
|
||||
.set({ revokedAt: changedAt })
|
||||
.where(inArray(albumGrants.id, grantIds));
|
||||
|
||||
for (const grant of activeGrants) {
|
||||
await transaction.insert(auditEvents).values({
|
||||
eventType: "album_revoked",
|
||||
actorUserId: adminUserId,
|
||||
actorType: "admin",
|
||||
entityType: "album_grant",
|
||||
entityId: grant.immichAlbumId,
|
||||
previousValues: {
|
||||
editorialId: editorialId,
|
||||
immichAlbumId: grant.immichAlbumId,
|
||||
granted: true,
|
||||
},
|
||||
newValues: {
|
||||
editorialId: editorialId,
|
||||
immichAlbumId: grant.immichAlbumId,
|
||||
granted: false,
|
||||
},
|
||||
occurredAt: changedAt,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
await transaction.insert(auditEvents).values({
|
||||
eventType: "editorial_archived",
|
||||
actorUserId: adminUserId,
|
||||
actorType: "admin",
|
||||
entityType: "editorial_account",
|
||||
entityId: editorialId,
|
||||
previousValues: { status: editorial.status, archived: false },
|
||||
newValues: { status: newStatus, archived: true },
|
||||
occurredAt: changedAt,
|
||||
});
|
||||
});
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user