The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Improve HubSpot webhook processing (#3799)
Kiran K9a08399a2a21
diff --git a/apps/web/app/(ee)/api/hubspot/webhook/route.ts b/apps/web/app/(ee)/api/hubspot/webhook/route.tsindex d0540b1efb..7e2b4f49b7 100644--- a/apps/web/app/(ee)/api/hubspot/webhook/route.ts+++ b/apps/web/app/(ee)/api/hubspot/webhook/route.ts@@ -1,6 +1,6 @@import { DubApiError, handleAndReturnErrorResponse } from "@/lib/api/errors";import { withAxiom } from "@/lib/axiom/server";-import { qstash } from "@/lib/cron";+import { enqueueBatchJobs } from "@/lib/cron/enqueue-batch-jobs";import { APP_DOMAIN_WITH_NGROK } from "@dub/utils";import crypto from "crypto";import { logAndRespond } from "../../cron/utils";@@ -50,9 +50,12 @@ export const POST = withAxiom(async (req) => {// to QStash and process each event independently in /api/hubspot/webhook/process.// This keeps the webhook handler fast and ensures a slow/failing event doesn't// block or fail the rest of the batch.- const qstashResponse = await qstash.batchJSON(+ const qstashResponse = await enqueueBatchJobs(finalEvents.map((event) => ({+ queueName: "process-hubspot-webhook",url: `${APP_DOMAIN_WITH_NGROK}/api/hubspot/webhook/process`,+ deduplicationId: event.eventId,+ method: "POST",body: event,})),);diff --git a/apps/web/lib/cron/enqueue-batch-jobs.ts b/apps/web/lib/cron/enqueue-batch-jobs.tsindex 6bb58e50f6..a8b4f8c56f 100644--- a/apps/web/lib/cron/enqueue-batch-jobs.ts+++ b/apps/web/lib/cron/enqueue-batch-jobs.ts@@ -7,7 +7,8 @@ type EnqueueBatchJobsProps = PublishBatchRequest<unknown> & {| "ban-partner"| "send-partner-summary"| "create-discount-code"- | "sync-bounty-social-metrics";+ | "sync-bounty-social-metrics"+ | "process-hubspot-webhook";};// Generic helper to enqueue a batch of QStash jobs.diff --git a/apps/web/lib/integrations/hubspot/track-lead.ts b/apps/web/lib/integrations/hubspot/track-lead.tsindex d1e3edb391..6b5c02f09d 100644--- a/apps/web/lib/integrations/hubspot/track-lead.ts+++ b/apps/web/lib/integrations/hubspot/track-lead.ts@@ -147,15 +147,15 @@ export const trackHubSpotLeadEvent = async ({return `No contact info found for contact ${objectId}.`;}+ const { properties } = contactInfo;+if (- contactInfo.properties.lifecyclestage?.toLowerCase() !==+ properties.lifecyclestage?.toLowerCase() !==settings.leadLifecycleStageId?.toLowerCase()) {- return `Unknown contact lifecyclestage ${contactInfo.properties.lifecyclestage}. Expected ${settings.leadLifecycleStageId}.`;+ return `Unknown contact lifecyclestage ${properties.lifecyclestage}. Expected ${settings.leadLifecycleStageId}.`;}- const { properties } = contactInfo;-if (!properties.dub_id) {return `No dub_id found for contact ${objectId}.`;}@@ -163,23 +163,16 @@ export const trackHubSpotLeadEvent = async ({const customer = await prisma.customer.findFirst({where: {projectId: workspace.id,- OR: [- { externalId: contactInfo.id },- { externalId: contactInfo.properties.email },- ],+ OR: [{ externalId: contactInfo.id }, { externalId: properties.email }],},});- if (!customer) {- return `No customer found for contact ID ${contactInfo.id} or email ${contactInfo.properties.email}.`;- }-const trackLeadResult = await trackLead({- clickId: "",+ clickId: properties.dub_id,eventName: `Contact ${properties.lifecyclestage}`,- customerExternalId: customer.externalId!,- customerName: `${contactInfo.properties.firstname} ${contactInfo.properties.lastname}`,- customerEmail: contactInfo.properties.email,+ customerExternalId: customer?.externalId || properties.email,+ customerName: `${properties.firstname} ${properties.lastname}`,+ customerEmail: properties.email,mode: "async",workspace,rawBody: payload,