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.

allow removing default discount

Steven Teybafe8f175710
diff --git a/apps/web/lib/actions/partners/delete-discount.ts b/apps/web/lib/actions/partners/delete-discount.ts
index 111b1e196a..c20ae69c86 100644
--- a/apps/web/lib/actions/partners/delete-discount.ts
+++ b/apps/web/lib/actions/partners/delete-discount.ts
@@ -1,6 +1,5 @@
"use server";
-import { DubApiError } from "@/lib/api/errors";
import { getDiscountOrThrow } from "@/lib/api/partners/get-discount-or-throw";
import { getProgramOrThrow } from "@/lib/api/programs/get-program-or-throw";
import { prisma } from "@dub/prisma";
@@ -29,25 +28,28 @@ export const deleteDiscountAction = authActionClient
discountId,
});
- if (program.defaultDiscountId === discountId) {
- throw new DubApiError({
- code: "bad_request",
- message: "This is a default discount and cannot be deleted.",
+ await prisma.$transaction(async (tx) => {
+ // if this is the default discount, set the program default discount to null
+ if (program.defaultDiscountId === discountId) {
+ await tx.program.update({
+ where: { id: programId },
+ data: { defaultDiscountId: null },
+ });
+ }
+ // update all program enrollments to have no discount
+ await tx.programEnrollment.updateMany({
+ where: {
+ discountId,
+ },
+ data: {
+ discountId: null,
+ },
+ });
+ // delete the discount
+ await tx.discount.delete({
+ where: {
+ id: discountId,
+ },
});
- }
-
- await prisma.discount.delete({
- where: {
- id: discountId,
- },
- });
-
- await prisma.programEnrollment.updateMany({
- where: {
- discountId,
- },
- data: {
- discountId: null,
- },
});
});
diff --git a/apps/web/lib/zod/schemas/misc.ts b/apps/web/lib/zod/schemas/misc.ts
index 67c77f1e47..f85ff6363f 100644
--- a/apps/web/lib/zod/schemas/misc.ts
+++ b/apps/web/lib/zod/schemas/misc.ts
@@ -48,4 +48,4 @@ export const maxDurationSchema = z.coerce
.refine((val) => RECURRING_MAX_DURATIONS.includes(val), {
message: `Max duration must be ${RECURRING_MAX_DURATIONS.join(", ")}`,
})
- .nullable();
+ .nullish();
diff --git a/apps/web/ui/partners/add-edit-discount-sheet.tsx b/apps/web/ui/partners/add-edit-discount-sheet.tsx
index f4c9352abd..ccec9f7914 100644
--- a/apps/web/ui/partners/add-edit-discount-sheet.tsx
+++ b/apps/web/ui/partners/add-edit-discount-sheet.tsx
@@ -88,12 +88,12 @@ function DiscountSheetContent({
} = useForm<FormData>({
defaultValues: {
...(discount && {
- amount: discount?.amount
+ amount: discount.amount
? discount.type === "flat"
? discount.amount / 100
: discount.amount
: 0,
- type: discount?.type || "flat",
+ type: discount.type || "flat",
maxDuration:
discount?.maxDuration === null
? Infinity
@@ -104,7 +104,7 @@ function DiscountSheetContent({
},
});
- const [partnerIds = []] = watch(["partnerIds"]);
+ const [type, partnerIds = []] = watch(["type", "partnerIds"]);
const partnersCount = discount?.partnersCount || 0;
const { data: discountPartners, loading: isLoadingDiscountPartners } =
@@ -200,6 +200,7 @@ function DiscountSheetContent({
};
if (!discount) {
+ console.log("payload", payload);
await createDiscount(payload);
} else {
await updateDiscount({
@@ -287,9 +288,6 @@ function DiscountSheetContent({
isUpdating ||
(!isDefault && (!partnerIds || partnerIds.length === 0));
- const canDeleteDiscount =
- discount && program?.defaultDiscountId !== discount.id;
-
return (
<>
<form
@@ -432,11 +430,17 @@ function DiscountSheetContent({
Amount
</label>
<div className="relative mt-2 rounded-md shadow-sm">
+ {type === "flat" && (
+ <span className="absolute inset-y-0 left-0 flex items-center pl-3 text-sm text-neutral-400">
+ $
+ </span>
+ )}
<input
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",
errors.amount &&
- "border-red-600 pr-7 focus:border-red-500 focus:ring-red-600",
+ "border-red-600 focus:border-red-500 focus:ring-red-600",
+ type === "flat" ? "pl-6 pr-12" : "pr-7",
)}
{...register("amount", {
valueAsNumber: true,
@@ -449,7 +453,7 @@ function DiscountSheetContent({
placeholder={"0"}
/>
<span className="absolute inset-y-0 right-0 flex items-center pr-3 text-sm text-neutral-400">
- %
+ {type === "flat" ? "USD" : "%"}
</span>
</div>
</div>
@@ -561,12 +565,6 @@ function DiscountSheetContent({
text="Remove discount"
onClick={onDelete}
loading={isDeleting}
- disabled={!canDeleteDiscount}
- disabledTooltip={
- program?.defaultDiscountId === discount.id
- ? "This is a default discount and cannot be deleted."
- : undefined
- }
/>
)}
</div>