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.

15.4 KB
Raw Blame
import { convertCurrency } from "@/lib/analytics/convert-currency";
import { isFirstConversion } from "@/lib/analytics/is-first-conversion";
import { getOrCreateCustomer } from "@/lib/api/customers/get-or-create-customer";
import { DubApiError } from "@/lib/api/errors";
import { includeTags } from "@/lib/api/links/include-tags";
import { getNormalizedEmail } from "@/lib/email/normalize-email";
import { queueGoogleAdsConversionUpload } from "@/lib/integrations/google-ads/upload-conversion";
import { generateRandomName } from "@/lib/names";
import { prisma } from "@/lib/prisma";
import { isStored, storage } from "@/lib/storage";
import {
  getClickEvent,
  getLeadEvent,
  recordLead,
  recordSale,
} from "@/lib/tinybird";
import { LeadEventTB, WorkspaceProps } from "@/lib/types";
import { redis } from "@/lib/upstash";
import { sendWorkspaceWebhook } from "@/lib/webhook/publish";
import {
  transformLeadEventData,
  transformSaleEventData,
} from "@/lib/webhook/transform";
import {
  trackSaleRequestSchema,
  trackSaleResponseSchema,
} from "@/lib/zod/schemas/sales";
import { nanoid, R2_URL } from "@dub/utils";
import { Customer } from "@prisma/client";
import { waitUntil } from "@vercel/functions";
import * as z from "zod/v4";
import { createId } from "../create-id";

type TrackSaleParams = z.input<typeof trackSaleRequestSchema> & {
  workspace: Pick<WorkspaceProps, "id" | "stripeConnectId" | "webhookEnabled">;
};

export const trackSale = async ({
  clickId,
  customerExternalId,
  customerName,
  customerEmail,
  customerAvatar,
  amount,
  currency = "usd",
  eventName,
  paymentProcessor,
  invoiceId,
  leadEventName,
  metadata,
  workspace,
}: TrackSaleParams) => {
  let existingCustomer: Customer | null = null;
  let newCustomer: Customer | null = null;
  let leadEventData: LeadEventTB | null = null;
  let shouldTrackDirectSaleLead = false;

  // Return idempotent response if invoiceId is already processed
  if (invoiceId) {
    const cachedResponse = await redis.get(
      `trackSale:${workspace.id}:invoiceId:${invoiceId}`,
    );
    if (cachedResponse) {
      return cachedResponse;
    }
  }

  // Find existing customer
  const existingCustomerData = await prisma.customer.findUnique({
    where: {
      projectId_externalId: {
        projectId: workspace.id,
        externalId: customerExternalId,
      },
    },
    include: {
      link: {
        select: {
          id: true,
          projectId: true,
          disabledAt: true,
        },
      },
    },
  });

  // run link checks for existing customer if found
  if (existingCustomerData) {
    const { link: customerLink, ...rest } = existingCustomerData;
    existingCustomer = rest;

    if (!customerLink) {
      throw new DubApiError({
        code: "not_found",
        message: `Link not found for customer ${existingCustomer.id}`,
      });
    }

    if (customerLink.projectId !== workspace.id) {
      throw new DubApiError({
        code: "not_found",
        message: `Link ${customerLink.id} for customer ${existingCustomer.id} does not belong to the workspace`,
      });
    }

    if (customerLink.disabledAt) {
      throw new DubApiError({
        code: "not_found",
        message: `Link ${customerLink.id} for customer ${existingCustomer.id} is disabled, sale not tracked`,
      });
    }

    const leadEvent = await getLeadEvent({
      customerId: existingCustomer.id,
      eventName: leadEventName,
    });

    if (!leadEvent) {
      const errorMessage = `Lead event not found for externalId: ${customerExternalId} and leadEventName: ${leadEventName}`;

      throw new DubApiError({
        code: "not_found",
        message: errorMessage,
      });
    }

    leadEventData = {
      ...leadEvent,
      workspace_id: leadEvent.workspace_id || workspace.id, // in case for some reason the lead event doesn't have workspace_id
    };
  }

  // If no existing customer is found and no clickId is provided, return early
  if (!existingCustomer && !clickId) {
    return {
      eventName,
      customer: null,
      sale: null,
    };
  }

  // Direct sale tracking: create the customer from the passed clickId (if exists)
  if (!existingCustomer && clickId) {
    const clickData = await getClickEvent({
      clickId,
    });

    if (!clickData) {
      throw new DubApiError({
        code: "not_found",
        message: `Click event not found for clickId: ${clickId}`,
      });
    }

    // For the same customer, a sale event might come from a different link click than the original lead event.
    // We want to attribute the sale to the correct link (the one from the clickId) for direct sale tracking.
    if (leadEventData) {
      leadEventData = {
        ...leadEventData,
        ...clickData,
      };
    }

    const clickDataLink = await prisma.link.findUnique({
      where: {
        id: clickData.link_id,
      },
      select: {
        id: true,
        projectId: true,
        disabledAt: true,
      },
    });

    // same link checks as above for existing customer
    if (!clickDataLink) {
      throw new DubApiError({
        code: "not_found",
        message: `Link not found for clickId: ${clickData.click_id}`,
      });
    }

    if (clickDataLink.projectId !== workspace.id) {
      throw new DubApiError({
        code: "not_found",
        message: `Link ${clickDataLink.id} for clickId ${clickData.click_id} does not belong to the workspace`,
      });
    }

    if (clickDataLink.disabledAt) {
      throw new DubApiError({
        code: "not_found",
        message: `Link ${clickDataLink.id} for clickId ${clickData.click_id} is disabled, sale not tracked`,
      });
    }

    const finalCustomerId = createId({ prefix: "cus_" });
    const finalCustomerName =
      customerName || customerEmail || generateRandomName();
    const finalCustomerAvatar =
      customerAvatar && !isStored(customerAvatar)
        ? `${R2_URL}/customers/${finalCustomerId}/avatar_${nanoid(7)}`
        : customerAvatar;

    const { customer: existingOrNewCustomer, created } =
      await getOrCreateCustomer({
        where: {
          projectId_externalId: {
            projectId: workspace.id,
            externalId: customerExternalId,
          },
        },
        create: {
          id: finalCustomerId,
          name: finalCustomerName,
          email: customerEmail,
          normalizedEmail: getNormalizedEmail(customerEmail),
          avatar: finalCustomerAvatar,
          externalId: customerExternalId,
          linkId: clickData.link_id,
          clickId: clickData.click_id,
          country: clickData.country,
          projectId: workspace.id,
          projectConnectId: workspace.stripeConnectId,
          clickedAt: new Date(clickData.timestamp + "Z"),
        },
      });

    if (created) {
      newCustomer = existingOrNewCustomer;
    } else {
      existingCustomer = existingOrNewCustomer;
    }

    // Persist customer avatar to R2 if it's not already stored
    if (customerAvatar && !isStored(customerAvatar) && finalCustomerAvatar) {
      waitUntil(
        storage
          .upload({
            key: finalCustomerAvatar.replace(`${R2_URL}/`, ""),
            body: customerAvatar,
            opts: {
              width: 128,
              height: 128,
            },
          })
          .catch(async (error) => {
            console.error("Error persisting customer avatar to R2", error);

            // if the avatar fails to upload to R2, set the avatar to null in the database
            if (newCustomer) {
              await prisma.customer.update({
                where: {
                  id: newCustomer.id,
                },
                data: {
                  avatar: null,
                },
              });
            }
          }),
      );
    }

    // if leadEventName is provided, use it
    // otherwise use "Direct sale tracking lead event" (since it's for direct sale tracking)
    const finalLeadEventName =
      leadEventName ?? "Direct sale tracking lead event";

    if (newCustomer) {
      leadEventData = {
        ...clickData,
        event_id: nanoid(16),
        event_name: finalLeadEventName,
        customer_id: newCustomer.id,
        metadata: metadata ? JSON.stringify(metadata) : "",
      };
    } else if (existingCustomer) {
      const leadEvent = await getLeadEvent({
        customerId: existingCustomer.id,
        eventName: leadEventName,
      });

      leadEventData = leadEvent
        ? {
            ...leadEvent,
            ...clickData,
          }
        : {
            ...clickData,
            event_id: nanoid(16),
            event_name: finalLeadEventName,
            customer_id: existingCustomer.id,
            metadata: metadata ? JSON.stringify(metadata) : "",
          };
    }

    // Deduplicate lead events across concurrent direct sale requests
    if (leadEventData) {
      const cacheKey = `directSaleTrackLead:${workspace.id}:${customerExternalId}:${finalLeadEventName.toLowerCase().replaceAll(" ", "-")}`;
      const cachedLeadEvent = await redis.set(
        cacheKey,
        {
          timestamp: Date.now(),
        },
        {
          ex: 30, // 30 seconds
          nx: true,
        },
      );

      shouldTrackDirectSaleLead = cachedLeadEvent !== null;
    }
  }

  const customer = existingCustomer ?? newCustomer;

  // This should never happen, but just in case
  if (!customer) {
    return {
      eventName,
      customer: null,
      sale: null,
    };
  }

  const [_, trackedSale] = await Promise.all([
    shouldTrackDirectSaleLead &&
      _trackLead({
        workspace,
        leadEventData,
        customer,
      }),

    _trackSale({
      amount,
      currency,
      eventName,
      paymentProcessor,
      invoiceId,
      metadata,
      workspace,
      leadEventData,
      customer,
    }),
  ]);

  return trackedSale;
};

// Track the lead event
const _trackLead = async ({
  workspace,
  leadEventData,
  customer,
}: Pick<TrackSaleParams, "workspace"> & {
  leadEventData: LeadEventTB | null;
  customer: Customer;
}) => {
  if (!leadEventData) {
    throw new DubApiError({
      code: "not_found",
      message: `Lead event data not found for the customer ${customer.id}`,
    });
  }

  waitUntil(
    (async () => {
      const [_leadEvent, link, _workspace] = await Promise.all([
        // Record the lead event for the customer
        recordLead({
          ...leadEventData,
          workspace_id: leadEventData.workspace_id || workspace.id, // in case for some reason the lead event doesn't have workspace_id
        }),

        // Update link leads count + lastLeadAt date
        prisma.link.update({
          where: {
            id: leadEventData.link_id,
          },
          data: {
            leads: {
              increment: 1,
            },
            lastLeadAt: new Date(),
          },
          include: includeTags,
        }),

        // Update workspace events usage
        prisma.project.update({
          where: {
            id: workspace.id,
          },
          data: {
            usage: {
              increment: 1,
            },
          },
        }),
      ]);

      await sendWorkspaceWebhook({
        trigger: "lead.created",
        data: transformLeadEventData({
          ...leadEventData,
          link,
          customer,
        }),
        workspace,
      });
    })(),
  );
};

// Track the sale event
const _trackSale = async ({
  amount,
  currency = "usd",
  eventName,
  paymentProcessor,
  invoiceId,
  metadata,
  workspace,
  leadEventData,
  customer,
}: Omit<TrackSaleParams, "customerExternalId"> & {
  leadEventData: LeadEventTB | null;
  customer: Customer;
}) => {
  if (!leadEventData) {
    throw new DubApiError({
      code: "not_found",
      message: `Lead event data not found for the customer ${customer.id}`,
    });
  }

  // Skip if amount is 0 or less
  if (amount <= 0) {
    return {
      eventName,
      customer: null,
      sale: null,
    };
  }

  // if currency is not USD, convert it to USD based on the current FX rate
  // TODO: allow custom "defaultCurrency" on workspace table in the future
  if (currency !== "usd") {
    const { currency: convertedCurrency, amount: convertedAmount } =
      await convertCurrency({
        currency,
        amount,
      });

    currency = convertedCurrency;
    amount = convertedAmount;
  }

  const saleData = {
    ...leadEventData,
    workspace_id: leadEventData.workspace_id || workspace.id, // in case for some reason the lead event doesn't have workspace_id
    event_id: nanoid(16),
    event_name: eventName,
    customer_id: customer.id,
    payment_processor: paymentProcessor,
    amount,
    currency,
    invoice_id: invoiceId || "",
    metadata: metadata ? JSON.stringify(metadata) : "",
  };

  let firstConversionFlag = isFirstConversion({
    customer,
    linkId: saleData.link_id,
  });

  // Deduplicate concurrent first sales for the same customer + link so only one
  // request is counted as the first conversion.
  if (firstConversionFlag) {
    const claim = await redis.set(
      `firstConversion:${customer.id}:${saleData.link_id}`,
      1,
      {
        ex: 30,
        nx: true,
      },
    );

    firstConversionFlag = claim !== null;
  }

  waitUntil(
    (async () => {
      // Update link conversions, sales, and saleAmount
      const link = await prisma.link.update({
        where: {
          id: saleData.link_id,
        },
        data: {
          ...(firstConversionFlag && {
            conversions: {
              increment: 1,
            },
            lastConversionAt: new Date(),
          }),
          sales: {
            increment: 1,
          },
          saleAmount: {
            increment: amount,
          },
        },
        include: includeTags,
      });

      await Promise.allSettled([
        recordSale({
          ...saleData,
          timestamp: undefined,
        }),

        sendWorkspaceWebhook({
          trigger: "sale.created",
          data: transformSaleEventData({
            ...saleData,
            clickedAt: customer.clickedAt || customer.createdAt,
            link,
            customer,
            metadata,
          }),
          workspace,
        }),

        queueGoogleAdsConversionUpload({
          workspaceId: workspace.id,
          eventType: "sale",
          eventName: saleData.event_name,
          conversionDateTime: new Date().toISOString(),
          eventId: saleData.event_id,
          conversionValue: saleData.amount,
          currencyCode: saleData.currency,
          click: {
            id: saleData.click_id,
            url: saleData.url,
          },
        }),

        prisma.project.update({
          where: {
            id: workspace.id,
          },
          data: {
            usage: {
              increment: 1,
            },
          },
        }),
      ]);

      // Update customer stats
      await prisma.customer.update({
        where: {
          id: customer.id,
        },
        data: {
          sales: {
            increment: 1,
          },
          saleAmount: {
            increment: amount,
          },
          firstSaleAt: customer.firstSaleAt ? undefined : new Date(),
        },
      });
    })(),
  );

  const trackSaleResponse = trackSaleResponseSchema.parse({
    eventName,
    customer,
    sale: {
      amount,
      currency,
      invoiceId,
      paymentProcessor,
      metadata,
    },
  });

  if (invoiceId) {
    waitUntil(
      redis.set(
        `trackSale:${workspace.id}:invoiceId:${invoiceId}`,
        trackSaleResponse,
        {
          ex: 60 * 60 * 24 * 7, // cache for 1 week
        },
      ),
    );
  }

  return trackSaleResponse;
};