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.

code improvements

Pedro Ladeira3c09b87f18af
diff --git a/apps/web/app/(ee)/api/cron/bounties/upsert-draft-submissions/route.ts b/apps/web/app/(ee)/api/cron/bounties/upsert-draft-submissions/route.ts
index 19d525e85d..a8d2022e8d 100644
--- a/apps/web/app/(ee)/api/cron/bounties/upsert-draft-submissions/route.ts
+++ b/apps/web/app/(ee)/api/cron/bounties/upsert-draft-submissions/route.ts
@@ -174,30 +174,33 @@ export async function POST(req: Request) {
console.table(toCreate);
console.table(toUpdate);
- const [createdBountySubmissions] = await Promise.all([
+ const createdBountySubmissions =
toCreate.length > 0
- ? prisma.bountySubmission.createMany({
+ ? await prisma.bountySubmission.createMany({
data: toCreate,
skipDuplicates: true,
})
- : Promise.resolve({ count: 0 }),
- toUpdate.length > 0
- ? prisma.$transaction(
- toUpdate.map((update) =>
- prisma.bountySubmission.update({
- where: { id: update.id },
- data: {
- performanceCount: update.performanceCount,
- ...(update.promoteToSubmitted && {
- status: "submitted",
- completedAt: new Date(),
- }),
- },
+ : { count: 0 };
+
+ if (toUpdate.length > 0) {
+ await prisma.$transaction(
+ toUpdate.map((update) =>
+ prisma.bountySubmission.updateMany({
+ where: {
+ id: update.id,
+ status: update.expectedStatus,
+ },
+ data: {
+ performanceCount: update.performanceCount,
+ ...(update.promoteToSubmitted && {
+ status: "submitted",
+ completedAt: new Date(),
}),
- ),
- )
- : Promise.resolve([]),
- ]);
+ },
+ }),
+ ),
+ );
+ }
console.log(
`Upserted bounty submissions for bounty ${bountyId}: created ${createdBountySubmissions.count}, updated ${toUpdate.length}.`,
diff --git a/apps/web/lib/bounty/api/upsert-draft-bounty-submissions.ts b/apps/web/lib/bounty/api/upsert-draft-bounty-submissions.ts
index 12ce51cbe9..24a94d5e74 100644
--- a/apps/web/lib/bounty/api/upsert-draft-bounty-submissions.ts
+++ b/apps/web/lib/bounty/api/upsert-draft-bounty-submissions.ts
@@ -29,6 +29,7 @@ export type DraftBountySubmissionUpdate = {
id: string;
performanceCount: number;
promoteToSubmitted: boolean;
+ expectedStatus: "draft" | "submitted";
};
export function shouldUpsertDraftSubmissionsOnReopen({
@@ -83,8 +84,22 @@ export function planDraftBountySubmissionUpserts({
for (const partner of partners) {
const performanceCount = partner[condition.attribute];
+ const existing = existingByPartnerId.get(partner.id);
if (performanceCount <= 0) {
+ if (!existing) {
+ continue;
+ }
+
+ if (existing.status === "draft" || existing.status === "submitted") {
+ toUpdate.push({
+ id: existing.id,
+ performanceCount,
+ promoteToSubmitted: false,
+ expectedStatus: existing.status,
+ });
+ }
+
continue;
}
@@ -95,8 +110,6 @@ export function planDraftBountySubmissionUpserts({
},
});
- const existing = existingByPartnerId.get(partner.id);
-
if (!existing) {
toCreate.push({
id: createId({ prefix: "bnty_sub_" }),
@@ -117,6 +130,7 @@ export function planDraftBountySubmissionUpserts({
id: existing.id,
performanceCount,
promoteToSubmitted: conditionMet,
+ expectedStatus: "draft",
});
continue;
}
@@ -126,6 +140,7 @@ export function planDraftBountySubmissionUpserts({
id: existing.id,
performanceCount,
promoteToSubmitted: false,
+ expectedStatus: "submitted",
});
}
}
diff --git a/apps/web/tests/bounties/upsert-draft-bounty-submissions.test.ts b/apps/web/tests/bounties/upsert-draft-bounty-submissions.test.ts
index b4141e5ec0..043f746867 100644
--- a/apps/web/tests/bounties/upsert-draft-bounty-submissions.test.ts
+++ b/apps/web/tests/bounties/upsert-draft-bounty-submissions.test.ts
@@ -202,6 +202,7 @@ describe("planDraftBountySubmissionUpserts", () => {
id: "bnty_sub_1",
performanceCount: 8_000,
promoteToSubmitted: false,
+ expectedStatus: "draft",
},
]);
});
@@ -220,6 +221,7 @@ describe("planDraftBountySubmissionUpserts", () => {
id: "bnty_sub_1",
performanceCount: 12_000,
promoteToSubmitted: true,
+ expectedStatus: "draft",
},
]);
});
@@ -238,6 +240,7 @@ describe("planDraftBountySubmissionUpserts", () => {
id: "bnty_sub_1",
performanceCount: 20_000,
promoteToSubmitted: false,
+ expectedStatus: "submitted",
},
]);
});
@@ -269,7 +272,7 @@ describe("planDraftBountySubmissionUpserts", () => {
expect(toUpdate).toHaveLength(0);
});
- it("skips partners with zero performanceCount", () => {
+ it("skips partners with zero performanceCount when there is no existing row", () => {
const { toCreate, toUpdate } = planDraftBountySubmissionUpserts({
partners: [makePartner({ totalSaleAmount: 0 })],
existingSubmissions: [],
@@ -281,4 +284,71 @@ describe("planDraftBountySubmissionUpserts", () => {
expect(toCreate).toHaveLength(0);
expect(toUpdate).toHaveLength(0);
});
+
+ it("refreshes a draft submission when performanceCount drops to zero", () => {
+ const { toCreate, toUpdate } = planDraftBountySubmissionUpserts({
+ partners: [makePartner({ totalSaleAmount: 0 })],
+ existingSubmissions: [makeSubmission({ status: "draft" })],
+ condition,
+ programId: PROGRAM_ID,
+ bountyId: BOUNTY_ID,
+ });
+
+ expect(toCreate).toHaveLength(0);
+ expect(toUpdate).toEqual([
+ {
+ id: "bnty_sub_1",
+ performanceCount: 0,
+ promoteToSubmitted: false,
+ expectedStatus: "draft",
+ },
+ ]);
+ });
+
+ it("refreshes a submitted submission when performanceCount drops to zero", () => {
+ const { toCreate, toUpdate } = planDraftBountySubmissionUpserts({
+ partners: [makePartner({ totalSaleAmount: 0 })],
+ existingSubmissions: [makeSubmission({ status: "submitted" })],
+ condition,
+ programId: PROGRAM_ID,
+ bountyId: BOUNTY_ID,
+ });
+
+ expect(toCreate).toHaveLength(0);
+ expect(toUpdate).toEqual([
+ {
+ id: "bnty_sub_1",
+ performanceCount: 0,
+ promoteToSubmitted: false,
+ expectedStatus: "submitted",
+ },
+ ]);
+ });
+
+ it("skips approved and rejected submissions when performanceCount is zero", () => {
+ const { toCreate, toUpdate } = planDraftBountySubmissionUpserts({
+ partners: [
+ makePartner({ id: "pn_approved", totalSaleAmount: 0 }),
+ makePartner({ id: "pn_rejected", totalSaleAmount: 0 }),
+ ],
+ existingSubmissions: [
+ makeSubmission({
+ id: "bnty_sub_approved",
+ partnerId: "pn_approved",
+ status: "approved",
+ }),
+ makeSubmission({
+ id: "bnty_sub_rejected",
+ partnerId: "pn_rejected",
+ status: "rejected",
+ }),
+ ],
+ condition,
+ programId: PROGRAM_ID,
+ bountyId: BOUNTY_ID,
+ });
+
+ expect(toCreate).toHaveLength(0);
+ expect(toUpdate).toHaveLength(0);
+ });
});