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 partiallyApproved status for social metrics bounty submissions

Rafia355884d7a93
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 e2507fb8ae..0383fe66be 100644
--- a/apps/web/app/(ee)/api/bounties/[bountyId]/submissions/route.ts
+++ b/apps/web/app/(ee)/api/bounties/[bountyId]/submissions/route.ts
@@ -33,7 +33,7 @@ export const GET = withWorkspace(
where: {
bountyId,
status: status ?? {
- in: ["draft", "submitted", "approved"],
+ in: ["draft", "submitted", "partiallyApproved", "approved"],
},
...(groupId && {
programEnrollment: {
diff --git a/apps/web/app/(ee)/api/bounties/[bountyId]/sync-social-metrics/route.ts b/apps/web/app/(ee)/api/bounties/[bountyId]/sync-social-metrics/route.ts
index 122f7207f2..5db613cf51 100644
--- a/apps/web/app/(ee)/api/bounties/[bountyId]/sync-social-metrics/route.ts
+++ b/apps/web/app/(ee)/api/bounties/[bountyId]/sync-social-metrics/route.ts
@@ -6,7 +6,10 @@ import { getEffectiveBountyPeriod } from "@/lib/bounty/api/bounty-availability";
import { getBountyOrThrow } from "@/lib/bounty/api/get-bounty-or-throw";
import { getSocialMetricsUpdates } from "@/lib/bounty/api/get-social-metrics-updates";
import { isBountyEnded, isBountyStarted } from "@/lib/bounty/bounty-period";
-import { hasReachedSocialMetricsEarningCap } from "@/lib/bounty/social-metrics-milestones";
+import {
+ getPendingSocialMetricsMilestones,
+ hasReachedSocialMetricsEarningCap,
+} from "@/lib/bounty/social-metrics-milestones";
import { resolveBountyDetails } from "@/lib/bounty/utils";
import { qstash } from "@/lib/cron";
import { prisma } from "@/lib/prisma";
@@ -55,6 +58,7 @@ export const POST = withWorkspace(
urls: true,
status: true,
socialMetricCount: true,
+ approvedSocialMetricThreshold: true,
partner: true,
programEnrollment: {
select: {
@@ -181,14 +185,45 @@ export const POST = withWorkspace(
updateData.completedAt = new Date();
}
- await prisma.bountySubmission.update({
- where: {
- id: submissionId,
- },
- data: {
- ...updateData,
- },
- });
+ // A partially approved submission goes back to review when it reaches a new milestone
+ const hasReachedNewMilestone =
+ submission.status === "partiallyApproved" &&
+ getPendingSocialMetricsMilestones({
+ bounty,
+ submission: {
+ socialMetricCount,
+ approvedSocialMetricThreshold:
+ submission.approvedSocialMetricThreshold,
+ },
+ }).length > 0;
+
+ await prisma.$transaction([
+ prisma.bountySubmission.update({
+ where: {
+ id: submissionId,
+ },
+ data: {
+ ...updateData,
+ },
+ }),
+
+ // Skips the change if a review approved more milestones after we read the submission
+ ...(hasReachedNewMilestone
+ ? [
+ prisma.bountySubmission.updateMany({
+ where: {
+ id: submissionId,
+ status: "partiallyApproved",
+ approvedSocialMetricThreshold:
+ submission.approvedSocialMetricThreshold,
+ },
+ data: {
+ status: "submitted",
+ },
+ }),
+ ]
+ : []),
+ ]);
const { partner } = submission;
diff --git a/apps/web/app/(ee)/api/bounties/route.ts b/apps/web/app/(ee)/api/bounties/route.ts
index a3f4206d22..08937b0eb1 100644
--- a/apps/web/app/(ee)/api/bounties/route.ts
+++ b/apps/web/app/(ee)/api/bounties/route.ts
@@ -89,7 +89,7 @@ export const GET = withWorkspace(
where: {
programId,
status: {
- in: ["submitted", "approved"],
+ in: ["submitted", "partiallyApproved", "approved"],
},
},
_count: {
diff --git a/apps/web/app/(ee)/api/cron/bounties/sync-social-metrics/route.ts b/apps/web/app/(ee)/api/cron/bounties/sync-social-metrics/route.ts
index b071005a31..fdc4180080 100644
--- a/apps/web/app/(ee)/api/cron/bounties/sync-social-metrics/route.ts
+++ b/apps/web/app/(ee)/api/cron/bounties/sync-social-metrics/route.ts
@@ -4,7 +4,10 @@ import {
} from "@/lib/bounty/api/bounty-availability";
import { getSocialMetricsUpdates } from "@/lib/bounty/api/get-social-metrics-updates";
import { isBountyEnded } from "@/lib/bounty/bounty-period";
-import { hasReachedSocialMetricsEarningCap } from "@/lib/bounty/social-metrics-milestones";
+import {
+ getPendingSocialMetricsMilestones,
+ hasReachedSocialMetricsEarningCap,
+} from "@/lib/bounty/social-metrics-milestones";
import { resolveBountyDetails } from "@/lib/bounty/utils";
import { qstash } from "@/lib/cron";
import { withCron } from "@/lib/cron/with-cron";
@@ -88,6 +91,7 @@ export const POST = withCron(async ({ rawBody }) => {
id: true,
urls: true,
socialMetricCount: true,
+ approvedSocialMetricThreshold: true,
status: true,
partner: {
select: {
@@ -171,6 +175,18 @@ export const POST = withCron(async ({ rawBody }) => {
const shouldTransitionToSubmitted =
submission.status === "draft" && hasMetCriteria;
+ // A partially approved submission goes back to review when it reaches a new milestone
+ const hasReachedNewMilestone =
+ submission.status === "partiallyApproved" &&
+ getPendingSocialMetricsMilestones({
+ bounty,
+ submission: {
+ socialMetricCount,
+ approvedSocialMetricThreshold:
+ submission.approvedSocialMetricThreshold,
+ },
+ }).length > 0;
+
const updateData: Prisma.BountySubmissionUpdateInput = {
socialMetricCount,
socialMetricsLastSyncedAt,
@@ -195,10 +211,28 @@ export const POST = withCron(async ({ rawBody }) => {
data: updateData,
}),
);
+
+ syncedCount++;
+
+ // Skips the change if a review approved more milestones after we read the submission
+ if (hasReachedNewMilestone) {
+ updates.push(
+ prisma.bountySubmission.updateMany({
+ where: {
+ id,
+ status: "partiallyApproved",
+ approvedSocialMetricThreshold:
+ submission.approvedSocialMetricThreshold,
+ },
+ data: {
+ status: "submitted",
+ },
+ }),
+ );
+ }
}
await prisma.$transaction(updates);
- syncedCount = updates.length;
if (notifications.length > 0) {
await sendBatchEmail(
diff --git a/apps/web/app/(ee)/app.dub.co/embed/referrals/bounties/submissions-table.tsx b/apps/web/app/(ee)/app.dub.co/embed/referrals/bounties/submissions-table.tsx
index a86ea484bf..6d159b9ba8 100644
--- a/apps/web/app/(ee)/app.dub.co/embed/referrals/bounties/submissions-table.tsx
+++ b/apps/web/app/(ee)/app.dub.co/embed/referrals/bounties/submissions-table.tsx
@@ -113,7 +113,11 @@ export function EmbedBountySubmissionsTable({
} else {
buttonText = "Continue";
}
- } else if (["submitted", "approved", "rejected"].includes(status)) {
+ } else if (
+ ["submitted", "partiallyApproved", "approved", "rejected"].includes(
+ status,
+ )
+ ) {
buttonText = "View";
}
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/[bountyId]/bounty-submissions-table.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/[bountyId]/bounty-submissions-table.tsx
index f5c62f2bab..bca7310cc3 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/[bountyId]/bounty-submissions-table.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/[bountyId]/bounty-submissions-table.tsx
@@ -356,9 +356,9 @@ export function BountySubmissionsTable() {
user={row.original.user!}
date={row.original.reviewedAt}
label={
- row.original.status === "approved"
- ? "Approved at"
- : "Rejected at"
+ row.original.status === "rejected"
+ ? "Rejected at"
+ : "Approved at"
}
/>
) : (
@@ -423,13 +423,17 @@ export function BountySubmissionsTable() {
thClassName: "border-l-0",
tdClassName: "border-l-0",
resourceName: (p) => `submission${p ? "s" : ""}`,
- // if status is not set, we count draft, submitted and approved submissions
+ // if status is not set, we count draft, submitted, partially approved and approved submissions
// else, we count the submissions for the status
rowCount: searchParams.get("status")
? submissionsCount?.find((s) => s.status === searchParams.get("status"))
?.count || 0
: submissionsCount
- ?.filter((s) => ["draft", "submitted", "approved"].includes(s.status))
+ ?.filter((s) =>
+ ["draft", "submitted", "partiallyApproved", "approved"].includes(
+ s.status,
+ ),
+ )
.reduce((acc, curr) => acc + curr.count, 0) || 0,
loading: isLoading || isBountyLoading,
error: error ? "Failed to load bounty submissions" : undefined,
diff --git a/apps/web/lib/api/workflows/award-bounty/execute.ts b/apps/web/lib/api/workflows/award-bounty/execute.ts
index 1e6ee92d54..1f102fb082 100644
--- a/apps/web/lib/api/workflows/award-bounty/execute.ts
+++ b/apps/web/lib/api/workflows/award-bounty/execute.ts
@@ -25,6 +25,7 @@ const terminalStatusReason: Record<
string
> = {
submitted: "finished",
+ partiallyApproved: "been partially awarded",
approved: "been awarded",
rejected: "been rejected",
};
diff --git a/apps/web/lib/bounty/api/approve-bounty-submission.ts b/apps/web/lib/bounty/api/approve-bounty-submission.ts
index b051ab7a32..c72f15785d 100644
--- a/apps/web/lib/bounty/api/approve-bounty-submission.ts
+++ b/apps/web/lib/bounty/api/approve-bounty-submission.ts
@@ -248,7 +248,7 @@ async function approveSocialMetricsMilestones({
},
data: {
approvedSocialMetricThreshold: approvedThreshold,
- status: completesEarningCap ? "approved" : "submitted",
+ status: completesEarningCap ? "approved" : "partiallyApproved",
reviewedAt: new Date(),
userId: user.id,
rejectionNote: null,
diff --git a/apps/web/lib/bounty/api/get-social-metrics-updates.ts b/apps/web/lib/bounty/api/get-social-metrics-updates.ts
index c664b7ee7b..38b5a41d94 100644
--- a/apps/web/lib/bounty/api/get-social-metrics-updates.ts
+++ b/apps/web/lib/bounty/api/get-social-metrics-updates.ts
@@ -22,12 +22,18 @@ const submissionWithUrl = (submission: { id: string; urls: unknown }) => {
};
};
+type SubmissionToSync = {
+ id: string;
+ urls: unknown;
+ socialMetricCount?: number | null;
+};
+
export async function getSocialMetricsUpdates({
bounty,
submissions,
}: {
bounty: Pick<Bounty, "submissionRequirements">;
- submissions: { id: string; urls: unknown } | { id: string; urls: unknown }[];
+ submissions: SubmissionToSync | SubmissionToSync[];
}): Promise<SocialMetricsUpdate[]> {
const bountyInfo = resolveBountyDetails(bounty);
const socialPlatform = bountyInfo?.socialPlatform;
@@ -84,6 +90,11 @@ export async function getSocialMetricsUpdates({
continue;
}
+ // A removed or private post returns 0, so keep the last known count
+ if (socialMetricCount === 0 && (submission.socialMetricCount ?? 0) > 0) {
+ continue;
+ }
+
updates.push({
id: submission.id,
socialMetricCount,
diff --git a/apps/web/lib/bounty/bounty-submission-status-badges.ts b/apps/web/lib/bounty/bounty-submission-status-badges.ts
index 69da3be6a4..3f01215cbc 100644
--- a/apps/web/lib/bounty/bounty-submission-status-badges.ts
+++ b/apps/web/lib/bounty/bounty-submission-status-badges.ts
@@ -32,6 +32,12 @@ export const BountySubmissionStatusBadges = {
icon: CircleHalfDottedCheck,
iconClassName: "text-blue-600",
},
+ partiallyApproved: {
+ label: "Partially approved",
+ variant: "success",
+ icon: CircleHalfDottedCheck,
+ iconClassName: "text-green-600",
+ },
approved: {
label: "Approved",
variant: "success",
diff --git a/apps/web/lib/bounty/periods.ts b/apps/web/lib/bounty/periods.ts
index 82a78997e7..96707db8df 100644
--- a/apps/web/lib/bounty/periods.ts
+++ b/apps/web/lib/bounty/periods.ts
@@ -6,6 +6,7 @@ export type SubmissionPeriodStatus =
| "notOpen"
| "draft"
| "submitted"
+ | "partiallyApproved"
| "approved"
| "rejected";
diff --git a/apps/web/prisma/schema/bounty.prisma b/apps/web/prisma/schema/bounty.prisma
index da2c84cd5a..bf7fd452d6 100644
--- a/apps/web/prisma/schema/bounty.prisma
+++ b/apps/web/prisma/schema/bounty.prisma
@@ -11,6 +11,7 @@ enum BountyPerformanceScope {
enum BountySubmissionStatus {
draft
submitted
+ partiallyApproved
approved
rejected
}
diff --git a/apps/web/scripts/migrations/backfill-bounty-approved-threshold.ts b/apps/web/scripts/migrations/backfill-bounty-approved-threshold.ts
index 83d954627d..6be2d249ec 100644
--- a/apps/web/scripts/migrations/backfill-bounty-approved-threshold.ts
+++ b/apps/web/scripts/migrations/backfill-bounty-approved-threshold.ts
@@ -10,7 +10,7 @@ import "dotenv-flow/config";
type SubmissionThresholdUpdate = Pick<BountySubmission, "id"> & {
approvedSocialMetricThreshold: number;
- status: Extract<BountySubmissionStatus, "submitted" | "approved">;
+ status: Extract<BountySubmissionStatus, "partiallyApproved" | "approved">;
};
type SkippedSubmission = Pick<BountySubmission, "id"> &
@@ -30,7 +30,7 @@ const PAID_COMMISSION_STATUSES: CommissionStatus[] = [
// Backfill BountySubmission.approvedSocialMetricThreshold for social metrics submissions approved before the column existed
// The threshold is derived from the commission earnings (the stored socialMetricCount can be past what was actually paid)
-// If the paid threshold is below the earning cap, sets status back to submitted so the remaining milestones can be approved
+// If the paid threshold is below the earning cap, sets status to partiallyApproved so the sync can reopen it at the next milestone
// Safe to re-run: only approved submissions with a null threshold are updated
// Run backfill-bounty-submission-commissions.ts first: commissions are looked up by Commission.bountySubmissionId only
async function main() {
@@ -151,12 +151,15 @@ async function main() {
updates.push({
id,
approvedSocialMetricThreshold: milestones[paidIndex].threshold,
- status: paidIndex === milestones.length - 1 ? "approved" : "submitted",
+ status:
+ paidIndex === milestones.length - 1
+ ? "approved"
+ : "partiallyApproved",
});
}
const reopened = updates.filter(
- ({ status }) => status === "submitted",
+ ({ status }) => status === "partiallyApproved",
).length;
let updated = updates.length;
diff --git a/apps/web/scripts/migrations/backfill-bounty-partially-approved.ts b/apps/web/scripts/migrations/backfill-bounty-partially-approved.ts
new file mode 100644
index 0000000000..3fa7fc339f
--- /dev/null
+++ b/apps/web/scripts/migrations/backfill-bounty-partially-approved.ts
@@ -0,0 +1,105 @@
+import { getPendingSocialMetricsMilestones } from "@/lib/bounty/social-metrics-milestones";
+import { prisma } from "@/lib/prisma";
+import "dotenv-flow/config";
+
+const DRY_RUN = true;
+const BATCH_SIZE = 500;
+
+// Moves partially approved social metrics submissions from submitted to partiallyApproved
+// Submissions that already reached a new milestone stay submitted so they remain in the review queue
+// Safe to re-run: only submitted submissions with an approved threshold are updated
+async function main() {
+ console.log(`DRY_RUN=${DRY_RUN} BATCH_SIZE=${BATCH_SIZE}`);
+
+ let cursor: string | undefined;
+ let totalProcessed = 0;
+ let totalUpdated = 0;
+
+ while (true) {
+ const submissions = await prisma.bountySubmission.findMany({
+ where: {
+ status: "submitted",
+ approvedSocialMetricThreshold: {
+ not: null,
+ },
+ ...(cursor && {
+ id: {
+ gt: cursor,
+ },
+ }),
+ },
+ select: {
+ id: true,
+ socialMetricCount: true,
+ approvedSocialMetricThreshold: true,
+ bounty: {
+ select: {
+ rewardAmount: true,
+ submissionRequirements: true,
+ },
+ },
+ },
+ orderBy: {
+ id: "asc",
+ },
+ take: BATCH_SIZE,
+ });
+
+ if (submissions.length === 0) {
+ break;
+ }
+
+ const toUpdate = submissions.filter(
+ (submission) =>
+ getPendingSocialMetricsMilestones({
+ bounty: submission.bounty,
+ submission,
+ }).length === 0,
+ );
+
+ let updated = toUpdate.length;
+
+ if (DRY_RUN) {
+ console.table(
+ toUpdate.map(
+ ({ id, socialMetricCount, approvedSocialMetricThreshold }) => ({
+ id,
+ socialMetricCount,
+ approvedSocialMetricThreshold,
+ }),
+ ),
+ );
+ } else if (toUpdate.length > 0) {
+ const results = await Promise.all(
+ toUpdate.map(({ id, approvedSocialMetricThreshold }) =>
+ prisma.bountySubmission.updateMany({
+ where: {
+ id,
+ status: "submitted",
+ approvedSocialMetricThreshold,
+ },
+ data: {
+ status: "partiallyApproved",
+ },
+ }),
+ ),
+ );
+
+ updated = results.reduce((total, { count }) => total + count, 0);
+ }
+
+ cursor = submissions[submissions.length - 1].id;
+ totalProcessed += submissions.length;
+ totalUpdated += updated;
+
+ console.log(
+ `Processed ${submissions.length} submissions, ${DRY_RUN ? "would update" : "updated"} ${updated} (last submission: ${cursor})`,
+ );
+ }
+
+ console.log(
+ `Done. Processed ${totalProcessed} submissions, ${DRY_RUN ? "would set" : "set"} partiallyApproved on ${totalUpdated}.`,
+ );
+}
+
+main();