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.

add discount model

Kiran Kf1e58d6436d0
diff --git a/apps/web/lib/actions/partners/create-discount.ts b/apps/web/lib/actions/partners/create-discount.ts
index 7a25b470f1..3ad81991e0 100644
--- a/apps/web/lib/actions/partners/create-discount.ts
+++ b/apps/web/lib/actions/partners/create-discount.ts
@@ -10,7 +10,7 @@ export const createDiscountAction = authActionClient
.schema(createDiscountSchema)
.action(async ({ parsedInput, ctx }) => {
const { workspace } = ctx;
- const { programId, partnerIds, amount, maxDuration } = parsedInput;
+ const { programId, partnerIds, amount, type, maxDuration } = parsedInput;
const program = await getProgramOrThrow({
workspaceId: workspace.id,
@@ -56,6 +56,7 @@ export const createDiscountAction = authActionClient
data: {
id: createId({ prefix: "dis_" }),
programId,
+ type,
amount,
maxDuration,
},
diff --git a/apps/web/lib/actions/partners/update-discount.ts b/apps/web/lib/actions/partners/update-discount.ts
index 872a21a46f..eedf1e601e 100644
--- a/apps/web/lib/actions/partners/update-discount.ts
+++ b/apps/web/lib/actions/partners/update-discount.ts
@@ -10,7 +10,7 @@ export const updateDiscountAction = authActionClient
.schema(updateDiscountSchema)
.action(async ({ parsedInput, ctx }) => {
const { workspace } = ctx;
- const { programId, discountId, partnerIds, amount, maxDuration } =
+ const { programId, discountId, partnerIds, amount, type, maxDuration } =
parsedInput;
const program = await getProgramOrThrow({
@@ -52,6 +52,7 @@ export const updateDiscountAction = authActionClient
id: discountId,
},
data: {
+ type,
amount,
maxDuration,
},
diff --git a/apps/web/lib/zod/schemas/discount.ts b/apps/web/lib/zod/schemas/discount.ts
index 8b302a4993..dd62f0e792 100644
--- a/apps/web/lib/zod/schemas/discount.ts
+++ b/apps/web/lib/zod/schemas/discount.ts
@@ -15,6 +15,7 @@ export const DiscountSchema = z.object({
export const createDiscountSchema = z.object({
amount: z.number().min(0),
+ type: z.nativeEnum(CommissionType).default("flat"),
maxDuration: z.coerce
.number()
.refine((val) => RECURRING_MAX_DURATIONS.includes(val), {
diff --git a/apps/web/ui/partners/add-edit-discount-sheet.tsx b/apps/web/ui/partners/add-edit-discount-sheet.tsx
index 4c9d3bbd11..a394339975 100644
--- a/apps/web/ui/partners/add-edit-discount-sheet.tsx
+++ b/apps/web/ui/partners/add-edit-discount-sheet.tsx
@@ -10,10 +10,8 @@ import usePartners from "@/lib/swr/use-partners";
import useProgram from "@/lib/swr/use-program";
import useWorkspace from "@/lib/swr/use-workspace";
import { DiscountProps, EnrolledPartnerProps } from "@/lib/types";
-import {
- createRewardSchema,
- RECURRING_MAX_DURATIONS,
-} from "@/lib/zod/schemas/rewards";
+import { createDiscountSchema } from "@/lib/zod/schemas/discount";
+import { RECURRING_MAX_DURATIONS } from "@/lib/zod/schemas/rewards";
import { SelectEligiblePartnersSheet } from "@/ui/partners/select-eligible-partners-sheet";
import { X } from "@/ui/shared/icons";
import {
@@ -49,7 +47,7 @@ interface DiscountSheetProps {
isDefault?: boolean;
}
-type FormData = z.infer<typeof createRewardSchema>;
+type FormData = z.infer<typeof createDiscountSchema>;
const discountTypes = [
{
@@ -89,13 +87,15 @@ function DiscountSheetContent({
formState: { errors },
} = useForm<FormData>({
defaultValues: {
- maxDuration: discount
- ? discount.maxDuration === null
- ? Infinity
- : discount.maxDuration
- : 0,
- amount: discount?.amount,
partnerIds: null,
+ maxDuration: 0,
+ ...(discount && {
+ amount:
+ discount.type === "flat" ? discount.amount / 100 : discount.amount,
+ type: discount.type,
+ maxDuration:
+ discount.maxDuration === null ? Infinity : discount.maxDuration,
+ }),
},
});
@@ -188,10 +188,11 @@ function DiscountSheetContent({
const payload = {
...data,
- maxDuration:
- Number(data.maxDuration) === Infinity ? null : data.maxDuration,
workspaceId,
programId: program.id,
+ amount: data.type === "flat" ? data.amount * 100 : data.amount,
+ maxDuration:
+ Number(data.maxDuration) === Infinity ? null : data.maxDuration,
};
if (!discount) {
@@ -422,6 +423,24 @@ function DiscountSheetContent({
</div>
</div>
+ <div>
+ <label
+ htmlFor="type"
+ className="text-sm font-medium text-neutral-800"
+ >
+ Discount model
+ </label>
+ <div className="relative mt-2 rounded-md shadow-sm">
+ <select
+ className="block w-full rounded-md border-neutral-300 text-neutral-900 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm"
+ {...register("type")}
+ >
+ <option value="percentage">Percentage</option>
+ <option value="flat">Flat</option>
+ </select>
+ </div>
+ </div>
+
<div>
<label
htmlFor="amount"