Skip to content

Commit 1389485

Browse files
committed
add ReferralsEmbedLinkSchema
1 parent 5da573e commit 1389485

File tree

12 files changed

+41
-33
lines changed

12 files changed

+41
-33
lines changed

apps/web/app/api/embed/referrals/links/[linkId]/route.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { DubApiError, ErrorCodes } from "@/lib/api/errors";
22
import { processLink, updateLink } from "@/lib/api/links";
33
import { parseRequestBody } from "@/lib/api/utils";
44
import { withReferralsEmbedToken } from "@/lib/embed/referrals/auth";
5-
import { LinkSchema } from "@/lib/zod/schemas/links";
65
import { createPartnerLinkSchema } from "@/lib/zod/schemas/partners";
6+
import { ReferralsEmbedLinkSchema } from "@/lib/zod/schemas/referrals-embed";
77
import { getApexDomain } from "@dub/utils";
88
import { NextResponse } from "next/server";
99

@@ -86,12 +86,7 @@ export const PATCH = withReferralsEmbedToken(
8686
updatedLink: processedLink,
8787
});
8888

89-
const updatedLink = LinkSchema.pick({
90-
id: true,
91-
domain: true,
92-
key: true,
93-
url: true,
94-
}).parse(partnerLink);
89+
const updatedLink = ReferralsEmbedLinkSchema.parse(partnerLink);
9590

9691
return NextResponse.json(updatedLink);
9792
},

apps/web/app/api/embed/referrals/links/route.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
import { DubApiError, ErrorCodes } from "@/lib/api/errors";
22
import { createLink, processLink } from "@/lib/api/links";
3+
import { PARTNER_LINKS_LIMIT } from "@/lib/api/partners/constants";
34
import { parseRequestBody } from "@/lib/api/utils";
45
import { withReferralsEmbedToken } from "@/lib/embed/referrals/auth";
5-
import { LinkSchema } from "@/lib/zod/schemas/links";
66
import { createPartnerLinkSchema } from "@/lib/zod/schemas/partners";
7+
import { ReferralsEmbedLinkSchema } from "@/lib/zod/schemas/referrals-embed";
78
import { getApexDomain } from "@dub/utils";
89
import { NextResponse } from "next/server";
910

10-
// TODO: Move this to a constant
11-
const PARTNER_LINKS_LIMIT = 5;
12-
1311
// POST /api/embed/referrals/links – create links for a partner
1412
export const POST = withReferralsEmbedToken(
1513
async ({ req, programEnrollment, program, links }) => {
@@ -78,13 +76,7 @@ export const POST = withReferralsEmbedToken(
7876
}
7977

8078
const partnerLink = await createLink(link);
81-
82-
const createdLink = LinkSchema.pick({
83-
id: true,
84-
domain: true,
85-
key: true,
86-
url: true,
87-
}).parse(partnerLink);
79+
const createdLink = ReferralsEmbedLinkSchema.parse(partnerLink);
8880

8981
return NextResponse.json(createdLink, { status: 201 });
9082
},

apps/web/app/api/partner-profile/programs/[programId]/links/route.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { DubApiError, ErrorCodes } from "@/lib/api/errors";
22
import { createLink, processLink } from "@/lib/api/links";
3+
import { PARTNER_LINKS_LIMIT } from "@/lib/api/partners/constants";
34
import { getProgramEnrollmentOrThrow } from "@/lib/api/programs/get-program-enrollment-or-throw";
45
import { parseRequestBody } from "@/lib/api/utils";
56
import { withPartnerProfile } from "@/lib/auth/partner";
@@ -8,8 +9,6 @@ import { createPartnerLinkSchema } from "@/lib/zod/schemas/partners";
89
import { getApexDomain } from "@dub/utils";
910
import { NextResponse } from "next/server";
1011

11-
const PARTNER_LINKS_LIMIT = 5;
12-
1312
// GET /api/partner-profile/programs/[programId]/links - get a partner's links in a program
1413
export const GET = withPartnerProfile(async ({ partner, params }) => {
1514
const { links } = await getProgramEnrollmentOrThrow({

apps/web/app/app.dub.co/embed/referrals/add-edit-link.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { mutateSuffix } from "@/lib/swr/mutate";
22
import { Lock } from "@/ui/shared/icons";
3-
import { Link } from "@dub/prisma/client";
43
import {
54
Button,
65
InfoTooltip,
@@ -12,11 +11,12 @@ import { cn, linkConstructor, TAB_ITEM_ANIMATION_SETTINGS } from "@dub/utils";
1211
import { motion } from "framer-motion";
1312
import { useMemo, useState } from "react";
1413
import { useForm } from "react-hook-form";
14+
import { ReferralsEmbedLink } from "./types";
1515

1616
interface Props {
1717
destinationDomain: string;
1818
shortLinkDomain: string;
19-
link?: Link | null;
19+
link?: ReferralsEmbedLink | null;
2020
onCancel: () => void;
2121
}
2222

apps/web/app/app.dub.co/embed/referrals/links-list.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { AnimatedEmptyState } from "@/ui/shared/animated-empty-state";
2-
import { Link } from "@dub/prisma/client";
32
import { Button, CopyButton, Table, Users, useTable } from "@dub/ui";
43
import { Pen2, Plus2 } from "@dub/ui/icons";
54
import { getPrettyUrl, TAB_ITEM_ANIMATION_SETTINGS } from "@dub/utils";
65
import { motion } from "framer-motion";
6+
import { ReferralsEmbedLink } from "./types";
77

88
interface Props {
9-
links: Link[];
9+
links: ReferralsEmbedLink[];
1010
onCreateLink: () => void;
11-
onEditLink: (link: Link) => void;
11+
onEditLink: (link: ReferralsEmbedLink) => void;
1212
}
1313

1414
export function ReferralsEmbedLinksList({

apps/web/app/app.dub.co/embed/referrals/links.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { Link } from "@dub/prisma/client";
21
import { useState } from "react";
32
import { ReferralsEmbedCreateUpdateLink } from "./add-edit-link";
43
import { ReferralsEmbedLinksList } from "./links-list";
4+
import { ReferralsEmbedLink } from "./types";
55

66
interface Props {
7-
links: Link[];
7+
links: ReferralsEmbedLink[];
88
destinationDomain: string;
99
shortLinkDomain: string;
1010
}
@@ -15,7 +15,7 @@ export default function ReferralsEmbedLinks({
1515
shortLinkDomain,
1616
}: Props) {
1717
const [createLink, setCreateLink] = useState(false);
18-
const [link, setLink] = useState<Link | null>(null);
18+
const [link, setLink] = useState<ReferralsEmbedLink | null>(null);
1919

2020
return (
2121
<div className="flex flex-col space-y-6">

apps/web/app/app.dub.co/embed/referrals/page-client.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { programResourcesSchema } from "@/lib/zod/schemas/program-resources";
66
import { HeroBackground } from "@/ui/partners/hero-background";
77
import { ProgramRewardList } from "@/ui/partners/program-reward-list";
88
import { ThreeDots } from "@/ui/shared/icons";
9-
import { Link, PayoutStatus, Program } from "@dub/prisma/client";
9+
import { PayoutStatus, Program } from "@dub/prisma/client";
1010
import {
1111
Button,
1212
Check,
@@ -31,6 +31,7 @@ import { ReferralsEmbedQuickstart } from "./quickstart";
3131
import { ReferralsEmbedResources } from "./resources";
3232
import { ThemeOptions } from "./theme-options";
3333
import { ReferralsReferralsEmbedToken } from "./token";
34+
import { ReferralsEmbedLink } from "./types";
3435

3536
export function ReferralsEmbedPageClient({
3637
program,
@@ -42,7 +43,7 @@ export function ReferralsEmbedPageClient({
4243
themeOptions,
4344
}: {
4445
program: Program;
45-
links: Link[];
46+
links: ReferralsEmbedLink[];
4647
rewards: RewardProps[];
4748
discount?: DiscountProps | null;
4849
payouts: {

apps/web/app/app.dub.co/embed/referrals/quickstart.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Link, Program } from "@dub/prisma/client";
1+
import { Program } from "@dub/prisma/client";
22
import {
33
Button,
44
Carousel,
@@ -12,6 +12,7 @@ import {
1212
} from "@dub/ui";
1313
import { cn, DUB_LOGO, TAB_ITEM_ANIMATION_SETTINGS } from "@dub/utils";
1414
import { motion } from "framer-motion";
15+
import { ReferralsEmbedLink } from "./types";
1516

1617
const BUTTON_CLASSNAME = "h-9 rounded-lg bg-bg-inverted hover:bg-neutral-800";
1718

@@ -21,7 +22,7 @@ export function ReferralsEmbedQuickstart({
2122
onViewResources,
2223
}: {
2324
program: Program;
24-
link: Link;
25+
link: ReferralsEmbedLink;
2526
onViewResources?: () => void;
2627
}) {
2728
const [copied, copyToClipboard] = useCopyToClipboard();
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { ReferralsEmbedLinkSchema } from "@/lib/zod/schemas/referrals-embed";
2+
import { z } from "zod";
3+
4+
export type ReferralsEmbedLink = z.infer<typeof ReferralsEmbedLinkSchema>;

apps/web/app/app.dub.co/embed/referrals/utils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ import { referralsEmbedToken } from "@/lib/embed/referrals/token-class";
22
import { determinePartnerDiscount } from "@/lib/partners/determine-partner-discount";
33
import { determinePartnerRewards } from "@/lib/partners/determine-partner-rewards";
44
import { RewardProps } from "@/lib/types";
5+
import { ReferralsEmbedLinkSchema } from "@/lib/zod/schemas/referrals-embed";
56
import { prisma } from "@dub/prisma";
67
import { notFound } from "next/navigation";
8+
import { z } from "zod";
79

810
export const getReferralsEmbedData = async (token: string) => {
911
const { programId, partnerId } = (await referralsEmbedToken.get(token)) ?? {};
@@ -56,7 +58,7 @@ export const getReferralsEmbedData = async (token: string) => {
5658

5759
return {
5860
program,
59-
links,
61+
links: z.array(ReferralsEmbedLinkSchema).parse(links),
6062
rewards: rewards as RewardProps[],
6163
discount,
6264
payouts: payouts.map((payout) => ({
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const PARTNER_LINKS_LIMIT = 5;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { LinkSchema } from "./links";
2+
3+
export const ReferralsEmbedLinkSchema = LinkSchema.pick({
4+
id: true,
5+
domain: true,
6+
key: true,
7+
url: true,
8+
shortLink: true,
9+
clicks: true,
10+
leads: true,
11+
sales: true,
12+
saleAmount: true,
13+
});

0 commit comments

Comments
 (0)