Die sechs noch offenen Typen haben kein fremdes Ziel, auf das man weiterleiten koennte — sie brauchen eine Seite. Dafuer bekommt der Typ-Vertrag `hasLandingPage`: /r/[slug] schickt solche Codes auf /s/[slug], statt `redirectTarget` zu befragen. Die oeffentlichen Seiten duerfen die Navigation der Anwendung nicht zeigen — wer einen Code scannt, hat mit „Verwaltung" nichts zu tun. Deshalb liegt das App-Chrome jetzt in der Routengruppe (app); das Wurzel-Layout traegt nur noch html und body. Die URLs aendern sich dadurch nicht.
89 lines
2.7 KiB
TypeScript
89 lines
2.7 KiB
TypeScript
import "server-only";
|
|
|
|
import type { Prisma, QrCode } from "@/generated/prisma/client";
|
|
import type { QrCodeDto } from "@/lib/dto";
|
|
import { landingUrl, shortUrl } from "@/lib/env";
|
|
import type { EcLevel } from "@/lib/qr/design";
|
|
import { getQrType } from "@/lib/qr/registry";
|
|
import type { QrTypeId } from "@/lib/qr/type-module";
|
|
|
|
export type { QrCodeDto };
|
|
|
|
/**
|
|
* Die Typ-Module liefern validierte, JSON-serialisierbare Objekte. Prisma
|
|
* kann das aus `Record<string, unknown>` nicht ableiten, deshalb hier die
|
|
* einzige Stelle mit einer expliziten Umdeutung.
|
|
*/
|
|
export function toJsonInput(
|
|
data: Record<string, unknown>,
|
|
): Prisma.InputJsonValue {
|
|
return data as Prisma.InputJsonValue;
|
|
}
|
|
|
|
function asRecord(value: unknown): Record<string, unknown> {
|
|
return value !== null && typeof value === "object" && !Array.isArray(value)
|
|
? (value as Record<string, unknown>)
|
|
: {};
|
|
}
|
|
|
|
/** Ein Code samt der Beziehungen, die für die Anzeige gebraucht werden. */
|
|
export type QrCodeWithRelations = QrCode & {
|
|
group?: { name: string } | null;
|
|
domain?: { hostname: string } | null;
|
|
createdBy?: { name: string | null; email: string } | null;
|
|
};
|
|
|
|
export function toQrCodeDto(code: QrCodeWithRelations): QrCodeDto {
|
|
const data = asRecord(code.data);
|
|
const qrType = getQrType(code.type);
|
|
|
|
let target = "";
|
|
let summary = "";
|
|
if (qrType) {
|
|
try {
|
|
// Typen mit eigener Landingpage führen auf eine Seite dieser Anwendung.
|
|
// Die ist als „Ziel“ das Ehrlichste — und obendrein anklickbar, um sie
|
|
// sich anzusehen.
|
|
target = qrType.hasLandingPage
|
|
? landingUrl(code.slug, code.domain?.hostname)
|
|
: qrType.redirectTarget(data);
|
|
summary = qrType.summary(data);
|
|
} catch {
|
|
// Datensatz passt nicht mehr zum Typ-Schema (z. B. nach einem
|
|
// Schema-Wechsel). Die Liste soll trotzdem anzeigbar bleiben.
|
|
summary = "Ungültige Daten";
|
|
}
|
|
} else {
|
|
summary = "Unbekannter Typ";
|
|
}
|
|
|
|
return {
|
|
id: code.id,
|
|
slug: code.slug,
|
|
type: code.type as QrTypeId,
|
|
label: code.label,
|
|
data,
|
|
shortUrl: shortUrl(code.slug, code.domain?.hostname),
|
|
target,
|
|
summary,
|
|
design: {
|
|
fgColor: code.fgColor,
|
|
bgColor: code.bgColor,
|
|
size: code.size,
|
|
ecLevel: code.ecLevel as EcLevel,
|
|
margin: code.margin,
|
|
},
|
|
scans: code.scans,
|
|
lastScanAt: code.lastScanAt?.toISOString() ?? null,
|
|
createdAt: code.createdAt.toISOString(),
|
|
updatedAt: code.updatedAt.toISOString(),
|
|
groupId: code.groupId,
|
|
groupName: code.group?.name ?? null,
|
|
domainId: code.domainId,
|
|
domainHostname: code.domain?.hostname ?? null,
|
|
createdByName: code.createdBy
|
|
? (code.createdBy.name?.trim() || code.createdBy.email)
|
|
: null,
|
|
};
|
|
}
|