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.

account for null sale.revenue in tolt importer

Steven Teyfd9c30bf1b21
diff --git a/apps/web/app/(ee)/api/programs/[programId]/applications/export/route.ts b/apps/web/app/(ee)/api/programs/[programId]/applications/export/route.ts
index ee960d5c24..e3c13cad9d 100644
--- a/apps/web/app/(ee)/api/programs/[programId]/applications/export/route.ts
+++ b/apps/web/app/(ee)/api/programs/[programId]/applications/export/route.ts
@@ -1,4 +1,5 @@
import { convertToCSV } from "@/lib/analytics/utils/convert-to-csv";
+import { getDefaultProgramIdOrThrow } from "@/lib/api/programs/get-default-program-id-or-throw";
import { withWorkspace } from "@/lib/auth";
import {
exportApplicationColumns,
@@ -22,8 +23,9 @@ const applicationsExportQuerySchema = z.object({
// GET /api/programs/[programId]/applications/export – export applications to CSV
export const GET = withWorkspace(
- async ({ searchParams, params }) => {
- const { programId } = params;
+ async ({ searchParams, workspace }) => {
+ const programId = getDefaultProgramIdOrThrow(workspace);
+
let { columns } = applicationsExportQuerySchema.parse(searchParams);
const programEnrollments = await prisma.programEnrollment.findMany({
diff --git a/apps/web/lib/tolt/import-commissions.ts b/apps/web/lib/tolt/import-commissions.ts
index 14d72830ff..520f7f5c53 100644
--- a/apps/web/lib/tolt/import-commissions.ts
+++ b/apps/web/lib/tolt/import-commissions.ts
@@ -210,17 +210,17 @@ async function createCommission({
return;
}
- // Sale amount
- let amount = Number(sale.amount);
+ // Sale amount (can potentially be null)
+ let saleAmount = Number(sale.revenue ?? 0);
if (programCurrency.toUpperCase() !== "USD" && fxRates) {
const { amount: convertedAmount } = convertCurrencyWithFxRates({
currency: programCurrency,
- amount,
+ amount: saleAmount,
fxRates,
});
- amount = convertedAmount;
+ saleAmount = convertedAmount;
}
// Earnings
@@ -250,7 +250,7 @@ async function createCommission({
},
customerId: customerFound.id,
type: "sale",
- amount,
+ amount: saleAmount,
},
});
@@ -321,7 +321,7 @@ async function createCommission({
partnerId: customerFound.link.partnerId,
linkId: customerFound.linkId,
customerId: customerFound.id,
- amount,
+ amount: saleAmount,
earnings,
// TODO: allow custom "defaultCurrency" on workspace table in the future
currency: "usd",
@@ -332,20 +332,21 @@ async function createCommission({
},
}),
- recordSaleWithTimestamp({
- ...clickData,
- event_id: eventId,
- event_name: "Invoice paid",
- amount,
- customer_id: customerFound.id,
- payment_processor: "stripe",
- // TODO: allow custom "defaultCurrency" on workspace table in the future
- currency: "usd",
- metadata: JSON.stringify(commission),
- timestamp: new Date(sale.created_at).toISOString(),
- }),
+ saleAmount > 0 &&
+ recordSaleWithTimestamp({
+ ...clickData,
+ event_id: eventId,
+ event_name: "Invoice paid",
+ amount: saleAmount,
+ customer_id: customerFound.id,
+ payment_processor: "stripe",
+ // TODO: allow custom "defaultCurrency" on workspace table in the future
+ currency: "usd",
+ metadata: JSON.stringify(commission),
+ timestamp: new Date(sale.created_at).toISOString(),
+ }),
- // update link stats
+ // update link stats (if sale amount is greater than 0)
prisma.link.update({
where: {
id: customerFound.linkId,
@@ -359,29 +360,32 @@ async function createCommission({
increment: 1,
},
}),
- sales: {
- increment: 1,
- },
- saleAmount: {
- increment: amount,
- },
+ ...(saleAmount > 0 && {
+ sales: {
+ increment: 1,
+ },
+ saleAmount: {
+ increment: saleAmount,
+ },
+ }),
},
}),
- // update customer stats
- prisma.customer.update({
- where: {
- id: customerFound.id,
- },
- data: {
- sales: {
- increment: 1,
+ // update customer stats (if sale amount is greater than 0)
+ saleAmount > 0 &&
+ prisma.customer.update({
+ where: {
+ id: customerFound.id,
},
- saleAmount: {
- increment: amount,
+ data: {
+ sales: {
+ increment: 1,
+ },
+ saleAmount: {
+ increment: saleAmount,
+ },
},
- },
- }),
+ }),
]);
await syncTotalCommissions({
diff --git a/apps/web/lib/tolt/schemas.ts b/apps/web/lib/tolt/schemas.ts
index fc1a6a0be0..6cf3b1129c 100644
--- a/apps/web/lib/tolt/schemas.ts
+++ b/apps/web/lib/tolt/schemas.ts
@@ -62,9 +62,12 @@ export const ToltCustomerSchema = z.object({
export const ToltCommissionSchema = z.object({
id: z.string(),
amount: z.string().describe("Amount of the commission in cents."),
- revenue: z.string().describe("Revenue of the commission in cents."),
- transaction_id: z.string().nullable(), // this can be null
- charge_id: z.string(),
+ revenue: z
+ .string()
+ .nullable()
+ .describe("Revenue of the transaction in cents."),
+ transaction_id: z.string().nullable(),
+ charge_id: z.string().nullable(),
status: z.string(),
created_at: z.string(),
updated_at: z.string(),