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 + fix amount

Steven Teyeb409b0bb36e
diff --git a/apps/web/lib/rewardful/import-commissions.ts b/apps/web/lib/rewardful/import-commissions.ts
index b70795f403..ffbe2534c0 100644
--- a/apps/web/lib/rewardful/import-commissions.ts
+++ b/apps/web/lib/rewardful/import-commissions.ts
@@ -229,6 +229,7 @@ async function createCommission({
linkId: customerFound.linkId,
customerId: customerFound.id,
amount: sale.sale_amount_cents,
+ earnings: commission.amount,
currency: sale.currency.toLowerCase(),
quantity: 1,
status: toDubStatus[commission.state],
diff --git a/apps/web/scripts/tella/update-commission-amount.ts b/apps/web/scripts/tella/update-commission-flat.ts
similarity index 95%
rename from apps/web/scripts/tella/update-commission-amount.ts
rename to apps/web/scripts/tella/update-commission-flat.ts
index 9a7c17e102..d5ac6f7301 100644
--- a/apps/web/scripts/tella/update-commission-amount.ts
+++ b/apps/web/scripts/tella/update-commission-flat.ts
@@ -4,7 +4,7 @@ import "dotenv-flow/config";
import * as fs from "fs";
import * as Papa from "papaparse";
-const partnersToUpdate: string[] = [];
+const flatRatePartners: string[] = [];
// update commissions for a program
async function main() {
@@ -12,7 +12,7 @@ async function main() {
header: true,
skipEmptyLines: true,
step: (result: { data: { Email: string } }) => {
- partnersToUpdate.push(result.data.Email);
+ flatRatePartners.push(result.data.Email);
},
complete: async () => {
const where: Prisma.CommissionWhereInput = {
@@ -23,7 +23,7 @@ async function main() {
},
partner: {
email: {
- in: partnersToUpdate,
+ in: flatRatePartners,
},
},
};
diff --git a/apps/web/scripts/tella/update-commission-percentage.ts b/apps/web/scripts/tella/update-commission-percentage.ts
new file mode 100644
index 0000000000..80d81ed78a
--- /dev/null
+++ b/apps/web/scripts/tella/update-commission-percentage.ts
@@ -0,0 +1,81 @@
+import { prisma } from "@dub/prisma";
+import { Prisma } from "@dub/prisma/client";
+import "dotenv-flow/config";
+import * as fs from "fs";
+import * as Papa from "papaparse";
+
+const flatRatePartners: string[] = [];
+
+// update commissions for a program
+async function main() {
+ Papa.parse(fs.createReadStream("xxx.csv", "utf-8"), {
+ header: true,
+ skipEmptyLines: true,
+ step: (result: { data: { Email: string } }) => {
+ flatRatePartners.push(result.data.Email);
+ },
+ complete: async () => {
+ const where: Prisma.CommissionWhereInput = {
+ programId: "prog_xxx",
+ status: "pending",
+ earnings: 0,
+ partner: {
+ email: {
+ notIn: flatRatePartners,
+ },
+ },
+ };
+
+ const commissions = await prisma.commission.findMany({
+ where,
+ take: 100,
+ });
+
+ console.table(commissions, [
+ "id",
+ "partnerId",
+ "amount",
+ "earnings",
+ "status",
+ "createdAt",
+ ]);
+
+ await Promise.all(
+ commissions.map(async (commission) => {
+ const updatedEarnings = commission.amount * 0.3;
+ console.log(
+ `Updating ${commission.id} from ${commission.earnings} to ${updatedEarnings}`,
+ );
+ await prisma.commission.update({
+ where: { id: commission.id },
+ data: {
+ earnings: updatedEarnings,
+ },
+ });
+ if (commission.status === "processed" && commission.payoutId) {
+ const difference = updatedEarnings - commission.earnings;
+
+ console.log(
+ `Updating payout ${commission.payoutId} by ${difference}`,
+ );
+ await prisma.payout.update({
+ where: { id: commission.payoutId },
+ data: {
+ amount: {
+ increment: difference,
+ },
+ },
+ });
+ }
+ }),
+ );
+
+ const remainingCommissions = await prisma.commission.count({
+ where,
+ });
+ console.log(`${remainingCommissions} commissions left to update`);
+ },
+ });
+}
+
+main();