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 HubSpot integration settings for deal stage ID

Kiran K982af2db67e9
diff --git a/apps/web/app/(ee)/api/hubspot/webhook/route.ts b/apps/web/app/(ee)/api/hubspot/webhook/route.ts
index 70207b2de5..6a4983b4d6 100644
--- a/apps/web/app/(ee)/api/hubspot/webhook/route.ts
+++ b/apps/web/app/(ee)/api/hubspot/webhook/route.ts
@@ -1,7 +1,13 @@
import { DubApiError, handleAndReturnErrorResponse } from "@/lib/api/errors";
-import { HUBSPOT_CLIENT_SECRET } from "@/lib/integrations/hubspot/constants";
+import {
+ DEFAULT_CLOSED_WON_DEAL_STAGE_ID,
+ HUBSPOT_CLIENT_SECRET,
+} from "@/lib/integrations/hubspot/constants";
import { refreshAccessToken } from "@/lib/integrations/hubspot/refresh-token";
-import { hubSpotWebhookSchema } from "@/lib/integrations/hubspot/schema";
+import {
+ hubSpotSettingsSchema,
+ hubSpotWebhookSchema,
+} from "@/lib/integrations/hubspot/schema";
import { trackHubSpotLeadEvent } from "@/lib/integrations/hubspot/track-lead";
import { trackHubSpotSaleEvent } from "@/lib/integrations/hubspot/track-sale";
import { HubSpotAuthToken } from "@/lib/integrations/hubspot/types";
@@ -122,10 +128,14 @@ async function processWebhookEvent(event: any) {
// Track the sale event when deal is closed won
if (subscriptionType === "object.propertyChange") {
+ const settings = hubSpotSettingsSchema.parse(installation.settings);
+
await trackHubSpotSaleEvent({
payload: event,
workspace,
authToken,
+ closedWonDealStageId:
+ settings?.closedWonDealStageId ?? DEFAULT_CLOSED_WON_DEAL_STAGE_ID,
});
}
}
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/(basic-layout)/integrations/[integrationSlug]/page-client.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/(basic-layout)/integrations/[integrationSlug]/page-client.tsx
index f8268b3233..bf37ef194c 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/(basic-layout)/integrations/[integrationSlug]/page-client.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/(basic-layout)/integrations/[integrationSlug]/page-client.tsx
@@ -1,6 +1,7 @@
"use client";
import { getIntegrationInstallUrl } from "@/lib/actions/get-integration-install-url";
+import { HubSpotSettings } from "@/lib/integrations/hubspot/ui/settings";
import { SegmentSettings } from "@/lib/integrations/segment/ui/settings";
import { SlackSettings } from "@/lib/integrations/slack/ui/settings";
import { ZapierSettings } from "@/lib/integrations/zapier/ui/settings";
@@ -56,6 +57,7 @@ const integrationSettings = {
[ZAPIER_INTEGRATION_ID]: ZapierSettings,
[SLACK_INTEGRATION_ID]: SlackSettings,
[SEGMENT_INTEGRATION_ID]: SegmentSettings,
+ [HUBSPOT_INTEGRATION_ID]: HubSpotSettings,
};
export default function IntegrationPageClient({
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/(basic-layout)/integrations/[integrationSlug]/page.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/(basic-layout)/integrations/[integrationSlug]/page.tsx
index 1771f0dfa2..87e7f6c500 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/(basic-layout)/integrations/[integrationSlug]/page.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/(basic-layout)/integrations/[integrationSlug]/page.tsx
@@ -57,6 +57,10 @@ export default async function IntegrationPage({
? integration.installations[0]?.credentials
: undefined;
+ const settings = installed
+ ? integration.installations[0]?.settings
+ : undefined;
+
// TODO:
// Fix this, we only displaying the first webhook only
const webhookId = installed
@@ -82,6 +86,7 @@ export default async function IntegrationPage({
}
: null,
credentials,
+ settings,
webhookId,
}}
/>
diff --git a/apps/web/lib/integrations/hubspot/constants.ts b/apps/web/lib/integrations/hubspot/constants.ts
index 3045244b6a..30ed9008f2 100644
--- a/apps/web/lib/integrations/hubspot/constants.ts
+++ b/apps/web/lib/integrations/hubspot/constants.ts
@@ -23,3 +23,5 @@ export const HUBSPOT_OBJECT_TYPE_IDS = [
"0-1", // contact
"0-3", // deal
] as const;
+
+export const DEFAULT_CLOSED_WON_DEAL_STAGE_ID = "closedwon";
diff --git a/apps/web/lib/integrations/hubspot/schema.ts b/apps/web/lib/integrations/hubspot/schema.ts
index 0948d9a233..2759fb16a1 100644
--- a/apps/web/lib/integrations/hubspot/schema.ts
+++ b/apps/web/lib/integrations/hubspot/schema.ts
@@ -11,6 +11,16 @@ export const hubSpotAuthTokenSchema = z.object({
created_at: z.number(),
});
+// Integration settings
+export const hubSpotSettingsSchema = z
+ .object({
+ closedWonDealStageId: z
+ .string()
+ .nullish()
+ .describe("The ID of the deal stage that represents a closed won deal."),
+ })
+ .nullish();
+
export const hubSpotRefreshTokenSchema = z.object({
access_token: z.string(),
refresh_token: z.string(),
diff --git a/apps/web/lib/integrations/hubspot/track-sale.ts b/apps/web/lib/integrations/hubspot/track-sale.ts
index 7315895c33..7e01c7fc8a 100644
--- a/apps/web/lib/integrations/hubspot/track-sale.ts
+++ b/apps/web/lib/integrations/hubspot/track-sale.ts
@@ -9,10 +9,12 @@ export const trackHubSpotSaleEvent = async ({
payload,
workspace,
authToken,
+ closedWonDealStageId,
}: {
payload: Record<string, any>;
workspace: Pick<WorkspaceProps, "id" | "stripeConnectId" | "webhookEnabled">;
authToken: HubSpotAuthToken;
+ closedWonDealStageId: string;
}) => {
const { objectId, subscriptionType, propertyName, propertyValue } =
hubSpotSaleEventSchema.parse(payload);
@@ -27,7 +29,7 @@ export const trackHubSpotSaleEvent = async ({
return;
}
- if (propertyValue !== "closedwon") {
+ if (propertyValue !== closedWonDealStageId) {
console.error(`[HubSpot] Unknown propertyValue ${propertyValue}`);
return;
}
diff --git a/apps/web/lib/integrations/hubspot/ui/settings.tsx b/apps/web/lib/integrations/hubspot/ui/settings.tsx
new file mode 100644
index 0000000000..00db1b44e2
--- /dev/null
+++ b/apps/web/lib/integrations/hubspot/ui/settings.tsx
@@ -0,0 +1,90 @@
+"use client";
+
+import useWorkspace from "@/lib/swr/use-workspace";
+import { InstalledIntegrationInfoProps } from "@/lib/types";
+import { Button } from "@dub/ui";
+import { useAction } from "next-safe-action/hooks";
+import { useState } from "react";
+import { toast } from "sonner";
+import { DEFAULT_CLOSED_WON_DEAL_STAGE_ID } from "../constants";
+import { updateHubSpotSettingsAction } from "../update-hubspot-settings";
+
+export const HubSpotSettings = ({
+ installed,
+ settings,
+}: InstalledIntegrationInfoProps) => {
+ const { id: workspaceId } = useWorkspace();
+ const [closedWonDealStageId, setClosedWonDealStageId] = useState(
+ (settings as any)?.closedWonDealStageId || DEFAULT_CLOSED_WON_DEAL_STAGE_ID,
+ );
+
+ const { executeAsync, isPending } = useAction(updateHubSpotSettingsAction, {
+ async onSuccess() {
+ toast.success("HubSpot settings updated successfully.");
+ },
+ onError({ error }) {
+ toast.error(error.serverError || "Failed to update HubSpot settings.");
+ },
+ });
+
+ const onSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
+ e.preventDefault();
+
+ if (!workspaceId) {
+ return;
+ }
+
+ await executeAsync({
+ workspaceId,
+ closedWonDealStageId: closedWonDealStageId || null,
+ });
+ };
+
+ if (!installed) {
+ return null;
+ }
+
+ return (
+ <form className="mt-4 flex items-end gap-2" onSubmit={onSubmit}>
+ <div className="w-full rounded-lg border border-neutral-200 bg-white">
+ <div className="flex items-center gap-x-2 border-b border-neutral-200 px-4 py-4">
+ <p className="text-sm font-medium text-neutral-700">
+ Closed Won Deal Stage ID
+ </p>
+ </div>
+
+ <div className="p-4">
+ <p className="text-sm leading-normal text-neutral-600">
+ Enter the HubSpot deal stage ID that represents a closed won deal.
+ This will be used to track when deals are marked as closed won in
+ HubSpot.
+ </p>
+
+ <div className="relative mt-4 rounded-md shadow-sm">
+ <input
+ className="w-full rounded-md border-neutral-300 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm"
+ placeholder={`Enter deal stage ID (e.g., ${DEFAULT_CLOSED_WON_DEAL_STAGE_ID})`}
+ type="text"
+ autoComplete="off"
+ name="closedWonDealStageId"
+ value={closedWonDealStageId}
+ onChange={(e) => setClosedWonDealStageId(e.target.value)}
+ />
+ </div>
+ </div>
+
+ <div className="flex items-center justify-end rounded-b-lg border-t border-neutral-200 bg-neutral-50 px-4 py-3">
+ <div className="shrink-0">
+ <Button
+ type="submit"
+ variant="primary"
+ text="Save changes"
+ className="h-8 w-fit"
+ loading={isPending}
+ />
+ </div>
+ </div>
+ </div>
+ </form>
+ );
+};
diff --git a/apps/web/lib/integrations/hubspot/update-hubspot-settings.ts b/apps/web/lib/integrations/hubspot/update-hubspot-settings.ts
new file mode 100644
index 0000000000..b37fb711b9
--- /dev/null
+++ b/apps/web/lib/integrations/hubspot/update-hubspot-settings.ts
@@ -0,0 +1,46 @@
+"use server";
+
+import { authActionClient } from "@/lib/actions/safe-action";
+import { prisma } from "@dub/prisma";
+import { HUBSPOT_INTEGRATION_ID } from "@dub/utils";
+import { revalidatePath } from "next/cache";
+import { z } from "zod";
+import { hubSpotSettingsSchema } from "./schema";
+
+const schema = hubSpotSettingsSchema
+ .pick({ closedWonDealStageId: true })
+ .extend({
+ workspaceId: z.string(),
+ });
+
+export const updateHubSpotSettingsAction = authActionClient
+ .schema(schema)
+ .action(async ({ parsedInput, ctx }) => {
+ const { workspace } = ctx;
+ const { closedWonDealStageId } = parsedInput;
+
+ const installedIntegration = await prisma.installedIntegration.findFirst({
+ where: {
+ integrationId: HUBSPOT_INTEGRATION_ID,
+ },
+ });
+
+ if (!installedIntegration) {
+ throw new Error(
+ "HubSpot integration is not installed on your workspace.",
+ );
+ }
+
+ await prisma.installedIntegration.update({
+ where: {
+ id: installedIntegration.id,
+ },
+ data: {
+ settings: {
+ closedWonDealStageId,
+ },
+ },
+ });
+
+ revalidatePath(`/${workspace.slug}/settings/integrations/hubspot`);
+ });
diff --git a/apps/web/lib/types.ts b/apps/web/lib/types.ts
index 35df3dfedf..d6112714d1 100644
--- a/apps/web/lib/types.ts
+++ b/apps/web/lib/types.ts
@@ -379,6 +379,7 @@ export type InstalledIntegrationInfoProps = Pick<
};
} | null;
credentials?: Prisma.JsonValue;
+ settings?: Prisma.JsonValue;
webhookId?: string; // Only if the webhook is managed by an integration
};
diff --git a/packages/prisma/schema/integration.prisma b/packages/prisma/schema/integration.prisma
index 170ab82f64..043709fee9 100644
--- a/packages/prisma/schema/integration.prisma
+++ b/packages/prisma/schema/integration.prisma
@@ -35,6 +35,7 @@ model InstalledIntegration {
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
credentials Json?
+ settings Json?
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
integration Integration @relation(fields: [integrationId], references: [id], onDelete: Cascade)