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.

Trigger workflows for acceptProgramInviteAction (#3749)

Steven Teyda1fcdfe2896
diff --git a/apps/web/app/(ee)/api/workflows/partner-approved/route.ts b/apps/web/app/(ee)/api/workflows/partner-approved/route.ts
index eb3a902692..4598012fd2 100644
--- a/apps/web/app/(ee)/api/workflows/partner-approved/route.ts
+++ b/apps/web/app/(ee)/api/workflows/partner-approved/route.ts
@@ -1,11 +1,10 @@
-import { createDiscountCode } from "@/lib/api/discounts/create-discount-code";
+import { generateDiscountCodeForPartner } from "@/lib/api/discounts/generate-discount-code-for-partner";
import { createPartnerDefaultLinks } from "@/lib/api/partners/create-partner-default-links";
import { getGroupRewardsAndBounties } from "@/lib/api/partners/get-group-rewards-and-bounties";
import { getProgramEnrollmentOrThrow } from "@/lib/api/programs/get-program-enrollment-or-throw";
import { executeWorkflows } from "@/lib/api/workflows/execute-workflows";
import { triggerDraftBountySubmissionCreation } from "@/lib/bounty/api/trigger-draft-bounty-submissions";
import { createWorkflowLogger } from "@/lib/cron/qstash-workflow-logger";
-import { stripeIntegrationSettingsSchema } from "@/lib/integrations/stripe/schema";
import { polyfillSocialMediaFields } from "@/lib/social-utils";
import { PlanProps } from "@/lib/types";
import { sendWorkspaceWebhook } from "@/lib/webhook/publish";
@@ -14,7 +13,6 @@ import { ProgramPartnerLinkSchema } from "@/lib/zod/schemas/programs";
import { sendBatchEmail } from "@dub/email";
import PartnerApplicationApproved from "@dub/email/templates/partner-application-approved";
import { prisma } from "@dub/prisma";
-import { STRIPE_INTEGRATION_ID } from "@dub/utils";
import { serve } from "@upstash/workflow/nextjs";
import * as z from "zod/v4";
@@ -171,87 +169,19 @@ export const { POST } = serve<Payload>(
// Step 2: Auto-provision discount code if enabled
await context.run("create-discount-codes", async () => {
- if (!groupId) {
- return;
- }
-
- const group = await prisma.partnerGroup.findUnique({
- where: {
- id: groupId,
- },
- include: {
- discount: true,
- },
- });
-
- if (!group?.discount?.autoProvisionEnabledAt) {
- return;
- }
-
- const workspace = await prisma.project.findUniqueOrThrow({
- where: {
- id: program.workspaceId,
- },
- select: {
- stripeConnectId: true,
- installedIntegrations: {
- where: {
- integrationId: STRIPE_INTEGRATION_ID,
- },
- },
- },
+ logger.info({
+ message: "Started executing workflow step 'create-discount-codes'.",
+ data: input,
});
- if (!workspace.stripeConnectId) {
- console.log("Workspace does not have stripeConnectId");
- return;
- }
-
- if (!workspace.installedIntegrations.length) {
- console.log("Workspace does not have the Stripe integration installed");
- return;
- }
-
- const stripeIntegrationSettings = stripeIntegrationSettingsSchema.parse(
- workspace.installedIntegrations[0].settings || {},
- );
-
- const partnerLinks = await prisma.link.findMany({
- where: {
- programId,
- partnerId,
- partnerGroupDefaultLinkId: {
- not: null,
- },
- discountCode: {
- is: null,
- },
- },
- select: {
- id: true,
+ await generateDiscountCodeForPartner({
+ workspaceId: program.workspaceId,
+ partner: {
+ id: partner.id,
+ name: partner.name,
+ groupId,
},
});
-
- if (partnerLinks.length === 0) {
- return;
- }
-
- for (const link of partnerLinks) {
- try {
- await createDiscountCode({
- stripeConnectId: workspace.stripeConnectId,
- stripeMode: stripeIntegrationSettings.stripeMode,
- partner,
- link,
- discount: group.discount,
- });
- } catch (error) {
- console.error(
- `Failed to create discount code for link ${link.id}:`,
- error,
- );
- }
- }
});
// Step 3: Send email to partner application approved
@@ -364,7 +294,6 @@ export const { POST } = serve<Payload>(
...partner,
...polyfillSocialMediaFields(partnerPlatforms),
id: partner.id,
- status: programEnrollment.status,
links: allPartnerLinks,
});
@@ -401,10 +330,6 @@ export const { POST } = serve<Payload>(
programId,
partnerIds: [partnerId],
});
-
- logger.info({
- message: `Triggered draft bounty submission creation for partner ${partnerId} in program ${programId}.`,
- });
});
// Step 6: Execute Dub workflows using the “partnerEnrolled” trigger.
diff --git a/apps/web/lib/actions/partners/accept-program-invite.ts b/apps/web/lib/actions/partners/accept-program-invite.ts
index 86de8597d3..01838fd1ed 100644
--- a/apps/web/lib/actions/partners/accept-program-invite.ts
+++ b/apps/web/lib/actions/partners/accept-program-invite.ts
@@ -1,5 +1,9 @@
"use server";
+import { generateDiscountCodeForPartner } from "@/lib/api/discounts/generate-discount-code-for-partner";
+import { executeWorkflows } from "@/lib/api/workflows/execute-workflows";
+import { triggerDraftBountySubmissionCreation } from "@/lib/bounty/api/trigger-draft-bounty-submissions";
+import { polyfillSocialMediaFields } from "@/lib/social-utils";
import { sendWorkspaceWebhook } from "@/lib/webhook/publish";
import { EnrolledPartnerSchema } from "@/lib/zod/schemas/partners";
import { prisma } from "@dub/prisma";
@@ -31,15 +35,25 @@ export const acceptProgramInviteAction = authPartnerActionClient
},
include: {
links: true,
+ partner: {
+ include: {
+ platforms: true,
+ },
+ },
},
});
+ // TODO: Move these into a workflow similar to `/api/workflows/partner-approved/route.ts`
waitUntil(
(async () => {
const workspace = await prisma.project.findUnique({
where: {
defaultProgramId: programId,
},
+ select: {
+ id: true,
+ webhookEnabled: true,
+ },
});
if (!workspace) {
@@ -51,13 +65,36 @@ export const acceptProgramInviteAction = authPartnerActionClient
...partner,
...enrollment,
id: partner.id,
+ ...polyfillSocialMediaFields(enrollment.partner.platforms),
});
- await sendWorkspaceWebhook({
- workspace,
- trigger: "partner.enrolled",
- data: enrolledPartner,
- });
+ await Promise.allSettled([
+ // 1. Generate discount code for partner (if enabled)
+ generateDiscountCodeForPartner({
+ workspaceId: workspace.id,
+ partner: enrolledPartner,
+ }),
+ // 2. Send "partner.enrolled" webhook to workspace
+ sendWorkspaceWebhook({
+ workspace,
+ trigger: "partner.enrolled",
+ data: enrolledPartner,
+ }),
+ // 3. Trigger draft bounty submission creation
+ triggerDraftBountySubmissionCreation({
+ programId,
+ partnerIds: [enrolledPartner.id],
+ }),
+ // 4. Execute Dub workflows using the “partnerEnrolled” trigger.
+ executeWorkflows({
+ trigger: "partnerEnrolled",
+ identity: {
+ workspaceId: workspace.id,
+ programId,
+ partnerId: enrolledPartner.id,
+ },
+ }),
+ ]);
})(),
);
});
diff --git a/apps/web/lib/api/discounts/generate-discount-code-for-partner.ts b/apps/web/lib/api/discounts/generate-discount-code-for-partner.ts
new file mode 100644
index 0000000000..545c519216
--- /dev/null
+++ b/apps/web/lib/api/discounts/generate-discount-code-for-partner.ts
@@ -0,0 +1,102 @@
+import { stripeIntegrationSettingsSchema } from "@/lib/integrations/stripe/schema";
+import { EnrolledPartnerProps } from "@/lib/types";
+import { prisma } from "@dub/prisma";
+import { STRIPE_INTEGRATION_ID } from "@dub/utils";
+import { createDiscountCode } from "./create-discount-code";
+
+export async function generateDiscountCodeForPartner({
+ workspaceId,
+ partner,
+}: {
+ workspaceId: string;
+ partner: Pick<EnrolledPartnerProps, "id" | "name" | "groupId">;
+}) {
+ if (!partner.groupId) {
+ console.log(
+ `No group ID provided for partner ${partner.id}, skipping discount code creation...`,
+ );
+ return;
+ }
+
+ const group = await prisma.partnerGroup.findUnique({
+ where: {
+ id: partner.groupId,
+ },
+ include: {
+ discount: true,
+ },
+ });
+
+ if (!group?.discount?.autoProvisionEnabledAt) {
+ console.log(
+ `Group ${partner.groupId} does not have auto provision enabled, skipping discount code creation...`,
+ );
+ return;
+ }
+
+ const workspace = await prisma.project.findUniqueOrThrow({
+ where: {
+ id: workspaceId,
+ },
+ select: {
+ stripeConnectId: true,
+ installedIntegrations: {
+ where: {
+ integrationId: STRIPE_INTEGRATION_ID,
+ },
+ },
+ },
+ });
+
+ if (!workspace.stripeConnectId) {
+ console.log("Workspace does not have stripeConnectId");
+ return;
+ }
+
+ if (!workspace.installedIntegrations.length) {
+ console.log("Workspace does not have the Stripe integration installed");
+ return;
+ }
+
+ const stripeIntegrationSettings = stripeIntegrationSettingsSchema.parse(
+ workspace.installedIntegrations[0].settings || {},
+ );
+
+ const partnerDefaultLink = await prisma.link.findFirst({
+ where: {
+ programId: group.programId,
+ partnerId: partner.id,
+ partnerGroupDefaultLinkId: {
+ not: null,
+ },
+ discountCode: {
+ is: null,
+ },
+ },
+ select: {
+ id: true,
+ },
+ });
+
+ if (!partnerDefaultLink) {
+ console.log(
+ `No partner default link found for partner ${partner.id}, skipping discount code creation...`,
+ );
+ return;
+ }
+
+ try {
+ await createDiscountCode({
+ stripeConnectId: workspace.stripeConnectId,
+ stripeMode: stripeIntegrationSettings.stripeMode,
+ partner,
+ link: partnerDefaultLink,
+ discount: group.discount,
+ });
+ } catch (error) {
+ console.error(
+ `Failed to create discount code for link ${partnerDefaultLink.id}:`,
+ error,
+ );
+ }
+}