Просмотр исходного кода

RHL-046 feat(support-mailto): implement buildSupportMailto function and corresponding tests

Code_Uwe 1 месяц назад
Родитель
Сommit
00e82ca868
2 измененных файлов с 117 добавлено и 0 удалено
  1. 64 0
      lib/frontend/support/supportMailto.js
  2. 53 0
      lib/frontend/support/supportMailto.test.js

+ 64 - 0
lib/frontend/support/supportMailto.js

@@ -0,0 +1,64 @@
+function formatRole(role) {
+	if (role === "branch") return "Niederlassung";
+	if (role === "admin") return "Admin";
+	if (role === "superadmin") return "Superadmin";
+	if (role === "dev") return "Entwicklung";
+	return role ? String(role) : "Unbekannt";
+}
+
+export function buildSupportMailto({ user, currentUrl, pathname, userAgent }) {
+	const to = "info@attus.de";
+
+	const roleLabel = user ? formatRole(user.role) : "Unbekannt";
+	const userLabel = user?.branchId
+		? `${roleLabel} (${user.branchId})`
+		: roleLabel;
+
+	const now = new Date();
+	const tz =
+		typeof Intl !== "undefined"
+			? Intl.DateTimeFormat().resolvedOptions().timeZone
+			: "";
+
+	const timestampLocal = now.toLocaleString("de-DE");
+	const timestampIso = now.toISOString();
+
+	const routeLine = pathname ? `Route: ${pathname}` : "Route: (unbekannt)";
+	const urlLine = currentUrl ? `URL: ${currentUrl}` : "URL: (bitte einfügen)";
+	const uaLine = userAgent
+		? `User-Agent: ${userAgent}`
+		: "User-Agent: (unbekannt)";
+	const timeLine = tz
+		? `Zeitpunkt: ${timestampLocal} (${tz})`
+		: `Zeitpunkt: ${timestampLocal}`;
+	const isoLine = `ISO: ${timestampIso}`;
+
+	const subject = user?.branchId
+		? `Support – RHL Lieferscheine (${user.branchId})`
+		: "Support – RHL Lieferscheine";
+
+	const body = [
+		"Hallo attus Support,",
+		"",
+		"bitte beschreibt hier kurz das Anliegen:",
+		"",
+		"- Was wollten Sie tun?",
+		"- Was ist passiert?",
+		"- (Optional) Screenshot / Zeitpunkt",
+		"",
+		"--- Kontext (bitte drin lassen) ---",
+		`Benutzer: ${userLabel}`,
+		routeLine,
+		urlLine,
+		timeLine,
+		isoLine,
+		uaLine,
+		"----------------------------------",
+		"",
+		"Vielen Dank.",
+	].join("\r\n");
+
+	return `mailto:${to}?subject=${encodeURIComponent(subject)}&body=${encodeURIComponent(
+		body,
+	)}`;
+}

+ 53 - 0
lib/frontend/support/supportMailto.test.js

@@ -0,0 +1,53 @@
+/* @vitest-environment node */
+
+import { describe, it, expect } from "vitest";
+import { buildSupportMailto } from "./supportMailto.js";
+
+function decodeMailto(mailto) {
+	const url = new URL(mailto);
+	return {
+		to: url.pathname,
+		subject: url.searchParams.get("subject") || "",
+		body: url.searchParams.get("body") || "",
+	};
+}
+
+describe("lib/frontend/support/supportMailto", () => {
+	it("builds a branch-specific subject and includes context lines", () => {
+		const mailto = buildSupportMailto({
+			user: { role: "branch", branchId: "NL20" },
+			pathname: "/profile",
+			currentUrl: "https://example.local/profile",
+			userAgent: "Vitest-UA",
+		});
+
+		const parsed = decodeMailto(mailto);
+
+		expect(parsed.to).toBe("info@attus.de");
+		expect(parsed.subject).toBe("Support – RHL Lieferscheine (NL20)");
+		expect(parsed.body).toContain("Benutzer: Niederlassung (NL20)");
+		expect(parsed.body).toContain("Route: /profile");
+		expect(parsed.body).toContain("URL: https://example.local/profile");
+		expect(parsed.body).toContain("User-Agent: Vitest-UA");
+		expect(parsed.body).toContain("Zeitpunkt:");
+		expect(parsed.body).toContain("ISO:");
+	});
+
+	it("builds a generic subject without branch", () => {
+		const mailto = buildSupportMailto({
+			user: { role: "admin", branchId: null },
+			pathname: "/",
+			currentUrl: "",
+			userAgent: "",
+		});
+
+		const parsed = decodeMailto(mailto);
+
+		expect(parsed.to).toBe("info@attus.de");
+		expect(parsed.subject).toBe("Support – RHL Lieferscheine");
+		expect(parsed.body).toContain("Benutzer: Admin");
+		expect(parsed.body).toContain("Route: /");
+		expect(parsed.body).toContain("URL: (bitte einfügen)");
+		expect(parsed.body).toContain("User-Agent: (unbekannt)");
+	});
+});