The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Refactor program rewards handling in API and database queries
Kiran Kba4e41cdf2bf
diff --git a/apps/web/app/api/og/program/route.tsx b/apps/web/app/api/og/program/route.tsxindex 8082cc3774..def4eb367b 100644--- a/apps/web/app/api/og/program/route.tsx+++ b/apps/web/app/api/og/program/route.tsx@@ -1,4 +1,5 @@import { constructRewardAmount } from "@/lib/api/sales/construct-reward-amount";+import { sortRewardsByEvent } from "@/lib/partners/reorder-top-program-rewards";import { getProgramViaEdge } from "@/lib/planetscale/get-program-via-edge";import { ImageResponse } from "next/og";import { NextRequest } from "next/server";@@ -13,6 +14,7 @@ const DARK_CELLS = [[53, 1],];+// GET - /api/og/program?slug=export async function GET(req: NextRequest) {const [interMedium, interSemibold] = await Promise.all([fetch(new URL("@/styles/Inter-Medium.ttf", import.meta.url)).then((res) =>@@ -25,10 +27,11 @@ export async function GET(req: NextRequest) {const slug = req.nextUrl.searchParams.get("slug");- if (!slug)+ if (!slug) {return new Response("Missing 'slug' parameter", {status: 400,});+ }const program = await getProgramViaEdge({ slug });@@ -41,6 +44,9 @@ export async function GET(req: NextRequest) {const logo = program.wordmark || program.logo;const brandColor = program.brandColor || "#000000";+ const rewards = sortRewardsByEvent(program.rewards);+ const reward = rewards?.length > 0 ? rewards[0] : null;+return new ImageResponse((<div@@ -118,7 +124,7 @@ export async function GET(req: NextRequest) {{`Join the ${program.name} affiliate program`}</div><div tw="mt-10 flex">- {program.rewardAmount && program.rewardType && (+ {reward && (<div tw="w-full flex items-center rounded-md bg-neutral-100 border border-neutral-200 p-8 text-2xl">{/* @ts-ignore */}<InvoiceDollar tw="w-8 h-8 mr-4" />@@ -127,19 +133,15 @@ export async function GET(req: NextRequest) {style={{ fontFamily: "Inter Semibold" }}>{constructRewardAmount({- amount: program.rewardAmount,- type: program.rewardType as any,+ amount: reward.amount,+ type: reward.type,})}{" "}- per {program.rewardEvent}+ per {reward.event}</strong>- {program.rewardMaxDuration === null ? (+ {reward.maxDuration === null ? ("for the customer's lifetime"- ) : program.rewardMaxDuration &&- program.rewardMaxDuration > 1 ? (- <>- , and again every month for {program.rewardMaxDuration}{" "}- months- </>+ ) : reward.maxDuration && reward.maxDuration > 1 ? (+ <>, and again every month for {reward.maxDuration} months</>) : null}</div>)}diff --git a/apps/web/lib/partners/reorder-top-program-rewards.ts b/apps/web/lib/partners/reorder-top-program-rewards.tsindex 865f04604b..d10722e7e5 100644--- a/apps/web/lib/partners/reorder-top-program-rewards.ts+++ b/apps/web/lib/partners/reorder-top-program-rewards.ts@@ -1,5 +1,8 @@import { Reward } from "@prisma/client";+// TODO:+// Need some cleanup here+/*** Gets the top rewards for a program in order (click, lead, sale)*/@@ -8,3 +11,11 @@ export function reorderTopProgramRewards(rewards: Reward[]) {.map((event) => rewards.find((reward) => reward.event === event)).filter((reward) => reward !== undefined) as Reward[];}++export function sortRewardsByEvent<+ T extends Pick<Reward, "event" | "type" | "amount" | "maxDuration">,+>(rewards: T[]) {+ return ["sale", "lead", "click"]+ .map((event) => rewards.find((reward) => reward.event === event))+ .filter((reward) => reward !== undefined) as T[];+}diff --git a/apps/web/lib/planetscale/get-program-via-edge.ts b/apps/web/lib/planetscale/get-program-via-edge.tsindex e51bf356c7..6d61685161 100644--- a/apps/web/lib/planetscale/get-program-via-edge.ts+++ b/apps/web/lib/planetscale/get-program-via-edge.ts@@ -1,21 +1,37 @@import { conn } from "./connection";import { EdgeProgramProps } from "./types";+// Fetch program and its default rewardsexport const getProgramViaEdge = async ({ slug }: { slug: string }) => {- const { rows } =- (await conn.execute(- `SELECT Program.*,- Reward.event AS rewardEvent, Reward.type AS rewardType, Reward.amount AS rewardAmount, Reward.maxDuration as rewardMaxDuration- FROM Program- LEFT JOIN Reward ON Reward.id = Program.defaultRewardId- WHERE slug = ?`,- [slug],- )) || {};+ const { rows } = await conn.execute(+ `SELECT Program.*, Reward.event, Reward.type, Reward.amount, Reward.maxDuration+ FROM Program+ LEFT JOIN Reward ON Reward.programId = Program.id AND Reward.default = 1+ WHERE slug = ?`,+ [slug],+ );- const program =- rows && Array.isArray(rows) && rows.length > 0- ? (rows[0] as EdgeProgramProps)- : null;+ if (!rows || !Array.isArray(rows) || rows.length === 0) {+ return null;+ }- return program;+ const program = rows[0] as EdgeProgramProps;++ const rewards = rows.map((row) => ({+ event: row.event,+ type: row.type,+ amount: row.amount,+ maxDuration: row.maxDuration,+ }));++ return {+ id: program.id,+ name: program.name,+ slug: program.slug,+ logo: program.logo,+ wordmark: program.wordmark,+ brandColor: program.brandColor,+ landerData: program.landerData,+ rewards,+ };};diff --git a/apps/web/lib/planetscale/types.ts b/apps/web/lib/planetscale/types.tsindex b229768dd7..fafc1a1932 100644--- a/apps/web/lib/planetscale/types.ts+++ b/apps/web/lib/planetscale/types.ts@@ -48,9 +48,10 @@ export interface EdgeProgramProps {wordmark: string | null;brandColor: string | null;landerData: any;- defaultRewardId: string | null;- rewardEvent: string | null;- rewardType: string | null;- rewardAmount: number | null;- rewardMaxDuration: number | null;+ rewards: {+ event: "click" | "lead" | "sale";+ type: "percentage" | "flat";+ amount: number;+ maxDuration: number | null;+ }[];}