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 support for `disabledAt` in DiscountCode table

Steven Tey16245c930c3d
diff --git a/apps/web/app/(ee)/api/stripe/integration/webhook/utils/attribute-via-promotion-code-id.ts b/apps/web/app/(ee)/api/stripe/integration/webhook/utils/attribute-via-promotion-code-id.ts
index d7465ab9c0..3376fbc1ac 100644
--- a/apps/web/app/(ee)/api/stripe/integration/webhook/utils/attribute-via-promotion-code-id.ts
+++ b/apps/web/app/(ee)/api/stripe/integration/webhook/utils/attribute-via-promotion-code-id.ts
@@ -70,14 +70,21 @@ export async function attributeViaPromotionCodeId({
code: promotionCode.code,
},
},
- select: {
+ include: {
link: true,
},
});
if (!discountCode) {
console.log(
- `Couldn't find link associated with promotion code ${promotionCode.code}, skipping...`,
+ `Couldn't find discount code "${promotionCode.code}" in program "${workspace.defaultProgramId}", skipping...`,
+ );
+ return null;
+ }
+
+ if (discountCode.disabledAt) {
+ console.log(
+ `Discount code "${discountCode.code}" is disabled, skipping...`,
);
return null;
}
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/partners/[partnerId]/links/page.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/partners/[partnerId]/links/page.tsx
index 80704276aa..11256da140 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/partners/[partnerId]/links/page.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/partners/[partnerId]/links/page.tsx
@@ -333,7 +333,12 @@ const PartnerDiscountCodes = ({
{
id: "code",
header: "Code",
- cell: ({ row }) => <DiscountCodeBadge code={row.original.code} />,
+ cell: ({ row }) => (
+ <DiscountCodeBadge
+ code={row.original.code}
+ disabledAt={row.original.disabledAt}
+ />
+ ),
},
{
id: "shortLink",
diff --git a/apps/web/lib/discounts/delete-discount-code.ts b/apps/web/lib/discounts/delete-discount-code.ts
index a902751d6e..33c0232b36 100644
--- a/apps/web/lib/discounts/delete-discount-code.ts
+++ b/apps/web/lib/discounts/delete-discount-code.ts
@@ -30,7 +30,23 @@ export async function deleteDiscountCodes(
return;
}
- if (!isSoftDelete) {
+ if (isSoftDelete) {
+ // Soft delete the discount codes from the database (mark them as disabled)
+ const disabledDiscountCodes = await prisma.discountCode.updateMany({
+ where: {
+ id: {
+ in: discountCodes.map(({ id }) => id),
+ },
+ },
+ data: {
+ disabledAt: new Date(),
+ },
+ });
+
+ console.log(
+ `[deleteDiscountCodes] Disabled ${disabledDiscountCodes.count} discount codes.`,
+ );
+ } else {
// Delete the discount codes from the database
const deletedDiscountCodes = await prisma.discountCode.deleteMany({
where: {
diff --git a/apps/web/lib/zod/schemas/discount.ts b/apps/web/lib/zod/schemas/discount.ts
index 44c773972b..194d6d422f 100644
--- a/apps/web/lib/zod/schemas/discount.ts
+++ b/apps/web/lib/zod/schemas/discount.ts
@@ -62,6 +62,12 @@ export const DiscountCodeSchema = z.object({
discountId: z.string().nullable(),
partnerId: z.string(),
linkId: z.string(),
+ disabledAt: z.coerce
+ .date()
+ .nullish()
+ .describe(
+ "When this discount code was disabled, which happens when a partner is banned or deactivated.",
+ ),
});
export const createDiscountCodeSchema = z.object({
diff --git a/apps/web/prisma/schema/discount.prisma b/apps/web/prisma/schema/discount.prisma
index 937d56377f..296dc26043 100644
--- a/apps/web/prisma/schema/discount.prisma
+++ b/apps/web/prisma/schema/discount.prisma
@@ -34,6 +34,7 @@ model DiscountCode {
linkId String @unique
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
+ disabledAt DateTime?
program Program @relation(fields: [programId], references: [id], onDelete: Cascade)
discount Discount? @relation(fields: [discountId], references: [id], onDelete: SetNull)
diff --git a/apps/web/ui/partners/discounts/discount-code-badge.tsx b/apps/web/ui/partners/discounts/discount-code-badge.tsx
index a51d3a0108..946f414ee0 100644
--- a/apps/web/ui/partners/discounts/discount-code-badge.tsx
+++ b/apps/web/ui/partners/discounts/discount-code-badge.tsx
@@ -1,29 +1,65 @@
-import { Tag, useCopyToClipboard } from "@dub/ui";
+import { StatusBadge, Tag, Tooltip, useCopyToClipboard } from "@dub/ui";
import { cn } from "@dub/utils";
import { toast } from "sonner";
-export function DiscountCodeBadge({ code }: { code: string }) {
+export function DiscountCodeBadge({
+ code,
+ disabledAt,
+}: {
+ code: string;
+ disabledAt?: Date | string | null;
+}) {
const [copied, copyToClipboard] = useCopyToClipboard();
+ const isDisabled = !!disabledAt;
+
return (
- <button
- type="button"
- className={cn(
- "group/discountcode relative flex w-fit cursor-copy items-center gap-1 rounded-lg bg-green-200 px-2 py-1",
- "transition-colors duration-150 hover:bg-green-300/80",
- copied && "cursor-default",
+ <div className="flex items-center gap-2">
+ <button
+ type="button"
+ className={cn(
+ "group/discountcode relative flex w-fit cursor-copy items-center gap-1 rounded-lg px-2 py-1",
+ "transition-colors duration-150",
+ copied && "cursor-default",
+ isDisabled
+ ? "bg-neutral-200/80 line-through hover:bg-neutral-200/80 hover:no-underline"
+ : "bg-green-200 hover:bg-green-300/80",
+ )}
+ onClick={() =>
+ copyToClipboard(code, {
+ onSuccess: () => {
+ toast.success("Copied discount code to clipboard");
+ },
+ })
+ }
+ >
+ <Tag
+ className={cn(
+ "size-3",
+ isDisabled ? "text-neutral-500" : "text-green-700",
+ )}
+ strokeWidth={1.5}
+ />
+ <div
+ className={cn(
+ "text-xs font-medium decoration-dotted underline-offset-2 transition-colors group-hover/discountcode:underline",
+ isDisabled ? "text-neutral-600" : "text-green-700",
+ )}
+ >
+ {code}
+ </div>
+ </button>
+ {isDisabled && (
+ <Tooltip content="This discount code was disabled because the partner was banned or deactivated. To re-enable it, delete this code and create a new one.">
+ <StatusBadge
+ variant="neutral"
+ size="sm"
+ icon={null}
+ className="cursor-help"
+ >
+ Disabled
+ </StatusBadge>
+ </Tooltip>
)}
- onClick={() =>
- copyToClipboard(code, {
- onSuccess: () => {
- toast.success("Copied discount code to clipboard");
- },
- })
- }
- >
- <Tag className="size-3 text-green-700" strokeWidth={1.5} />
- <div className="text-xs font-medium text-green-700 decoration-dotted underline-offset-2 transition-colors group-hover/discountcode:underline">
- {code}
- </div>
- </button>
+ </div>
);
}