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.

remove recalculateSalesEarnings for now

Steven Tey8fa8edc8f73d
diff --git a/apps/web/lib/actions/partners/create-manual-payout.ts b/apps/web/lib/actions/partners/create-manual-payout.ts
index 815a810f03..777ae77421 100644
--- a/apps/web/lib/actions/partners/create-manual-payout.ts
+++ b/apps/web/lib/actions/partners/create-manual-payout.ts
@@ -2,12 +2,11 @@
import { getAnalytics } from "@/lib/analytics/get-analytics";
import { getProgramOrThrow } from "@/lib/api/programs/get-program";
-import { calculateEarnings } from "@/lib/api/sales/commission";
import { createId } from "@/lib/api/utils";
import { createSalesPayout } from "@/lib/partners/create-sales-payout";
import { createManualPayoutSchema } from "@/lib/zod/schemas/payouts";
import { prisma } from "@dub/prisma";
-import { Payout, Program } from "@dub/prisma/client";
+import { Payout } from "@dub/prisma/client";
import { authActionClient } from "../safe-action";
const schema = createManualPayoutSchema.refine(
@@ -54,17 +53,7 @@ export const createManualPayoutAction = authActionClient
// Create a payout for sales
if (type === "sales") {
- await recalculateSalesEarnings({
- start: start!,
- end: end!,
- partnerId,
- program: {
- commissionAmount: amount,
- commissionType: program.commissionType,
- },
- });
-
- // after recalculating the earnings, create the payout
+ // create the payout
payout = await createSalesPayout({
programId,
partnerId,
@@ -113,74 +102,3 @@ export const createManualPayoutAction = authActionClient
return payout;
});
-
-// Recalculate earnings for sale based on new commission amount
-const recalculateSalesEarnings = async ({
- start,
- end,
- partnerId,
- program,
-}: {
- start: Date;
- end: Date;
- partnerId: string;
- program: Pick<Program, "commissionAmount" | "commissionType">;
-}) => {
- const sales = await prisma.sale.findMany({
- where: {
- AND: {
- status: "pending",
- partnerId,
- createdAt: {
- gte: start.toISOString(),
- lte: end.toISOString(),
- },
- },
- OR: [
- {
- commissionAmount: {
- not: program.commissionAmount,
- },
- },
- {
- commissionType: {
- not: program.commissionType,
- },
- },
- ],
- },
- select: {
- id: true,
- amount: true,
- },
- });
-
- if (!sales.length) {
- return;
- }
-
- // Calculate the new earnings for each sale
- const updatedEarnings = sales.map((sale) => {
- return {
- saleId: sale.id,
- earnings: calculateEarnings({
- program,
- sales: 1,
- saleAmount: sale.amount,
- }),
- };
- });
-
- await Promise.all(
- updatedEarnings.map(({ saleId, earnings }) =>
- prisma.sale.update({
- where: { id: saleId },
- data: {
- commissionAmount: program.commissionAmount,
- commissionType: program.commissionType,
- earnings,
- },
- }),
- ),
- );
-};
diff --git a/apps/web/lib/api/sales/recalculate-sales-earnings.ts b/apps/web/lib/api/sales/recalculate-sales-earnings.ts
new file mode 100644
index 0000000000..884b50ca1a
--- /dev/null
+++ b/apps/web/lib/api/sales/recalculate-sales-earnings.ts
@@ -0,0 +1,74 @@
+import { prisma } from "@dub/prisma";
+import { Program } from "@dub/prisma/client";
+import { calculateEarnings } from "./commission";
+
+// Recalculate earnings for sale based on new commission amount
+export const recalculateSalesEarnings = async ({
+ start,
+ end,
+ partnerId,
+ program,
+}: {
+ start: Date;
+ end: Date;
+ partnerId: string;
+ program: Pick<Program, "commissionAmount" | "commissionType">;
+}) => {
+ const sales = await prisma.sale.findMany({
+ where: {
+ AND: {
+ status: "pending",
+ partnerId,
+ createdAt: {
+ gte: start.toISOString(),
+ lte: end.toISOString(),
+ },
+ },
+ OR: [
+ {
+ commissionAmount: {
+ not: program.commissionAmount,
+ },
+ },
+ {
+ commissionType: {
+ not: program.commissionType,
+ },
+ },
+ ],
+ },
+ select: {
+ id: true,
+ amount: true,
+ },
+ });
+
+ if (!sales.length) {
+ return;
+ }
+
+ // Calculate the new earnings for each sale
+ const updatedEarnings = sales.map((sale) => {
+ return {
+ saleId: sale.id,
+ earnings: calculateEarnings({
+ program,
+ sales: 1,
+ saleAmount: sale.amount,
+ }),
+ };
+ });
+
+ await Promise.all(
+ updatedEarnings.map(({ saleId, earnings }) =>
+ prisma.sale.update({
+ where: { id: saleId },
+ data: {
+ commissionAmount: program.commissionAmount,
+ commissionType: program.commissionType,
+ earnings,
+ },
+ }),
+ ),
+ );
+};
diff --git a/apps/web/ui/partners/create-payout-sheet.tsx b/apps/web/ui/partners/create-payout-sheet.tsx
index 9a8d2d5e73..8c2f451a33 100644
--- a/apps/web/ui/partners/create-payout-sheet.tsx
+++ b/apps/web/ui/partners/create-payout-sheet.tsx
@@ -105,11 +105,7 @@ function CreatePayoutSheetContent({
} = useForm<FormData>({
resolver: zodResolver(schema),
defaultValues: {
- type: "sales",
- amount:
- program?.commissionType === "flat"
- ? Number(program?.commissionAmount) / 100
- : program?.commissionAmount,
+ type: "custom",
},
});
@@ -463,12 +459,13 @@ function CreatePayoutSheetContent({
clearErrors(["start", "end"]);
}
- if (
- type === "sales" &&
- program?.commissionAmount &&
- program.commissionType === "flat"
- ) {
- setValue("amount", program.commissionAmount / 100);
+ if (type === "sales" && program?.commissionAmount) {
+ setValue(
+ "amount",
+ program.commissionType === "percentage"
+ ? program.commissionAmount
+ : program.commissionAmount / 100,
+ );
} else {
// @ts-ignore
setValue("amount", null);
@@ -486,9 +483,18 @@ function CreatePayoutSheetContent({
<div>
<label
htmlFor="amount"
- className="text-sm font-medium text-neutral-800"
+ className="flex justify-between text-sm font-medium text-neutral-800"
>
Reward amount
+ {payoutType === "sales" && (
+ <a
+ href={`/${slug}/programs/${programId}/settings`}
+ target="_blank"
+ className="font-normal text-gray-400 underline-offset-2 transition-all hover:text-gray-600 hover:underline"
+ >
+ Manage
+ </a>
+ )}
</label>
<div className="relative mt-2 rounded-md shadow-sm">
{!isPercentageBased && (
@@ -500,6 +506,8 @@ function CreatePayoutSheetContent({
className={cn(
"block w-full rounded-md border-neutral-300 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm",
"pr-[6.5rem]",
+ payoutType === "sales" &&
+ "cursor-not-allowed bg-neutral-100 text-neutral-500",
payoutType === "custom" && "pr-12",
!isPercentageBased && "pl-6",
errors.amount &&
@@ -508,9 +516,10 @@ function CreatePayoutSheetContent({
{...register("amount", {
required: true,
valueAsNumber: true,
+ disabled: payoutType === "sales",
})}
autoComplete="off"
- placeholder="5"
+ placeholder="100"
/>
<span className="absolute inset-y-0 right-0 flex items-center pr-3 text-sm text-neutral-400">
{isPercentageBased ? "%" : "USD"}