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.

Add partner creation webhook support with additional partner details

Kiran Kf12ad6481df9
diff --git a/apps/web/app/api/partners/route.ts b/apps/web/app/api/partners/route.ts
index 58260d8b27..4d1fed2e60 100644
--- a/apps/web/app/api/partners/route.ts
+++ b/apps/web/app/api/partners/route.ts
@@ -135,8 +135,17 @@ export const GET = withWorkspace(
// POST /api/partners - add a partner for a program
export const POST = withWorkspace(
async ({ workspace, req, session }) => {
- const { programId, name, email, image, username, linkProps, tenantId } =
- createPartnerSchema.parse(await parseRequestBody(req));
+ const {
+ programId,
+ name,
+ email,
+ image,
+ username,
+ country,
+ description,
+ linkProps,
+ tenantId,
+ } = createPartnerSchema.parse(await parseRequestBody(req));
const program = await getProgramOrThrow({
workspaceId: workspace.id,
@@ -186,10 +195,13 @@ export const POST = withWorkspace(
programId,
tenantId,
linkId: partnerLink.id,
+ workspace,
partner: {
name,
email,
image,
+ // country,
+ // description,
},
});
diff --git a/apps/web/lib/actions/partners/add-partner.ts b/apps/web/lib/actions/partners/add-partner.ts
index 6f1c2ef6cc..6474640783 100644
--- a/apps/web/lib/actions/partners/add-partner.ts
+++ b/apps/web/lib/actions/partners/add-partner.ts
@@ -59,6 +59,7 @@ export const addPartnerAction = authActionClient
return await enrollPartner({
programId,
linkId,
+ workspace,
partner: {
name,
email,
diff --git a/apps/web/lib/api/partners/enroll-partner.ts b/apps/web/lib/api/partners/enroll-partner.ts
index d86ea7915d..e00af81688 100644
--- a/apps/web/lib/api/partners/enroll-partner.ts
+++ b/apps/web/lib/api/partners/enroll-partner.ts
@@ -3,6 +3,9 @@
import { createId } from "@/lib/api/utils";
import { updateConfig } from "@/lib/edge-config";
import { recordLink } from "@/lib/tinybird";
+import { WorkspaceProps } from "@/lib/types";
+import { sendWorkspaceWebhook } from "@/lib/webhook/publish";
+import { partnerWebhookEventSchema } from "@/lib/webhook/schemas";
import { prisma } from "@dub/prisma";
import { Prisma } from "@dub/prisma/client";
import { waitUntil } from "@vercel/functions";
@@ -13,11 +16,13 @@ export const enrollPartner = async ({
programId,
linkId,
tenantId,
+ workspace,
partner,
}: {
programId: string;
linkId: string;
tenantId?: string;
+ workspace: Pick<WorkspaceProps, "id" | "webhookEnabled">;
partner: {
name: string;
email?: string | null;
@@ -111,6 +116,12 @@ export const enrollPartner = async ({
key: "partnersPortal",
value: partner.email,
}),
+
+ sendWorkspaceWebhook({
+ trigger: "partner.created",
+ workspace,
+ data: partnerWebhookEventSchema.parse(upsertedPartner),
+ }),
]),
);
diff --git a/apps/web/lib/webhook/publish.ts b/apps/web/lib/webhook/publish.ts
index 615376f177..c9c1687eca 100644
--- a/apps/web/lib/webhook/publish.ts
+++ b/apps/web/lib/webhook/publish.ts
@@ -4,6 +4,7 @@ import { sendWebhooks } from "./qstash";
import {
LeadEventDataProps,
LinkEventDataProps,
+ PartnerEventDataProps,
SaleEventDataProps,
} from "./types";
@@ -15,7 +16,7 @@ export const sendWorkspaceWebhook = async ({
}: {
trigger: WebhookTrigger;
workspace: Pick<WorkspaceProps, "id" | "webhookEnabled">;
- data: LinkEventDataProps | LeadEventDataProps | SaleEventDataProps;
+ data: LinkEventDataProps | LeadEventDataProps | SaleEventDataProps | PartnerEventDataProps;
}) => {
if (!workspace.webhookEnabled) {
return;
diff --git a/apps/web/lib/webhook/types.ts b/apps/web/lib/webhook/types.ts
index 3d36491818..a48dc3faf7 100644
--- a/apps/web/lib/webhook/types.ts
+++ b/apps/web/lib/webhook/types.ts
@@ -1,26 +1,32 @@
import z from "../zod";
-import { clickEventSchema } from "../zod/schemas/clicks";
import { linkEventSchema } from "../zod/schemas/links";
import { WEBHOOK_TRIGGERS } from "./constants";
import {
clickWebhookEventSchema,
leadWebhookEventSchema,
+ partnerWebhookEventSchema,
saleWebhookEventSchema,
} from "./schemas";
+// TODO:
+// Remove the duplicate types
+
export type LinkEventDataProps = z.infer<typeof linkEventSchema>;
-export type ClickEventDataProps = z.infer<typeof clickEventSchema>;
+export type ClickEventDataProps = z.infer<typeof clickWebhookEventSchema>;
export type LeadEventDataProps = z.infer<typeof leadWebhookEventSchema>;
export type SaleEventDataProps = z.infer<typeof saleWebhookEventSchema>;
+export type PartnerEventDataProps = z.infer<typeof partnerWebhookEventSchema>;
+
export type EventDataProps =
| LinkEventDataProps
| ClickEventDataProps
| LeadEventDataProps
- | SaleEventDataProps;
+ | SaleEventDataProps
+ | PartnerEventDataProps;
export type WebhookTrigger = (typeof WEBHOOK_TRIGGERS)[number];
@@ -29,3 +35,5 @@ export type ClickEventWebhookData = z.infer<typeof clickWebhookEventSchema>;
export type LeadEventWebhookData = z.infer<typeof leadWebhookEventSchema>;
export type SaleEventWebhookData = z.infer<typeof saleWebhookEventSchema>;
+
+export type PartnerEventWebhookData = z.infer<typeof partnerWebhookEventSchema>;