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.

reuse invitePartner

Kiran K81f4da7adf2e
diff --git a/apps/web/app/api/referrals/invite/route.ts b/apps/web/app/api/referrals/invite/route.ts
index 2c48617913..3f9a160bab 100644
--- a/apps/web/app/api/referrals/invite/route.ts
+++ b/apps/web/app/api/referrals/invite/route.ts
@@ -1,11 +1,8 @@
-import { createId, parseRequestBody } from "@/lib/api/utils";
+import { invitePartner } from "@/lib/api/partners/invite-partner";
+import { parseRequestBody } from "@/lib/api/utils";
import { requestPartnerInviteSchema } from "@/lib/dots/schemas";
-import { updateConfig } from "@/lib/edge-config";
import { prisma } from "@/lib/prisma";
import { withAuth } from "@/lib/referrals/auth";
-import { recordLink } from "@/lib/tinybird";
-import { sendEmail } from "emails";
-import PartnerInvite from "emails/partner-invite";
import { NextResponse } from "next/server";
// GET /api/referrals/invite - check if a partner is already invited to a program
@@ -34,98 +31,16 @@ export const GET = withAuth(async ({ link, program }) => {
// POST /api/referrals/invite - invite a partner to dub partners
export const POST = withAuth(async ({ req, link, program, workspace }) => {
- // TODO:
- // We're repating the same logic in the invite-partner action.
- // We should move it to a shared location if it makes sense.
-
const { email } = requestPartnerInviteSchema.parse(
await parseRequestBody(req),
);
- const [programEnrollment, programInvite, tags] = await Promise.all([
- prisma.programEnrollment.findFirst({
- where: {
- programId: program.id,
- partner: {
- users: {
- some: {
- user: {
- email,
- },
- },
- },
- },
- },
- }),
-
- prisma.programInvite.findUnique({
- where: {
- email_programId: {
- email,
- programId: program.id,
- },
- },
- }),
-
- prisma.tag.findMany({
- where: {
- links: {
- some: {
- linkId: link.id,
- },
- },
- },
- }),
- ]);
-
- if (programEnrollment) {
- throw new Error("You're already enrolled in this program.");
- }
-
- if (programInvite) {
- throw new Error("You've already been invited to this program.");
- }
-
- const [programInvited] = await Promise.all([
- prisma.programInvite.create({
- data: {
- id: createId({ prefix: "pgi_" }),
- email,
- linkId: link.id,
- programId: program.id,
- },
- }),
-
- updateConfig({
- key: "partnersPortal",
- value: email,
- }),
-
- recordLink({
- domain: link.domain,
- key: link.key,
- link_id: link.id,
- created_at: link.createdAt,
- url: link.url,
- tag_ids: tags.map((t) => t.id) || [],
- program_id: program.id,
- workspace_id: workspace.id,
- deleted: false,
- }),
-
- sendEmail({
- subject: `${program.name} invited you to join Dub Partners`,
- email,
- react: PartnerInvite({
- email,
- appName: `${process.env.NEXT_PUBLIC_APP_NAME}`,
- program: {
- name: program.name,
- logo: program.logo,
- },
- }),
- }),
- ]);
+ const programInvited = await invitePartner({
+ email,
+ program,
+ link,
+ workspace,
+ });
return NextResponse.json(programInvited);
});
diff --git a/apps/web/app/app.dub.co/embed/page-client.tsx b/apps/web/app/app.dub.co/embed/page-client.tsx
index b0e4ef2d16..7b4a5ef84a 100644
--- a/apps/web/app/app.dub.co/embed/page-client.tsx
+++ b/apps/web/app/app.dub.co/embed/page-client.tsx
@@ -54,6 +54,11 @@ export function ReferralsEmbedPageClient() {
programInvite: ProgramInvite;
}>(`/api/referrals/invite`, fetcher);
+ const { data: salesCount } = useSWR<{ count: number }>(
+ "/api/referrals/sales/count",
+ fetcher,
+ );
+
const hasEnrolled = data?.programEnrollment;
const hasInvited = data?.programInvite;
const color = program?.brandColor || "#8B5CF6";
@@ -62,6 +67,7 @@ export function ReferralsEmbedPageClient() {
notFound();
}
+ // Inform parent that the token has expired
if (linkError && linkError.status === 401) {
window.parent.postMessage("TOKEN_EXPIRED", "*");
}
@@ -147,7 +153,7 @@ export function ReferralsEmbedPageClient() {
</div>
{/* TODO: UI needs tweaking */}
- {!isLoading && !hasEnrolled && !hasInvited && (
+ {!isLoading && !hasEnrolled && !hasInvited && salesCount?.count && (
<RequestPartnerInviteForm />
)}
diff --git a/apps/web/lib/actions/invite-partner.ts b/apps/web/lib/actions/invite-partner.ts
index 1435e6cf94..ddb635a12c 100644
--- a/apps/web/lib/actions/invite-partner.ts
+++ b/apps/web/lib/actions/invite-partner.ts
@@ -1,14 +1,9 @@
"use server";
-import { prisma } from "@/lib/prisma";
-import { sendEmail } from "emails";
-import PartnerInvite from "emails/partner-invite";
import { z } from "zod";
import { getLinkOrThrow } from "../api/links/get-link-or-throw";
+import { invitePartner } from "../api/partners/invite-partner";
import { getProgramOrThrow } from "../api/programs/get-program";
-import { createId } from "../api/utils";
-import { updateConfig } from "../edge-config";
-import { recordLink } from "../tinybird";
import { authActionClient } from "./safe-action";
const invitePartnerSchema = z.object({
@@ -24,7 +19,7 @@ export const invitePartnerAction = authActionClient
const { workspace } = ctx;
const { email, linkId, programId } = parsedInput;
- const [program, link, tags] = await Promise.all([
+ const [program, link] = await Promise.all([
getProgramOrThrow({
workspaceId: workspace.id,
programId,
@@ -34,105 +29,16 @@ export const invitePartnerAction = authActionClient
workspaceId: workspace.id,
linkId,
}),
-
- prisma.tag.findMany({
- where: {
- links: {
- some: {
- linkId,
- },
- },
- },
- }),
]);
if (link.programId) {
throw new Error("Link is already associated with another partner.");
}
- const [programEnrollment, programInvite] = await Promise.all([
- prisma.programEnrollment.findFirst({
- where: {
- programId,
- partner: {
- users: {
- some: {
- user: {
- email,
- },
- },
- },
- },
- },
- }),
-
- prisma.programInvite.findUnique({
- where: {
- email_programId: {
- email,
- programId,
- },
- },
- }),
- ]);
-
- if (programEnrollment) {
- throw new Error(`Partner ${email} already enrolled in this program.`);
- }
-
- if (programInvite) {
- throw new Error(`Partner ${email} already invited to this program.`);
- }
-
- const [result, _] = await Promise.all([
- prisma.programInvite.create({
- data: {
- id: createId({ prefix: "pgi_" }),
- email,
- linkId,
- programId,
- },
- }),
-
- prisma.link.update({
- where: {
- id: linkId,
- },
- data: {
- programId,
- },
- }),
-
- updateConfig({
- key: "partnersPortal",
- value: email,
- }),
-
- recordLink({
- domain: link.domain,
- key: link.key,
- link_id: link.id,
- created_at: link.createdAt,
- url: link.url,
- tag_ids: tags.map((t) => t.id) || [],
- program_id: program.id,
- workspace_id: workspace.id,
- deleted: false,
- }),
- ]);
-
- await sendEmail({
- subject: `${program.name} invited you to join Dub Partners`,
+ return await invitePartner({
email,
- react: PartnerInvite({
- email,
- appName: `${process.env.NEXT_PUBLIC_APP_NAME}`,
- program: {
- name: program.name,
- logo: program.logo,
- },
- }),
+ program,
+ link,
+ workspace,
});
-
- return result;
});
diff --git a/apps/web/lib/api/partners/invite-partner.ts b/apps/web/lib/api/partners/invite-partner.ts
new file mode 100644
index 0000000000..9b1d33b150
--- /dev/null
+++ b/apps/web/lib/api/partners/invite-partner.ts
@@ -0,0 +1,114 @@
+import { prisma } from "@/lib/prisma";
+import { ProgramProps } from "@/lib/types";
+import { Link, Project } from "@prisma/client";
+import { sendEmail } from "emails";
+import PartnerInvite from "emails/partner-invite";
+import { createId } from "../utils";
+
+export const invitePartner = async ({
+ email,
+ program,
+ workspace,
+ link,
+}: {
+ email: string;
+ program: ProgramProps;
+ workspace: Project;
+ link: Link;
+}) => {
+ const [programEnrollment, programInvite] = await Promise.all([
+ prisma.programEnrollment.findFirst({
+ where: {
+ programId: program.id,
+ partner: {
+ users: {
+ some: {
+ user: {
+ email,
+ },
+ },
+ },
+ },
+ },
+ }),
+
+ prisma.programInvite.findUnique({
+ where: {
+ email_programId: {
+ email,
+ programId: program.id,
+ },
+ },
+ }),
+ ]);
+
+ if (programEnrollment) {
+ throw new Error(`Partner ${email} already enrolled in this program.`);
+ }
+
+ if (programInvite) {
+ throw new Error(`Partner ${email} already invited to this program.`);
+ }
+
+ const tags = await prisma.tag.findMany({
+ where: {
+ links: {
+ some: {
+ linkId: link.id,
+ },
+ },
+ },
+ });
+
+ const [programInvited] = await Promise.all([
+ prisma.programInvite.create({
+ data: {
+ id: createId({ prefix: "pgi_" }),
+ email,
+ linkId: link.id,
+ programId: program.id,
+ },
+ }),
+
+ prisma.link.update({
+ where: {
+ id: link.id,
+ },
+ data: {
+ programId: program.id,
+ },
+ }),
+
+ // updateConfig({
+ // key: "partnersPortal",
+ // value: email,
+ // }),
+
+ // recordLink({
+ // domain: link.domain,
+ // key: link.key,
+ // link_id: link.id,
+ // created_at: link.createdAt,
+ // url: link.url,
+ // tag_ids: tags.map((t) => t.id) || [],
+ // program_id: program.id,
+ // workspace_id: workspace.id,
+ // deleted: false,
+ // }),
+
+ sendEmail({
+ subject: `${program.name} invited you to join Dub Partners`,
+ email,
+ react: PartnerInvite({
+ email,
+ appName: `${process.env.NEXT_PUBLIC_APP_NAME}`,
+ program: {
+ name: program.name,
+ logo: program.logo,
+ },
+ }),
+ }),
+ ]);
+
+ return programInvited;
+};