Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Export document serialization utilities

Rafi9f479d113f30
diff --git a/apps/web/lib/api/partners/search/index.ts b/apps/web/lib/api/partners/search/index.ts
index a025445e12..ebe5ce1b5d 100644
--- a/apps/web/lib/api/partners/search/index.ts
+++ b/apps/web/lib/api/partners/search/index.ts
@@ -1,3 +1,4 @@
export * from "./mock-provider";
export * from "./searchable-values";
+export * from "./serialize-document";
export * from "./types";
diff --git a/apps/web/lib/api/partners/search/serialize-document.ts b/apps/web/lib/api/partners/search/serialize-document.ts
new file mode 100644
index 0000000000..e84de21d1a
--- /dev/null
+++ b/apps/web/lib/api/partners/search/serialize-document.ts
@@ -0,0 +1,88 @@
+import { Prisma } from "@prisma/client";
+import { PartnerSearchDocument } from "./types";
+
+export const partnerSearchDocumentSelect = {
+ id: true,
+ programId: true,
+ partnerId: true,
+ status: true,
+ groupId: true,
+ createdAt: true,
+ updatedAt: true,
+ partner: {
+ select: {
+ name: true,
+ email: true,
+ companyName: true,
+ description: true,
+ country: true,
+ updatedAt: true,
+ platforms: {
+ select: {
+ type: true,
+ identifier: true,
+ updatedAt: true,
+ },
+ },
+ },
+ },
+ links: {
+ select: {
+ domain: true,
+ key: true,
+ shortLink: true,
+ url: true,
+ updatedAt: true,
+ },
+ },
+} satisfies Prisma.ProgramEnrollmentSelect;
+
+export type PartnerSearchDocumentSource = Prisma.ProgramEnrollmentGetPayload<{
+ select: typeof partnerSearchDocumentSelect;
+}>;
+
+function unique<T>(values: T[]): T[] {
+ return Array.from(new Set(values));
+}
+
+function getDocumentUpdatedAt(enrollment: PartnerSearchDocumentSource): string {
+ const timestamps = [
+ enrollment.updatedAt,
+ enrollment.partner.updatedAt,
+ ...enrollment.partner.platforms.map(({ updatedAt }) => updatedAt),
+ ...enrollment.links.map(({ updatedAt }) => updatedAt),
+ ];
+
+ return new Date(
+ Math.max(...timestamps.map((timestamp) => timestamp.getTime())),
+ ).toISOString();
+}
+
+export function serializePartnerSearchDocument(
+ enrollment: PartnerSearchDocumentSource,
+): PartnerSearchDocument {
+ const { partner, links } = enrollment;
+
+ return {
+ id: enrollment.id,
+ programId: enrollment.programId,
+ partnerId: enrollment.partnerId,
+ name: partner.name,
+ email: partner.email,
+ companyName: partner.companyName,
+ description: partner.description,
+ platformTypes: unique(partner.platforms.map(({ type }) => type)),
+ platformIdentifiers: unique(
+ partner.platforms.map(({ identifier }) => identifier),
+ ),
+ linkDomains: unique(links.map(({ domain }) => domain)),
+ linkKeys: unique(links.map(({ key }) => key)),
+ shortLinks: unique(links.map(({ shortLink }) => shortLink)),
+ destinationUrls: unique(links.map(({ url }) => url)),
+ status: enrollment.status,
+ groupId: enrollment.groupId,
+ country: partner.country,
+ createdAt: enrollment.createdAt.toISOString(),
+ updatedAt: getDocumentUpdatedAt(enrollment),
+ };
+}
diff --git a/apps/web/tests/partners/partner-search-document.test.ts b/apps/web/tests/partners/partner-search-document.test.ts
new file mode 100644
index 0000000000..087e9ed58e
--- /dev/null
+++ b/apps/web/tests/partners/partner-search-document.test.ts
@@ -0,0 +1,79 @@
+import {
+ PartnerSearchDocumentSource,
+ serializePartnerSearchDocument,
+} from "@/lib/api/partners/search";
+import { describe, expect, it } from "vitest";
+
+const source: PartnerSearchDocumentSource = {
+ id: "pge_test",
+ programId: "prog_test",
+ partnerId: "pn_test",
+ status: "approved",
+ groupId: "grp_test",
+ createdAt: new Date("2026-01-01T00:00:00.000Z"),
+ updatedAt: new Date("2026-01-02T00:00:00.000Z"),
+ partner: {
+ name: "Rafi Hasan",
+ email: "[email protected]",
+ companyName: "Dub Partners",
+ description: "Developer tools educator",
+ country: "CA",
+ updatedAt: new Date("2026-01-03T00:00:00.000Z"),
+ platforms: [
+ {
+ type: "website",
+ identifier: "https://rafi.dev",
+ updatedAt: new Date("2026-01-04T00:00:00.000Z"),
+ },
+ {
+ type: "twitter",
+ identifier: "@rafi-on-x",
+ updatedAt: new Date("2026-01-05T00:00:00.000Z"),
+ },
+ ],
+ },
+ links: [
+ {
+ domain: "dub.sh",
+ key: "rafi",
+ shortLink: "https://dub.sh/rafi",
+ url: "https://example.com/referrals/rafi",
+ updatedAt: new Date("2026-01-06T00:00:00.000Z"),
+ },
+ {
+ domain: "dub.sh",
+ key: "rafi-tools",
+ shortLink: "https://dub.sh/rafi-tools",
+ url: "https://rafi.dev/tools",
+ updatedAt: new Date("2026-01-07T00:00:00.000Z"),
+ },
+ ],
+};
+
+describe("serializePartnerSearchDocument", () => {
+ it("serializes all partner search fields", () => {
+ expect(serializePartnerSearchDocument(source)).toEqual({
+ id: "pge_test",
+ programId: "prog_test",
+ partnerId: "pn_test",
+ name: "Rafi Hasan",
+ email: "[email protected]",
+ companyName: "Dub Partners",
+ description: "Developer tools educator",
+ platformTypes: ["website", "twitter"],
+ platformIdentifiers: ["https://rafi.dev", "@rafi-on-x"],
+ linkDomains: ["dub.sh"],
+ linkKeys: ["rafi", "rafi-tools"],
+ shortLinks: ["https://dub.sh/rafi", "https://dub.sh/rafi-tools"],
+ destinationUrls: [
+ "https://example.com/referrals/rafi",
+ "https://rafi.dev/tools",
+ ],
+ status: "approved",
+ groupId: "grp_test",
+ country: "CA",
+ createdAt: "2026-01-01T00:00:00.000Z",
+ updatedAt: "2026-01-07T00:00:00.000Z",
+ });
+ });
+});