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.

Use effective bounty periods for submissions and social metrics sync.

Kiran K1884bdccf4d2
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 26bec94e94..4090e76021 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
@@ -2,8 +2,10 @@ import { DubApiError } from "@/lib/api/errors";
import { getDefaultProgramIdOrThrow } from "@/lib/api/programs/get-default-program-id-or-throw";
import { parseRequestBody } from "@/lib/api/utils";
import { withWorkspace } from "@/lib/auth";
+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 { isBountyExpired, isBountyStarted } from "@/lib/bounty/bounty-period";
import { resolveBountyDetails } from "@/lib/bounty/utils";
import { qstash } from "@/lib/cron";
import { prisma } from "@/lib/prisma";
@@ -52,6 +54,12 @@ export const POST = withWorkspace(
urls: true,
status: true,
partner: true,
+ programEnrollment: {
+ select: {
+ groupJoinedAt: true,
+ createdAt: true,
+ },
+ },
},
},
}
@@ -67,58 +75,60 @@ export const POST = withWorkspace(
});
}
- const submission = submissionId ? bounty.submissions?.[0] : undefined;
-
- if (submissionId) {
- if (!submission) {
- throw new DubApiError({
- code: "not_found",
- message: `Submission ${submissionId} not found.`,
- });
- }
+ // Bounty-wide sync (no submissionId): run asynchronously via a background job
+ if (!submissionId) {
+ const response = await qstash.publishJSON({
+ url: `${APP_DOMAIN_WITH_NGROK}/api/cron/bounties/sync-social-metrics`,
+ method: "POST",
+ body: {
+ bountyId,
+ },
+ });
- if (submission.status === "approved") {
+ if (!response.messageId) {
throw new DubApiError({
code: "bad_request",
- message: "Social metrics can't be synced for an approved submission.",
+ message: "Could not sync social metrics for this bounty now.",
});
}
+
+ return NextResponse.json({});
}
- const now = new Date();
+ // Single-submission sync
+ const submission = bounty.submissions?.[0];
- if (bounty.startsAt && bounty.startsAt > now) {
+ if (!submission || !submission.programEnrollment) {
throw new DubApiError({
- code: "bad_request",
- message: "Social metrics can only be synced after the bounty starts.",
+ code: "not_found",
+ message: `Submission ${submissionId} not found.`,
});
}
- if (bounty.endsAt && bounty.endsAt < now) {
+ if (submission.status === "approved") {
throw new DubApiError({
code: "bad_request",
- message: "Social metrics can't be synced after the bounty ends.",
+ message: "Social metrics can't be synced for an approved submission.",
});
}
- // Do the sync in a background job if no submissionId is provided
- if (!submissionId) {
- const response = await qstash.publishJSON({
- url: `${APP_DOMAIN_WITH_NGROK}/api/cron/bounties/sync-social-metrics`,
- method: "POST",
- body: {
- bountyId,
- },
- });
+ const { startsAt, endsAt } = getEffectiveBountyPeriod({
+ programEnrollment: submission.programEnrollment,
+ bounty,
+ });
- if (!response.messageId) {
- throw new DubApiError({
- code: "bad_request",
- message: "Could not sync social metrics for this bounty now.",
- });
- }
+ if (!isBountyStarted(startsAt)) {
+ throw new DubApiError({
+ code: "bad_request",
+ message: "Social metrics can only be synced after the bounty starts.",
+ });
+ }
- return NextResponse.json({});
+ if (isBountyExpired(endsAt)) {
+ throw new DubApiError({
+ code: "bad_request",
+ message: "Social metrics can't be synced after the bounty ends.",
+ });
}
// Otherwise, do the sync for the specific submission
diff --git a/apps/web/lib/bounty/api/create-bounty-submission.ts b/apps/web/lib/bounty/api/create-bounty-submission.ts
index e652689e86..94c65a7dff 100644
--- a/apps/web/lib/bounty/api/create-bounty-submission.ts
+++ b/apps/web/lib/bounty/api/create-bounty-submission.ts
@@ -3,6 +3,11 @@ import { DubApiError } from "@/lib/api/errors";
import { getWorkspaceUsers } from "@/lib/api/get-workspace-users";
import { getProgramEnrollmentOrThrow } from "@/lib/api/programs/get-program-enrollment-or-throw";
import { getSocialContent } from "@/lib/api/scrape-creators/get-social-content";
+import {
+ canPartnerSubmitBounty,
+ getEffectiveBountyPeriod,
+} from "@/lib/bounty/api/bounty-availability";
+import { getBountyOrThrow } from "@/lib/bounty/api/get-bounty-or-throw";
import { BOUNTY_MAX_SUBMISSION_URLS } from "@/lib/bounty/constants";
import { addFrequency, getCurrentPeriodNumber } from "@/lib/bounty/periods";
import { resolveBountyDetails } from "@/lib/bounty/utils";
@@ -57,7 +62,14 @@ export class BountySubmissionHandler {
private submissions: BountySubmission[];
private submissionData: Partial<Prisma.BountySubmissionUncheckedCreateInput>;
private programEnrollment: Prisma.ProgramEnrollmentGetPayload<{
- include: {};
+ include: {
+ program: {
+ select: {
+ id: true;
+ defaultGroupId: true;
+ };
+ };
+ };
}>;
constructor(params: CreateBountySubmissionParams) {
@@ -99,13 +111,19 @@ export class BountySubmissionHandler {
getProgramEnrollmentOrThrow({
partnerId: this.partner.id,
programId: this.programId,
- include: {},
+ include: {
+ program: {
+ select: {
+ id: true,
+ defaultGroupId: true,
+ },
+ },
+ },
}),
- prisma.bounty.findUniqueOrThrow({
- where: {
- id: this.bountyId,
- },
+ getBountyOrThrow({
+ bountyId: this.bountyId,
+ programId: this.programId,
include: {
groups: true,
submissions: {
@@ -155,9 +173,14 @@ export class BountySubmissionHandler {
}
// Multi-submission WITH frequency — time-gated
+ const { startsAt, endsAt } = getEffectiveBountyPeriod({
+ programEnrollment: this.programEnrollment,
+ bounty: this.bounty,
+ });
+
const currentPeriod = getCurrentPeriodNumber({
- startsAt: this.bounty.startsAt,
- endsAt: this.bounty.endsAt,
+ startsAt,
+ endsAt,
submissionFrequency: this.bounty.submissionFrequency,
maxSubmissions: this.bounty.maxSubmissions,
});
@@ -186,7 +209,7 @@ export class BountySubmissionHandler {
// Validate the period has started
const periodStart = addFrequency({
- date: this.bounty.startsAt,
+ date: startsAt,
frequency: this.bounty.submissionFrequency,
amount: periodNumber - 1,
});
@@ -210,20 +233,19 @@ export class BountySubmissionHandler {
// Validate the eligibility of the submission
private validateEligibility() {
- if (!["approved", "pending"].includes(this.programEnrollment.status)) {
+ if (
+ !canPartnerSubmitBounty({
+ program: this.programEnrollment.program,
+ bounty: this.bounty,
+ programEnrollment: this.programEnrollment,
+ })
+ ) {
throw new DubApiError({
code: "forbidden",
message: "You are not allowed to submit a bounty for this program.",
});
}
- if (this.bounty.programId !== this.programId) {
- throw new DubApiError({
- code: "bad_request",
- message: "This bounty is not for this program.",
- });
- }
-
// Check existing submission for this period
const existingSubmission = this.submissions.find(
(s) => s.periodNumber === this.finalPeriodNumber,
@@ -243,44 +265,6 @@ export class BountySubmissionHandler {
}
}
- // Check group membership
- if (this.bounty.groups.length > 0) {
- const isInGroup = this.bounty.groups.find(
- ({ groupId }) => groupId === this.programEnrollment.groupId,
- );
-
- if (!isInGroup) {
- throw new DubApiError({
- code: "forbidden",
- message: "You are not allowed to submit this bounty.",
- });
- }
- }
-
- // Validate bounty dates and status
- const now = new Date();
-
- if (this.bounty.startsAt && this.bounty.startsAt > now) {
- throw new DubApiError({
- code: "bad_request",
- message: "This bounty is not yet available.",
- });
- }
-
- if (this.bounty.endsAt && this.bounty.endsAt < now) {
- throw new DubApiError({
- code: "bad_request",
- message: "This bounty is no longer available.",
- });
- }
-
- if (this.bounty.archivedAt) {
- throw new DubApiError({
- code: "bad_request",
- message: "This bounty is archived.",
- });
- }
-
if (this.bounty.type === "performance") {
throw new DubApiError({
code: "forbidden",
@@ -288,6 +272,8 @@ export class BountySubmissionHandler {
});
}
+ const now = new Date();
+
if (
!this.isDraft &&
this.bounty.submissionsOpenAt &&
@@ -522,16 +508,19 @@ export class BountySubmissionHandler {
});
}
- if (
- socialContent.publishedAt &&
- this.bounty.startsAt &&
- isBefore(socialContent.publishedAt, this.bounty.startsAt)
- ) {
- throw new DubApiError({
- code: "unprocessable_entity",
- message:
- "This content was published before the bounty started. Please submit content posted after the start date.",
+ if (socialContent.publishedAt) {
+ const { startsAt } = getEffectiveBountyPeriod({
+ programEnrollment: this.programEnrollment,
+ bounty: this.bounty,
});
+
+ if (isBefore(socialContent.publishedAt, startsAt)) {
+ throw new DubApiError({
+ code: "unprocessable_entity",
+ message:
+ "This content was published before the bounty started. Please submit content posted after the start date.",
+ });
+ }
}
this.submissionData = {