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 changes

Steven Tey2059ec9cba3f
diff --git a/apps/web/app/api/cron/aggregate-clicks/route.ts b/apps/web/app/api/cron/aggregate-clicks/route.ts
index 59ebbd0922..2e08eb7a11 100644
--- a/apps/web/app/api/cron/aggregate-clicks/route.ts
+++ b/apps/web/app/api/cron/aggregate-clicks/route.ts
@@ -18,6 +18,18 @@ export async function GET(req: Request) {
try {
await verifyVercelSignature(req);
+ const now = new Date();
+
+ // set 'start' to the beginning of the previous day (00:00:00)
+ const start = new Date(now);
+ start.setDate(start.getDate() - 1);
+ start.setHours(0, 0, 0, 0);
+
+ // set 'end' to the end of the previous day (23:59:59)
+ const end = new Date(now);
+ end.setDate(end.getDate() - 1);
+ end.setHours(23, 59, 59, 999);
+
const rewards = await prisma.reward.findMany({
where: {
event: "click",
@@ -48,7 +60,6 @@ export async function GET(req: Request) {
({ programEnrollment }) => programEnrollment.partnerId,
);
- // get partner links that were clicked on in the last 24 hours
const links = await prisma.link.findMany({
where: {
programId: reward.programId,
@@ -59,7 +70,7 @@ export async function GET(req: Request) {
gt: 0,
},
lastClicked: {
- gte: new Date(Date.now() - 24 * 60 * 60 * 1000), // links that were clicked in the last 24 hours
+ gte: start, // links that were clicked on before the start of the previous day
},
},
select: {
@@ -77,21 +88,10 @@ export async function GET(req: Request) {
for (const { id: linkId, programId, partnerId } of links) {
if (!linkId || !programId || !partnerId) {
+ console.log("Invalid link", { linkId, programId, partnerId });
continue;
}
- const now = new Date();
-
- // set 'start' to the beginning of the previous day (00:00:00)
- const start = new Date(now);
- start.setDate(start.getDate() - 1);
- start.setHours(0, 0, 0, 0);
-
- // set 'end' to the end of the previous day (23:59:59)
- const end = new Date(now);
- end.setDate(end.getDate() - 1);
- end.setHours(23, 59, 59, 999);
-
const { clicks: quantity } = await getAnalytics({
linkId,
start,
@@ -101,10 +101,24 @@ export async function GET(req: Request) {
});
if (!quantity || quantity === 0) {
+ console.log("No clicks found for link", {
+ linkId,
+ programId,
+ partnerId,
+ });
continue;
}
+ console.log("Creating commission", {
+ reward,
+ event: "click",
+ programId,
+ partnerId,
+ linkId,
+ quantity,
+ });
await createPartnerCommission({
+ reward,
event: "click",
programId,
partnerId,
diff --git a/apps/web/lib/partners/create-partner-commission.ts b/apps/web/lib/partners/create-partner-commission.ts
index 482981ca42..eb08104d96 100644
--- a/apps/web/lib/partners/create-partner-commission.ts
+++ b/apps/web/lib/partners/create-partner-commission.ts
@@ -4,9 +4,11 @@ import { log } from "@dub/utils";
import { differenceInMonths } from "date-fns";
import { createId } from "../api/create-id";
import { calculateSaleEarnings } from "../api/sales/calculate-sale-earnings";
+import { RewardProps } from "../types";
import { determinePartnerReward } from "./determine-partner-reward";
export const createPartnerCommission = async ({
+ reward,
event,
programId,
partnerId,
@@ -18,6 +20,9 @@ export const createPartnerCommission = async ({
quantity,
currency,
}: {
+ // we optionally let the caller pass in a reward to avoid a db call
+ // (e.g. in aggregate-clicks route)
+ reward?: RewardProps | null;
event: EventType;
partnerId: string;
programId: string;
@@ -29,17 +34,19 @@ export const createPartnerCommission = async ({
quantity: number;
currency?: string;
}) => {
- const reward = await determinePartnerReward({
- event,
- partnerId,
- programId,
- });
-
if (!reward) {
- console.log(
- `Partner ${partnerId} has no reward for ${event} event, skipping commission creation...`,
- );
- return;
+ reward = await determinePartnerReward({
+ event,
+ partnerId,
+ programId,
+ });
+
+ if (!reward) {
+ console.log(
+ `Partner ${partnerId} has no reward for ${event} event, skipping commission creation...`,
+ );
+ return;
+ }
}
// handle sale rewards that have a max duration limit