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.

update scripts

Steven Tey21cd3d73cbd0
diff --git a/apps/web/scripts/programs/update-commissions-canceled.ts b/apps/web/scripts/programs/update-commissions-canceled.ts
new file mode 100644
index 0000000000..d6b0b8ae58
--- /dev/null
+++ b/apps/web/scripts/programs/update-commissions-canceled.ts
@@ -0,0 +1,99 @@
+import { prisma } from "@dub/prisma";
+import "dotenv-flow/config";
+import { syncTotalCommissions } from "../../lib/api/partners/sync-total-commissions";
+
+async function main() {
+ const programId = "prog_xxx";
+
+ const commissions = await prisma.commission.findMany({
+ where: {
+ programId,
+ partnerId: "pn_xxx",
+ status: {
+ in: ["pending", "processed"],
+ },
+ },
+ });
+
+ if (commissions.length === 0) {
+ console.log("No commissions to update");
+ return;
+ }
+
+ console.log(`Found ${commissions.length} commissions to update`);
+ console.table(commissions, [
+ "id",
+ "partnerId",
+ "amount",
+ "earnings",
+ "status",
+ "createdAt",
+ ]);
+
+ const payoutIdsToRetally = [
+ ...new Set(
+ commissions
+ .filter((commission) => commission.payoutId)
+ .map((commission) => commission.payoutId!),
+ ),
+ ];
+
+ const updatedCommissions = await prisma.commission.updateMany({
+ where: {
+ id: {
+ in: commissions.map((commission) => commission.id),
+ },
+ },
+ data: {
+ payoutId: null,
+ status: "canceled",
+ },
+ });
+
+ console.log(
+ `Updated ${updatedCommissions.count} commissions to have status "canceled"`,
+ );
+
+ for (const payoutId of payoutIdsToRetally) {
+ const data = await prisma.commission.aggregate({
+ _sum: {
+ earnings: true,
+ },
+ where: {
+ payoutId,
+ },
+ });
+ const payoutAmount = data._sum.earnings ?? 0;
+ if (payoutAmount === 0) {
+ console.log(`Deleting payout ${payoutId}`);
+ await prisma.payout.delete({
+ where: {
+ id: payoutId,
+ },
+ });
+ } else {
+ console.log(`Updating payout ${payoutId} with amount ${payoutAmount}`);
+ await prisma.payout.update({
+ where: {
+ id: payoutId,
+ },
+ data: {
+ amount: payoutAmount,
+ },
+ });
+ }
+ }
+ const partnerIdsToRetally = [
+ ...new Set(commissions.map((commission) => commission.partnerId)),
+ ];
+
+ for (const partnerId of partnerIdsToRetally) {
+ await syncTotalCommissions({
+ partnerId,
+ programId,
+ mode: "direct",
+ });
+ }
+}
+
+main();
diff --git a/apps/web/scripts/programs/update-commissions-paid.ts b/apps/web/scripts/programs/update-commissions-paid.ts
index 98993c25b0..f35d78f0b0 100644
--- a/apps/web/scripts/programs/update-commissions-paid.ts
+++ b/apps/web/scripts/programs/update-commissions-paid.ts
@@ -1,18 +1,21 @@
import { prisma } from "@dub/prisma";
import "dotenv-flow/config";
-const commissionIdList: string[] = [];
-
async function main() {
const commissions = await prisma.commission.findMany({
where: {
- id: {
- in: commissionIdList,
- },
- status: "processed",
- payout: {
- status: "pending",
+ programId: "prog_xxx",
+ status: {
+ in: ["pending", "processed"],
},
+ OR: [
+ { payoutId: null },
+ {
+ payout: {
+ status: "pending",
+ },
+ },
+ ],
},
});