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.

Link bounty submissions to many commissions via Commission.bountySubmissionId

Kiran K4d471c8be4ce
diff --git a/apps/web/app/(ee)/api/bounties/[bountyId]/submissions/route.ts b/apps/web/app/(ee)/api/bounties/[bountyId]/submissions/route.ts
index 34faf5a188..e2507fb8ae 100644
--- a/apps/web/app/(ee)/api/bounties/[bountyId]/submissions/route.ts
+++ b/apps/web/app/(ee)/api/bounties/[bountyId]/submissions/route.ts
@@ -46,7 +46,11 @@ export const GET = withWorkspace(
},
include: {
user: true,
- commission: true,
+ commissions: {
+ orderBy: {
+ createdAt: "asc",
+ },
+ },
partner: true,
programEnrollment: true,
},
@@ -58,7 +62,7 @@ export const GET = withWorkspace(
});
const bountySubmissions = submissions.map(
- ({ partner, programEnrollment, commission, user, ...submissionData }) =>
+ ({ partner, programEnrollment, commissions, user, ...submissionData }) =>
BountySubmissionExtendedSchema.parse({
...submissionData,
partner: {
@@ -67,7 +71,7 @@ export const GET = withWorkspace(
id: partner.id,
status: programEnrollment?.status ?? null,
},
- commission,
+ commissions,
user,
}),
);
diff --git a/apps/web/app/(ee)/api/partner-profile/programs/[programId]/bounties/[bountyId]/route.ts b/apps/web/app/(ee)/api/partner-profile/programs/[programId]/bounties/[bountyId]/route.ts
index d783c09868..9d2840f0d5 100644
--- a/apps/web/app/(ee)/api/partner-profile/programs/[programId]/bounties/[bountyId]/route.ts
+++ b/apps/web/app/(ee)/api/partner-profile/programs/[programId]/bounties/[bountyId]/route.ts
@@ -58,13 +58,16 @@ export const GET = withPartnerProfile(async ({ partner, params }) => {
partnerId: partner.id,
},
include: {
- commission: {
+ commissions: {
select: {
id: true,
earnings: true,
status: true,
createdAt: true,
},
+ orderBy: {
+ createdAt: "asc",
+ },
},
},
},
diff --git a/apps/web/app/(ee)/api/workflows/create-partner-commission/route.ts b/apps/web/app/(ee)/api/workflows/create-partner-commission/route.ts
index aabca257e9..565c394f46 100644
--- a/apps/web/app/(ee)/api/workflows/create-partner-commission/route.ts
+++ b/apps/web/app/(ee)/api/workflows/create-partner-commission/route.ts
@@ -129,16 +129,16 @@ export const { POST } = serve<Input>(
});
// Step 3 (optional): Link the created commission to the bounty submission
+ // TODO: Remove once in-flight workflows from before the bountySubmissionId migration have drained (new commissions are linked in stepCreateCommission)
if (commission && bountySubmissionId) {
await context.run("set-bounty-commission", async () => {
- const { count } = await prisma.bountySubmission.updateMany({
+ const { count } = await prisma.commission.updateMany({
where: {
- id: bountySubmissionId,
- status: "approved",
- commissionId: null,
+ id: commission.id,
+ bountySubmissionId: null,
},
data: {
- commissionId: commission.id,
+ bountySubmissionId,
},
});
@@ -148,7 +148,7 @@ export const { POST } = serve<Input>(
});
} else {
return logAndReturn({
- outputLog: `Bounty submission ${bountySubmissionId} not found or already linked to a commission, skipping...`,
+ outputLog: `Commission ${commission.id} already linked to a bounty submission, skipping...`,
});
}
});
@@ -202,6 +202,7 @@ async function stepCreateCommission(
metadata,
context,
programEnrollment,
+ bountySubmissionId,
} = input;
if (typeof amount !== "number") {
@@ -495,6 +496,7 @@ async function stepCreateCommission(
status,
description,
createdAt,
+ bountySubmissionId,
metadata: metadata ?? Prisma.DbNull,
},
});
diff --git a/apps/web/app/(ee)/app.dub.co/embed/referrals/bounties/detail.tsx b/apps/web/app/(ee)/app.dub.co/embed/referrals/bounties/detail.tsx
index cd0923b357..b4a708db89 100644
--- a/apps/web/app/(ee)/app.dub.co/embed/referrals/bounties/detail.tsx
+++ b/apps/web/app/(ee)/app.dub.co/embed/referrals/bounties/detail.tsx
@@ -54,12 +54,12 @@ export function EmbedBountyDetail({
}) {
const [bounty, setBounty] = useState<PartnerBountyProps>(initialBounty);
- const hasRewards = bounty.submissions.some((s) => s.commission != null);
+ const hasRewards = bounty.submissions.some((s) => s.commissions.length > 0);
const handleSubmissionSuccess = (newSubmission: PartnerBountySubmission) => {
const normalized: PartnerBountySubmission = {
...newSubmission,
- commission: newSubmission.commission ?? null,
+ commissions: newSubmission.commissions ?? [],
};
setBounty((prev) => {
const existing = prev.submissions.find(
diff --git a/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/[programSlug]/(enrolled)/bounties/bounty-card.tsx b/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/[programSlug]/(enrolled)/bounties/bounty-card.tsx
index db8b3f2e20..f360857363 100644
--- a/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/[programSlug]/(enrolled)/bounties/bounty-card.tsx
+++ b/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/[programSlug]/(enrolled)/bounties/bounty-card.tsx
@@ -84,7 +84,8 @@ export function PartnerBountyCard({
</div>
)}
- {showRewards && bounty.submissions.some((s) => s.commission != null) && (
+ {showRewards &&
+ bounty.submissions.some((s) => s.commissions.length > 0) && (
<div className="@3xl/page:block border-border-subtle hidden border-t p-4">
<BountyRewardsTable
bounty={bounty}
@@ -105,9 +106,7 @@ export function BountyRewardsTable({
programSlug?: string;
className?: string;
}) {
- const rewards = bounty.submissions
- .filter((s) => s.commission != null)
- .map((s) => s.commission!);
+ const rewards = bounty.submissions.flatMap((s) => s.commissions);
const { table, ...tableProps } = useTable({
data: rewards,
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/[bountyId]/bounty-submission-details-sheet.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/[bountyId]/bounty-submission-details-sheet.tsx
index d760851ed5..b860e54c70 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/[bountyId]/bounty-submission-details-sheet.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/[bountyId]/bounty-submission-details-sheet.tsx
@@ -318,9 +318,12 @@ function BountySubmissionDetailsSheetContent({
{
label: "Reward",
value: (() => {
- if (submission.commission?.earnings != null) {
+ if (submission.commissions.length > 0) {
return currencyFormatter(
- submission.commission.earnings,
+ submission.commissions.reduce(
+ (total, { earnings }) => total + earnings,
+ 0,
+ ),
);
}
const estimatedEarnings =
@@ -536,9 +539,10 @@ function BountySubmissionDetailsSheetContent({
<div className="sticky bottom-0 z-10 border-t border-neutral-200 bg-white">
<div className="flex items-center justify-between gap-2 p-5">
- {submission.status === "approved" && submission.commission?.id ? (
+ {submission.status === "approved" &&
+ submission.commissions.length > 0 ? (
<Link
- href={`/${workspaceSlug}/program/commissions/${submission.commission.id}`}
+ href={`/${workspaceSlug}/program/commissions/${submission.commissions[0].id}`}
target="_blank"
className="w-full"
>
diff --git a/apps/web/lib/actions/partners/reopen-bounty-submission.ts b/apps/web/lib/actions/partners/reopen-bounty-submission.ts
index 1a70a994b7..de58f4e47f 100644
--- a/apps/web/lib/actions/partners/reopen-bounty-submission.ts
+++ b/apps/web/lib/actions/partners/reopen-bounty-submission.ts
@@ -37,7 +37,6 @@ export const reopenBountySubmissionAction = authActionClient
program: true,
bounty: true,
partner: true,
- commission: true,
},
});
@@ -63,24 +62,19 @@ export const reopenBountySubmissionAction = authActionClient
});
waitUntil(
- Promise.allSettled([
- recordAuditLog({
- workspaceId: workspace.id,
- programId: program.id,
- action: "bounty_submission.reopened",
- description: `Bounty submission reopened for ${partner.id}`,
- actor: user,
- targets: [
- {
- type: "bounty_submission",
- id: submissionId,
- metadata: BountySubmissionSchema.parse(bountySubmission),
- },
- ],
- }),
-
- // Email notification can be added later if needed
- Promise.resolve(),
- ]),
+ recordAuditLog({
+ workspaceId: workspace.id,
+ programId: program.id,
+ action: "bounty_submission.reopened",
+ description: `Bounty submission reopened for ${partner.id}`,
+ actor: user,
+ targets: [
+ {
+ type: "bounty_submission",
+ id: submissionId,
+ metadata: BountySubmissionSchema.parse(bountySubmission),
+ },
+ ],
+ }),
);
});
diff --git a/apps/web/lib/bounty/api/approve-bounty-submission.ts b/apps/web/lib/bounty/api/approve-bounty-submission.ts
index d99e94c686..80ea9da6e1 100644
--- a/apps/web/lib/bounty/api/approve-bounty-submission.ts
+++ b/apps/web/lib/bounty/api/approve-bounty-submission.ts
@@ -107,35 +107,49 @@ export async function approveBountySubmission({
});
}
- const approvedSubmission = await prisma.bountySubmission.update({
- where: {
- id: submissionId,
- },
- data: {
- status: "approved",
- reviewedAt: new Date(),
- userId: user.id,
- rejectionNote: null,
- rejectionReason: null,
- },
- include: {
- partner: {
- select: {
- id: true,
- email: true,
+ const approvedSubmission = await prisma.bountySubmission
+ .update({
+ where: {
+ id: submissionId,
+ status: {
+ not: "approved",
},
},
- program: {
- select: {
- workspaceId: true,
- id: true,
- name: true,
- slug: true,
- supportEmail: true,
+ data: {
+ status: "approved",
+ reviewedAt: new Date(),
+ userId: user.id,
+ rejectionNote: null,
+ rejectionReason: null,
+ },
+ include: {
+ partner: {
+ select: {
+ id: true,
+ email: true,
+ },
+ },
+ program: {
+ select: {
+ workspaceId: true,
+ id: true,
+ name: true,
+ slug: true,
+ supportEmail: true,
+ },
},
},
- },
- });
+ })
+ .catch((error) => {
+ if (error.code === "P2025") {
+ throw new DubApiError({
+ code: "bad_request",
+ message: "This bounty submission has already been approved.",
+ });
+ }
+
+ throw error;
+ });
await queuePartnerCommissionCreation({
event: "custom",
diff --git a/apps/web/lib/bounty/api/get-bounties-for-partner.ts b/apps/web/lib/bounty/api/get-bounties-for-partner.ts
index 372389702c..a8e1083b44 100644
--- a/apps/web/lib/bounty/api/get-bounties-for-partner.ts
+++ b/apps/web/lib/bounty/api/get-bounties-for-partner.ts
@@ -70,13 +70,16 @@ export async function getBountiesForPartner({
partnerId,
},
include: {
- commission: {
+ commissions: {
select: {
id: true,
earnings: true,
status: true,
createdAt: true,
},
+ orderBy: {
+ createdAt: "asc",
+ },
},
},
},
diff --git a/apps/web/lib/bounty/api/reject-bounty-submission.ts b/apps/web/lib/bounty/api/reject-bounty-submission.ts
index dab3385aee..61a9042eba 100644
--- a/apps/web/lib/bounty/api/reject-bounty-submission.ts
+++ b/apps/web/lib/bounty/api/reject-bounty-submission.ts
@@ -99,7 +99,6 @@ export async function rejectBountySubmission({
userId: user.id,
rejectionReason,
rejectionNote,
- commissionId: null,
},
include: {
partner: {
diff --git a/apps/web/lib/zod/schemas/bounties.ts b/apps/web/lib/zod/schemas/bounties.ts
index ea3cfff66d..7012b21cfc 100644
--- a/apps/web/lib/zod/schemas/bounties.ts
+++ b/apps/web/lib/zod/schemas/bounties.ts
@@ -344,13 +344,17 @@ export const BountySubmissionExtendedSchema = BountySubmissionSchema.extend({
bannedAt: true,
bannedReason: true,
}),
- commission: CommissionSchema.pick({
- id: true,
- amount: true,
- earnings: true,
- status: true,
- createdAt: true,
- }).nullable(),
+ commissions: z
+ .array(
+ CommissionSchema.pick({
+ id: true,
+ amount: true,
+ earnings: true,
+ status: true,
+ createdAt: true,
+ }),
+ )
+ .default([]),
user: UserSchema.pick({
id: true,
name: true,
diff --git a/apps/web/lib/zod/schemas/partner-profile.ts b/apps/web/lib/zod/schemas/partner-profile.ts
index a6026e7357..5af81e29ca 100644
--- a/apps/web/lib/zod/schemas/partner-profile.ts
+++ b/apps/web/lib/zod/schemas/partner-profile.ts
@@ -159,14 +159,16 @@ export const partnerNotificationTypes = z.enum([
]);
export const partnerBountySubmissionSchema = BountySubmissionSchema.extend({
- commission: PartnerEarningsSchema.pick({
- id: true,
- earnings: true,
- status: true,
- createdAt: true,
- })
- .nullable()
- .default(null),
+ commissions: z
+ .array(
+ PartnerEarningsSchema.pick({
+ id: true,
+ earnings: true,
+ status: true,
+ createdAt: true,
+ }),
+ )
+ .default([]),
});
export const PartnerBountySchema = BountySchema.omit({
diff --git a/apps/web/prisma/schema/bounty.prisma b/apps/web/prisma/schema/bounty.prisma
index 9e2fed99d3..0cf038e227 100644
--- a/apps/web/prisma/schema/bounty.prisma
+++ b/apps/web/prisma/schema/bounty.prisma
@@ -98,7 +98,7 @@ model BountySubmission {
bountyId String
performanceCount BigInt?
socialMetricCount Int?
- commissionId String? @unique
+ commissionId String? @unique // deprecated: kept only for the Commission.bountySubmissionId backfill, drop in a follow-up
userId String?
description String? @db.Text
status BountySubmissionStatus @default(draft)
@@ -115,7 +115,7 @@ model BountySubmission {
bounty Bounty @relation(fields: [bountyId], references: [id], onDelete: Cascade)
partner Partner @relation(fields: [partnerId], references: [id], onDelete: Cascade)
- commission Commission? @relation(fields: [commissionId], references: [id], onDelete: Cascade)
+ commissions Commission[]
program Program @relation(fields: [programId], references: [id], onDelete: Cascade)
user User? @relation(fields: [userId], references: [id])
programEnrollment ProgramEnrollment? @relation(fields: [programId, partnerId], references: [programId, partnerId], onDelete: Cascade)
diff --git a/apps/web/prisma/schema/commission.prisma b/apps/web/prisma/schema/commission.prisma
index 8e7f50a552..d625c79aaf 100644
--- a/apps/web/prisma/schema/commission.prisma
+++ b/apps/web/prisma/schema/commission.prisma
@@ -55,6 +55,7 @@ model Commission {
source CommissionSource?
sourceCommissionId String?
sourcePartnerId String?
+ bountySubmissionId String?
metadata Json? @db.Json
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@ -66,7 +67,7 @@ model Commission {
link Link? @relation(fields: [linkId], references: [id])
customer Customer? @relation(fields: [customerId], references: [id])
reward Reward? @relation(fields: [rewardId], references: [id])
- bountySubmission BountySubmission?
+ bountySubmission BountySubmission? @relation(fields: [bountySubmissionId], references: [id], onDelete: SetNull)
user User? @relation(fields: [userId], references: [id])
@@unique([invoiceId, programId])
@@ -82,5 +83,6 @@ model Commission {
@@index(status)
@@index(rewardId)
@@index(userId)
+ @@index(bountySubmissionId)
@@index([status, payoutId, source]) // temp index for migration script
}
diff --git a/apps/web/scripts/migrations/backfill-bounty-submission-commissions.ts b/apps/web/scripts/migrations/backfill-bounty-submission-commissions.ts
new file mode 100644
index 0000000000..b897105b34
--- /dev/null
+++ b/apps/web/scripts/migrations/backfill-bounty-submission-commissions.ts
@@ -0,0 +1,65 @@
+import { prisma } from "@/lib/prisma";
+import "dotenv-flow/config";
+
+const BATCH_SIZE = 500;
+
+// Backfill Commission.bountySubmissionId from the legacy BountySubmission.commissionId
+async function main() {
+ let cursor: string | undefined;
+ let totalLinked = 0;
+
+ while (true) {
+ const submissions = await prisma.bountySubmission.findMany({
+ where: {
+ commissionId: {
+ not: null,
+ },
+ },
+ select: {
+ id: true,
+ commissionId: true,
+ },
+ orderBy: {
+ id: "asc",
+ },
+ take: BATCH_SIZE,
+ ...(cursor && {
+ skip: 1,
+ cursor: {
+ id: cursor,
+ },
+ }),
+ });
+
+ if (submissions.length === 0) {
+ break;
+ }
+
+ cursor = submissions[submissions.length - 1].id;
+
+ const results = await prisma.$transaction(
+ submissions.map(({ id, commissionId }) =>
+ prisma.commission.updateMany({
+ where: {
+ id: commissionId!,
+ bountySubmissionId: null,
+ },
+ data: {
+ bountySubmissionId: id,
+ },
+ }),
+ ),
+ );
+
+ const linked = results.reduce((total, { count }) => total + count, 0);
+ totalLinked += linked;
+
+ console.log(
+ `Processed ${submissions.length} submissions, linked ${linked} commissions (total linked: ${totalLinked})`,
+ );
+ }
+
+ console.log(`Done. Linked ${totalLinked} commissions.`);
+}
+
+main();
diff --git a/apps/web/ui/partners/bounties/bounty-social-metrics-rewards-table.tsx b/apps/web/ui/partners/bounties/bounty-social-metrics-rewards-table.tsx
index 880a0b964b..e4cabe00ac 100644
--- a/apps/web/ui/partners/bounties/bounty-social-metrics-rewards-table.tsx
+++ b/apps/web/ui/partners/bounties/bounty-social-metrics-rewards-table.tsx
@@ -36,7 +36,7 @@ const displayStatusMap = {
interface SubmissionForRewards {
socialMetricCount: number | null;
- commission: { earnings: number } | null;
+ commissions: { earnings: number }[];
status: BountySubmissionProps["status"];
}
@@ -48,11 +48,7 @@ function getDisplayStatus(
return "inProgress";
}
- if (
- submission.status === "approved" &&
- submission.commission != null &&
- submission.commission.earnings != null
- ) {
+ if (submission.status === "approved" && submission.commissions.length > 0) {
return "approved";
}
diff --git a/apps/web/ui/partners/bounties/bounty-submission-details-sheet.tsx b/apps/web/ui/partners/bounties/bounty-submission-details-sheet.tsx
index d4e4a2ed98..a46951cd09 100644
--- a/apps/web/ui/partners/bounties/bounty-submission-details-sheet.tsx
+++ b/apps/web/ui/partners/bounties/bounty-submission-details-sheet.tsx
@@ -308,10 +308,8 @@ export function SubmissionRewardTable({
}: {
submission: PartnerBountySubmission;
}) {
- const rewards = submission.commission ? [submission.commission] : [];
-
const { table, ...tableProps } = useTable({
- data: rewards,
+ data: submission.commissions,
columns: [
{
id: "amount",
@@ -356,7 +354,7 @@ export function SubmissionRewardTable({
tdClassName: "border-l-transparent",
});
- if (rewards.length === 0) {
+ if (submission.commissions.length === 0) {
return null;
}