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.

Move unban partner side effects to a background job.

Kiran K39ea8337c134
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 8f48189e90..f1fcced4d9 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
@@ -55,6 +55,14 @@ export const POST = withCron(async ({ rawBody }) => {
);
}
+ const minCount = bountyInfo.socialMetrics?.minCount;
+
+ if (!minCount) {
+ return logAndRespond(
+ `Bounty ${bountyId} has no minimum social metrics count. Skipping...`,
+ );
+ }
+
const submissions = await prisma.bountySubmission.findMany({
where: {
bountyId,
@@ -100,33 +108,9 @@ export const POST = withCron(async ({ rawBody }) => {
);
}
- const newMetrics = await getSocialMetricsUpdates({
- bounty,
- submissions,
- });
-
- const minCount = bountyInfo.socialMetrics?.minCount;
-
- if (!minCount) {
- return logAndRespond(
- `Bounty ${bountyId} has no minimum social metrics count. Skipping...`,
- );
- }
-
- const submissionById = new Map(submissions.map((s) => [s.id, s]));
-
- const updates: Prisma.PrismaPromise<unknown>[] = [];
- const notifications: Pick<Partner, "email">[] = [];
-
- for (const {
- id,
- socialMetricCount,
- socialMetricsLastSyncedAt,
- } of newMetrics) {
- const submission = submissionById.get(id);
-
- if (!submission || !submission.programEnrollment) {
- continue;
+ const activeSubmissions = submissions.filter((submission) => {
+ if (!submission.programEnrollment) {
+ return false;
}
const { endsAt } = getEffectiveBountyPeriod({
@@ -134,66 +118,96 @@ export const POST = withCron(async ({ rawBody }) => {
bounty,
});
- if (isBountyEnded(endsAt)) {
- continue;
- }
+ return !isBountyEnded(endsAt);
+ });
+
+ let syncedCount = 0;
+
+ if (activeSubmissions.length > 0) {
+ const newMetrics = await getSocialMetricsUpdates({
+ bounty,
+ submissions: activeSubmissions,
+ });
- const hasMetCriteria =
- socialMetricCount != null && socialMetricCount >= minCount;
+ const submissionById = new Map(activeSubmissions.map((s) => [s.id, s]));
- const shouldTransitionToSubmitted =
- submission.status === "draft" && hasMetCriteria;
+ const updates: Prisma.PrismaPromise<unknown>[] = [];
+ const notifications: Pick<Partner, "email">[] = [];
- const updateData: Prisma.BountySubmissionUpdateInput = {
+ for (const {
+ id,
socialMetricCount,
socialMetricsLastSyncedAt,
- };
-
- if (shouldTransitionToSubmitted) {
- updateData.status = "submitted";
- updateData.completedAt = new Date();
+ } of newMetrics) {
+ const submission = submissionById.get(id);
- if (submission.partner?.email) {
- notifications.push({
- email: submission.partner.email,
- });
+ if (!submission) {
+ continue;
}
- }
- updates.push(
- prisma.bountySubmission.update({
- where: {
- id,
- },
- data: updateData,
- }),
- );
- }
+ const hasMetCriteria =
+ socialMetricCount != null && socialMetricCount >= minCount;
- await prisma.$transaction(updates);
-
- if (notifications.length > 0) {
- await sendBatchEmail(
- notifications.map(({ email }) => ({
- subject: "Bounty completed!",
- to: email!,
- variant: "notifications",
- replyTo: bounty.program.supportEmail || "noreply",
- react: BountyCompleted({
- email: email!,
- bounty: {
- name: bounty.name,
- type: bounty.type,
- },
- program: {
- name: bounty.program.name,
- slug: bounty.program.slug,
+ const shouldTransitionToSubmitted =
+ submission.status === "draft" && hasMetCriteria;
+
+ const updateData: Prisma.BountySubmissionUpdateInput = {
+ socialMetricCount,
+ socialMetricsLastSyncedAt,
+ };
+
+ if (shouldTransitionToSubmitted) {
+ updateData.status = "submitted";
+ updateData.completedAt = new Date();
+
+ if (submission.partner?.email) {
+ notifications.push({
+ email: submission.partner.email,
+ });
+ }
+ }
+
+ updates.push(
+ prisma.bountySubmission.update({
+ where: {
+ id,
},
+ data: updateData,
}),
- })),
- );
+ );
+ }
+
+ await prisma.$transaction(updates);
+ syncedCount = updates.length;
+
+ if (notifications.length > 0) {
+ await sendBatchEmail(
+ notifications.map(({ email }) => ({
+ subject: "Bounty completed!",
+ to: email!,
+ variant: "notifications",
+ replyTo: bounty.program.supportEmail || "noreply",
+ react: BountyCompleted({
+ email: email!,
+ bounty: {
+ name: bounty.name,
+ type: bounty.type,
+ },
+ program: {
+ name: bounty.program.name,
+ slug: bounty.program.slug,
+ },
+ }),
+ })),
+ );
+ }
}
+ const summary =
+ activeSubmissions.length === 0
+ ? `No active submissions found for bounty ${bountyId}.`
+ : `Synced ${syncedCount} submission(s) for bounty ${bountyId}.`;
+
if (submissions.length === SUBMISSION_BATCH_SIZE) {
const startingAfter = submissions[submissions.length - 1].id;
@@ -207,7 +221,7 @@ export const POST = withCron(async ({ rawBody }) => {
});
return logAndRespond(
- `Synced ${updates.length} submissions for bounty ${bountyId}. Queued next batch (startingAfter: ${startingAfter}).`,
+ `${summary} Queued next batch (startingAfter: ${startingAfter}).`,
);
}
@@ -220,7 +234,5 @@ export const POST = withCron(async ({ rawBody }) => {
},
});
- return logAndRespond(
- `Synced ${updates.length} submission(s) for bounty ${bountyId}.`,
- );
+ return logAndRespond(summary);
});