The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Use Map lookups and narrow selects in the grouped earnings counts
Rafi8a27f702470d
diff --git a/apps/web/lib/api/partner-profile/get-partner-earnings-count.ts b/apps/web/lib/api/partner-profile/get-partner-earnings-count.tsindex bfad848393..6b47c6f24f 100644--- a/apps/web/lib/api/partner-profile/get-partner-earnings-count.ts+++ b/apps/web/lib/api/partner-profile/get-partner-earnings-count.ts@@ -93,6 +93,11 @@ export async function getPartnerEarningsCount({.filter((id): id is string => id !== null),},},+ select: {+ id: true,+ email: true,+ name: true,+ },}),prisma.programEnrollment.findMany({where: {@@ -108,11 +113,18 @@ export async function getPartnerEarningsCount({}),]);+ const customersById = new Map(customers.map((c) => [c.id, c]));+ const dataSharingByProgramId = new Map(+ programEnrollments.map((p) => [+ p.programId,+ p.customerDataSharingEnabledAt,+ ]),+ );+return counts.map(({ customerId, programId, _count }) => {- const customer = customers.find((c) => c.id === customerId);- const customerDataSharingEnabledAt = programEnrollments.find(- (p) => p.programId === programId,- )?.customerDataSharingEnabledAt;+ const customer = customerId ? customersById.get(customerId) : undefined;+ const customerDataSharingEnabledAt =+ dataSharingByProgramId.get(programId);return {id: customerId,@@ -146,10 +158,18 @@ export async function getPartnerEarningsCount({.filter((id): id is string => id !== null),},},+ select: {+ id: true,+ domain: true,+ key: true,+ url: true,+ },});+ const linksById = new Map(links.map((l) => [l.id, l]));+return counts.map(({ linkId, _count }) => {- const link = links.find((l) => l.id === linkId);+ const link = linkId ? linksById.get(linkId) : undefined;return {id: linkId,domain: link?.domain,diff --git a/apps/web/playwright/api/partner-profile/earnings.spec.ts b/apps/web/playwright/api/partner-profile/earnings.spec.tsindex 1e9679ccbc..7739f032d1 100644--- a/apps/web/playwright/api/partner-profile/earnings.spec.ts+++ b/apps/web/playwright/api/partner-profile/earnings.spec.ts@@ -140,6 +140,21 @@ test.describe("GET /partner-profile/earnings", () => {});});+ test("counts earnings grouped by link and by customer", async ({+ playwright,+ }) => {+ await withPartnerApi(playwright, async (partnerApi) => {+ for (const groupBy of ["linkId", "customerId"]) {+ const { status, data } = await partnerApi.get<+ { id: string | null; _count: number }[]+ >(`/api/partner-profile/earnings/count?groupBy=${groupBy}`);++ expect(status).toEqual(200);+ expect(data.reduce((sum, group) => sum + group._count, 0)).toEqual(2);+ }+ });+ });+test("filters by program ID or slug", async ({ playwright, program }) => {await withPartnerApi(playwright, async (partnerApi) => {for (const programIdOrSlug of [program.id, programSlug]) {