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.

finalize comments, add tests

Steven Teyf1c586c6c3c6
diff --git a/apps/web/lib/api/conversions/track-sale.ts b/apps/web/lib/api/conversions/track-sale.ts
index 5efd3e5c7c..b1d5fe9ebd 100644
--- a/apps/web/lib/api/conversions/track-sale.ts
+++ b/apps/web/lib/api/conversions/track-sale.ts
@@ -83,7 +83,7 @@ export const trackSale = async ({
},
});
- // Existing customer is found, find the lead event
+ // Existing customer is found, find the lead event to associate the sale with
if (existingCustomer) {
const leadEvent = await getLeadEvent({
customerId: existingCustomer.id,
@@ -123,7 +123,7 @@ export const trackSale = async ({
}
}
- // No existing customer is found, find the click event and create a new customer
+ // No existing customer is found, find the click event and create a new customer (for sale tracking without a pre-existing lead event)
else {
if (!clickId) {
waitUntil(
@@ -131,8 +131,7 @@ export const trackSale = async ({
workspace_id: workspace.id,
path: "/track/sale",
body: JSON.stringify(rawBody),
- error:
- "The `clickId` property was not provided in the request, and no existing customer with the provided `customerExternalId` was found.",
+ error: `No existing customer with the provided customerExternalId (${customerExternalId}) was found, and there was no clickId provided for sale tracking without a pre-existing lead event.`,
}),
);
@@ -235,14 +234,24 @@ export const trackSale = async ({
};
}
- const customer: Customer = existingCustomer ?? newCustomer!;
+ const customer = existingCustomer ?? newCustomer;
- // This should never happen
+ // This should never happen, but just in case
if (!customer) {
- throw new DubApiError({
- code: "not_found",
- message: "Customer not found.",
- });
+ waitUntil(
+ logConversionEvent({
+ workspace_id: workspace.id,
+ path: "/track/sale",
+ body: JSON.stringify(rawBody),
+ error: `Customer not found for customerExternalId: ${customerExternalId}`,
+ }),
+ );
+
+ return {
+ eventName,
+ customer: null,
+ sale: null,
+ };
}
const [_, trackedSale] = await Promise.all([
@@ -389,7 +398,7 @@ const _trackSale = async ({
});
}
- // if currency is not USD, convert it to USD based on the current FX rate
+ // 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 } =
diff --git a/apps/web/tests/tracks/track-sale.test.ts b/apps/web/tests/tracks/track-sale.test.ts
index 986808076f..db75d28d72 100644
--- a/apps/web/tests/tracks/track-sale.test.ts
+++ b/apps/web/tests/tracks/track-sale.test.ts
@@ -1,10 +1,15 @@
import { TrackSaleResponse } from "@/lib/types";
-import { randomId, randomSaleAmount } from "tests/utils/helpers";
+import {
+ randomCustomer,
+ randomId,
+ randomSaleAmount,
+} from "tests/utils/helpers";
import {
E2E_CUSTOMER_EXTERNAL_ID,
E2E_CUSTOMER_EXTERNAL_ID_2,
E2E_CUSTOMER_ID,
E2E_REWARD,
+ E2E_TRACK_CLICK_HEADERS,
} from "tests/utils/resource";
import { describe, expect, test } from "vitest";
import { IntegrationHarness } from "../utils/integration";
@@ -221,4 +226,53 @@ describe("POST /track/sale", async () => {
expect(response.data.sale?.amount).toBeGreaterThanOrEqual(900); // 900 cents
expect(response.data.sale?.amount).toBeLessThanOrEqual(1100); // 1100 cents
});
+
+ test("track a sale without a pre-existing lead event", async () => {
+ const clickResponse = await http.post<{ clickId: string }>({
+ path: "/track/click",
+ headers: E2E_TRACK_CLICK_HEADERS,
+ body: {
+ domain: "getacme.link",
+ key: "derek",
+ },
+ });
+ expect(clickResponse.status).toEqual(200);
+ expect(clickResponse.data.clickId).toStrictEqual(expect.any(String));
+ const trackedClickId = clickResponse.data.clickId;
+ const saleCustomer = randomCustomer();
+ const salePayload = {
+ ...sale,
+ eventName: "Purchase (no lead event)",
+ amount: randomSaleAmount(),
+ invoiceId: `INV_${randomId()}`,
+ };
+
+ const response = await http.post<TrackSaleResponse>({
+ path: "/track/sale",
+ body: {
+ ...salePayload,
+ clickId: trackedClickId,
+ customerExternalId: saleCustomer.externalId,
+ customerName: saleCustomer.name,
+ customerEmail: saleCustomer.email,
+ customerAvatar: saleCustomer.avatar,
+ },
+ });
+
+ expect(response.status).toEqual(200);
+ expect(response.data).toStrictEqual({
+ eventName: salePayload.eventName,
+ customer: {
+ id: expect.any(String),
+ ...saleCustomer,
+ },
+ sale: {
+ amount: salePayload.amount,
+ currency: salePayload.currency,
+ paymentProcessor: salePayload.paymentProcessor,
+ invoiceId: salePayload.invoiceId,
+ metadata: null,
+ },
+ });
+ });
});