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.

Partner invite decline (#3805)

Pedro Ladeira33b52914a279
diff --git a/apps/web/app/(ee)/api/cron/cleanup/declined-invites/route.ts b/apps/web/app/(ee)/api/cron/cleanup/declined-invites/route.ts
new file mode 100644
index 0000000000..b5b2f315e8
--- /dev/null
+++ b/apps/web/app/(ee)/api/cron/cleanup/declined-invites/route.ts
@@ -0,0 +1,68 @@
+import { handleAndReturnErrorResponse } from "@/lib/api/errors";
+import { verifyQstashSignature } from "@/lib/cron/verify-qstash";
+import { prisma } from "@dub/prisma";
+import { log } from "@dub/utils";
+import { subDays } from "date-fns";
+import { NextResponse } from "next/server";
+
+export const dynamic = "force-dynamic";
+
+// Removes declined programEnrollments after 90 days so partners can be re-invited
+// Runs once every day at 03:00:00 AM UTC (0 3 * * *)
+// POST /api/cron/cleanup/declined-invites
+export async function POST(req: Request) {
+ try {
+ const rawBody = await req.text();
+
+ await verifyQstashSignature({
+ req,
+ rawBody,
+ });
+
+ while (true) {
+ // declined programEnrollments more than 90 days ago
+ const declinedProgramEnrollments =
+ await prisma.programEnrollment.findMany({
+ where: {
+ status: "declined",
+ updatedAt: {
+ lt: subDays(new Date(), 90),
+ },
+ // only delete if there are no messages (e.g. prior network messages)
+ messages: {
+ none: {},
+ },
+ },
+ take: 250,
+ });
+
+ if (declinedProgramEnrollments.length === 0) {
+ console.log(
+ "No more declined programEnrollments to delete, skipping...",
+ );
+ break;
+ }
+
+ const deletedRes = await prisma.programEnrollment.deleteMany({
+ where: {
+ id: {
+ in: declinedProgramEnrollments.map(({ id }) => id),
+ },
+ },
+ });
+
+ console.log(
+ `Deleted ${deletedRes.count} declined programEnrollments that are older than 90 days`,
+ );
+ }
+
+ return NextResponse.json({ status: "OK" });
+ } catch (error) {
+ await log({
+ message: `/api/cron/cleanup/declined-invites failed - ${error.message}`,
+ type: "errors",
+ });
+
+ return handleAndReturnErrorResponse(error);
+ }
+}
diff --git a/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/[programSlug]/invite/decline-program-invite-button.tsx b/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/[programSlug]/invite/decline-program-invite-button.tsx
new file mode 100644
index 0000000000..c3c896ab8c
--- /dev/null
+++ b/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/[programSlug]/invite/decline-program-invite-button.tsx
@@ -0,0 +1,39 @@
+"use client";
+
+import { declineProgramInviteAction } from "@/lib/actions/partners/decline-program-invite";
+import { mutatePrefix } from "@/lib/swr/mutate";
+import { ProgramProps } from "@/lib/types";
+import { Button } from "@dub/ui";
+import { useAction } from "next-safe-action/hooks";
+import { useRouter } from "next/navigation";
+import { toast } from "sonner";
+
+export function DeclineProgramInviteButton({
+ program,
+}: {
+ program: Pick<ProgramProps, "id">;
+}) {
+ const router = useRouter();
+
+ const { executeAsync: executeDeclineInvite, isPending: isDecliningInvite } =
+ useAction(declineProgramInviteAction, {
+ onSuccess: async () => {
+ await mutatePrefix("/api/partner-profile/programs");
+ toast.success("Program invite declined.");
+ router.push("/programs");
+ },
+ onError: ({ error }) => {
+ toast.error(error.serverError);
+ },
+ });
+
+ return (
+ <Button
+ variant="secondary"
+ onClick={() => executeDeclineInvite({ programId: program.id })}
+ loading={isDecliningInvite}
+ text="Decline"
+ className="h-9 rounded-lg"
+ />
+ );
+}
diff --git a/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/[programSlug]/invite/page.tsx b/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/[programSlug]/invite/page.tsx
index 7a44f1abfc..2cb92c8819 100644
--- a/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/[programSlug]/invite/page.tsx
+++ b/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/[programSlug]/invite/page.tsx
@@ -14,6 +14,7 @@ import { CircleCheckFill } from "@dub/ui";
import { OG_AVATAR_URL, cn } from "@dub/utils";
import { redirect } from "next/navigation";
import { AcceptProgramInviteButton } from "./accept-program-invite-button";
+import { DeclineProgramInviteButton } from "./decline-program-invite-button";
import { ProgramInviteConfetti } from "./program-invite-confetti";
export default async function ProgramInvitePage(props: {
@@ -116,7 +117,8 @@ export default async function ProgramInvitePage(props: {
on plans they purchase through your referral.
</p>
- <div className="mt-4 flex w-full justify-center">
+ <div className="mt-4 flex w-full justify-center gap-2">
+ <DeclineProgramInviteButton program={program} />
<AcceptProgramInviteButton program={program} />
</div>
</div>
diff --git a/apps/web/lib/actions/partners/decline-program-invite.ts b/apps/web/lib/actions/partners/decline-program-invite.ts
new file mode 100644
index 0000000000..9a47ed1230
--- /dev/null
+++ b/apps/web/lib/actions/partners/decline-program-invite.ts
@@ -0,0 +1,29 @@
+"use server";
+
+import { prisma } from "@dub/prisma";
+import * as z from "zod/v4";
+import { authPartnerActionClient } from "../safe-action";
+
+const declineProgramInviteSchema = z.object({
+ programId: z.string(),
+});
+
+export const declineProgramInviteAction = authPartnerActionClient
+ .inputSchema(declineProgramInviteSchema)
+ .action(async ({ parsedInput, ctx }) => {
+ const { partner } = ctx;
+ const { programId } = parsedInput;
+
+ await prisma.programEnrollment.update({
+ where: {
+ partnerId_programId: {
+ partnerId: partner.id,
+ programId,
+ },
+ status: "invited",
+ },
+ data: {
+ status: "declined",
+ },
+ });
+ });
diff --git a/apps/web/lib/actions/partners/message-partner.ts b/apps/web/lib/actions/partners/message-partner.ts
index 2f01c34756..c2f5ae50ff 100644
--- a/apps/web/lib/actions/partners/message-partner.ts
+++ b/apps/web/lib/actions/partners/message-partner.ts
@@ -43,7 +43,7 @@ export const messagePartnerAction = authActionClient
}
// Make sure partner is either discoverable, enrolled in the program, or already has a message with the program
- const { _count } = await prisma.partner.findFirstOrThrow({
+ const { _count, programs } = await prisma.partner.findFirstOrThrow({
where: {
id: partnerId,
OR: [
@@ -71,11 +71,6 @@ export const messagePartnerAction = authActionClient
include: {
_count: {
select: {
- programs: {
- where: {
- programId,
- },
- },
messages: {
where: {
programId,
@@ -83,12 +78,33 @@ export const messagePartnerAction = authActionClient
},
},
},
+ programs: {
+ where: {
+ programId,
+ },
+ select: {
+ status: true,
+ },
+ },
},
});
+ // if the partner is not enrolled / is in invited status, cap at one message
+ const enrollment = programs[0];
+ if (
+ (!enrollment || enrollment.status === "invited") &&
+ _count.messages >= 1
+ ) {
+ throw new DubApiError({
+ code: "forbidden",
+ message:
+ "You can only send one initial message to a partner you've invited.",
+ });
+ }
+
// if partner is not enrolled in the program and it's the first message
// it means the program is reaching out via the partner network
- if (_count.programs === 0 && _count.messages === 0) {
+ if (!enrollment && _count.messages === 0) {
const networkInvitesUsage = await getNetworkInvitesUsage(workspace);
if (networkInvitesUsage >= workspace.networkInvitesLimit) {