feat: implement hard delete, email notifications, watermark, and fix bulk download

This commit is contained in:
Leon Tonhäuser committed 2026-08-11 12:28:39 +02:00
1 parent 8dd1e10fea
commit 4667615ed0
15 files changed
+369 -65

No files matched your search

+31 -10
View File
@@ -2,7 +2,7 @@
import { useState, useTransition } from "react";
import { archiveEditorialAction } from "@/app/admin/redaktionen/[id]/actions";
import { archiveEditorialAction, hardDeleteEditorialAction } from "@/app/admin/redaktionen/[id]/actions";
import { Card, CardHeader, CardTitle, CardDescription, CardContent } from "@/components/ui/card";
import { Input } from "@/components/ui/input";
import { Button } from "@/components/ui/button";
@@ -15,6 +15,7 @@ type DangerZoneProps = {
export function DangerZone({ editorialId, editorialName }: DangerZoneProps) {
const [confirmText, setConfirmText] = useState("");
const [isPending, startTransition] = useTransition();
const [isDeleting, setIsDeleting] = useState(false);
const isConfirmed = confirmText === editorialName;
function handleArchive() {
@@ -24,12 +25,21 @@ export function DangerZone({ editorialId, editorialName }: DangerZoneProps) {
});
}
function handleDelete() {
if (!isConfirmed) return;
setIsDeleting(true);
startTransition(async () => {
await hardDeleteEditorialAction(editorialId);
});
}
return (
<Card className="border-destructive/50 bg-destructive/5 dark:bg-destructive/10">
<CardHeader>
<CardTitle className="text-destructive">Gefahrenzone</CardTitle>
<CardDescription>
Alle Albumrechte werden widerrufen und der Login wird deaktiviert. Abrechnungsdaten bleiben erhalten.
Archivieren widerruft alle Albumrechte und deaktiviert den Login.
Endgültiges Löschen entfernt den Account inklusive aller verknüpften Daten unwiderruflich!
</CardDescription>
</CardHeader>
<CardContent className="space-y-4">
@@ -46,14 +56,25 @@ export function DangerZone({ editorialId, editorialName }: DangerZoneProps) {
className="border-destructive/30 focus-visible:ring-destructive"
/>
</div>
<Button
type="button"
variant="destructive"
disabled={!isConfirmed || isPending}
onClick={handleArchive}
>
{isPending ? "Wird archiviert…" : "Redaktion archivieren"}
</Button>
<div className="flex gap-4">
<Button
type="button"
variant="destructive"
disabled={!isConfirmed || isPending}
onClick={handleArchive}
>
{isPending && !isDeleting ? "Wird archiviert…" : "Redaktion archivieren"}
</Button>
<Button
type="button"
variant="outline"
className="border-destructive text-destructive hover:bg-destructive hover:text-white"
disabled={!isConfirmed || isPending}
onClick={handleDelete}
>
{isDeleting ? "Wird gelöscht…" : "Endgültig löschen"}
</Button>
</div>
</CardContent>
</Card>
);
+1
View File
@@ -27,6 +27,7 @@ function createRepository(): EditorialServiceRepository & {
transitionStatus: vi.fn().mockResolvedValue(undefined),
claimApprovalNotice: vi.fn().mockResolvedValue(true),
archiveEditorial: vi.fn().mockResolvedValue(undefined),
hardDeleteEditorial: vi.fn().mockResolvedValue(undefined),
};
}
+42
View File
@@ -9,6 +9,11 @@ import {
auditEvents,
billingAddressVersions,
editorialAccounts,
downloadEvents,
usages,
lightboxItems,
billingReports,
albumAutoRules,
} from "@/db/schema";
import type * as schema from "@/db/schema";
import type { AccountStatus } from "@/modules/auth/principal";
@@ -51,6 +56,7 @@ export type EditorialServiceRepository = {
adminUserId: string;
}): Promise<void>;
archiveEditorial(input: { editorialId: string; adminUserId: string }): Promise<void>;
hardDeleteEditorial(editorialId: string): Promise<void>;
};
/**
@@ -156,6 +162,10 @@ export function createEditorialService({
async archiveEditorial(input: { editorialId: string; adminUserId: string }): Promise<void> {
await repository.archiveEditorial(input);
},
async hardDeleteEditorial(editorialId: string): Promise<void> {
await repository.hardDeleteEditorial(editorialId);
},
};
}
@@ -510,6 +520,38 @@ export function createEditorialServiceRepository(
});
});
},
async hardDeleteEditorial(editorialId: string) {
await db.transaction(async (transaction) => {
await lockEditorialForMutation(transaction, editorialId);
const [editorial] = await transaction
.select({ userId: editorialAccounts.userId })
.from(editorialAccounts)
.where(eq(editorialAccounts.id, editorialId));
if (!editorial) return;
// Cascade deletions manually due to restrict constraints
await transaction.delete(downloadEvents).where(eq(downloadEvents.editorialAccountId, editorialId));
await transaction.delete(usages).where(eq(usages.editorialAccountId, editorialId));
await transaction.delete(lightboxItems).where(eq(lightboxItems.editorialAccountId, editorialId));
await transaction.delete(billingReports).where(eq(billingReports.editorialAccountId, editorialId));
await transaction.delete(albumAutoRules).where(eq(albumAutoRules.editorialAccountId, editorialId));
await transaction.delete(albumGrants).where(eq(albumGrants.editorialAccountId, editorialId));
await transaction.delete(billingAddressVersions).where(eq(billingAddressVersions.editorialAccountId, editorialId));
await transaction.delete(auditEvents).where(and(eq(auditEvents.entityId, editorialId), eq(auditEvents.entityType, "editorial_account")));
await transaction.delete(editorialAccounts).where(eq(editorialAccounts.id, editorialId));
if (editorial.userId) {
const { accounts, sessions, users } = await import("@/db/schema/auth");
await transaction.delete(accounts).where(eq(accounts.userId, editorial.userId));
await transaction.delete(sessions).where(eq(sessions.userId, editorial.userId));
await transaction.delete(users).where(eq(users.id, editorial.userId));
}
});
},
};
}
+41
View File
@@ -12,6 +12,45 @@ const transporter = nodemailer.createTransport({
},
});
function buildHtml(title: string, content: string, button?: { text: string, url: string }) {
return `<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; background-color: #f4f4f5; color: #18181b; line-height: 1.6; padding: 20px; }
.container { max-width: 600px; margin: 0 auto; background: #ffffff; border-radius: 12px; overflow: hidden; box-shadow: 0 4px 6px rgba(0,0,0,0.05); }
.header { padding: 32px 32px 0 32px; text-align: center; }
.header img { max-height: 64px; }
.content { padding: 32px; font-size: 16px; }
.title { font-size: 24px; font-weight: 700; margin-bottom: 24px; text-align: center; }
.button { display: inline-block; background-color: #18181b; color: #ffffff !important; padding: 14px 28px; text-decoration: none; border-radius: 8px; font-weight: 600; margin-top: 32px; }
.footer { padding: 32px; text-align: center; font-size: 14px; color: #71717a; border-top: 1px solid #e4e4e7; background: #fafafa; }
.footer a { color: #71717a; text-decoration: underline; margin: 0 8px; }
</style>
</head>
<body>
<div class="container">
<div class="header">
<img src="${env.BETTER_AUTH_URL}/brand/logo-black.png" alt="Logo" />
</div>
<div class="content">
<div class="title">${title}</div>
<p style="white-space: pre-line; color: #3f3f46; text-align: center;">${content}</p>
${button ? `<div style="text-align: center;"><a href="${button.url}" class="button">${button.text}</a></div>` : ''}
</div>
<div class="footer">
<p>
<a href="${env.LEGAL_IMPRINT_URL}">Impressum</a> |
<a href="${env.LEGAL_PRIVACY_URL}">Datenschutz</a> |
<a href="${env.LEGAL_TERMS_URL}">AGB</a>
</p>
</div>
</div>
</body>
</html>`;
}
export const emailService = {
async sendBillingReport(toEmail: string, reportMonth: string, pdfBytes: Buffer, invoiceNumber: string) {
const filename = invoiceNumber.replace(/[^A-Za-z0-9_-]/g, "_") + ".pdf";
@@ -21,6 +60,7 @@ export const emailService = {
to: toEmail,
subject: `Ihre Abrechnung für ${reportMonth}`,
text: `Hallo,\n\nanbei erhalten Sie Ihre Abrechnung für den Monat ${reportMonth} als PDF-Dokument.\n\nViele Grüße,\nIhr Redaktions-Team`,
html: buildHtml("Ihre Abrechnung ist da", `Hallo,\n\nanbei erhalten Sie Ihre Abrechnung für den Monat **${reportMonth}** bequem als PDF-Dokument angehängt.\n\nViele Grüße,\nIhr Redaktions-Team`),
attachments: [
{
filename,
@@ -37,6 +77,7 @@ export const emailService = {
to: toEmail,
subject: `Neues Fotoalbum verfügbar: ${albumName}`,
text: `Hallo,\n\nein neues Fotoalbum "${albumName}" wurde für Ihre Redaktion freigegeben. Sie können die Bilder ab sofort im Redaktionsportal ansehen und lizenzieren.\n\nViele Grüße,\nIhr Redaktions-Team`,
html: buildHtml("Neues Album verfügbar", `Hallo,\n\nein neues Fotoalbum **"${albumName}"** wurde soeben für Ihre Redaktion freigegeben.\n\nSie können die Bilder ab sofort im Redaktionsportal ansehen und herunterladen.\n\nViele Grüße,\nIhr Redaktions-Team`, { text: "Zum Portal", url: `${env.BETTER_AUTH_URL}/login` }),
});
},
};
+2 -1
View File
@@ -381,7 +381,8 @@ export function createImmichClient({
);
},
getThumbnail(assetId, signal) {
return requestBinary(`/assets/${encodeURIComponent(assetId)}/thumbnail`, signal);
// Request 'preview' size to get a larger image, so we can resize it cleanly to 480px
return requestBinary(`/assets/${encodeURIComponent(assetId)}/thumbnail?size=preview`, signal);
},
getOriginal(assetId, signal) {
return requestBinary(`/assets/${encodeURIComponent(assetId)}/original`, signal);
+56 -6
View File
@@ -9,6 +9,7 @@ export type MailMessage = {
to: string;
subject: string;
text: string;
html?: string;
};
export type MailTransport = {
@@ -30,26 +31,75 @@ export function createMailer({ from, transport }: { from: string; transport: Mai
await transport.sendMail({ from, ...message });
}
function buildHtml(title: string, content: string, button?: { text: string, url: string }) {
return `<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; background-color: #f4f4f5; color: #18181b; line-height: 1.6; padding: 20px; }
.container { max-width: 600px; margin: 0 auto; background: #ffffff; border-radius: 12px; overflow: hidden; box-shadow: 0 4px 6px rgba(0,0,0,0.05); }
.header { padding: 32px 32px 0 32px; text-align: center; }
.header img { max-height: 64px; }
.content { padding: 32px; font-size: 16px; }
.title { font-size: 24px; font-weight: 700; margin-bottom: 24px; text-align: center; }
.button { display: inline-block; background-color: #18181b; color: #ffffff !important; padding: 14px 28px; text-decoration: none; border-radius: 8px; font-weight: 600; margin-top: 32px; }
.footer { padding: 32px; text-align: center; font-size: 14px; color: #71717a; border-top: 1px solid #e4e4e7; background: #fafafa; }
.footer a { color: #71717a; text-decoration: underline; margin: 0 8px; }
</style>
</head>
<body>
<div class="container">
<div class="header">
<img src="${env.BETTER_AUTH_URL}/brand/logo-black.png" alt="Logo" />
</div>
<div class="content">
<div class="title">${title}</div>
<p style="white-space: pre-line; color: #3f3f46; text-align: center;">${content}</p>
${button ? `<div style="text-align: center;"><a href="${button.url}" class="button">${button.text}</a></div>` : ''}
</div>
<div class="footer">
<p>
<a href="${env.LEGAL_IMPRINT_URL}">Impressum</a> |
<a href="${env.LEGAL_PRIVACY_URL}">Datenschutz</a> |
<a href="${env.LEGAL_TERMS_URL}">AGB</a>
</p>
</div>
</div>
</body>
</html>`;
}
return {
async sendVerification({ to, verificationUrl }) {
const subject = "Bitte bestätigen Sie Ihre E-Mail-Adresse";
const text = `Bitte bestätigen Sie Ihre E-Mail-Adresse für das Redaktionsportal:\n\n${verificationUrl}`;
await send({
to,
subject: "Bitte bestätigen Sie Ihre E-Mail-Adresse",
text: `Bitte bestätigen Sie Ihre E-Mail-Adresse für das Redaktionsportal:\n\n${verificationUrl}`,
subject,
text,
html: buildHtml("E-Mail bestätigen", "Bitte bestätigen Sie Ihre E-Mail-Adresse für das Redaktionsportal, um Ihren Account zu aktivieren.", { text: "Jetzt bestätigen", url: verificationUrl }),
});
},
async sendPasswordReset({ to, resetUrl }) {
const subject = "Passwort für das Redaktionsportal zurücksetzen";
const text = `Setzen Sie Ihr Passwort für das Redaktionsportal zurück:\n\n${resetUrl}`;
await send({
to,
subject: "Passwort für das Redaktionsportal zurücksetzen",
text: `Setzen Sie Ihr Passwort für das Redaktionsportal zurück:\n\n${resetUrl}`,
subject,
text,
html: buildHtml("Passwort zurücksetzen", "Wir haben eine Anfrage zum Zurücksetzen Ihres Passworts erhalten. Klicken Sie auf den Button, um ein neues Passwort zu vergeben.", { text: "Passwort zurücksetzen", url: resetUrl }),
});
},
async sendApprovalNotice({ to, editorialName }) {
const subject = "Ihr Zugang zum Redaktionsportal ist freigegeben";
const text = `Der Zugang für ${editorialName} wurde freigegeben. Sie können sich jetzt im Redaktionsportal anmelden.`;
const loginUrl = `${env.BETTER_AUTH_URL}/login`;
await send({
to,
subject: "Ihr Zugang zum Redaktionsportal ist freigegeben",
text: `Der Zugang für ${editorialName} wurde freigegeben. Sie können sich jetzt im Redaktionsportal anmelden.`,
subject,
text,
html: buildHtml("Account freigegeben", `Herzlichen Glückwunsch!\nDer Zugang für **${editorialName}** wurde soeben freigegeben.\n\nSie können sich ab sofort im Portal anmelden und auf freigegebene Bilder zugreifen.`, { text: "Zum Portal", url: loginUrl }),
});
},
};
+12 -9
View File
@@ -57,7 +57,7 @@ export type UsageRepository = {
};
type UsageImmich = {
getAlbum(albumId: string): Promise<{ id: string; albumName: string }>;
getAlbum(albumId: string): Promise<{ id: string; albumName: string; assets: Array<{ id: string }> }>;
getAsset(assetId: string): Promise<{
id: string;
originalFileName: string;
@@ -90,7 +90,7 @@ export function createUsageService({
if (existing) return resolveIntent(existing, fingerprint, "new_usage");
const asset = await getValidatedAsset(immich, input.assetId);
const album = await loadRequestedAlbum(immich, asset, input.albumId);
const album = await loadRequestedAlbum(immich, input.assetId, input.albumId);
const persisted = await repository.createBillableIntent({
...input,
albumName: normalizeUpstreamSnapshot(album.albumName),
@@ -98,7 +98,7 @@ export function createUsageService({
requestFingerprint: fingerprint,
}, async () => {
const currentAsset = await getValidatedAsset(immich, input.assetId);
const currentAlbum = await loadRequestedAlbum(immich, currentAsset, input.albumId);
const currentAlbum = await loadRequestedAlbum(immich, input.assetId, input.albumId);
return {
albumName: normalizeUpstreamSnapshot(currentAlbum.albumName),
filename: normalizeUpstreamSnapshot(currentAsset.originalFileName),
@@ -330,18 +330,21 @@ function normalizeNewUsageInput(input: CreateNewUsageInput): CreateNewUsageInput
async function loadRequestedAlbum(
immich: UsageImmich,
asset: { albums: Array<{ id: string }> },
assetId: string,
albumId: string,
): Promise<{ id: string; albumName: string }> {
if (!asset.albums.some((a) => a.id === albumId)) {
throw new UsageAccountingError("ASSET_NOT_IN_ALBUM");
}
let album;
try {
return await immich.getAlbum(albumId);
album = await immich.getAlbum(albumId);
} catch {
throw new UsageAccountingError("IMMICH_UNAVAILABLE");
}
if (!album.assets.some((a) => a.id === assetId)) {
throw new UsageAccountingError("ASSET_NOT_IN_ALBUM");
}
return album;
}
async function getValidatedAsset(immich: UsageImmich, assetId: string) {