From 94c143e17d48980211a4b14e53552a59f61df396 Mon Sep 17 00:00:00 2001 From: neodk2004 Date: Fri, 2 Oct 2026 20:39:40 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20Download-Weiterleitung=20/app=20f=C3=BC?= =?UTF-8?q?r=20Flyer=20und=20QR-Codes=20(D3)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Android → Google Play, UTM-Werte als Play-referrer (Play Console) - iPhone → App Store, sobald APP_STORE_URL gesetzt ist (D5); bis dahin Web - sonst → Web-Startseite; ?ref= führt auf die Einladungsseite /r/ - nur sichere UTM-Werte werden übernommen, nie gecacht (302, no-store) - Zählung pro Tag/Quelle/Gerät in Redis, Anzeige im Admin unter Empfehlungen → Übersicht (30 Tage) Co-Authored-By: Claude Opus 5.5 --- src/__tests__/app-redirect.test.ts | 94 +++++++++++++++++++ src/app/app/route.ts | 29 ++++++ .../admin/referrals/OverviewTab.tsx | 34 +++++++ src/lib/app-links.ts | 7 ++ src/lib/app-redirect-stats.ts | 49 ++++++++++ src/lib/app-redirect.ts | 74 +++++++++++++++ src/proxy.ts | 1 + src/trpc/routers/referral-admin.ts | 5 + 8 files changed, 293 insertions(+) create mode 100644 src/__tests__/app-redirect.test.ts create mode 100644 src/app/app/route.ts create mode 100644 src/lib/app-redirect-stats.ts create mode 100644 src/lib/app-redirect.ts diff --git a/src/__tests__/app-redirect.test.ts b/src/__tests__/app-redirect.test.ts new file mode 100644 index 0000000..c2094c4 --- /dev/null +++ b/src/__tests__/app-redirect.test.ts @@ -0,0 +1,94 @@ +/** + * /app — Download-Weiterleitung (src/lib/app-redirect.ts, app-redirect-stats.ts). + */ + +import { describe, it, expect, vi, beforeEach } from "vitest"; + +vi.mock("server-only", () => ({})); +const redisStore = new Map>(); +vi.mock("@/lib/redis", () => ({ + redis: { + hincrby: vi.fn(async (key: string, field: string, by: number) => { + const hash = redisStore.get(key) ?? new Map(); + hash.set(field, (hash.get(field) ?? 0) + by); + redisStore.set(key, hash); + return hash.get(field); + }), + expire: vi.fn(async () => 1), + hgetall: vi.fn(async (key: string) => Object.fromEntries(redisStore.get(key) ?? new Map())), + }, +})); + +import { PLAY_STORE_URL } from "@/lib/app-links"; +import { detectPlatform, resolveAppRedirect } from "@/lib/app-redirect"; +import { countAppRedirect, getAppRedirectStats } from "@/lib/app-redirect-stats"; + +const ORIGIN = "https://pawfeed.org"; +const ANDROID = "Mozilla/5.0 (Linux; Android 14; Pixel 8) AppleWebKit/537.36 Chrome/128 Mobile Safari/537.36"; +const IPHONE = "Mozilla/5.0 (iPhone; CPU iPhone OS 18_0 like Mac OS X) AppleWebKit/605.1.15 Version/18.0 Mobile/15E148 Safari/604.1"; +const DESKTOP = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/128 Safari/537.36"; + +const params = (query: string) => new URLSearchParams(query); + +describe("detectPlatform", () => { + it("recognises Android, iPhone and everything else", () => { + expect(detectPlatform(ANDROID)).toBe("android"); + expect(detectPlatform(IPHONE)).toBe("ios"); + expect(detectPlatform(DESKTOP)).toBe("other"); + expect(detectPlatform(null)).toBe("other"); + }); +}); + +describe("resolveAppRedirect", () => { + it("sends Android to Google Play with the UTM values as Play referrer", () => { + const result = resolveAppRedirect(ANDROID, params("utm_source=flyer&utm_campaign=tierheim-xy"), ORIGIN); + expect(result.platform).toBe("android"); + expect(result.source).toBe("flyer"); + expect(result.location.startsWith(PLAY_STORE_URL)).toBe(true); + const referrer = new URL(result.location).searchParams.get("referrer"); + expect(referrer).toBe("utm_source=flyer&utm_campaign=tierheim-xy"); + }); + + it("sends iPhones to the web start page while there is no App Store URL", () => { + const result = resolveAppRedirect(IPHONE, params("utm_source=sticker"), ORIGIN, null); + expect(result.location).toBe("https://pawfeed.org/?utm_source=sticker"); + }); + + it("sends iPhones to the App Store once the URL is configured", () => { + const store = "https://apps.apple.com/app/id123"; + expect(resolveAppRedirect(IPHONE, params(""), ORIGIN, store).location).toBe(store); + }); + + it("turns ?ref= into the invite page on the web", () => { + const result = resolveAppRedirect(DESKTOP, params("ref=bubi-7k3m&utm_source=qr"), ORIGIN, null); + expect(result.location).toBe("https://pawfeed.org/r/BUBI-7K3M?utm_source=qr"); + }); + + it("drops unsafe UTM values and invalid codes instead of passing them on", () => { + const result = resolveAppRedirect(DESKTOP, params("utm_source=