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.

fix create commission saleEventDate + leadEventDate

Steven Tey99d03f028931
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/commissions/create-commission-sheet.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/commissions/create-commission-sheet.tsx
index b0c4e97180..028309843a 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/commissions/create-commission-sheet.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/commissions/create-commission-sheet.tsx
@@ -39,6 +39,8 @@ function CreateCommissionSheetContent({
const [isNewCustomer, setIsNewCustomer] = useState(false);
const [hasDifferentCreationDate, setHasDifferentCreationDate] =
useState(false);
+ const [hasDifferentLeadEventName, setHasDifferentLeadEventName] =
+ useState(false);
const {
register,
@@ -48,17 +50,17 @@ function CreateCommissionSheetContent({
formState: { errors },
} = useForm<FormData>();
- const [partnerId, linkId, customerId, saleDate, leadDate] = watch([
+ const [partnerId, linkId, customerId, saleEventDate, leadEventDate] = watch([
"partnerId",
"linkId",
"customerId",
- "saleDate",
- "leadDate",
+ "saleEventDate",
+ "leadEventDate",
]);
useEffect(() => {
if (!hasDifferentCreationDate) {
- setValue("leadDate", null);
+ setValue("leadEventDate", null);
}
}, [hasDifferentCreationDate, setValue]);
@@ -106,25 +108,25 @@ function CreateCommissionSheetContent({
return;
}
- const saleDate = data.saleDate
- ? new Date(data.saleDate).toISOString()
+ const saleEventDate = data.saleEventDate
+ ? new Date(data.saleEventDate).toISOString()
: null;
- const leadDate = data.leadDate
- ? new Date(data.leadDate).toISOString()
+ const leadEventDate = data.leadEventDate
+ ? new Date(data.leadEventDate).toISOString()
: null;
await executeAsync({
...data,
workspaceId,
programId: program.id,
- saleDate,
- leadDate: leadDate || saleDate,
saleAmount: data.saleAmount ? data.saleAmount * 100 : null,
+ saleEventDate,
+ leadEventDate,
});
};
- const displayCustomerCreationDate =
+ const displayLeadEventDate =
customerId && !isNewCustomer && programDefaultReward?.event === "sale";
return (
@@ -201,9 +203,9 @@ function CreateCommissionSheetContent({
<div className="grid grid-cols-1 gap-6 rounded-xl border border-neutral-200 px-4 py-5">
{programDefaultReward?.event === "sale" && (
<SmartDateTimePicker
- value={saleDate}
+ value={saleEventDate}
onChange={(date) => {
- setValue("saleDate", date, { shouldDirty: true });
+ setValue("saleEventDate", date, { shouldDirty: true });
}}
label="Sale date"
required={programDefaultReward?.event === "sale"}
@@ -249,9 +251,9 @@ function CreateCommissionSheetContent({
{programDefaultReward?.event === "lead" && (
<SmartDateTimePicker
- value={leadDate}
+ value={leadEventDate}
onChange={(date) => {
- setValue("leadDate", date, { shouldDirty: true });
+ setValue("leadEventDate", date, { shouldDirty: true });
}}
label="Lead date"
placeholder='E.g. "2024-03-01", "Last Thursday", "2 hours ago"'
@@ -331,7 +333,7 @@ function CreateCommissionSheetContent({
</div>
</div>
- {displayCustomerCreationDate && (
+ {displayLeadEventDate && (
<AnimatedSizeContainer
height
transition={{ ease: "easeInOut", duration: 0.2 }}
@@ -351,7 +353,7 @@ function CreateCommissionSheetContent({
/>
<div className="flex flex-col gap-1">
<h3 className="text-sm font-medium text-neutral-700">
- Customer creation date is different than the sale date
+ Set a custom lead event date
</h3>
</div>
</div>
@@ -359,11 +361,13 @@ function CreateCommissionSheetContent({
{hasDifferentCreationDate && (
<div className="p-px">
<SmartDateTimePicker
- value={leadDate}
+ value={leadEventDate}
onChange={(date) => {
- setValue("leadDate", date, { shouldDirty: true });
+ setValue("leadEventDate", date, {
+ shouldDirty: true,
+ });
}}
- label="Customer creation date"
+ label="Lead event date"
placeholder='E.g. "2024-03-01", "Last Thursday", "2 hours ago"'
/>
</div>
@@ -371,6 +375,51 @@ function CreateCommissionSheetContent({
</div>
</AnimatedSizeContainer>
)}
+
+ {displayLeadEventDate && (
+ <AnimatedSizeContainer
+ height
+ transition={{ ease: "easeInOut", duration: 0.2 }}
+ style={{
+ height: hasDifferentLeadEventName ? "auto" : "0px",
+ overflow: "hidden",
+ }}
+ >
+ <div className="flex flex-col gap-6">
+ <div className="flex items-center gap-4">
+ <Switch
+ fn={setHasDifferentLeadEventName}
+ checked={hasDifferentLeadEventName}
+ trackDimensions="w-8 h-4"
+ thumbDimensions="w-3 h-3"
+ thumbTranslate="translate-x-4"
+ />
+ <div className="flex flex-col gap-1">
+ <h3 className="text-sm font-medium text-neutral-700">
+ Set a custom lead event name
+ </h3>
+ </div>
+ </div>
+
+ {hasDifferentLeadEventName && (
+ <div className="p-px">
+ <input
+ type="text"
+ className={cn(
+ "block w-full rounded-md border-neutral-300 px-3 py-2 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm",
+ errors.leadEventName &&
+ "border-red-600 focus:border-red-500 focus:ring-red-600",
+ )}
+ {...register("leadEventName", {
+ setValueAs: (value) => (value === "" ? null : value),
+ })}
+ placeholder="Enter lead event name"
+ />
+ </div>
+ )}
+ </div>
+ </AnimatedSizeContainer>
+ )}
</div>
</div>
</div>
diff --git a/apps/web/lib/actions/partners/create-commission.ts b/apps/web/lib/actions/partners/create-commission.ts
index c090f52e8d..755477b918 100644
--- a/apps/web/lib/actions/partners/create-commission.ts
+++ b/apps/web/lib/actions/partners/create-commission.ts
@@ -23,10 +23,13 @@ export const createCommissionAction = authActionClient
invoiceId,
customerId,
saleAmount,
- saleDate,
- leadDate,
+ saleEventDate,
+ leadEventDate,
+ leadEventName,
} = parsedInput;
+ const finalLeadEventDate = leadEventDate ?? saleEventDate ?? new Date();
+
const [programEnrollment, customer] = await Promise.all([
getProgramEnrollmentOrThrow({
programId,
@@ -77,7 +80,7 @@ export const createCommissionAction = authActionClient
}
}
- const shouldRecordSale = saleAmount && saleDate;
+ const shouldRecordSale = saleAmount && saleEventDate;
// Record click
const dummyRequest = new Request(link.url, {
@@ -100,7 +103,7 @@ export const createCommissionAction = authActionClient
workspaceId: workspace.id,
skipRatelimit: true,
timestamp: new Date(
- new Date(leadDate!).getTime() - 5 * 60 * 1000,
+ new Date(finalLeadEventDate).getTime() - 5 * 60 * 1000,
).toISOString(),
});
@@ -116,9 +119,9 @@ export const createCommissionAction = authActionClient
await recordLeadWithTimestamp({
...clickEvent,
event_id: leadEventId,
- event_name: "Sign up",
+ event_name: leadEventName || "Sign up",
customer_id: customerId,
- timestamp: new Date(leadDate!).toISOString(),
+ timestamp: new Date(finalLeadEventDate).toISOString(),
});
if (!customer.linkId && clickData) {
@@ -143,6 +146,7 @@ export const createCommissionAction = authActionClient
customerId,
amount: 0,
quantity: 1,
+ createdAt: finalLeadEventDate,
});
// Record sale
@@ -163,7 +167,7 @@ export const createCommissionAction = authActionClient
customer_id: customerId,
payment_processor: "custom",
currency: "usd",
- timestamp: new Date(saleDate).toISOString(),
+ timestamp: new Date(saleEventDate).toISOString(),
});
await createPartnerCommission({
@@ -177,7 +181,7 @@ export const createCommissionAction = authActionClient
quantity: 1,
invoiceId,
currency: "usd",
- createdAt: saleDate,
+ createdAt: saleEventDate,
});
}
});
diff --git a/apps/web/lib/zod/schemas/commissions.ts b/apps/web/lib/zod/schemas/commissions.ts
index b3e1f2d606..02d50a9b65 100644
--- a/apps/web/lib/zod/schemas/commissions.ts
+++ b/apps/web/lib/zod/schemas/commissions.ts
@@ -54,8 +54,9 @@ export const createCommissionSchema = z.object({
partnerId: z.string(),
linkId: z.string(),
customerId: z.string(),
- saleDate: parseDateSchema.nullish(),
+ saleEventDate: parseDateSchema.nullish(),
saleAmount: z.number().min(0).nullish(),
invoiceId: z.string().nullish(),
- leadDate: parseDateSchema.nullish(),
+ leadEventDate: parseDateSchema.nullish(),
+ leadEventName: z.string().nullish(),
});