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.

cleanup more scripts

Steven Tey193bdd867025
diff --git a/apps/web/scripts/analyze-bundle.ts b/apps/web/scripts/analyze-bundle.ts
deleted file mode 100644
index d91d1fb3cf..0000000000
--- a/apps/web/scripts/analyze-bundle.ts
+++ /dev/null
@@ -1,200 +0,0 @@
-import fs from "fs";
-import path from "path";
-
-// Types and interfaces
-interface Dependency {
- name: string;
- version: string;
- size: number;
- formattedSize: string;
-}
-
-interface PackageGroup {
- group: string;
- packages: Dependency[];
- totalSize: number;
-}
-
-interface PackageJson {
- dependencies: Record<string, string>;
- devDependencies: Record<string, string>;
-}
-
-// Function to get directory size recursively
-function getDirectorySize(dirPath: string): number {
- let totalSize = 0;
-
- try {
- const items = fs.readdirSync(dirPath);
-
- for (const item of items) {
- const itemPath = path.join(dirPath, item);
- const stats = fs.statSync(itemPath);
-
- if (stats.isDirectory()) {
- totalSize += getDirectorySize(itemPath);
- } else {
- totalSize += stats.size;
- }
- }
- } catch (error) {
- // Skip if directory doesn't exist or can't be read
- }
-
- return totalSize;
-}
-
-// Function to format bytes to human readable format
-function formatBytes(bytes: number): string {
- if (bytes === 0) return "0 Bytes";
-
- const k = 1024;
- const sizes = ["Bytes", "KB", "MB", "GB"];
- const i = Math.floor(Math.log(bytes) / Math.log(k));
-
- return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + " " + sizes[i];
-}
-
-// Function to get dependencies from package.json
-function getDependencies(): Record<string, string> {
- const packageJsonPath = path.join(__dirname, "../package.json");
- const packageJson: PackageJson = JSON.parse(
- fs.readFileSync(packageJsonPath, "utf8"),
- );
-
- return {
- ...packageJson.dependencies,
- ...packageJson.devDependencies,
- };
-}
-
-// Function to analyze dependencies
-function analyzeDependencies(): Dependency[] {
- const allDependencies = getDependencies();
- const dependencySizes: Dependency[] = [];
- const nodeModulesPath = path.join(__dirname, "../node_modules");
-
- for (const [packageName, version] of Object.entries(allDependencies)) {
- const packagePath = path.join(nodeModulesPath, packageName);
-
- if (fs.existsSync(packagePath)) {
- const size = getDirectorySize(packagePath);
- dependencySizes.push({
- name: packageName,
- version,
- size,
- formattedSize: formatBytes(size),
- });
- }
- }
-
- // Sort by size (largest first)
- return dependencySizes.sort((a, b) => b.size - a.size);
-}
-
-// Function to find duplicate packages
-function findDuplicatePackages(dependencySizes: Dependency[]): PackageGroup[] {
- const packageGroups: Record<string, Dependency[]> = {};
-
- dependencySizes.forEach((dep) => {
- const baseName = dep.name.split("/")[0];
- if (!packageGroups[baseName]) {
- packageGroups[baseName] = [];
- }
- packageGroups[baseName].push(dep);
- });
-
- return Object.entries(packageGroups)
- .filter(([, packages]) => packages.length > 1)
- .map(([name, packages]) => ({
- group: name,
- packages,
- totalSize: packages.reduce((sum, pkg) => sum + pkg.size, 0),
- }));
-}
-
-// Function to print analysis results
-function printAnalysis(dependencySizes: Dependency[]): void {
- console.log("πŸ” Analyzing bundle size...\n");
-
- // Print top 20 largest dependencies
- console.log("πŸ“¦ Top 20 Largest Dependencies:\n");
- console.log("Package Name".padEnd(40) + "Version".padEnd(20) + "Size");
- console.log("-".repeat(80));
-
- dependencySizes.slice(0, 20).forEach((dep, index) => {
- const rank = (index + 1).toString().padStart(2);
- console.log(
- `${rank}. ${dep.name.padEnd(37)} ${dep.version.padEnd(20)} ${dep.formattedSize}`,
- );
- });
-
- // Calculate totals
- const totalSize = dependencySizes.reduce((sum, dep) => sum + dep.size, 0);
- const top20Size = dependencySizes
- .slice(0, 20)
- .reduce((sum, dep) => sum + dep.size, 0);
-
- console.log("\n" + "=".repeat(80));
- console.log(`Total size of all dependencies: ${formatBytes(totalSize)}`);
- console.log(`Top 20 dependencies size: ${formatBytes(top20Size)}`);
- console.log(
- `Top 20 represent ${((top20Size / totalSize) * 100).toFixed(1)}% of total size`,
- );
-}
-
-// Function to print optimization opportunities
-function printOptimizationOpportunities(dependencySizes: Dependency[]): void {
- console.log("\n🎯 Potential Optimization Opportunities:\n");
-
- // Find large packages (>1MB)
- const largePackages = dependencySizes.filter((dep) => dep.size > 1024 * 1024);
-
- // Find duplicate packages
- const duplicatePackages = findDuplicatePackages(dependencySizes);
-
- if (duplicatePackages.length > 0) {
- console.log("πŸ“‹ Potential duplicate/similar packages:");
- duplicatePackages.forEach((group) => {
- console.log(` ${group.group}:`);
- group.packages.forEach((pkg) => {
- console.log(` - ${pkg.name}@${pkg.version}: ${pkg.formattedSize}`);
- });
- console.log(` Total: ${formatBytes(group.totalSize)}\n`);
- });
- }
-
- // Check for very large packages (>5MB)
- const veryLargePackages = dependencySizes.filter(
- (dep) => dep.size > 5 * 1024 * 1024,
- );
-
- if (veryLargePackages.length > 0) {
- console.log("⚠️ Very large packages (>5MB) - consider alternatives:");
- veryLargePackages.forEach((dep) => {
- console.log(` - ${dep.name}@${dep.version}: ${dep.formattedSize}`);
- });
- }
-
- console.log("\nπŸ’‘ Tips for reducing bundle size:");
- console.log("1. Use dynamic imports for large libraries");
- console.log("2. Consider tree-shaking friendly alternatives");
- console.log("3. Use bundle analyzer to identify unused code");
- console.log("4. Consider code splitting for large components");
- console.log("5. Remove unused dependencies");
-}
-
-// Main function
-function main(): void {
- try {
- const dependencySizes = analyzeDependencies();
- printAnalysis(dependencySizes);
- printOptimizationOpportunities(dependencySizes);
- } catch (error) {
- console.error("❌ Error analyzing bundle:", error);
- process.exit(1);
- }
-}
-
-// Run the analysis
-main();
diff --git a/apps/web/scripts/analyze-domains.ts b/apps/web/scripts/analyze-domains.ts
deleted file mode 100644
index 9b3787e64f..0000000000
--- a/apps/web/scripts/analyze-domains.ts
+++ /dev/null
@@ -1,85 +0,0 @@
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-
-async function main() {
- const totalDomains = await prisma.domain.count({
- where: {
- verified: true,
- },
- });
-
- const subdomains = await prisma.domain.count({
- where: {
- verified: true,
- OR: [
- {
- slug: {
- startsWith: "go.",
- },
- },
- {
- slug: {
- startsWith: "link.",
- },
- },
- {
- slug: {
- startsWith: "links.",
- },
- },
- ],
- },
- });
-
- const tldDomains = await prisma.domain.count({
- where: {
- verified: true,
- OR: [
- {
- slug: {
- endsWith: ".link",
- },
- },
- {
- slug: {
- endsWith: ".fyi",
- },
- },
- {
- slug: {
- endsWith: ".to",
- },
- },
- ],
- },
- });
-
- const domainProjects = await prisma.domain.findMany({
- where: {
- verified: true,
- },
- select: {
- slug: true,
- project: {
- select: {
- slug: true,
- },
- },
- },
- });
-
- const domainHacks = domainProjects.filter(
- (domain) => domain.project?.slug === domain.slug.replace(".", ""),
- );
-
- console.table(domainHacks);
-
- console.log({
- totalDomains,
- subdomains,
- tldDomains,
- domainHacks: domainHacks.length,
- });
-}
-
-main();
diff --git a/apps/web/scripts/analyze-link-webhooks.ts b/apps/web/scripts/analyze-link-webhooks.ts
deleted file mode 100644
index 06123d2f88..0000000000
--- a/apps/web/scripts/analyze-link-webhooks.ts
+++ /dev/null
@@ -1,42 +0,0 @@
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-
-/*
-
- One time script to analyze links that are associated with multiple webhooks.
-
- Also analyze projects that those links belong to.
-
-*/
-
-async function main() {
- const linkWebhooks = await prisma.linkWebhook.groupBy({
- by: ["linkId"],
- _count: true,
- });
-
- // order by _count desc
- const sortedLinkWebhooks = linkWebhooks
- .filter((l) => l._count > 1)
- .sort((a, b) => b._count - a._count);
-
- console.table(sortedLinkWebhooks);
- console.log(
- `Found ${sortedLinkWebhooks.length} links with multiple webhooks`,
- );
-
- const links = await prisma.link.groupBy({
- by: ["projectId"],
- where: {
- id: {
- in: sortedLinkWebhooks.map((l) => l.linkId),
- },
- },
- _count: true,
- });
-
- console.table(links);
- console.log(`Found ${links.length} projects with multiple links webhooks`);
-}
-
-main();
diff --git a/apps/web/scripts/analyze-top-utms.ts b/apps/web/scripts/analyze-top-utms.ts
deleted file mode 100644
index 572ae4da71..0000000000
--- a/apps/web/scripts/analyze-top-utms.ts
+++ /dev/null
@@ -1,34 +0,0 @@
-import { prisma } from "@dub/prisma";
-import { truncate } from "@dub/utils";
-import "dotenv-flow/config";
-
-const utmTag = "utm_campaign";
-
-async function main() {
- const utms = await prisma.link.groupBy({
- where: {
- [utmTag]: {
- not: null,
- },
- },
- by: [utmTag],
- _count: {
- [utmTag]: true,
- },
- take: 100,
- orderBy: {
- _count: {
- [utmTag]: "desc",
- },
- },
- });
-
- console.table(
- utms.map((utm) => ({
- [utmTag]: truncate(utm[utmTag], 24),
- count: utm._count[utmTag],
- })),
- );
-}
-
-main();
diff --git a/apps/web/scripts/analyze-utm-usage.ts b/apps/web/scripts/analyze-utm-usage.ts
deleted file mode 100644
index a34bcde7d6..0000000000
--- a/apps/web/scripts/analyze-utm-usage.ts
+++ /dev/null
@@ -1,45 +0,0 @@
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-
-async function main() {
- const linksWithUtms = await prisma.link.findMany({
- where: {
- OR: [
- { utm_source: { not: null } },
- { utm_campaign: { not: null } },
- { utm_medium: { not: null } },
- { utm_content: { not: null } },
- { utm_term: { not: null } },
- ],
- },
- select: {
- utm_source: true,
- utm_campaign: true,
- utm_medium: true,
- utm_content: true,
- utm_term: true,
- },
- });
-
- const linkUtms = linksWithUtms.map(
- (link) => Object.keys(link).filter((key) => link[key] !== null).length,
- );
-
- const totalUtms = linkUtms.reduce((acc, curr) => acc + curr, 0);
-
- const averageUtms = totalUtms / linkUtms.length;
-
- const groupByUtmCount = linkUtms.reduce((acc, curr) => {
- acc[curr] = (acc[curr] || 0) + 1;
- return acc;
- }, {});
-
- console.log({
- totalUtms,
- totalLinks: linkUtms.length,
- averageUtms,
- groupByUtmCount,
- });
-}
-
-main();
diff --git a/apps/web/scripts/bulk-archive-links.ts b/apps/web/scripts/bulk-archive-links.ts
deleted file mode 100644
index 23e475f838..0000000000
--- a/apps/web/scripts/bulk-archive-links.ts
+++ /dev/null
@@ -1,18 +0,0 @@
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-
-const domain = "song.fyi";
-
-async function main() {
- const links = await prisma.link.updateMany({
- where: {
- domain,
- },
- data: {
- archived: true,
- },
- });
- console.log(links);
-}
-
-main();
diff --git a/apps/web/scripts/bulk-create-domains.ts b/apps/web/scripts/bulk-create-domains.ts
deleted file mode 100644
index 43db4bee7f..0000000000
--- a/apps/web/scripts/bulk-create-domains.ts
+++ /dev/null
@@ -1,72 +0,0 @@
-// @ts-nocheck – old migration script
-
-import { addDomainToVercel } from "@/lib/api/domains/add-domain-vercel";
-import { createLink } from "@/lib/api/links";
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-import * as fs from "fs";
-import * as Papa from "papaparse";
-
-const projectId = "xxx";
-const domains: { domain: string; target: string }[] = [];
-
-async function main() {
- Papa.parse(fs.createReadStream("domains_to_add.csv", "utf-8"), {
- header: true,
- skipEmptyLines: true,
- step: (result: { data: any }) => {
- const domain = result.data["Domain Name"];
- if (domain.length > 0) {
- domains.push({
- domain,
- target: result.data["ReDirect To"],
- });
- }
- },
- complete: async () => {
- domains.slice(0, 10).forEach(async ({ domain, target }) => {
- const vercelResponse = await addDomainToVercel(domain);
-
- if (
- vercelResponse.error &&
- vercelResponse.error.code !== "domain_already_in_use" // ignore this error
- ) {
- console.error(vercelResponse.error.message);
- return;
- }
-
- let response = await prisma.domain.findUnique({
- where: {
- slug: domain,
- },
- });
-
- if (!response) {
- response = await prisma.domain.create({
- data: {
- slug: domain,
- projectId,
- },
- });
- }
-
- const effects = await createLink({
- url: target,
- domain: domain,
- key: "_root",
- projectId,
- archived: false,
- publicStats: false,
- doIndex: false,
- trackConversion: false,
- proxy: false,
- rewrite: false,
- });
-
- console.log({ vercelResponse, prisma: response.id, effects });
- });
- },
- });
-}
-
-main();
diff --git a/apps/web/scripts/bulk-create-links.ts b/apps/web/scripts/bulk-create-links.ts
deleted file mode 100644
index d3d246ccd5..0000000000
--- a/apps/web/scripts/bulk-create-links.ts
+++ /dev/null
@@ -1,41 +0,0 @@
-import { chunk } from "@dub/utils";
-import "dotenv-flow/config";
-import * as fs from "fs";
-import * as Papa from "papaparse";
-import { bulkCreateLinks } from "../lib/api/links/bulk-create-links";
-
-const links: any[] = [];
-
-async function main() {
- Papa.parse(fs.createReadStream("links.csv", "utf-8"), {
- header: true,
- skipEmptyLines: true,
- step: (result: { data: any }) => {
- const urlObj = new URL(result.data["Short link"]);
- links.push({
- domain: urlObj.hostname,
- key: urlObj.pathname.slice(1),
- url: result.data["Destination URL"],
- createdAt: new Date(result.data["Creation date"]),
- projectId: "ws_xxx",
- userId: "user_xxx",
- });
- },
- complete: async () => {
- const chunks = chunk(links, 500);
- for (let i = 0; i < chunks.length; i++) {
- const chunk = chunks[i];
- await bulkCreateLinks({ links: chunk, skipRedisCache: true });
- console.log(
- `Created ${chunk.length * (i + 1)} of ${links.length} links, remaining: ${
- links.length - chunk.length * (i + 1)
- }`,
- );
- await new Promise((resolve) => setTimeout(resolve, 500));
- }
- console.log(`Imported ${links.length} links`);
- },
- });
-}
-
-main();
diff --git a/apps/web/scripts/bulk-delete-links.ts b/apps/web/scripts/bulk-delete-links.ts
deleted file mode 100644
index 52316d2994..0000000000
--- a/apps/web/scripts/bulk-delete-links.ts
+++ /dev/null
@@ -1,38 +0,0 @@
-import { includeProgramEnrollment } from "@/lib/api/links/include-program-enrollment";
-import { includeTags } from "@/lib/api/links/include-tags";
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-import { recordLink } from "../lib/tinybird";
-
-const programId = "prog_xxx";
-
-async function main() {
- const links = await prisma.link.findMany({
- where: {
- programId,
- },
- include: {
- ...includeTags,
- ...includeProgramEnrollment,
- },
- });
-
- console.log(`Found ${links.length} links to delete`);
- console.table(links.slice(-10), ["domain", "key"]);
-
- const response = await recordLink(links, { deleted: true });
-
- console.log("Deleted links in Tinybird", response);
-
- const deletedLinks = await prisma.link.deleteMany({
- where: {
- id: {
- in: links.map((link) => link.id),
- },
- },
- });
-
- console.log(`Deleted ${deletedLinks.count} links`);
-}
-
-main();
diff --git a/apps/web/scripts/bulk-update-links.ts b/apps/web/scripts/bulk-update-links.ts
deleted file mode 100644
index d3b41872f5..0000000000
--- a/apps/web/scripts/bulk-update-links.ts
+++ /dev/null
@@ -1,47 +0,0 @@
-import { includeProgramEnrollment } from "@/lib/api/links/include-program-enrollment";
-import { includeTags } from "@/lib/api/links/include-tags";
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-import { recordLink } from "../lib/tinybird";
-
-async function main() {
- const linksToUpdate = await prisma.link.findMany({
- where: {
- programId: "prog_xxx",
- partnerId: "pn_xxx",
- },
- });
-
- console.log(`Found ${linksToUpdate.length} links to update`);
- console.table(linksToUpdate.slice(-10), ["domain", "key"]);
-
- const res = await prisma.link.updateMany({
- where: {
- id: {
- in: linksToUpdate.map((link) => link.id),
- },
- },
- data: {
- tenantId: "xxx",
- },
- });
-
- console.log(res);
-
- const updatedLinks = await prisma.link.findMany({
- where: {
- id: {
- in: linksToUpdate.map((link) => link.id),
- },
- },
- include: {
- ...includeTags,
- ...includeProgramEnrollment,
- },
- });
-
- const tbRes = await recordLink(updatedLinks);
- console.log(tbRes);
-}
-
-main();
diff --git a/apps/web/scripts/cache-popular-urls.ts b/apps/web/scripts/cache-popular-urls.ts
deleted file mode 100644
index b82576d7cc..0000000000
--- a/apps/web/scripts/cache-popular-urls.ts
+++ /dev/null
@@ -1,60 +0,0 @@
-import { prisma } from "@dub/prisma";
-import { getApexDomain } from "@dub/utils";
-import "dotenv-flow/config";
-import * as fs from "fs";
-
-async function main() {
- const links = await prisma.link.findMany({
- where: {
- domain: "dub.sh",
- },
- select: {
- url: true,
- },
- });
-
- const apexDomains = links.map(({ url }) => {
- return getApexDomain(url);
- });
-
- // array of apex domains with their count of occurrences
- const apexDomainsWithCount = apexDomains.reduce((acc, domain) => {
- if (!domain) {
- return acc;
- }
- acc[domain] = (acc[domain] || 0) + 1;
- return acc;
- }, {});
-
- // sort apex domains by count of occurrences
- const sortedApexDomains = Object.keys(apexDomainsWithCount).sort(
- (a, b) => apexDomainsWithCount[b] - apexDomainsWithCount[a],
- );
-
- const topApexDomains = sortedApexDomains.slice(0, 100).map((domain) => {
- return {
- domain,
- count: apexDomainsWithCount[domain],
- };
- });
-
- console.table(topApexDomains);
-
- // apex domains with count of occurrences >= 5
-
- const popularApexDomains = sortedApexDomains.filter(
- (domain) => apexDomainsWithCount[domain] >= 5,
- );
-
- console.log({
- allApexDomains: sortedApexDomains.length,
- popularApexDomains: popularApexDomains.length,
- });
-
- fs.writeFileSync(
- "popular-domains.json",
- JSON.stringify(popularApexDomains, null, 2),
- );
-}
-
-main();
diff --git a/apps/web/scripts/check-customers.ts b/apps/web/scripts/check-customers.ts
deleted file mode 100644
index 65a8ebfe01..0000000000
--- a/apps/web/scripts/check-customers.ts
+++ /dev/null
@@ -1,67 +0,0 @@
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-
-async function main() {
- const customerIds = await prisma.customer
- .findMany({
- select: {
- externalId: true,
- },
- })
- .then(
- (customers) =>
- customers
- .map((customer) => customer.externalId)
- .filter((id) => id !== null) as string[],
- );
-
- console.log(customerIds);
-
- const projects = await prisma.project
- .findMany({
- where: {
- plan: {
- not: "free",
- },
- users: {
- some: {
- userId: {
- in: customerIds,
- },
- },
- },
- },
- select: {
- name: true,
- slug: true,
- plan: true,
- users: {
- select: {
- user: {
- select: {
- id: true,
- email: true,
- },
- },
- },
- where: {
- userId: {
- in: customerIds,
- },
- },
- },
- },
- })
- .then((projects) =>
- projects.map((project) => ({
- ...project,
- userId: project.users[0].user.id,
- email: project.users[0].user.email,
- users: undefined,
- })),
- );
-
- console.table(projects);
-}
-
-main();
diff --git a/apps/web/scripts/conversion-customers.ts b/apps/web/scripts/conversion-customers.ts
deleted file mode 100644
index 3d4e37e912..0000000000
--- a/apps/web/scripts/conversion-customers.ts
+++ /dev/null
@@ -1,76 +0,0 @@
-import { prisma } from "@dub/prisma";
-import { chunk } from "@dub/utils";
-import "dotenv-flow/config";
-
-// one-time script to get all workspaces/users that used conversion tracking on Dub in the last 3 months
-async function main() {
- const projectsByCustomerCount = await prisma.customer.groupBy({
- by: ["projectId"],
- where: {
- createdAt: {
- gte: new Date(Date.now() - 3 * 30 * 24 * 60 * 60 * 1000),
- },
- },
- _count: {
- id: true,
- },
- orderBy: {
- _count: {
- id: "desc",
- },
- },
- });
-
- const projectUserEmails = await prisma.project.findMany({
- where: {
- id: {
- in: projectsByCustomerCount.map((project) => project.projectId),
- },
- },
- select: {
- id: true,
- slug: true,
- users: {
- select: {
- user: {
- select: {
- email: true,
- },
- },
- },
- where: {
- user: {
- email: {
- not: null,
- },
- },
- },
- },
- },
- });
-
- console.table(
- projectsByCustomerCount.map((project) => {
- const projectData = projectUserEmails.find(
- (p) => p.id === project.projectId,
- );
-
- return {
- id: project.projectId,
- slug: projectData?.slug,
- userEmails: projectData?.users.map((u) => u.user.email),
- };
- }),
- );
-
- const chunks = chunk(
- projectUserEmails.flatMap((p) => p.users.map((u) => u.user.email)),
- 49,
- );
-
- for (const chunk of chunks) {
- console.log(chunk.join(", "));
- }
-}
-
-main();
diff --git a/apps/web/scripts/convert-manual-commissions.ts b/apps/web/scripts/convert-manual-commissions.ts
deleted file mode 100644
index 8fa735d6ca..0000000000
--- a/apps/web/scripts/convert-manual-commissions.ts
+++ /dev/null
@@ -1,48 +0,0 @@
-import { createId } from "@/lib/api/create-id";
-import { prisma } from "@dub/prisma";
-import { CommissionStatus, CommissionType, Prisma } from "@dub/prisma/client";
-import "dotenv-flow/config";
-
-async function main() {
- const manualPayouts = await prisma.payout.findMany({
- where: {
- periodStart: null,
- periodEnd: null,
- },
- include: {
- partner: true,
- },
- });
-
- console.table(manualPayouts);
-
- const manualCommissions: Prisma.CommissionCreateManyInput[] =
- manualPayouts.map((payout) => {
- return {
- id: createId({ prefix: "cm_" }),
- programId: payout.programId,
- partnerId: payout.partnerId,
- payoutId: payout.id,
- type: CommissionType.custom,
- amount: 0,
- quantity: 1,
- earnings: payout.amount,
- description: payout.description,
- status: CommissionStatus.processed,
- createdAt: payout.createdAt,
- updatedAt: new Date(),
- };
- });
-
- console.table(manualCommissions);
-
- // Add manual commissions to the database
- const createdCommissions = await prisma.commission.createMany({
- data: manualCommissions,
- skipDuplicates: true,
- });
-
- console.log({ createdCommissions });
-}
-
-main();
diff --git a/apps/web/scripts/customers/buffer/delete-old-links.ts b/apps/web/scripts/customers/buffer/delete-old-links.ts
deleted file mode 100644
index 1f67dd8a58..0000000000
--- a/apps/web/scripts/customers/buffer/delete-old-links.ts
+++ /dev/null
@@ -1,47 +0,0 @@
-import { prisma } from "@dub/prisma";
-import { Prisma } from "@dub/prisma/client";
-import "dotenv-flow/config";
-
-async function main() {
- const where: Prisma.LinkWhereInput = {
- projectId: "cm05wnnpo000711ztj05wwdbu",
- folderId: "fold_LIZsdjTgFVbQVGYSUmYAi5vT",
- archived: false,
- };
-
- const links = await prisma.link.findMany({
- where,
- select: {
- id: true,
- key: true,
- createdAt: true,
- },
- take: 1000,
- orderBy: {
- createdAt: "desc",
- },
- });
-
- if (!links.length) {
- console.log("No more links to delete.");
- return;
- }
-
- // console.table(links);
-
- await prisma.link.deleteMany({
- where: {
- id: {
- in: links.map((link) => link.id),
- },
- },
- });
-
- console.log(`Deleted ${links.length} links`);
- const finalDeletedLink = links[links.length - 1];
- console.log(
- `Final deleted link: ${finalDeletedLink.key} (${new Date(finalDeletedLink.createdAt).toISOString()})`,
- );
-}
-
-main();
diff --git a/apps/web/scripts/customers/perplexity/backfill-leads.ts b/apps/web/scripts/customers/perplexity/backfill-leads.ts
deleted file mode 100644
index d366116c45..0000000000
--- a/apps/web/scripts/customers/perplexity/backfill-leads.ts
+++ /dev/null
@@ -1,315 +0,0 @@
-// @ts-nocheck some weird typing issues below
-
-import { createId } from "@/lib/api/create-id";
-import { generateRandomName } from "@/lib/names";
-import { determinePartnerReward } from "@/lib/partners/determine-partner-reward";
-import { prisma } from "@dub/prisma";
-import { EventType } from "@dub/prisma/client";
-import { capitalize, nanoid } from "@dub/utils";
-import { COUNTRIES_TO_CONTINENTS } from "@dub/utils/src/constants/continents";
-import { geolocation } from "@vercel/functions";
-import "dotenv-flow/config";
-import * as fs from "fs";
-import { userAgent } from "next/server";
-import * as Papa from "papaparse";
-import { recordLeadWithTimestamp } from "../../../lib/tinybird/record-lead";
-
-let leadsToBackfill: {
- customerExternalId: string;
- partnerLinkKey: string;
- country: string;
- timestamp: string;
-}[] = [];
-
-async function main() {
- Papa.parse(fs.createReadStream("perplexity_leads.csv", "utf-8"), {
- header: true,
- skipEmptyLines: true,
- step: (result: {
- data: {
- CUSTOM_USER_ID: string;
- CAMPAIGN_NAME: string;
- COUNTRY: string;
- ADJUSTED_TIMESTAMP: string;
- };
- }) => {
- leadsToBackfill.push({
- customerExternalId: result.data.CUSTOM_USER_ID,
- partnerLinkKey: result.data.CAMPAIGN_NAME,
- country: result.data.COUNTRY,
- timestamp: result.data.ADJUSTED_TIMESTAMP,
- });
- },
- complete: async () => {
- console.log(`Found ${leadsToBackfill.length} leads to backfill`);
-
- const workspace = await prisma.project.findUniqueOrThrow({
- where: {
- id: "xxx",
- },
- });
-
- const partnerLinks = await prisma.link.findMany({
- where: {
- domain: "pplx.ai",
- key: {
- in: leadsToBackfill.map((lead) => lead.partnerLinkKey),
- },
- },
- });
-
- // const statsByLink = leadsToBackfill
- // .filter((lead) =>
- // partnerLinks.some(
- // (link) =>
- // link.key.toLowerCase() === lead.partnerLinkKey.toLowerCase(),
- // ),
- // )
- // .reduce(
- // (acc, lead) => {
- // const link = partnerLinks.find(
- // (link) =>
- // link.key.toLowerCase() === lead.partnerLinkKey.toLowerCase(),
- // )!;
- // acc[link.id] = {
- // clicks: (acc[link.id]?.clicks || 0) + 1,
- // leads: (acc[link.id]?.leads || 0) + 1,
- // conversions: (acc[link.id]?.conversions || 0) + 1,
- // };
- // return acc;
- // },
- // {} as Record<
- // string,
- // {
- // clicks: number;
- // leads: number;
- // conversions: number;
- // }
- // >,
- // );
-
- // for (const [linkId, stats] of Object.entries(statsByLink)) {
- // const res = await prisma.link.update({
- // where: { id: linkId },
- // data: {
- // clicks: {
- // increment: stats.clicks,
- // },
- // leads: {
- // increment: stats.leads,
- // },
- // },
- // });
- // console.log(
- // `Updated ${linkId} to ${res.clicks} clicks (+${stats.clicks} clicks), ${res.leads} leads (+${stats.leads} leads), ${res.conversions} conversions (+${stats.conversions} conversions)`,
- // );
- // }
-
- const clicksToCreate = await Promise.all(
- leadsToBackfill.map(async (lead) => {
- const link = partnerLinks.find(
- (link) =>
- link.key.toLowerCase() === lead.partnerLinkKey.toLowerCase(),
- );
- if (!link) {
- return null;
- }
- const req = new Request(link.url, {
- headers: new Headers({
- "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)",
- "x-forwarded-for": "127.0.0.1",
- "x-vercel-ip-country": lead.country.toUpperCase(),
- "x-vercel-ip-continent":
- COUNTRIES_TO_CONTINENTS[lead.country.toUpperCase()],
- }),
- });
-
- const clickId = nanoid(16);
- const geo = geolocation(req);
- const ua = userAgent(req);
-
- return {
- timestamp: new Date(lead.timestamp).toISOString(),
- identity_hash: lead.customerExternalId,
- click_id: clickId,
- workspace_id: workspace.id,
- link_id: link.id,
- domain: link.domain,
- key: link.key,
- url: link.url,
- ip: "",
- continent: req.headers.get("x-vercel-ip-continent") || "",
- country: geo.country || "Unknown",
- region: "Unknown",
- city: geo.city || "Unknown",
- latitude: geo.latitude || "Unknown",
- longitude: geo.longitude || "Unknown",
- vercel_region: geo.region || "",
- device: capitalize(ua.device.type) || "Desktop",
- device_vendor: ua.device.vendor || "Unknown",
- device_model: ua.device.model || "Unknown",
- browser: ua.browser.name || "Unknown",
- browser_version: ua.browser.version || "Unknown",
- engine: ua.engine.name || "Unknown",
- engine_version: ua.engine.version || "Unknown",
- os: ua.os.name || "Unknown",
- os_version: ua.os.version || "Unknown",
- cpu_architecture: ua.cpu?.architecture || "Unknown",
- ua: ua.ua || "Unknown",
- bot: 0,
- qr: 0,
- referer: "(direct)",
- referer_url: "(direct)",
- trigger: "link",
- };
- }),
- ).then((res) => res.filter((r) => r !== null));
-
- // Record clicks
- const clickRes = await fetch(
- `${process.env.TINYBIRD_API_URL}/v0/events?name=dub_click_events&wait=true`,
- {
- method: "POST",
- headers: {
- Authorization: `Bearer ${process.env.TINYBIRD_API_KEY}`,
- "Content-Type": "application/x-ndjson",
- },
- body: clicksToCreate.map((d) => JSON.stringify(d)).join("\n"),
- },
- ).then((res) => res.json());
- console.log("backfilled clicks", JSON.stringify(clickRes, null, 2));
-
- const customersToCreate = leadsToBackfill
- .map((lead, idx) => {
- const clickData = clicksToCreate[idx];
- if (!clickData) {
- return null;
- }
- return {
- id: createId({ prefix: "cus_" }),
- name: generateRandomName(),
- externalId: lead.customerExternalId,
- projectId: workspace.id,
- projectConnectId: workspace.stripeConnectId,
- clickId: clickData.click_id,
- linkId: clickData.link_id,
- country: clickData.country,
- clickedAt: new Date(lead.timestamp).toISOString(),
- createdAt: new Date(lead.timestamp).toISOString(),
- };
- })
- .filter((c) => c !== null);
-
- const customerRes = await prisma.customer.createMany({
- data: customersToCreate,
- skipDuplicates: true,
- });
- console.log("backfilled customers", JSON.stringify(customerRes, null, 2));
-
- const leadsToCreate = clicksToCreate.map((clickData, idx) => ({
- ...clickData,
- event_id: nanoid(16),
- event_name: "activated",
- customer_id: customersToCreate[idx]!.id,
- }));
-
- const leadRes = await recordLeadWithTimestamp(leadsToCreate);
- console.log("backfilled leads", JSON.stringify(leadRes, null, 2));
-
- const leadReward = await prisma.reward.findUniqueOrThrow({
- where: {
- id: "rw_1K1KTWJPS14SEENXFHE0FYN7J",
- },
- });
-
- const commissionsToCreate = leadsToCreate
- .map((leadData) => {
- const reward = determinePartnerReward({
- event: EventType.lead,
- programEnrollment: {
- totalCommissions: 0,
- leadReward: leadReward,
- },
- context: {
- customer: {
- country: leadData.country,
- },
- },
- });
-
- if (!reward) {
- return null;
- }
-
- const link = partnerLinks.find(
- (link) => link.id === leadData.link_id,
- );
-
- if (!link) {
- console.log(`Link not found for lead ${leadData.event_id}`);
- return null;
- }
-
- return {
- id: createId({ prefix: "cm_" }),
- programId: link.programId!,
- partnerId: link.partnerId!,
- rewardId: reward.id,
- customerId: leadData.customer_id,
- linkId: link.id,
- eventId: leadData.event_id,
- quantity: 1,
- amount: 0,
- type: EventType.lead,
- earnings: reward.amount,
- createdAt: new Date(leadData.timestamp).toISOString(),
- };
- })
- .filter((c) => c !== null);
-
- const commissionRes = await prisma.commission.createMany({
- data: commissionsToCreate,
- skipDuplicates: true,
- });
- console.log(
- "backfilled commissions",
- JSON.stringify(commissionRes, null, 2),
- );
-
- // const commissionByPartnerId = commissionsToCreate.reduce(
- // (acc, c) => {
- // acc[c.partnerId] = (acc[c.partnerId] || 0) + c.earnings;
- // return acc;
- // },
- // {} as Record<string, number>,
- // );
-
- // for (const [partnerId, amount] of Object.entries(commissionByPartnerId)) {
- // const res = await prisma.programEnrollment.update({
- // where: {
- // partnerId_programId: {
- // partnerId,
- // programId: workspace.defaultProgramId!,
- // },
- // },
- // data: {
- // totalCommissions: {
- // increment: amount,
- // },
- // },
- // });
- // console.log(
- // `Updated ${partnerId} to ${res.totalCommissions} total commissions (+${amount} commissions)`,
- // );
- // }
-
- const totalCommissionAmount = commissionsToCreate.reduce(
- (acc, c) => acc + c.earnings,
- 0,
- );
- console.log(`Total commission amount: ${totalCommissionAmount}`);
- },
- });
-}
-
-main();
diff --git a/apps/web/scripts/customers/perplexity/backfill-tenantids.ts b/apps/web/scripts/customers/perplexity/backfill-tenantids.ts
deleted file mode 100644
index 229a48658b..0000000000
--- a/apps/web/scripts/customers/perplexity/backfill-tenantids.ts
+++ /dev/null
@@ -1,100 +0,0 @@
-import { includeTags } from "@/lib/api/links/include-tags";
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-import * as fs from "fs";
-import * as Papa from "papaparse";
-import { recordLink } from "../../../lib/tinybird/record-link";
-
-const programId = "prog_xxx";
-
-let partnersToBackfill: {
- partnerId: string;
- tenantId: string;
-}[] = [];
-
-async function main() {
- Papa.parse(fs.createReadStream("pplx_partnerId_tenantId.csv", "utf-8"), {
- header: true,
- skipEmptyLines: true,
- step: (result: {
- data: {
- PARTNERID: string;
- TENANTID: string;
- };
- }) => {
- if (result.data.PARTNERID && result.data.TENANTID) {
- partnersToBackfill.push({
- partnerId: result.data.PARTNERID,
- tenantId: result.data.TENANTID,
- });
- }
- },
- complete: async () => {
- //filter out partners that are already backfilled
- const alreadyBackfilledPartners = await prisma.programEnrollment.findMany(
- {
- where: {
- programId,
- tenantId: {
- not: null,
- },
- },
- },
- );
- const filteredPartnersToBackfill = partnersToBackfill.filter(
- (partner) =>
- !alreadyBackfilledPartners.some(
- (p) => p.partnerId === partner.partnerId,
- ),
- );
-
- console.log(
- `Found ${filteredPartnersToBackfill.length} partners to backfill`,
- );
-
- for (const partner of filteredPartnersToBackfill) {
- const where = {
- programId,
- partnerId: partner.partnerId,
- };
-
- const programEnrollment = await prisma.$transaction(async (tx) => {
- await tx.link.updateMany({
- where,
- data: {
- tenantId: partner.tenantId,
- },
- });
- return await tx.programEnrollment.update({
- where: {
- partnerId_programId: where,
- },
- data: {
- tenantId: partner.tenantId,
- },
- include: {
- links: {
- include: includeTags,
- },
- },
- });
- });
-
- console.log(
- `Updated ${partner.partnerId} and their ${programEnrollment.links.length} links with tenantId ${partner.tenantId}`,
- );
-
- const tbRes = await recordLink(
- programEnrollment.links.map((l) => ({
- ...l,
- programEnrollment: { groupId: programEnrollment.groupId },
- })),
- );
-
- console.log("tbRes", tbRes);
- }
- },
- });
-}
-
-main();
diff --git a/apps/web/scripts/customers/perplexity/ban-partners.ts b/apps/web/scripts/customers/perplexity/ban-partners.ts
deleted file mode 100644
index 6466f4a920..0000000000
--- a/apps/web/scripts/customers/perplexity/ban-partners.ts
+++ /dev/null
@@ -1,202 +0,0 @@
-import { BAN_PARTNER_REASONS } from "@/lib/zod/schemas/partners";
-import PartnerBanned from "@dub/email/templates/partner-banned";
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-import * as fs from "fs";
-import * as Papa from "papaparse";
-import { linkCache } from "../../../lib/api/links/cache";
-import { syncTotalCommissions } from "../../../lib/api/partners/sync-total-commissions";
-import { queueBatchEmail } from "../../../lib/email/queue-batch-email";
-import { recordLink } from "../../../lib/tinybird";
-
-let partnersToBan: string[] = [];
-const bannedReason = "fraud";
-
-async function main() {
- Papa.parse(fs.createReadStream("pplx_fraud_ban.csv", "utf-8"), {
- header: true,
- skipEmptyLines: true,
- step: (result: {
- data: {
- PARTNER_ID: string;
- };
- }) => {
- if (result.data.PARTNER_ID) {
- partnersToBan.push(result.data.PARTNER_ID);
- }
- },
- complete: async () => {
- const programId = "prog_xxx";
- const program = await prisma.program.findUniqueOrThrow({
- where: {
- id: programId,
- },
- });
-
- while (true) {
- const programEnrollments = await prisma.programEnrollment.findMany({
- where: {
- programId,
- status: {
- not: "banned",
- },
- partnerId: {
- in: partnersToBan,
- },
- },
- include: {
- links: true,
- partner: true,
- },
- orderBy: {
- totalCommissions: "desc",
- },
- take: 100,
- });
-
- if (programEnrollments.length === 0) {
- console.log("No partners found");
- break;
- }
-
- console.log(`Found ${programEnrollments.length} partners to ban`);
-
- const commonWhere = {
- programId,
- partnerId: {
- in: programEnrollments.map((p) => p.partnerId),
- },
- };
-
- // if there are a lot of commissions, need to cancel them first
- // for (const enrollment of programEnrollments) {
- // while (true) {
- // const commissions = await prisma.commission.findMany({
- // where: {
- // programId,
- // partnerId: enrollment.partnerId,
- // status: "pending",
- // },
- // take: 500,
- // });
-
- // if (commissions.length === 0) {
- // break;
- // }
-
- // const res = await prisma.commission.updateMany({
- // where: {
- // id: {
- // in: commissions.map((commission) => commission.id),
- // },
- // },
- // data: {
- // status: "canceled",
- // },
- // });
- // console.log(
- // `Canceled ${res.count} commissions for partner ${enrollment.partnerId}`,
- // );
- // }
- // }
-
- const prismaRes = await prisma.$transaction([
- prisma.link.updateMany({
- where: commonWhere,
- data: {
- disabledAt: new Date(),
- expiresAt: new Date(),
- },
- }),
-
- prisma.programEnrollment.updateMany({
- where: commonWhere,
- data: {
- status: "banned",
- bannedAt: new Date(),
- bannedReason,
- clickRewardId: null,
- leadRewardId: null,
- saleRewardId: null,
- discountId: null,
- },
- }),
- prisma.commission.updateMany({
- where: {
- ...commonWhere,
- status: "pending",
- },
- data: {
- status: "canceled",
- },
- }),
-
- prisma.payout.updateMany({
- where: {
- ...commonWhere,
- status: "pending",
- },
- data: {
- status: "canceled",
- },
- }),
-
- prisma.bountySubmission.updateMany({
- where: {
- ...commonWhere,
- status: {
- not: "approved",
- },
- },
- data: {
- status: "rejected",
- },
- }),
- ]);
- console.log("prismaRes", prismaRes);
-
- const partnerLinks = programEnrollments.flatMap((p) => p.links);
- const redisRes = await linkCache.expireMany(partnerLinks);
- console.log("redisRes", redisRes);
-
- const tbRes = await recordLink(partnerLinks, { deleted: true });
- console.log("tbRes", tbRes);
-
- const commissionsRes = await Promise.all(
- programEnrollments.map(({ partner }) =>
- syncTotalCommissions({
- partnerId: partner.id,
- programId,
- }),
- ),
- );
-
- console.log("commissionsRes", commissionsRes);
-
- const qstashRes = await queueBatchEmail<typeof PartnerBanned>(
- programEnrollments.map((p) => ({
- to: p.partner.email!,
- subject: `You've been banned from the ${program.name} Partner Program`,
- variant: "notifications",
- replyTo: program.supportEmail || "noreply",
- templateName: "PartnerBanned",
- templateProps: {
- partner: {
- name: p.partner.name,
- email: p.partner.email!,
- },
- program: {
- name: program.name,
- slug: program.slug,
- },
- bannedReason: BAN_PARTNER_REASONS[bannedReason],
- },
- })),
- );
- console.log("qstashRes", qstashRes);
- }
- },
- });
-}
-
-main();
diff --git a/apps/web/scripts/customers/perplexity/deactivate-partners.ts b/apps/web/scripts/customers/perplexity/deactivate-partners.ts
deleted file mode 100644
index 2fffaa58c4..0000000000
--- a/apps/web/scripts/customers/perplexity/deactivate-partners.ts
+++ /dev/null
@@ -1,93 +0,0 @@
-import PartnerDeactivated from "@dub/email/templates/partner-deactivated";
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-import { linkCache } from "../../../lib/api/links/cache";
-import { queueBatchEmail } from "../../../lib/email/queue-batch-email";
-
-async function main() {
- const programId = "prog_xxx";
- const program = await prisma.program.findUniqueOrThrow({
- where: {
- id: programId,
- },
- });
-
- const partners = await prisma.programEnrollment.findMany({
- where: {
- programId,
- status: "approved",
- groupId: "grp_xxx",
- partner: {
- users: {
- some: {},
- },
- },
- },
- include: {
- links: true,
- partner: true,
- },
- take: 500,
- });
-
- if (partners.length === 0) {
- console.log("No partners found");
- return;
- }
-
- const where = {
- programId,
- partnerId: {
- in: partners.map((p) => p.partnerId),
- },
- };
-
- const prismaRes = await prisma.$transaction([
- prisma.link.updateMany({
- where,
- data: {
- expiresAt: new Date(),
- },
- }),
-
- prisma.programEnrollment.updateMany({
- where,
- data: {
- status: "deactivated",
- clickRewardId: null,
- leadRewardId: null,
- saleRewardId: null,
- discountId: null,
- },
- }),
- ]);
- console.log("prismaRes", prismaRes);
-
- const partnerLinks = partners.flatMap((p) => p.links);
- const redisRes = await linkCache.expireMany(partnerLinks);
- console.log("redisRes", redisRes);
-
- const qstashRes = await queueBatchEmail<typeof PartnerDeactivated>(
- partners.map((p) => ({
- variant: "notifications",
- subject: "Your partnership with Perplexity has been deactivated",
- to: p.partner.email!,
- replyTo: program.supportEmail || "noreply",
- templateName: "PartnerDeactivated",
- templateProps: {
- partner: {
- name: p.partner.name,
- email: p.partner.email!,
- },
- program: {
- name: program.name,
- slug: program.slug,
- },
- deactivatedReason: "because...",
- },
- })),
- );
- console.log("qstashRes", qstashRes);
-}
-
-main();
diff --git a/apps/web/scripts/customers/perplexity/move-partners.ts b/apps/web/scripts/customers/perplexity/move-partners.ts
deleted file mode 100644
index e572beca54..0000000000
--- a/apps/web/scripts/customers/perplexity/move-partners.ts
+++ /dev/null
@@ -1,54 +0,0 @@
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-
-const EMAIL_DOMAINS = [];
-
-async function main() {
- const matchingPartners = await prisma.partner.findMany({
- where: {
- OR: EMAIL_DOMAINS.map((domain) => ({
- email: {
- endsWith: `@${domain}`,
- },
- })),
- programs: {
- some: {
- programId: "prog_xxx",
- groupId: "grp_xxx",
- status: "approved",
- },
- },
- },
- select: {
- id: true,
- email: true,
- },
- });
-
- console.table(matchingPartners);
-
- const group = await prisma.partnerGroup.findUniqueOrThrow({
- where: {
- id: "grp_xxx",
- },
- });
-
- const { count } = await prisma.programEnrollment.updateMany({
- where: {
- partnerId: {
- in: matchingPartners.map((p) => p.id),
- },
- programId: "prog_xxx",
- },
- data: {
- groupId: group.id,
- clickRewardId: group.clickRewardId,
- leadRewardId: group.leadRewardId,
- saleRewardId: group.saleRewardId,
- discountId: group.discountId,
- },
- });
- console.log(`Moved ${count} partners to group ${group.id}`);
-}
-
-main();
diff --git a/apps/web/scripts/customers/perplexity/partners-updated-countries.ts b/apps/web/scripts/customers/perplexity/partners-updated-countries.ts
deleted file mode 100644
index 7edb1885c3..0000000000
--- a/apps/web/scripts/customers/perplexity/partners-updated-countries.ts
+++ /dev/null
@@ -1,72 +0,0 @@
-import { partnerProfileChangeHistoryLogSchema } from "@/lib/zod/schemas/partner-profile";
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-import * as fs from "fs";
-import * as Papa from "papaparse";
-
-const programId = "prog_xxx";
-
-async function main() {
- const partners = await prisma.partner.findMany({
- where: {
- programs: {
- some: {
- programId,
- status: "approved",
- },
- },
- country: {
- not: "US",
- },
- changeHistoryLog: {
- path: "$[*].from",
- array_contains: "US",
- },
- commissions: {
- some: {
- programId,
- status: {
- in: ["pending", "processed"],
- },
- },
- },
- },
- include: {
- commissions: {
- where: {
- programId,
- status: {
- in: ["pending", "processed"],
- },
- },
- },
- },
- });
-
- const finalPartners = partners.map((p) => {
- const changeHistoryLog = partnerProfileChangeHistoryLogSchema.parse(
- p.changeHistoryLog,
- );
- const finalCountryChange = changeHistoryLog
- .filter((ch) => ch.field === "country") // filter by country field
- .sort((a, b) => b.changedAt.getTime() - a.changedAt.getTime())[0]; // sort by changedAt descending
- return {
- id: p.id,
- email: p.email,
- pendingCommissions: p.commissions.reduce(
- (acc, commission) => acc + commission.earnings,
- 0,
- ),
- changedFrom: finalCountryChange?.from,
- changedTo: finalCountryChange?.to,
- currentCountry: p.country,
- changedAt: finalCountryChange?.changedAt,
- };
- });
-
- console.table(finalPartners);
-
- fs.writeFileSync("output.csv", Papa.unparse(finalPartners));
-}
-
-main();
diff --git a/apps/web/scripts/customers/perplexity/review-bounties.ts b/apps/web/scripts/customers/perplexity/review-bounties.ts
deleted file mode 100644
index 5e35d30d82..0000000000
--- a/apps/web/scripts/customers/perplexity/review-bounties.ts
+++ /dev/null
@@ -1,121 +0,0 @@
-import { createId } from "@/lib/api/create-id";
-import BountyApproved from "@dub/email/templates/bounty-approved";
-import { prisma } from "@dub/prisma";
-import { Prisma } from "@dub/prisma/client";
-import { chunk } from "@dub/utils";
-import "dotenv-flow/config";
-import { syncTotalCommissions } from "../../../lib/api/partners/sync-total-commissions";
-import { queueBatchEmail } from "../../../lib/email/queue-batch-email";
-
-const userId = "user_xxx";
-
-// approve bounty submissions
-async function main() {
- const bounty = await prisma.bounty.findUniqueOrThrow({
- where: {
- id: "bnty_xxx",
- },
- });
-
- const bountySubmissions = await prisma.bountySubmission.findMany({
- where: {
- bountyId: bounty.id,
- status: "submitted",
- },
- take: 1000,
- include: {
- partner: true,
- program: true,
- },
- });
-
- console.log(`Found ${bountySubmissions.length} bounty submissions`);
-
- const commissionsToCreate = bountySubmissions.map((submission) => ({
- id: createId({ prefix: "cm_" }),
- programId: submission.programId,
- partnerId: submission.partnerId,
- userId,
- quantity: 1,
- amount: 0,
- type: "custom",
- earnings: bounty.rewardAmount ?? 0,
- description: `Commission for successfully completed "${bounty.name}" bounty.`,
- createdAt: new Date(),
- })) satisfies Prisma.CommissionCreateManyInput[];
-
- const commissionRes = await prisma.commission.createMany({
- data: commissionsToCreate,
- skipDuplicates: true,
- });
-
- console.log(`Created ${commissionRes.count} commissions`);
-
- const submissionsUpdatedRes = await prisma.bountySubmission.updateMany({
- where: {
- id: {
- in: bountySubmissions.map((submission) => submission.id),
- },
- },
- data: {
- status: "approved",
- reviewedAt: new Date(),
- userId,
- },
- });
-
- console.log(`Updated ${submissionsUpdatedRes.count} submissions`);
-
- const chunks = chunk(bountySubmissions, 50);
- for (let i = 0; i < chunks.length; i++) {
- const chunk = chunks[i];
- await Promise.allSettled(
- chunk.map(async (submission) => {
- const commission = commissionsToCreate.find(
- (c) =>
- c.programId === submission.programId &&
- c.partnerId === submission.partnerId,
- );
- if (!commission) {
- console.log(`Commission not found for submission ${submission.id}`);
- return;
- }
- await Promise.allSettled([
- prisma.bountySubmission.update({
- where: { id: submission.id },
- data: { commissionId: commission.id },
- }),
- syncTotalCommissions({
- partnerId: submission.partnerId,
- programId: submission.programId,
- }),
- ]);
- }),
- );
- console.log(`Processed chunk ${i + 1} of ${chunks.length}`);
- }
-
- const qstashRes = await queueBatchEmail<typeof BountyApproved>(
- bountySubmissions.map((s) => ({
- subject: "Bounty approved!",
- to: s.partner.email!,
- variant: "notifications",
- replyTo: s.program.supportEmail || "noreply",
- templateName: "BountyApproved",
- templateProps: {
- email: s.partner.email!,
- program: {
- name: s.program.name,
- slug: s.program.slug,
- },
- bounty: {
- name: bounty.name,
- type: bounty.type,
- },
- },
- })),
- );
- console.log("qstashRes", qstashRes);
-}
-
-main();
diff --git a/apps/web/scripts/customers/perplexity/update-commissions.ts b/apps/web/scripts/customers/perplexity/update-commissions.ts
deleted file mode 100644
index a658c1c5f7..0000000000
--- a/apps/web/scripts/customers/perplexity/update-commissions.ts
+++ /dev/null
@@ -1,31 +0,0 @@
-import { prisma } from "@dub/prisma";
-import { Prisma } from "@dub/prisma/client";
-import "dotenv-flow/config";
-
-// update commissions for a program
-async function main() {
- const where: Prisma.CommissionWhereInput = {
- programId: "prog_xxx",
- partnerId: "pn_xxx",
- status: "processed",
- };
-
- const remainingCommissions = await prisma.commission.findMany({
- where,
- });
- console.log(`${remainingCommissions.length} commissions to update`);
-
- const updatedRes = await prisma.commission.updateMany({
- where: {
- id: {
- in: remainingCommissions.map((c) => c.id),
- },
- },
- data: {
- earnings: 0,
- },
- });
- console.log(`${updatedRes.count} commissions updated`);
-}
-
-main();
diff --git a/apps/web/scripts/customers/perplexity/update-notifications.ts b/apps/web/scripts/customers/perplexity/update-notifications.ts
deleted file mode 100644
index 23378f8db8..0000000000
--- a/apps/web/scripts/customers/perplexity/update-notifications.ts
+++ /dev/null
@@ -1,49 +0,0 @@
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-
-// bulk update notification preferences for a project
-async function main() {
- const notificationPreferences = await prisma.notificationPreference.findMany({
- where: {
- AND: [
- {
- projectUser: {
- projectId: "xxx",
- },
- },
- {
- projectUser: {
- user: {
- email: {
- notIn: ["[email protected]"],
- },
- },
- },
- },
- ],
- },
- });
-
- console.log(
- `Found ${notificationPreferences.length} notification preferences to update`,
- );
-
- const res = await prisma.notificationPreference.updateMany({
- where: {
- id: {
- in: notificationPreferences.map(
- (notificationPreference) => notificationPreference.id,
- ),
- },
- },
- data: {
- newPartnerApplication: false,
- newPartnerSale: false,
- newMessageFromPartner: false,
- },
- });
-
- console.log(`Updated ${res.count} notification preferences`);
-}
-
-main();
diff --git a/apps/web/scripts/customers/scaledmail/export-invoicecs-by-stripe-id.ts b/apps/web/scripts/customers/scaledmail/export-invoicecs-by-stripe-id.ts
deleted file mode 100644
index e109fc411b..0000000000
--- a/apps/web/scripts/customers/scaledmail/export-invoicecs-by-stripe-id.ts
+++ /dev/null
@@ -1,63 +0,0 @@
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-import * as fs from "fs";
-import * as Papa from "papaparse";
-import { stripeAppClient } from "../../../lib/stripe";
-
-const programId = "prog_xxx";
-const customerIdsToImport: string[] = [];
-
-// script to export stripe invoices based on the customer's stripeCustomerId
-async function main() {
- console.log(`Found ${customerIdsToImport.length} paying customers`);
- const program = await prisma.program.findUniqueOrThrow({
- where: {
- id: programId,
- },
- include: {
- workspace: true,
- },
- });
-
- console.log(`Found ${customerIdsToImport.length} paying customers`);
-
- for (const customerId of customerIdsToImport) {
- const invoices = await stripeAppClient({
- mode: "live",
- }).invoices.list(
- {
- customer: customerId,
- status: "paid",
- },
- {
- stripeAccount: program.workspace.stripeConnectId!,
- },
- );
-
- const invoicesToBackfill = invoices.data.map((invoice) => ({
- customerExternalId: customerId,
- invoiceId: invoice.id,
- amountPaid: invoice.amount_paid,
- createdAt: new Date(invoice.created * 1000).toISOString(),
- }));
-
- if (invoicesToBackfill.length > 0) {
- console.table(invoicesToBackfill, [
- "customerExternalId",
- "invoiceId",
- "amountPaid",
- "createdAt",
- ]);
-
- const filePath = "scaledmail_customer_stripe_invoices.csv";
- const fileExists = fs.existsSync(filePath);
-
- fs.appendFileSync(
- filePath,
- Papa.unparse(invoicesToBackfill, { header: !fileExists }) + "\n",
- );
- }
- }
-}
-
-main();
diff --git a/apps/web/scripts/customers/scaledmail/import-customers-by-stripe-id.ts b/apps/web/scripts/customers/scaledmail/import-customers-by-stripe-id.ts
deleted file mode 100644
index f9b5f1c943..0000000000
--- a/apps/web/scripts/customers/scaledmail/import-customers-by-stripe-id.ts
+++ /dev/null
@@ -1,279 +0,0 @@
-import { createId } from "@/lib/api/create-id";
-import { prisma } from "@dub/prisma";
-import { Prisma } from "@dub/prisma/client";
-import { chunk, nanoid, prettyPrint } from "@dub/utils";
-import "dotenv-flow/config";
-import { syncPartnerLinksStats } from "../../../lib/api/partners/sync-partner-links-stats";
-import { stripeAppClient } from "../../../lib/stripe";
-import { recordLeadWithTimestamp } from "../../../lib/tinybird/record-lead";
-
-const programId = "prog_xxx";
-const linkId = "link_xxx";
-const customerIdsToImport: string[] = [];
-
-// script to backfill customers + leads
-// we also use a batching logic for tinybird events ingestion
-async function main() {
- const program = await prisma.program.findUniqueOrThrow({
- where: {
- id: programId,
- },
- include: {
- workspace: true,
- },
- });
- const { workspace } = program;
-
- const link = await prisma.link.findUniqueOrThrow({
- where: {
- id: linkId,
- },
- });
-
- const existingCustomers = await prisma.customer.findMany({
- where: {
- stripeCustomerId: {
- in: customerIdsToImport,
- },
- },
- });
-
- const finalCustomerIdsToImport = customerIdsToImport.filter(
- (customerId) =>
- !existingCustomers.some(
- (customer) => customer.stripeCustomerId === customerId,
- ),
- );
-
- let finalLeadsToBackfill: {
- customerExternalId: string;
- stripeCustomerId: string;
- name: string | null;
- email: string | null;
- country: string;
- timestamp: Date;
- }[] = [];
-
- for (const customerId of finalCustomerIdsToImport) {
- const customer = await stripeAppClient({
- mode: "live",
- }).customers.retrieve(customerId, {
- stripeAccount: program.workspace.stripeConnectId!,
- });
-
- console.log(customer);
- if (customer.deleted) {
- continue;
- }
-
- finalLeadsToBackfill.push({
- customerExternalId: customer.id,
- stripeCustomerId: customerId,
- name: customer.name ?? null,
- email: customer.email,
- country: customer.address?.country ?? "US",
- timestamp: new Date(customer.created * 1000),
- });
- }
-
- console.log(`Found ${finalLeadsToBackfill.length} leads to backfill`);
- console.table(finalLeadsToBackfill.slice(0, 10));
-
- const clicksToCreate = finalLeadsToBackfill
- .map((lead) => {
- const clickId = nanoid(16);
-
- return {
- timestamp: new Date(lead.timestamp).toISOString(),
- identity_hash: lead.customerExternalId,
- click_id: clickId,
- workspace_id: workspace.id,
- link_id: link.id,
- domain: link.domain,
- key: link.key,
- url: link.url,
- ip: "",
- continent: "NA",
- country: lead.country,
- region: "Unknown",
- city: "Unknown",
- latitude: "Unknown",
- longitude: "Unknown",
- vercel_region: "",
- device: "Desktop",
- device_vendor: "Unknown",
- device_model: "Unknown",
- browser: "Unknown",
- browser_version: "Unknown",
- engine: "Unknown",
- engine_version: "Unknown",
- os: "Unknown",
- os_version: "Unknown",
- cpu_architecture: "Unknown",
- ua: "Unknown",
- bot: 0,
- qr: 0,
- referer: "(direct)",
- referer_url: "(direct)",
- trigger: "link",
- };
- })
- .filter((p): p is NonNullable<typeof p> => p !== null);
-
- // clickhouse only supports max 12 partitions (months) for a given event backfill
- // so we need to transform this into a list of lists, one for each year
- const clicksToCreateTB = clicksToCreate.reduce(
- (acc, curr) => {
- const year = new Date(curr.timestamp).getFullYear();
- if (!acc[year]) {
- acc[year] = [];
- }
- acc[year].push(curr);
- return acc;
- },
- {} as Record<number, any[]>,
- );
-
- // Record clicks
- Object.entries(clicksToCreateTB).forEach(async ([year, clicks]) => {
- const clicksBatch = clicks as typeof clicksToCreate;
- console.log(`backfilling ${clicksBatch.length} clicks for ${year}`);
- const clickRes = await fetch(
- `${process.env.TINYBIRD_API_URL}/v0/events?name=dub_click_events&wait=true`,
- {
- method: "POST",
- headers: {
- Authorization: `Bearer ${process.env.TINYBIRD_API_KEY}`,
- "Content-Type": "application/x-ndjson",
- },
- body: (clicksBatch as typeof clicksToCreate)
- .map((d) => JSON.stringify(d))
- .join("\n"),
- },
- ).then((res) => res.json());
- console.log("backfilled clicks", JSON.stringify(clickRes, null, 2));
- });
-
- const customersToCreate = finalLeadsToBackfill
- .map((lead, idx) => {
- const clickData = clicksToCreate[idx];
- if (!clickData) {
- return null;
- }
- return {
- id: createId({ prefix: "cus_" }),
- name: lead.name,
- email: lead.email,
- externalId: lead.customerExternalId,
- projectId: workspace.id,
- projectConnectId: workspace.stripeConnectId,
- stripeCustomerId: lead.stripeCustomerId,
- linkId: link.id,
- programId: link.programId,
- partnerId: link.partnerId,
- country: clickData.country,
- clickId: clickData.click_id,
- clickedAt: new Date(lead.timestamp).toISOString(),
- createdAt: new Date(lead.timestamp).toISOString(),
- };
- })
- .filter(
- (p): p is NonNullable<typeof p> => p !== null,
- ) satisfies Prisma.CustomerCreateManyInput[];
-
- console.table(customersToCreate.slice(0, 10));
-
- const customerRes = await prisma.customer.createMany({
- data: customersToCreate,
- skipDuplicates: true,
- });
- console.log("backfilled customers", prettyPrint(customerRes));
-
- const leadsToCreate = clicksToCreate.map((clickData, idx) => ({
- ...clickData,
- event_id: nanoid(16),
- event_name: "Sign up",
- customer_id: customersToCreate[idx]!.id,
- }));
-
- // same batching logic as above
- const leadsToCreateTB = leadsToCreate.reduce(
- (acc, curr) => {
- const year = new Date(curr.timestamp).getFullYear();
- if (!acc[year]) {
- acc[year] = [];
- }
- acc[year].push(curr);
- return acc;
- },
- {} as Record<number, any[]>,
- );
-
- Object.entries(leadsToCreateTB).forEach(async ([year, leads]) => {
- const leadsBatch = leads as typeof leadsToCreate;
- console.log(`backfilling ${leadsBatch.length} leads for ${year}`);
- const leadRes = await recordLeadWithTimestamp(leadsBatch);
- console.log("backfilled leads", prettyPrint(leadRes));
- });
-
- const statsByLink = finalLeadsToBackfill.reduce(
- (acc, lead) => {
- const leadCreatedAt = new Date(lead.timestamp);
- acc[link.id] = {
- clicks: (acc[link.id]?.clicks || 0) + 1,
- leads: (acc[link.id]?.leads || 0) + 1,
- // if there is no lastLeadAt, or the leadCreatedAt is greater than the lastLeadAt, set the lastLeadAt to the leadCreatedAt
- lastLeadAt:
- leadCreatedAt > (acc[link.id]?.lastLeadAt ?? new Date(0))
- ? leadCreatedAt
- : acc[link.id]?.lastLeadAt,
- };
- return acc;
- },
- {} as Record<
- string,
- {
- clicks: number;
- leads: number;
- lastLeadAt: Date | undefined;
- }
- >,
- );
-
- console.log(prettyPrint(Object.entries(statsByLink).slice(0, 10)));
-
- const statsByLinkChunks = chunk(Object.entries(statsByLink), 50);
- for (let i = 0; i < statsByLinkChunks.length; i++) {
- const chunk = statsByLinkChunks[i];
- console.log(
- `backfilling stats for ${chunk.length} links in batch ${i + 1} of ${statsByLinkChunks.length}`,
- );
- await Promise.all(
- chunk.map(async ([linkId, stats]) => {
- const res = await prisma.link.update({
- where: { id: linkId },
- data: {
- clicks: {
- increment: stats.clicks,
- },
- leads: {
- increment: stats.leads,
- },
- lastLeadAt: stats.lastLeadAt,
- },
- });
- console.log(
- `Updated ${linkId} to ${res.clicks} clicks (+${stats.clicks} clicks), ${res.leads} leads (+${stats.leads} leads)`,
- );
- const syncRes = await syncPartnerLinksStats({
- partnerId: res.partnerId!,
- programId: program.id,
- eventType: "lead",
- });
- console.log("synced stats", prettyPrint(syncRes));
- }),
- );
- }
-}
-
-main();
diff --git a/apps/web/scripts/customers/tella/remind-applications.ts b/apps/web/scripts/customers/tella/remind-applications.ts
deleted file mode 100644
index cc35943952..0000000000
--- a/apps/web/scripts/customers/tella/remind-applications.ts
+++ /dev/null
@@ -1,23 +0,0 @@
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-
-// update commissions for a program
-async function main() {
- const programApplications = await prisma.programApplication.findMany({
- where: {
- programId: "prog_xxx",
- enrollment: null,
- },
- select: {
- id: true,
- name: true,
- email: true,
- createdAt: true,
- },
- });
-
- console.log(`Found ${programApplications.length} applications`);
- console.table(programApplications);
-}
-
-main();
diff --git a/apps/web/scripts/customers/tella/update-commission-flat.ts b/apps/web/scripts/customers/tella/update-commission-flat.ts
deleted file mode 100644
index d3c8964a8e..0000000000
--- a/apps/web/scripts/customers/tella/update-commission-flat.ts
+++ /dev/null
@@ -1,111 +0,0 @@
-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 = {
- earnings: {
- not: 5000,
- },
- programId: "prog_xxx",
- status: "processed",
- partner: {
- email: {
- in: flatRatePartners,
- },
- },
- };
-
- const commissions = await prisma.commission.findMany({
- where,
- take: 50,
- });
-
- console.table(commissions, [
- "id",
- "partnerId",
- "amount",
- "earnings",
- "status",
- "createdAt",
- ]);
-
- await Promise.all(
- commissions.map(async (commission) => {
- // Get the first commission (earliest sale) for this customer-partner pair
- const firstCommission = await prisma.commission.findFirst({
- where: {
- partnerId: commission.partnerId,
- customerId: commission.customerId,
- type: "sale",
- },
- orderBy: {
- createdAt: "asc",
- },
- });
-
- // if the partner has already been rewarded for this customer, set earnings to 0
- // otherwise, set earnings to 5000
- const payload: Prisma.CommissionUpdateInput = {
- earnings:
- firstCommission && firstCommission.id !== commission.id
- ? 0
- : 5000,
- };
-
- console.log({
- firstCommissionId: firstCommission?.id,
- currentCommissionId: commission.id,
- payload,
- });
-
- if (payload.earnings === 5000) {
- await prisma.commission.update({
- where: { id: commission.id },
- data: payload,
- });
- }
-
- if (
- commission.status === "processed" &&
- commission.payoutId &&
- payload.earnings === 5000
- ) {
- const difference = 5000 - 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();
diff --git a/apps/web/scripts/customers/tella/update-commission-percentage.ts b/apps/web/scripts/customers/tella/update-commission-percentage.ts
deleted file mode 100644
index 7928c796b0..0000000000
--- a/apps/web/scripts/customers/tella/update-commission-percentage.ts
+++ /dev/null
@@ -1,81 +0,0 @@
-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 = {
- earnings: 0,
- programId: "prog_xxx",
- status: "pending",
- 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();
diff --git a/apps/web/scripts/customers/tella/update-commissions.ts b/apps/web/scripts/customers/tella/update-commissions.ts
deleted file mode 100644
index b7b4e036a5..0000000000
--- a/apps/web/scripts/customers/tella/update-commissions.ts
+++ /dev/null
@@ -1,86 +0,0 @@
-import { getProgramEnrollmentOrThrow } from "@/lib/api/programs/get-program-enrollment-or-throw";
-import { calculateSaleEarnings } from "@/lib/api/sales/calculate-sale-earnings";
-import { determinePartnerReward } from "@/lib/partners/determine-partner-reward";
-import { prisma } from "@dub/prisma";
-import { EventType, Prisma } from "@dub/prisma/client";
-import "dotenv-flow/config";
-
-// update commissions for a program
-async function main() {
- const where: Prisma.CommissionWhereInput = {
- earnings: 0,
- programId: "prog_xxx",
- status: "pending",
- };
-
- const commissions = await prisma.commission.findMany({
- where,
- take: 50,
- });
-
- const updatedCommissions = await Promise.all(
- commissions.map(async (commission) => {
- const programEnrollment = await getProgramEnrollmentOrThrow({
- partnerId: commission.partnerId,
- programId: commission.programId,
- include: {
- partner: true,
- links: true,
- ...(commission.type === "click" && { clickReward: true }),
- ...(commission.type === "lead" && { leadReward: true }),
- ...(commission.type === "sale" && { saleReward: true }),
- },
- });
-
- const reward = determinePartnerReward({
- event: commission.type as EventType,
- programEnrollment,
- });
-
- if (!reward) {
- return null;
- }
- // Recalculate the earnings based on the new amount
- const earnings = calculateSaleEarnings({
- reward,
- sale: {
- amount: commission.amount,
- quantity: commission.quantity,
- },
- });
-
- return prisma.commission.update({
- where: { id: commission.id },
- data: {
- earnings,
- },
- });
- }),
- );
- console.table(updatedCommissions, [
- "id",
- "partnerId",
- "amount",
- "earnings",
- "createdAt",
- ]);
-
- const remainingCommissions = await prisma.commission.count({
- where,
- });
- console.log(`${remainingCommissions} commissions left to update`);
- const pendingCommissions = await prisma.commission.findMany({
- where: {
- ...where,
- earnings: undefined,
- },
- });
- console.log(
- `${pendingCommissions.reduce((acc, curr) => acc + curr.amount, 0)} amount`,
- );
- console.log(
- `${pendingCommissions.reduce((acc, curr) => acc + curr.earnings, 0)} earnings`,
- );
-}
-
-main();
diff --git a/apps/web/scripts/customers/tella/update-reward-tier.ts b/apps/web/scripts/customers/tella/update-reward-tier.ts
deleted file mode 100644
index 2963348b62..0000000000
--- a/apps/web/scripts/customers/tella/update-reward-tier.ts
+++ /dev/null
@@ -1,49 +0,0 @@
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-import * as fs from "fs";
-import * as Papa from "papaparse";
-
-const partnersToUpdate: string[] = [];
-
-// one time script to help update partners to the right reward tier
-async function main() {
- Papa.parse(fs.createReadStream("xxx.csv", "utf-8"), {
- header: true,
- skipEmptyLines: true,
- step: (result: { data: { Email: string } }) => {
- partnersToUpdate.push(result.data.Email);
- },
- complete: async () => {
- const programEnrollments = await prisma.programEnrollment.findMany({
- where: {
- programId: "prog_xxx",
- partner: {
- email: { in: partnersToUpdate },
- },
- },
- include: {
- partner: true,
- },
- });
-
- // const res = await prisma.partnerReward.createMany({
- // skipDuplicates: true,
- // data: programEnrollments.map((pe) => ({
- // programEnrollmentId: pe.id,
- // rewardId: "rw_2LBaxoHvmvO7YpqAaY2kAUMm",
- // })),
- // });
-
- // console.log(res);
-
- const missingProgramEnrollments = partnersToUpdate.filter(
- (partnerEmail) =>
- !programEnrollments.some((pe) => pe.partner.email === partnerEmail),
- );
-
- console.log(missingProgramEnrollments);
- },
- });
-}
-
-main();
diff --git a/apps/web/scripts/customers/wispr-flow/update-links.ts b/apps/web/scripts/customers/wispr-flow/update-links.ts
deleted file mode 100644
index 2faeccc9d4..0000000000
--- a/apps/web/scripts/customers/wispr-flow/update-links.ts
+++ /dev/null
@@ -1,41 +0,0 @@
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-import { linkCache } from "../../../lib/api/links/cache";
-
-// update links
-async function main() {
- const url = undefined;
- if (!url) {
- console.log("No url provided");
- return;
- }
-
- const links = await prisma.link.findMany({
- where: {
- folderId: "fold_mslj7fMwqZPIFJqPQIoenL8H",
- ios: {
- not: url,
- },
- },
- take: 100,
- });
-
- const updatedLinks = await prisma.link.updateMany({
- where: {
- id: {
- in: links.map((link) => link.id),
- },
- },
- data: {
- ios: url,
- },
- });
-
- console.log(updatedLinks);
-
- const res = await linkCache.expireMany(links);
-
- console.log(res);
-}
-
-main();
diff --git a/apps/web/scripts/deactivate-programs.ts b/apps/web/scripts/deactivate-programs.ts
deleted file mode 100644
index c2a966d368..0000000000
--- a/apps/web/scripts/deactivate-programs.ts
+++ /dev/null
@@ -1,35 +0,0 @@
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-import { deactivateProgram } from "../lib/api/programs/deactivate-program";
-
-async function main() {
- const programs = await prisma.program.findMany({
- where: {
- deactivatedAt: null,
- workspace: {
- plan: {
- in: ["free", "pro"],
- },
- },
- },
- include: {
- workspace: true,
- },
- });
-
- console.log(`Found ${programs.length} programs to deactivate`);
- console.table(
- programs.map((p) => ({
- id: p.id,
- name: p.name,
- slug: p.slug,
- plan: p.workspace.plan,
- })),
- );
-
- for (const program of programs) {
- await deactivateProgram(program.id);
- }
-}
-
-main();
diff --git a/apps/web/scripts/delete-link-cache.ts b/apps/web/scripts/delete-link-cache.ts
deleted file mode 100644
index c41a894869..0000000000
--- a/apps/web/scripts/delete-link-cache.ts
+++ /dev/null
@@ -1,28 +0,0 @@
-import { redis } from "@/lib/upstash";
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-
-// This script delete the existing link's cache
-// Run this after https://github.com/dubinc/dub/pull/1335 is merged
-async function main() {
- const domains = await prisma.domain.findMany({
- take: 1,
- skip: 0,
- });
-
- if (domains.length === 0) {
- console.log("No domains found");
- return;
- }
-
- const pipeline = redis.pipeline();
-
- domains.map((domain) => pipeline.del(domain.slug.toLocaleLowerCase()));
-
- const result = await pipeline.exec();
-
- // 1 means success, 0 means failure
- console.log("Result", result);
-}
-
-main();
diff --git a/apps/web/scripts/download-links.ts b/apps/web/scripts/download-links.ts
deleted file mode 100644
index 5696525461..0000000000
--- a/apps/web/scripts/download-links.ts
+++ /dev/null
@@ -1,37 +0,0 @@
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-import * as fs from "fs";
-import * as Papa from "papaparse";
-
-async function main() {
- const links = await prisma.link.findMany({
- where: {
- projectId: "xxx",
- archived: false,
- folderId: null,
- domain: "xxx",
- clicks: {
- gt: 0,
- },
- key: {
- not: "_root",
- },
- },
- select: {
- id: true,
- shortLink: true,
- key: true,
- externalId: true,
- clicks: true,
- createdAt: true,
- },
- orderBy: {
- clicks: "desc",
- },
- });
- console.log(links.length);
-
- fs.writeFileSync("xxx.csv", Papa.unparse(links));
-}
-
-main();
diff --git a/apps/web/scripts/download-top-links.ts b/apps/web/scripts/download-top-links.ts
deleted file mode 100644
index 31ffb6d71b..0000000000
--- a/apps/web/scripts/download-top-links.ts
+++ /dev/null
@@ -1,45 +0,0 @@
-import { getAnalytics } from "@/lib/analytics/get-analytics";
-import { prisma } from "@dub/prisma";
-import { linkConstructor } from "@dub/utils";
-import "dotenv-flow/config";
-import * as fs from "fs";
-import * as Papa from "papaparse";
-
-async function main() {
- const topLinks = await getAnalytics({
- event: "clicks",
- groupBy: "top_links",
- workspaceId: "xxx",
- interval: "30d",
- root: false,
- }).then(async (data) => {
- return await Promise.all(
- data.map(
- async ({ link: linkId, clicks }: { link: string; clicks: number }) => {
- const link = await prisma.link.findUnique({
- where: {
- id: linkId,
- },
- select: {
- domain: true,
- key: true,
- },
- });
- if (!link) return;
- return {
- link: linkConstructor({
- domain: link.domain,
- key: link.key,
- pretty: true,
- }),
- clicks,
- };
- },
- ),
- );
- });
-
- fs.writeFileSync("xxx.csv", Papa.unparse(topLinks));
-}
-
-main();
diff --git a/apps/web/scripts/dub-domain-users.ts b/apps/web/scripts/dub-domain-users.ts
deleted file mode 100644
index 2a7a8bd258..0000000000
--- a/apps/web/scripts/dub-domain-users.ts
+++ /dev/null
@@ -1,23 +0,0 @@
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-
-async function main() {
- const users = await prisma.link.groupBy({
- by: ["userId"],
- where: {
- domain: "dub.sh",
- },
- _count: {
- userId: true,
- },
- orderBy: {
- _count: {
- userId: "desc",
- },
- },
- take: 100,
- });
- console.table(users);
-}
-
-main();
diff --git a/apps/web/scripts/dub-sdk.ts b/apps/web/scripts/dub-sdk.ts
deleted file mode 100644
index 83fa62b345..0000000000
--- a/apps/web/scripts/dub-sdk.ts
+++ /dev/null
@@ -1,13 +0,0 @@
-import { dub } from "@/lib/dub";
-import "dotenv-flow/config";
-
-async function main() {
- const data = await dub.analytics.retrieve({
- event: "clicks",
- groupBy: "triggers",
- interval: "30d",
- });
- console.log({ data });
-}
-
-main();
diff --git a/apps/web/scripts/fix-broken-link-tags.ts b/apps/web/scripts/fix-broken-link-tags.ts
deleted file mode 100644
index beb7b8c971..0000000000
--- a/apps/web/scripts/fix-broken-link-tags.ts
+++ /dev/null
@@ -1,45 +0,0 @@
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-
-async function main() {
- const linkTags = await prisma.link.findMany({
- where: {
- projectId: "clrei1gld0002vs9mzn93p8ik",
- },
- include: {
- tags: true,
- },
- orderBy: {
- createdAt: "desc",
- },
- });
-
- const tagIds = linkTags.flatMap((link) => link.tags.map((tag) => tag.tagId));
-
- const tags = await prisma.tag.findMany({
- where: {
- id: { in: tagIds },
- },
- });
- const tagsThatDontExist = tagIds.filter(
- (tagId) => !tags.some((tag) => tag.id === tagId),
- );
-
- const linksForTagsThatDontExist = await prisma.link.findMany({
- where: {
- tags: {
- some: { tagId: { in: tagsThatDontExist } },
- },
- },
- });
-
- const deleteLinks = await prisma.link.deleteMany({
- where: {
- id: { in: linksForTagsThatDontExist.map((link) => link.id) },
- },
- });
-
- console.log({ tagsThatDontExist, linksForTagsThatDontExist, deleteLinks });
-}
-
-main();
diff --git a/apps/web/scripts/fix-broken-root-domains.ts b/apps/web/scripts/fix-broken-root-domains.ts
deleted file mode 100644
index 8ef5f68da5..0000000000
--- a/apps/web/scripts/fix-broken-root-domains.ts
+++ /dev/null
@@ -1,67 +0,0 @@
-// @ts-nocheck
-
-import { createLink } from "@/lib/api/links";
-import { prisma } from "@dub/prisma";
-import { DEFAULT_LINK_PROPS } from "@dub/utils";
-import "dotenv-flow/config";
-
-async function main() {
- const domainsWithNoLinks = await prisma.domain.findMany({
- where: {
- links: {
- none: {
- key: "_root",
- },
- },
- },
- select: {
- slug: true,
- _count: {
- select: {
- links: true,
- },
- },
- createdAt: true,
- project: {
- select: {
- slug: true,
- users: {
- select: {
- userId: true,
- },
- },
- },
- },
- projectId: true,
- },
- take: 10,
- orderBy: {
- createdAt: "asc",
- },
- });
-
- await Promise.all(
- domainsWithNoLinks.map(async (domain) => {
- if (!domain.project?.users[0].userId) {
- console.log(`No user found for domain ${domain.slug}`);
- return;
- }
-
- const res = await createLink({
- ...DEFAULT_LINK_PROPS,
- domain: domain.slug,
- key: "_root",
- url: "",
- tags: undefined,
- createdAt: domain.createdAt,
- projectId: domain.projectId,
- userId: domain.project?.users[0].userId,
- });
- console.log({ res });
- }),
- );
-
- console.table(domainsWithNoLinks);
-}
-
-main();
diff --git a/apps/web/scripts/format-clicks.ts b/apps/web/scripts/format-clicks.ts
deleted file mode 100644
index 7d9fbb699b..0000000000
--- a/apps/web/scripts/format-clicks.ts
+++ /dev/null
@@ -1,108 +0,0 @@
-import { nanoid } from "@dub/utils";
-import "dotenv-flow/config";
-import * as fs from "fs";
-import * as Papa from "papaparse";
-
-function createLinksMapFromFile(filePath: string) {
- const fileContent = fs.readFileSync(filePath, { encoding: "utf-8" });
- const lines = fileContent.split("\n").filter((line) => line.trim());
-
- const linksMap = new Map();
-
- for (const line of lines) {
- try {
- const data = JSON.parse(line);
- // 'domain' and 'key' together form a unique identifier for each link
- const uniqueId = `${data.domain}/${
- data.key ? data.key.toLowerCase() : "_root"
- }`; // Customize this based on your data structure
- linksMap.set(uniqueId, data);
- } catch (error) {
- console.error("Error parsing line to JSON:", error);
- }
- }
-
- return linksMap;
-}
-
-// Function to determine the partition file name based on timestamp
-function getPartitionFileName(timestamp: string) {
- const date = new Date(timestamp);
- const start = new Date("2022-09-01");
- let current = new Date(start);
-
- // Calculate 8-month intervals until the timestamp falls within the current interval
- while (current < date) {
- const end = new Date(current);
- end.setMonth(end.getMonth() + 8);
- if (date >= current && date < end) {
- return `clicks-${current.getFullYear()}-${current.getMonth()}.ndjson`;
- }
- current = end;
- }
-
- // Fallback for any date beyond the calculated intervals
- return `clicks-${date.getFullYear()}-${date.getMonth()}.ndjson`;
-}
-
-// Function to get or create a write stream for a given partition
-function getWriteStreamForPartition(partitionFileName: string) {
- if (!writeStreams[partitionFileName]) {
- writeStreams[partitionFileName] = fs.createWriteStream(partitionFileName, {
- flags: "a",
- });
- }
- return writeStreams[partitionFileName];
-}
-
-const writeStreams: Record<string, fs.WriteStream> = {};
-
-async function main() {
- const links = createLinksMapFromFile("links-metadata.ndjson");
- const csvStream = fs.createReadStream("sql.csv", "utf-8");
-
- Papa.parse(csvStream, {
- header: true,
- skipEmptyLines: true,
- step: ({ data }) => {
- const link = links.get(
- `${data.domain}/${data.key ? data.key.toLowerCase() : "_root"}`,
- );
-
- if (!link) {
- // console.log(
- // `No link found for ${data.domain}/${data.key}. Probably deleted.`,
- // );
- return;
- }
-
- const { domain, key, id, projectId, url, alias, timestamp, ...rest } =
- data;
-
- const partitionFileName = getPartitionFileName(timestamp);
- const stream = getWriteStreamForPartition(partitionFileName);
-
- const clickData = {
- ...rest,
- timestamp,
- click_id: nanoid(16),
- link_id: link.link_id,
- url: link.url || "",
- bot: data.bot === "1" ? 1 : 0,
- };
-
- // Write directly to the appropriate file
- stream.write(JSON.stringify(clickData) + "\n");
- },
- complete: () => {
- // Close all write streams once processing is complete
- Object.values(writeStreams).forEach((stream) => stream.end());
- console.log("All data processed.");
- },
- error: (error) => {
- console.error("Error processing file:", error);
- },
- });
-}
-
-main();
diff --git a/apps/web/scripts/format-links.ts b/apps/web/scripts/format-links.ts
deleted file mode 100644
index 81c82a6178..0000000000
--- a/apps/web/scripts/format-links.ts
+++ /dev/null
@@ -1,68 +0,0 @@
-// @ts-nocheck – old migration script
-
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-import * as fs from "fs";
-
-const linkCriteria = {
- select: {
- id: true,
- domain: true,
- key: true,
- url: true,
- projectId: true,
- },
-};
-
-async function main() {
- const links = await Promise.all(
- [0, 1, 2, 3].map((idx) =>
- prisma.link.findMany({
- ...linkCriteria,
- skip: idx * 100000,
- take: 100000,
- }),
- ),
- ).then((results) => results.flat());
-
- const domains = await prisma.domain
- .findMany({
- select: {
- id: true,
- slug: true,
- target: true,
- projectId: true,
- },
- })
- .then((domains) =>
- domains.map((domain) => ({
- ...domain,
- domain: domain.slug,
- key: "_root",
- url: domain.target,
- })),
- );
-
- const file = fs.createWriteStream(`links-metadata.ndjson`);
-
- // Iterate over the array and write each object as a string
- [...links, ...domains].forEach((obj) => {
- file.write(
- JSON.stringify({
- ...obj,
- timestamp: new Date(Date.now()).toISOString(),
- id: undefined,
- projectId: undefined,
- link_id: obj.id,
- url: obj.url || "",
- project_id: obj.projectId || "",
- deleted: 0,
- }) + "\n",
- );
- });
-
- // Close the stream
- file.end();
-}
-
-main();
diff --git a/apps/web/scripts/generate-openapi.ts b/apps/web/scripts/generate-openapi.ts
deleted file mode 100644
index 5187a40848..0000000000
--- a/apps/web/scripts/generate-openapi.ts
+++ /dev/null
@@ -1,5 +0,0 @@
-import { document } from "@/lib/openapi";
-import fs from "fs";
-import path from "path";
-
-fs.writeFileSync(path.join("openapi.json"), JSON.stringify(document, null, 2));
diff --git a/apps/web/scripts/get-api-users.ts b/apps/web/scripts/get-api-users.ts
deleted file mode 100644
index fbba9af8f7..0000000000
--- a/apps/web/scripts/get-api-users.ts
+++ /dev/null
@@ -1,30 +0,0 @@
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-
-async function main() {
- const users = await prisma.user.findMany({
- where: {
- OR: [
- {
- tokens: {
- some: {},
- },
- },
- {
- restrictedTokens: {
- some: {},
- },
- },
- ],
- },
- });
-
- console.log(
- users
- .filter((user) => user.email)
- .map((user) => user.email)
- .join(", "),
- );
-}
-
-main();
diff --git a/apps/web/scripts/get-customers.ts b/apps/web/scripts/get-customers.ts
deleted file mode 100644
index f1fec6ba57..0000000000
--- a/apps/web/scripts/get-customers.ts
+++ /dev/null
@@ -1,51 +0,0 @@
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-
-async function main() {
- const projects = await prisma.project.findMany({
- where: {
- plan: "pro",
- stripeId: {
- not: null,
- },
- createdAt: {
- lt: new Date("2024-01-17"),
- },
- },
- select: {
- slug: true,
- plan: true,
- createdAt: true,
- users: {
- select: {
- user: {
- select: {
- email: true,
- },
- },
- },
- where: {
- role: "owner",
- },
- },
- },
- orderBy: {
- createdAt: "desc",
- },
- });
- // console.table(
- // projects.map((p) => ({
- // ...p,
- // emails: p.users.map((u) => u.user.email)[0],
- // })),
- // ["slug", "plan", "createdAt", "emails"],
- // );
-
- console.log(
- Array.from(
- new Set(projects.flatMap((p) => p.users.map((u) => u.user.email))),
- ).join(","),
- );
-}
-
-main();
diff --git a/apps/web/scripts/get-inactive-users.ts b/apps/web/scripts/get-inactive-users.ts
deleted file mode 100644
index 6597d08fd5..0000000000
--- a/apps/web/scripts/get-inactive-users.ts
+++ /dev/null
@@ -1,44 +0,0 @@
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-
-async function main() {
- const [users, count] = await Promise.all([
- prisma.user.findMany({
- where: {
- projects: {
- none: {},
- },
- links: {
- none: {},
- },
- },
- select: {
- email: true,
- createdAt: true,
- _count: {
- select: {
- projects: true,
- links: true,
- },
- },
- },
- orderBy: { createdAt: "asc" },
- take: 100,
- }),
- prisma.user.count({
- where: {
- projects: {
- none: {},
- },
- links: {
- none: {},
- },
- },
- }),
- ]);
- // log in table format
- console.table(users);
- console.log(`Total: ${count}`);
-}
-
-main();
diff --git a/apps/web/scripts/get-premium-workspaces.ts b/apps/web/scripts/get-premium-workspaces.ts
deleted file mode 100644
index 596308657f..0000000000
--- a/apps/web/scripts/get-premium-workspaces.ts
+++ /dev/null
@@ -1,51 +0,0 @@
-import { normalizeWorkspaceId } from "@/lib/api/workspaces/workspace-id";
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-import * as fs from "fs";
-import * as Papa from "papaparse";
-
-async function main() {
- const workspaces = await prisma.project.findMany({
- where: {
- NOT: {
- plan: "free",
- },
- slug: {
- notIn: ["acme", "legal"],
- },
- domains: {
- none: {
- verified: true,
- },
- },
- links: {
- some: {
- domain: "dub.sh",
- createdAt: {
- gt: new Date("2024-02-01"),
- },
- },
- },
- },
- select: {
- id: true,
- slug: true,
- },
- orderBy: {
- createdAt: "asc",
- },
- });
-
- console.table(workspaces);
- fs.writeFileSync(
- "premium_workspaces.csv",
- Papa.unparse(
- workspaces.map((w) => ({
- id: normalizeWorkspaceId(w.id),
- slug: w.slug,
- })),
- ),
- );
-}
-
-main();
diff --git a/apps/web/scripts/get-top-domains-for-links.ts b/apps/web/scripts/get-top-domains-for-links.ts
deleted file mode 100644
index fe53e32cde..0000000000
--- a/apps/web/scripts/get-top-domains-for-links.ts
+++ /dev/null
@@ -1,29 +0,0 @@
-import { prisma } from "@dub/prisma";
-import { LEGAL_USER_ID, LEGAL_WORKSPACE_ID } from "@dub/utils";
-import "dotenv-flow/config";
-
-async function main() {
- const topDomains = (await prisma.$queryRaw`
- SELECT
- SUBSTRING_INDEX(SUBSTRING_INDEX(REPLACE(REPLACE(url, 'https://', ''), 'http://', ''), '/', 1), '?', 1) as domain,
- COUNT(*) as count
- FROM Link
- WHERE
- projectId IS NOT NULL
- AND projectId != ${LEGAL_WORKSPACE_ID}
- AND userId IS NOT NULL
- AND userId != ${LEGAL_USER_ID}
- AND url NOT LIKE 'https%3A%2F%2F%'
- GROUP BY SUBSTRING_INDEX(SUBSTRING_INDEX(REPLACE(REPLACE(url, 'https://', ''), 'http://', ''), '/', 1), '?', 1)
- HAVING count >= 5
- ORDER BY count DESC
- `) as { domain: string; count: number }[];
-
- console.log(`Found ${topDomains.length} top domains`);
- console.table(topDomains);
- console.log(`"${topDomains.map(({ domain }) => domain).join('", "')}"`);
-
- await prisma.$disconnect();
-}
-
-main();
diff --git a/apps/web/scripts/get-top-links-for-workspace.ts b/apps/web/scripts/get-top-links-for-workspace.ts
deleted file mode 100644
index fa52e738a3..0000000000
--- a/apps/web/scripts/get-top-links-for-workspace.ts
+++ /dev/null
@@ -1,74 +0,0 @@
-import { getAnalytics } from "@/lib/analytics/get-analytics";
-import { prisma } from "@dub/prisma";
-import { linkConstructor } from "@dub/utils";
-import "dotenv-flow/config";
-
-async function main() {
- const workspace = await prisma.project.findUnique({
- where: {
- slug: "dub",
- },
- select: {
- id: true,
- name: true,
- slug: true,
- usage: true,
- usageLimit: true,
- plan: true,
- billingCycleStart: true,
- users: {
- select: {
- user: true,
- },
- },
- domains: {
- where: {
- verified: true,
- },
- },
- sentEmails: true,
- createdAt: true,
- },
- });
- if (!workspace) {
- console.log("No workspace found");
- return;
- }
- const topLinks = await getAnalytics({
- event: "clicks",
- groupBy: "top_links",
- workspaceId: workspace.id,
- interval: "30d",
- root: false,
- }).then(async (data) => {
- const topFive = data.slice(0, 5);
- return await Promise.all(
- topFive.map(
- async ({ link: linkId, clicks }: { link: string; clicks: number }) => {
- const link = await prisma.link.findUnique({
- where: {
- id: linkId,
- },
- select: {
- domain: true,
- key: true,
- },
- });
- if (!link) return;
- return {
- link: linkConstructor({
- domain: link.domain,
- key: link.key,
- pretty: true,
- }),
- clicks,
- };
- },
- ),
- );
- });
-
- console.table(topLinks);
-}
-
-main();
diff --git a/apps/web/scripts/get-users-by-links.ts b/apps/web/scripts/get-users-by-links.ts
deleted file mode 100644
index e5c44d6525..0000000000
--- a/apps/web/scripts/get-users-by-links.ts
+++ /dev/null
@@ -1,46 +0,0 @@
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-
-async function main() {
- const users = await prisma.link.groupBy({
- by: ["userId"],
- _count: {
- id: true,
- },
- where: {
- domain: {
- in: ["dub.sh", "chatg.pt", "spti.fi", "amzn.id"],
- },
- },
- orderBy: {
- _count: {
- id: "desc",
- },
- },
- take: 100,
- });
- const usersWithEmails = await Promise.all(
- users
- .filter(({ userId }) => userId !== null)
- .map(async (user) => {
- const u = await prisma.user.findUnique({
- where: {
- id: user.userId!,
- },
- select: {
- email: true,
- },
- });
- if (!u) {
- return user;
- }
- return {
- email: u.email,
- links: user._count.id,
- };
- }),
- );
- console.table(usersWithEmails);
-}
-
-main();
diff --git a/apps/web/scripts/get-users-with-multiple-free-workspaces.ts b/apps/web/scripts/get-users-with-multiple-free-workspaces.ts
deleted file mode 100644
index eeb6ba921f..0000000000
--- a/apps/web/scripts/get-users-with-multiple-free-workspaces.ts
+++ /dev/null
@@ -1,35 +0,0 @@
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-
-async function main() {
- const freeProjectUsers = await prisma.projectUsers
- .groupBy({
- by: ["userId"],
- where: {
- role: "owner",
- project: {
- plan: "free",
- },
- user: {
- createdAt: {
- gte: new Date("2024-01-01"), // we used to allow creating unlimited free projects before this date
- },
- },
- },
- _count: {
- projectId: true,
- },
- orderBy: {
- _count: {
- projectId: "desc",
- },
- },
- })
- .then((res) => res.filter((user) => user._count.projectId > 2));
-
- // only 2 users: user_1JQ4XWW8DE941MT2W58QEBH0H, user_1JQ6T72YAWND8716B3DTAE979
- // both via pen-testing
- console.log(freeProjectUsers);
-}
-
-main();
diff --git a/apps/web/scripts/get-users.ts b/apps/web/scripts/get-users.ts
deleted file mode 100644
index 61f1b3dcc9..0000000000
--- a/apps/web/scripts/get-users.ts
+++ /dev/null
@@ -1,20 +0,0 @@
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-import * as fs from "fs";
-import * as Papa from "papaparse";
-
-async function main() {
- const users = await prisma.user.findMany({
- select: {
- email: true,
- name: true,
- createdAt: true,
- },
- });
-
- console.log(users.length, "users found");
-
- fs.writeFileSync("dub-users.csv", Papa.unparse(users));
-}
-
-main();
diff --git a/apps/web/scripts/get-workspaces-by-clicks.ts b/apps/web/scripts/get-workspaces-by-clicks.ts
deleted file mode 100644
index 3200c33a97..0000000000
--- a/apps/web/scripts/get-workspaces-by-clicks.ts
+++ /dev/null
@@ -1,24 +0,0 @@
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-
-async function main() {
- const workspaces = await prisma.project.findMany({
- select: {
- slug: true,
- plan: true,
- usage: true,
- _count: {
- select: {
- domains: true,
- },
- },
- },
- orderBy: {
- usage: "desc",
- },
- take: 100,
- });
- console.table(workspaces, ["slug", "plan", "usage", "_count"]);
-}
-
-main();
diff --git a/apps/web/scripts/get-workspaces-by-links.ts b/apps/web/scripts/get-workspaces-by-links.ts
deleted file mode 100644
index cfc19a430c..0000000000
--- a/apps/web/scripts/get-workspaces-by-links.ts
+++ /dev/null
@@ -1,20 +0,0 @@
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-
-async function main() {
- const workspaces = await prisma.project.findMany({
- select: {
- slug: true,
- plan: true,
- linksUsage: true,
- linksLimit: true,
- },
- orderBy: {
- linksUsage: "desc",
- },
- take: 100,
- });
- console.table(workspaces, ["slug", "plan", "linksUsage", "linksLimit"]);
-}
-
-main();
diff --git a/apps/web/scripts/lua-convert.ts b/apps/web/scripts/lua-convert.ts
deleted file mode 100644
index 9da7d76ead..0000000000
--- a/apps/web/scripts/lua-convert.ts
+++ /dev/null
@@ -1,26 +0,0 @@
-import { redis } from "@/lib/upstash";
-import "dotenv-flow/config";
-
-async function main() {
- const script = `
-local list_key = KEYS[1]
-local sorted_set_key = KEYS[2]
-
-local list_length = 50
-
-local output = {}
-
-for i = 1, list_length do
- local json_item = redis.call('LINDEX', list_key, i - 1)
-
- table.insert(output, json_item)
-end
-
-return output`;
-
- const response = await redis.eval(script, ["metatags, metatags-set"], []);
-
- console.log(response);
-}
-
-main();
diff --git a/apps/web/scripts/migrate-commission-attributes.ts b/apps/web/scripts/migrate-commission-attributes.ts
deleted file mode 100644
index e9a4dbed0f..0000000000
--- a/apps/web/scripts/migrate-commission-attributes.ts
+++ /dev/null
@@ -1,37 +0,0 @@
-// @ts-nocheck – since this contains old schema code
-
-import { createId } from "@/lib/api/create-id";
-import { prisma } from "@dub/prisma";
-import { EventType } from "@dub/prisma/client";
-import "dotenv-flow/config";
-
-// Migrate the commission attributes from Program table to Reward table.
-async function main() {
- const programs = await prisma.program.findMany();
-
- for (const program of programs) {
- const maxDuration =
- program.commissionDuration === null
- ? program.commissionDuration
- : program.commissionInterval === "year"
- ? program.commissionDuration * 12
- : program.commissionDuration;
-
- const reward = await prisma.reward.create({
- data: {
- id: createId({ prefix: "rw_" }),
- programId: program.id,
- event: EventType.sale,
- type: program.commissionType,
- amount: program.commissionAmount,
- maxDuration,
- createdAt: program.createdAt,
- updatedAt: program.updatedAt,
- },
- });
-
- console.log(`Migrated commission attributes for program ${program.id}`);
- }
-}
-
-main();
diff --git a/apps/web/scripts/migrations/backfill-attribution.ts b/apps/web/scripts/migrations/backfill-attribution.ts
deleted file mode 100644
index 94ac89f24e..0000000000
--- a/apps/web/scripts/migrations/backfill-attribution.ts
+++ /dev/null
@@ -1,99 +0,0 @@
-import { createId } from "@/lib/api/create-id";
-import { generateRandomName } from "@/lib/names";
-import { prisma } from "@dub/prisma";
-import { DUB_WORKSPACE_ID, nanoid } from "@dub/utils";
-import "dotenv-flow/config";
-import { getClickEvent, recordLeadWithTimestamp } from "../../lib/tinybird";
-
-const referredUserEmail = "[email protected]";
-const referredWorkspaceSlug = "xxx";
-const clickId = "xxx";
-const linkId = "xxx";
-
-// backfill-attribution – backfill missing lead events for a link
-async function main() {
- const referredUser = await prisma.user.findUniqueOrThrow({
- where: {
- email: referredUserEmail,
- },
- });
-
- const leadTime = referredUser.createdAt;
-
- const referredWorkspace = await prisma.project.findUniqueOrThrow({
- where: {
- slug: referredWorkspaceSlug,
- },
- });
-
- if (!referredWorkspace.stripeId) {
- throw new Error("Referred workspace does not have a Stripe ID");
- }
-
- const dubWorkspace = await prisma.project.findUniqueOrThrow({
- where: {
- id: DUB_WORKSPACE_ID,
- },
- });
-
- const clickData = await getClickEvent({
- clickId,
- });
-
- if (!clickData) {
- throw new Error("Click event not found");
- }
-
- const existingCustomer = await prisma.customer.findUnique({
- where: {
- stripeCustomerId: referredWorkspace.stripeId,
- },
- });
-
- if (!existingCustomer) {
- const customer = await prisma.customer.create({
- data: {
- id: createId({ prefix: "cus_" }),
- name: referredUser.name || referredUser.email || generateRandomName(),
- email: referredUser.email,
- avatar: referredUser.image,
- externalId: referredUser.id,
- projectId: dubWorkspace.id,
- projectConnectId: dubWorkspace.stripeConnectId,
- clickId: clickId,
- linkId: linkId,
- country: clickData.country,
- clickedAt: new Date(clickData.timestamp),
- createdAt: leadTime,
- },
- });
-
- console.log("recorded customer", customer);
-
- const leadRes = await recordLeadWithTimestamp({
- ...clickData,
- event_id: nanoid(16),
- event_name: "Sign up",
- customer_id: customer.id,
- timestamp: leadTime.toISOString(),
- });
-
- console.log("recorded lead", leadRes);
- }
-
- // update link clicks and leads count
- const res = await prisma.link.update({
- where: {
- id: linkId,
- },
- data: {
- leads: {
- increment: 1,
- },
- },
- });
-
- console.log(`updated link ${linkId} leads to ${res.leads}`);
-}
-
-main();
diff --git a/apps/web/scripts/migrations/backfill-banned-partner-links.ts b/apps/web/scripts/migrations/backfill-banned-partner-links.ts
deleted file mode 100644
index ff23d0cf01..0000000000
--- a/apps/web/scripts/migrations/backfill-banned-partner-links.ts
+++ /dev/null
@@ -1,50 +0,0 @@
-import { includeProgramEnrollment } from "@/lib/api/links/include-program-enrollment";
-import { includeTags } from "@/lib/api/links/include-tags";
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-import { recordLink } from "../../lib/tinybird/record-link";
-
-async function main() {
- // Find links for partners that were banned
- const links = await prisma.link.findMany({
- where: {
- programEnrollment: {
- status: "banned",
- },
- },
- include: {
- ...includeTags,
- ...includeProgramEnrollment,
- },
- orderBy: {
- id: "asc",
- },
- skip: 1500,
- take: 500,
- });
-
- if (links.length === 0) {
- return;
- }
-
- console.log(`Found ${links.length} links to process.`);
- const prismaRes = await prisma.link.updateMany({
- where: {
- id: {
- in: links.map((link) => link.id),
- },
- },
- data: {
- disabledAt: new Date(),
- },
- });
- console.log(`Updated ${prismaRes.count} links to be disabled.`);
-
- const tbRes = await recordLink(links, {
- deleted: true,
- });
-
- console.log("Deleted links in Tinybird", tbRes);
-}
-
-main();
diff --git a/apps/web/scripts/migrations/backfill-click-commissions.ts b/apps/web/scripts/migrations/backfill-click-commissions.ts
deleted file mode 100644
index c6ff3f848e..0000000000
--- a/apps/web/scripts/migrations/backfill-click-commissions.ts
+++ /dev/null
@@ -1,88 +0,0 @@
-import { createId } from "@/lib/api/create-id";
-import { getProgramEnrollmentOrThrow } from "@/lib/api/programs/get-program-enrollment-or-throw";
-import { getRewardAmount } from "@/lib/partners/get-reward-amount";
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-import { getAnalytics } from "../../lib/analytics/get-analytics";
-import { determinePartnerReward } from "../../lib/partners/determine-partner-reward";
-
-async function main() {
- const programId = "prog_xxx";
- const partnerId = "pn_xxx";
-
- const programEnrollment = await getProgramEnrollmentOrThrow({
- partnerId,
- programId,
- include: {
- partner: true,
- links: true,
- clickReward: true,
- },
- });
-
- const reward = determinePartnerReward({
- event: "click",
- programEnrollment,
- });
-
- const link = await prisma.link.findFirst({
- where: {
- programId,
- partnerId,
- },
- });
-
- if (!reward) {
- throw new Error("Reward not found");
- }
-
- console.log(reward);
-
- const clicksData = await getAnalytics({
- programId,
- partnerId,
- event: "clicks",
- groupBy: "timeseries",
- interval: "90d",
- });
-
- const payoutIds = {
- "2024-11": "po_xxx",
- "2024-12": "po_xxx",
- "2025-01": "po_xxx",
- };
-
- const commissions = clicksData
- .map(({ clicks: quantity, start }) => {
- // skip today (2025-02-20)
- if (start.startsWith("2025-02-20")) {
- return null;
- }
- const payoutId = payoutIds[start.slice(0, 7)];
- return {
- id: createId({ prefix: "cm_" }),
- programId,
- partnerId,
- linkId: link?.id,
- payoutId,
- type: "click",
- amount: 0,
- quantity,
- earnings: getRewardAmount(reward) * quantity,
- status: payoutId ? "paid" : "pending",
- createdAt: new Date(start),
- updatedAt: new Date(start),
- };
- })
- .filter((c) => c && c.quantity > 0);
-
- console.table(commissions);
-
- await prisma.commission.createMany({
- data: commissions,
- });
-
- console.log(commissions);
-}
-
-main();
diff --git a/apps/web/scripts/migrations/backfill-commission-activity-log.ts b/apps/web/scripts/migrations/backfill-commission-activity-log.ts
deleted file mode 100644
index 22fc8ae046..0000000000
--- a/apps/web/scripts/migrations/backfill-commission-activity-log.ts
+++ /dev/null
@@ -1,219 +0,0 @@
-import { prisma } from "@dub/prisma";
-import { Prisma } from "@dub/prisma/client";
-import { addDays } from "date-fns";
-import "dotenv-flow/config";
-
-const BATCH_SIZE = 500;
-const TERMINAL_STATUSES = ["refunded", "duplicate", "fraud", "canceled"];
-
-async function main() {
- let cursor: string | undefined = undefined;
- let totalProcessed = 0;
- let totalLogsCreated = 0;
-
- while (true) {
- const commissions = await prisma.commission.findMany({
- where: {
- status: {
- not: "pending",
- },
- earnings: {
- not: 0,
- },
- },
- include: {
- program: {
- select: {
- workspaceId: true,
- },
- },
- programEnrollment: {
- select: {
- partnerGroup: {
- select: {
- holdingPeriodDays: true,
- },
- },
- },
- },
- payout: {
- select: {
- paidAt: true,
- },
- },
- },
- orderBy: {
- id: "asc",
- },
- take: BATCH_SIZE,
- ...(cursor
- ? {
- skip: 1,
- cursor: {
- id: cursor,
- },
- }
- : {}),
- });
-
- if (commissions.length === 0) {
- break;
- }
-
- cursor = commissions[commissions.length - 1].id;
-
- const existingActivityLogs = await prisma.activityLog.findMany({
- where: {
- resourceType: "commission",
- resourceId: {
- in: commissions.map((c) => c.id),
- },
- },
- });
-
- const activityLogs: Prisma.ActivityLogCreateManyInput[] = [];
-
- for (const commission of commissions) {
- const { amount, earnings, status, program, programEnrollment } =
- commission;
-
- const existingActivityLogsForCommission = existingActivityLogs.filter(
- (log) => log.resourceId === commission.id,
- );
- if (existingActivityLogsForCommission.length > 0) {
- console.log(
- `Commission ${commission.id} already has ${existingActivityLogsForCommission.length} activity logs, skipping...`,
- );
- continue;
- }
-
- const base = {
- workspaceId: program.workspaceId,
- programId: commission.programId,
- resourceType: "commission",
- resourceId: commission.id,
- };
-
- const statusBecameProcessed =
- commission.type === "custom"
- ? commission.createdAt
- : addDays(
- commission.createdAt,
- programEnrollment?.partnerGroup?.holdingPeriodDays ?? 0,
- );
-
- if (TERMINAL_STATUSES.includes(status)) {
- if (!commission.rewardId) {
- console.log(
- `Commission ${commission.id} with status ${status} has no rewardId, likely imported from another source, skipping...`,
- );
- continue;
- }
-
- let statusBeforeTerminal = "pending";
- // if the status became processed before it was updated to terminal
- // we should include the processed activity log
- if (statusBecameProcessed < commission.updatedAt) {
- activityLogs.push({
- ...base,
- action: "commission.updated",
- createdAt: statusBecameProcessed,
- changeSet: {
- commission: {
- old: { amount, earnings, status: "pending" },
- new: { amount, earnings, status: "processed" },
- },
- },
- });
- statusBeforeTerminal = "processed";
- }
-
- activityLogs.push({
- ...base,
- action: "commission.updated",
- createdAt: commission.updatedAt,
- changeSet: {
- commission: {
- old: { amount, earnings, status: statusBeforeTerminal },
- new: { amount, earnings, status },
- },
- },
- });
- } else {
- if (status === "paid" && !commission.payout) {
- console.log(
- `Commission ${commission.id} is paid but has no payout, likely imported from another source, skipping...`,
- );
- continue;
- }
-
- const commissionPaidAt = commission.payout?.paidAt;
-
- // pending β†’ processed
- activityLogs.push({
- ...base,
- action: "commission.updated",
- // if commissionPaidAt exists and is before statusBecameProcessed, use commission.createdAt
- // otherwise use statusBecameProcessed
- createdAt:
- commissionPaidAt && commissionPaidAt < statusBecameProcessed
- ? commission.createdAt
- : statusBecameProcessed,
- changeSet: {
- commission: {
- old: { amount, earnings, status: "pending" },
- new: { amount, earnings, status: "processed" },
- },
- },
- });
-
- // this should be there but just in case
- if (commission.payout?.paidAt) {
- // processed β†’ paid
- activityLogs.push({
- ...base,
- action: "commission.updated",
- createdAt: commission.payout?.paidAt,
- changeSet: {
- commission: {
- old: {
- amount,
- earnings,
- status: "processed",
- },
- new: {
- amount,
- earnings,
- status: "paid",
- },
- },
- },
- });
- }
- }
- }
-
- let logsCreated = 0;
-
- if (activityLogs.length > 0) {
- const created = await prisma.activityLog.createMany({
- data: activityLogs,
- skipDuplicates: true,
- });
- logsCreated = created.count;
- totalLogsCreated += logsCreated;
- }
-
- totalProcessed += commissions.length;
-
- console.log(
- `Batch done: ${commissions.length} commissions, ${activityLogs.length} logs created. Total processed: ${totalProcessed}, total logs: ${totalLogsCreated}`,
- );
- }
-
- console.log(
- `\nBackfill complete. Processed ${totalProcessed} commissions, created ${totalLogsCreated} activity logs.`,
- );
-}
-
-main();
diff --git a/apps/web/scripts/migrations/backfill-commissions-rewardId.ts b/apps/web/scripts/migrations/backfill-commissions-rewardId.ts
deleted file mode 100644
index 6857609a93..0000000000
--- a/apps/web/scripts/migrations/backfill-commissions-rewardId.ts
+++ /dev/null
@@ -1,62 +0,0 @@
-import { REWARD_EVENT_COLUMN_MAPPING } from "@/lib/zod/schemas/rewards";
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-
-// Backfill `rewardId` in `Commission` table
-async function main() {
- const event = "sale"; // Repeat this for click, lead, sale
- const rewarIdColumn = REWARD_EVENT_COLUMN_MAPPING[event];
-
- const programEnrollments = await prisma.programEnrollment.groupBy({
- by: ["programId", rewarIdColumn],
- _count: {
- _all: true,
- },
- });
-
- console.table(programEnrollments);
-
- for (const {
- _count,
- programId,
- [rewarIdColumn]: rewardId,
- } of programEnrollments) {
- if (_count._all === 0) {
- continue;
- }
-
- const partners = await prisma.programEnrollment.findMany({
- where: {
- programId,
- [rewarIdColumn]: rewardId!,
- },
- select: {
- partnerId: true,
- },
- });
-
- const partnerIds = partners.map((p) => p.partnerId);
-
- if (partnerIds.length === 0) {
- continue;
- }
-
- await prisma.commission.updateMany({
- where: {
- programId,
- partnerId: {
- in: partnerIds,
- },
- rewardId: null,
- type: {
- not: "custom",
- },
- },
- data: {
- rewardId,
- },
- });
- }
-}
-
-main();
diff --git a/apps/web/scripts/migrations/backfill-cross-program-ban-fraud-events.ts b/apps/web/scripts/migrations/backfill-cross-program-ban-fraud-events.ts
deleted file mode 100644
index 67a7695e43..0000000000
--- a/apps/web/scripts/migrations/backfill-cross-program-ban-fraud-events.ts
+++ /dev/null
@@ -1,136 +0,0 @@
-import { createId } from "@/lib/api/create-id";
-import { createFraudEventHash } from "@/lib/api/fraud/utils";
-import { prisma } from "@dub/prisma";
-import { FraudRuleType, Prisma } from "@dub/prisma/client";
-import { chunk, groupBy } from "@dub/utils";
-import "dotenv-flow/config";
-
-async function main() {
- const fraudGroups = await prisma.fraudEventGroup.findMany({
- where: {
- type: "partnerCrossProgramBan",
- fraudEvents: {
- some: {
- sourceProgramId: null,
- },
- },
- },
- select: {
- id: true,
- programId: true,
- partnerId: true,
- createdAt: true,
- },
- orderBy: {
- createdAt: "asc",
- },
- });
-
- if (fraudGroups.length === 0) {
- console.log("No fraud events to process.");
- return;
- }
-
- console.log(`Found ${fraudGroups.length} fraud groups to process.`);
-
- const bannedEnrollments = await prisma.programEnrollment.findMany({
- where: {
- partnerId: {
- in: fraudGroups.map((e) => e.partnerId),
- },
- status: "banned",
- },
- select: {
- programId: true,
- partnerId: true,
- bannedAt: true,
- bannedReason: true,
- },
- orderBy: {
- bannedAt: "asc",
- },
- });
-
- const bannedEnrollmentsByPartnerId = groupBy(
- bannedEnrollments,
- (e) => e.partnerId,
- );
-
- const fraudEventsToCreate: Prisma.FraudEventCreateManyInput[] = [];
-
- for (const fraudGroup of fraudGroups) {
- const bannedEnrollments =
- bannedEnrollmentsByPartnerId[fraudGroup.partnerId];
-
- if (!bannedEnrollments || bannedEnrollments.length === 0) {
- continue;
- }
-
- for (const bannedEnrollment of bannedEnrollments) {
- if (bannedEnrollment.programId === fraudGroup.programId) {
- continue;
- }
-
- const fraudEvent = {
- programId: fraudGroup.programId,
- partnerId: fraudGroup.partnerId,
- sourceProgramId: bannedEnrollment.programId,
- metadata: {
- bannedAt: bannedEnrollment.bannedAt,
- bannedReason: bannedEnrollment.bannedReason,
- },
- };
-
- fraudEventsToCreate.push({
- ...fraudEvent,
- id: createId({ prefix: "fre_" }),
- fraudEventGroupId: fraudGroup.id,
- hash: createFraudEventHash({
- ...fraudEvent,
- type: FraudRuleType.partnerCrossProgramBan,
- }),
- createdAt: bannedEnrollment.bannedAt ?? new Date(),
- updatedAt: bannedEnrollment.bannedAt ?? new Date(),
- });
- }
- }
-
- if (fraudEventsToCreate.length > 0) {
- const chunks = chunk(fraudEventsToCreate, 500);
-
- for (const chunk of chunks) {
- await prisma.fraudEvent.createMany({
- data: chunk,
- });
- }
-
- console.log(`Created ${fraudEventsToCreate.length} fraud events total.`);
- }
-
- // Delete old fraud events without sourceProgramId
- // These are the old fraud events that we're replacing with new ones that have sourceProgramId
- const deletedFraudEvent = await prisma.fraudEvent.deleteMany({
- where: {
- fraudEventGroup: {
- type: "partnerCrossProgramBan",
- },
- sourceProgramId: null,
- },
- });
-
- console.log(`Deleted ${deletedFraudEvent.count} old fraud events.`);
-
- // Delete fraud event groups that have no fraud events
- const deletedFraudEventGroups = await prisma.fraudEventGroup.deleteMany({
- where: {
- type: "partnerCrossProgramBan",
- fraudEvents: {
- none: {},
- },
- },
- });
-
- console.log(`Deleted ${deletedFraudEventGroups.count} fraud event groups.`);
-}
-
-main();
diff --git a/apps/web/scripts/migrations/backfill-customer-first-sale.ts b/apps/web/scripts/migrations/backfill-customer-first-sale.ts
deleted file mode 100644
index 9c62daa614..0000000000
--- a/apps/web/scripts/migrations/backfill-customer-first-sale.ts
+++ /dev/null
@@ -1,76 +0,0 @@
-import { prisma } from "@dub/prisma";
-import { chunk } from "@dub/utils";
-import "dotenv-flow/config";
-import * as z from "zod/v4";
-import { tb } from "../../lib/tinybird/client";
-
-export const getFirstSaleEvents = tb.buildPipe({
- pipe: "get_first_sale_events",
- parameters: z.object({
- customerIds: z.string().array(),
- }),
- data: z.object({
- customerId: z.string(),
- firstSaleAt: z.string(),
- }),
-});
-
-async function main() {
- while (true) {
- const customerWithSales = await prisma.customer.findMany({
- where: {
- sales: {
- gt: 0,
- },
- firstSaleAt: null,
- },
- take: 5000,
- });
-
- if (customerWithSales.length === 0) {
- console.log("No customers left to backfill");
- break;
- }
-
- let updated = 0;
-
- const chunks = chunk(customerWithSales, 100);
-
- for (const chunk of chunks) {
- const firstSaleEvents = await getFirstSaleEvents({
- customerIds: chunk.map((customer) => customer.id),
- }).then((res) => res.data);
-
- await Promise.all(
- chunk.map(async (customer) => {
- const firstSaleEvent = firstSaleEvents
- .filter((event) => event.customerId === customer.id)
- .sort(
- (a, b) =>
- new Date(a.firstSaleAt).getTime() -
- new Date(b.firstSaleAt).getTime(),
- )[0];
-
- if (!firstSaleEvent) {
- return;
- }
- try {
- await prisma.customer.update({
- where: { id: customer.id },
- data: {
- firstSaleAt: new Date(firstSaleEvent.firstSaleAt),
- },
- });
- updated++;
- } catch (_e) {}
- }),
- );
-
- console.log(
- `Updated ${updated}/${customerWithSales.length} customers (${(updated / customerWithSales.length) * 100}%)`,
- );
- }
- }
-}
-
-main();
diff --git a/apps/web/scripts/migrations/backfill-customer-partner-ids.ts b/apps/web/scripts/migrations/backfill-customer-partner-ids.ts
deleted file mode 100644
index eade76c259..0000000000
--- a/apps/web/scripts/migrations/backfill-customer-partner-ids.ts
+++ /dev/null
@@ -1,53 +0,0 @@
-import { prisma } from "@dub/prisma";
-import { chunk } from "@dub/utils";
-import "dotenv-flow/config";
-
-async function main() {
- while (true) {
- const customers = await prisma.customer.findMany({
- where: {
- programId: null,
- link: {
- programId: {
- not: null,
- },
- },
- },
- select: {
- id: true,
- link: {
- select: {
- programId: true,
- partnerId: true,
- },
- },
- },
- take: 10000,
- });
- if (customers.length === 0) {
- console.log("No customers left to backfill");
- break;
- }
-
- console.log(`Found ${customers.length} customers to backfill`);
-
- const chunks = chunk(customers, 100);
- for (let i = 0; i < chunks.length; i++) {
- const chunk = chunks[i];
- console.log(`Backfilling chunk ${i + 1} of ${chunks.length}`);
- await Promise.all(
- chunk.map((customer) =>
- prisma.customer.update({
- where: { id: customer.id },
- data: {
- programId: customer.link?.programId,
- partnerId: customer.link?.partnerId,
- },
- }),
- ),
- );
- }
- }
-}
-
-main();
diff --git a/apps/web/scripts/migrations/backfill-customer-sales.ts b/apps/web/scripts/migrations/backfill-customer-sales.ts
deleted file mode 100644
index c8eb8b42ba..0000000000
--- a/apps/web/scripts/migrations/backfill-customer-sales.ts
+++ /dev/null
@@ -1,64 +0,0 @@
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-import * as fs from "fs";
-import * as Papa from "papaparse";
-
-let customersToBackfill: {
- customerId: string;
- sales: number;
- saleAmount: number;
-}[] = [];
-
-async function main() {
- Papa.parse(fs.createReadStream("customer_sales.csv", "utf-8"), {
- header: true,
- skipEmptyLines: true,
- step: (result: {
- data: {
- customerId: string;
- sales: string;
- saleAmount: string;
- };
- }) => {
- customersToBackfill.push({
- customerId: result.data.customerId,
- sales: parseInt(result.data.sales),
- saleAmount: parseInt(result.data.saleAmount),
- });
- },
- complete: async () => {
- console.table(
- customersToBackfill
- .slice(0, 10)
- .concat(customersToBackfill.slice(90, 100)),
- );
-
- // take first 100 customers, backfill, and delete from csv
- const chunkedCustomers = customersToBackfill.slice(0, 100);
-
- await Promise.all(
- chunkedCustomers.map(async (customer) => {
- try {
- await prisma.customer.update({
- where: { id: customer.customerId },
- data: {
- sales: customer.sales,
- saleAmount: customer.saleAmount,
- },
- });
- } catch (error) {
- console.log(
- `Can't find customer ${customer.customerId} with sales ${customer.sales} and sale amount ${customer.saleAmount}`,
- );
- }
- }),
- );
-
- // delete from csv
- customersToBackfill = customersToBackfill.slice(100);
- fs.writeFileSync("customer_sales.csv", Papa.unparse(customersToBackfill));
- },
- });
-}
-
-main();
diff --git a/apps/web/scripts/migrations/backfill-customer-subscription-cancellation.ts b/apps/web/scripts/migrations/backfill-customer-subscription-cancellation.ts
deleted file mode 100644
index 22152b8b63..0000000000
--- a/apps/web/scripts/migrations/backfill-customer-subscription-cancellation.ts
+++ /dev/null
@@ -1,80 +0,0 @@
-import { prisma } from "@dub/prisma";
-import { chunk } from "@dub/utils";
-import "dotenv-flow/config";
-import { stripeAppClient } from "../../lib/stripe";
-
-async function main() {
- let page = 0;
- const pageSize = 5000;
-
- while (true) {
- const stripeCustomers = await prisma.customer.findMany({
- where: {
- stripeCustomerId: {
- not: null,
- },
- firstSaleAt: null,
- },
- take: pageSize,
- skip: page * pageSize,
- });
-
- if (stripeCustomers.length === 0) {
- console.log("No customers left to backfill");
- break;
- }
-
- const chunks = chunk(stripeCustomers, 20);
- for (const chunk of chunks) {
- await Promise.all(
- chunk.map(async (customer) => {
- try {
- const customerSubscriptions = await stripeAppClient({
- mode: "live",
- }).subscriptions.list(
- {
- customer: customer.stripeCustomerId!,
- status: "all",
- },
- {
- stripeAccount: customer.projectConnectId!,
- },
- );
-
- if (customerSubscriptions.data.length === 0) {
- console.log(
- `No subscriptions found for customer ${customer.email}`,
- );
- return;
- }
-
- const subscription = customerSubscriptions.data[0];
-
- const updatedCustomer = await prisma.customer.update({
- where: { id: customer.id },
- data: {
- firstSaleAt: new Date(subscription.created * 1000),
- subscriptionCanceledAt: subscription.canceled_at
- ? new Date(subscription.canceled_at * 1000)
- : null,
- },
- });
- console.log({
- email: updatedCustomer.email,
- firstSaleAt: updatedCustomer.firstSaleAt,
- subscriptionCanceledAt: updatedCustomer.subscriptionCanceledAt,
- });
- } catch (error) {
- console.log(
- `Test mode customer detected: ${customer.stripeCustomerId}`,
- );
- }
- }),
- );
- }
-
- page++;
- }
-}
-
-main();
diff --git a/apps/web/scripts/migrations/backfill-customers.ts b/apps/web/scripts/migrations/backfill-customers.ts
deleted file mode 100644
index a301514c88..0000000000
--- a/apps/web/scripts/migrations/backfill-customers.ts
+++ /dev/null
@@ -1,104 +0,0 @@
-import { prisma } from "@dub/prisma";
-import { Prisma } from "@dub/prisma/client";
-import "dotenv-flow/config";
-import * as z from "zod/v4";
-import { tb } from "../../lib/tinybird/client";
-import { getLeadEvents } from "../../lib/tinybird/get-lead-events";
-
-export const getClickEvents = tb.buildPipe({
- pipe: "get_click_events",
- parameters: z.object({
- clickIds: z.array(z.string()),
- }),
- data: z.any(),
-});
-
-// Backfill new customer columns such as linkId, clickId, country
-async function main() {
- const where: Prisma.CustomerWhereInput = {
- linkId: null,
- projectId: {
- not: "clrei1gld0002vs9mzn93p8ik",
- },
- };
-
- const customers = await prisma.customer.findMany({
- where,
- select: {
- id: true,
- createdAt: true,
- },
- take: 200,
- orderBy: {
- createdAt: "desc",
- },
- });
-
- if (customers.length === 0) {
- console.log("No customers left to update.");
- return;
- }
-
- // Find leads
- const customerIds = customers.map((customer) => customer.id);
-
- const leadEvents = await getLeadEvents({ customerIds }).then(
- (res) => res.data,
- );
-
- // Find clicks
- // We're fetching clicks, because leads doesn't have information about when click happened
- const clickIds = leadEvents
- .map((event) => event.click_id)
- .filter(Boolean) as string[];
-
- const clickEvents = await getClickEvents({ clickIds }).then(
- (res) => res.data,
- );
-
- // Update customers
- const result = await Promise.all(
- customers.map(async (customer) => {
- const leadEvent = leadEvents.find(
- (lead) => lead?.customer_id === customer.id,
- );
-
- const clickEvent = clickEvents.find(
- (click) => click?.click_id === leadEvent?.click_id,
- );
-
- if (!leadEvent || !clickEvent) {
- return customer;
- }
-
- return prisma.customer.update({
- where: { id: customer.id },
- data: {
- linkId: clickEvent.link_id,
- clickId: clickEvent.click_id,
- country: clickEvent.country,
- clickedAt: new Date(clickEvent.timestamp + "Z"),
- },
- select: {
- id: true,
- linkId: true,
- clickId: true,
- country: true,
- clickedAt: true,
- },
- });
- }),
- );
-
- const remaining = await prisma.customer.count({
- where,
- });
-
- console.table(result);
- console.log(`${remaining} remaining`);
-}
-
-main();
-
-// '2024-12-17 13:48:52.533'
-// new Date('2024-12-17 13:48:52.533')
diff --git a/apps/web/scripts/migrations/backfill-deepview.ts b/apps/web/scripts/migrations/backfill-deepview.ts
deleted file mode 100644
index ed15eb75c7..0000000000
--- a/apps/web/scripts/migrations/backfill-deepview.ts
+++ /dev/null
@@ -1,25 +0,0 @@
-import { prisma } from "@dub/prisma";
-import { Prisma } from "@dub/prisma/client";
-import "dotenv-flow/config";
-
-async function main() {
- const domains = await prisma.domain.findMany({
- where: {
- deepviewData: {
- equals: Prisma.AnyNull,
- },
- },
- take: 1000,
- });
-
- const deepviewData = await prisma.domain.updateMany({
- where: {
- id: { in: domains.map((domain) => domain.id) },
- },
- data: { deepviewData: {} },
- });
-
- console.log(deepviewData);
-}
-
-main();
diff --git a/apps/web/scripts/migrations/backfill-discoverableat.ts b/apps/web/scripts/migrations/backfill-discoverableat.ts
deleted file mode 100644
index d59c5be687..0000000000
--- a/apps/web/scripts/migrations/backfill-discoverableat.ts
+++ /dev/null
@@ -1,71 +0,0 @@
-import { buildSocialPlatformLookup } from "@/lib/social-utils";
-import { prisma } from "@dub/prisma";
-import { Prisma } from "@dub/prisma/client";
-import { ACME_PROGRAM_ID } from "@dub/utils";
-import "dotenv-flow/config";
-
-async function main() {
- const where: Prisma.PartnerWhereInput = {
- discoverableAt: null,
- users: {
- some: {},
- },
- programs: {
- some: {
- programId: {
- not: ACME_PROGRAM_ID,
- },
- totalCommissions: {
- gt: 0,
- },
- },
- },
- platforms: {
- some: {},
- },
- };
-
- const partners = await prisma.partner.findMany({
- where,
- include: {
- platforms: true,
- },
- });
-
- // Format partners for display with platform handles
- const partnersForDisplay = partners.map((partner) => {
- const platformsMap = buildSocialPlatformLookup(partner.platforms);
-
- return {
- name: partner.name,
- email: partner.email,
- website: platformsMap.website?.identifier || null,
- youtube: platformsMap.youtube?.identifier || null,
- twitter: platformsMap.twitter?.identifier || null,
- linkedin: platformsMap.linkedin?.identifier || null,
- instagram: platformsMap.instagram?.identifier || null,
- tiktok: platformsMap.tiktok?.identifier || null,
- };
- });
-
- console.table(partnersForDisplay);
-
- const count = await prisma.partner.count({
- where,
- });
-
- console.log(`Found ${count} partners to backfill`);
-
- const res = await prisma.partner.updateMany({
- where: {
- id: {
- in: partners.map((partner) => partner.id),
- },
- },
- data: { discoverableAt: new Date() },
- });
-
- console.log(`Updated ${res.count} partners with users to be discoverable`);
-}
-
-main();
diff --git a/apps/web/scripts/migrations/backfill-group-links-pgdl-acme.ts b/apps/web/scripts/migrations/backfill-group-links-pgdl-acme.ts
deleted file mode 100644
index f5180b24ab..0000000000
--- a/apps/web/scripts/migrations/backfill-group-links-pgdl-acme.ts
+++ /dev/null
@@ -1,95 +0,0 @@
-import { prisma } from "@dub/prisma";
-import { normalizeUrl } from "@dub/utils";
-import "dotenv-flow/config";
-
-// special script for checking if acme default links are set up properly
-async function main() {
- const program = await prisma.program.findUniqueOrThrow({
- where: {
- slug: "acme",
- },
- include: {
- groups: {
- include: {
- partnerGroupDefaultLinks: true,
- },
- },
- },
- });
-
- for (const group of program.groups) {
- const defaultLink = group.partnerGroupDefaultLinks[0];
-
- const programEnrollments = await prisma.programEnrollment.findMany({
- where: {
- groupId: group.id,
- },
- include: {
- links: {
- orderBy: {
- createdAt: "asc",
- },
- },
- },
- });
-
- const firstPartnerLinks = await Promise.all(
- programEnrollments.map(async (programEnrollment) => {
- const { programId, partnerId, links } = programEnrollment;
- const foundDefaultLink = links.find(
- (link) => link.partnerGroupDefaultLinkId,
- );
- const firstPartnerLink = links.find(
- (link) => normalizeUrl(link.url) === normalizeUrl(defaultLink.url),
- );
-
- const {
- groupDefaultLinkId,
- partnerDefaultLinkId,
- firstPartnerLinkDefaultLinkId,
- } = {
- groupDefaultLinkId: defaultLink.id,
- partnerDefaultLinkId: foundDefaultLink?.partnerGroupDefaultLinkId,
- firstPartnerLinkDefaultLinkId:
- firstPartnerLink?.partnerGroupDefaultLinkId,
- };
-
- const matchingDefaultLink = [
- partnerDefaultLinkId,
- firstPartnerLinkDefaultLinkId,
- ].every((id) => id === groupDefaultLinkId);
-
- if (!matchingDefaultLink) {
- await prisma.link.update({
- where: {
- id: firstPartnerLink?.id,
- },
- data: {
- partnerGroupDefaultLinkId: groupDefaultLinkId,
- },
- });
-
- console.log(
- `Updated link ${firstPartnerLink?.id} to have default link ${groupDefaultLinkId}`,
- );
- }
-
- return {
- id: firstPartnerLink?.id,
- shortLink: firstPartnerLink?.shortLink,
- url: firstPartnerLink?.url,
- programId,
- partnerId,
- groupDefaultLinkId,
- partnerDefaultLinkId,
- firstPartnerLinkDefaultLinkId,
- matchingDefaultLink,
- };
- }),
- );
-
- console.table(firstPartnerLinks);
- }
-}
-
-main();
diff --git a/apps/web/scripts/migrations/backfill-link-commissions.ts b/apps/web/scripts/migrations/backfill-link-commissions.ts
deleted file mode 100644
index 3e01d05b99..0000000000
--- a/apps/web/scripts/migrations/backfill-link-commissions.ts
+++ /dev/null
@@ -1,151 +0,0 @@
-import { SaleEvent } from "@/lib/types";
-import { prisma } from "@dub/prisma";
-import { EventType, Prisma } from "@dub/prisma/client";
-import { currencyFormatter } from "@dub/utils";
-import "dotenv-flow/config";
-import { getEvents } from "../../lib/analytics/get-events";
-import { createId } from "../../lib/api/create-id";
-import { syncTotalCommissions } from "../../lib/api/partners/sync-total-commissions";
-import { calculateSaleEarnings } from "../../lib/api/sales/calculate-sale-earnings";
-import { determinePartnerReward } from "../../lib/partners/determine-partner-reward";
-
-// script to backfill link commissions
-// NOTE: need to remove "server-only" from serialize-reward.ts to run this script
-async function main() {
- const linkId = "link_xxx";
- const link = await prisma.link.findUniqueOrThrow({
- where: {
- id: linkId,
- },
- });
- if (!link.partnerId || !link.programId) {
- throw new Error("Link does not have a partner or program");
- }
-
- const saleEvents = (await getEvents({
- workspaceId: link.projectId!,
- linkId: link.id,
- event: "sales",
- saleType: "new",
- page: 1,
- limit: 10000,
- sortOrder: "desc",
- sortBy: "timestamp",
- start: new Date("2026-01-01"),
- end: new Date("2026-03-06"),
- })) as SaleEvent[];
-
- const existingCommissions = await prisma.commission.findMany({
- where: {
- OR: [
- {
- eventId: {
- in: saleEvents.map((e) => e.eventId),
- },
- },
- {
- invoiceId: {
- in: saleEvents.map((e) => e.invoice_id),
- },
- },
- {
- customerId: {
- in: saleEvents.map((e) => e.customer.id),
- },
- },
- ],
- },
- });
- const missedSaleEvents = saleEvents.filter(
- (e) =>
- e.saleAmount > 20_00 && // special condition for program
- !existingCommissions.some(
- (c) =>
- c.eventId === e.eventId ||
- c.invoiceId === e.invoice_id ||
- c.customerId === e.customer.id,
- ),
- );
-
- const programEnrollment = await prisma.programEnrollment.findUniqueOrThrow({
- where: {
- partnerId_programId: {
- partnerId: link.partnerId,
- programId: link.programId,
- },
- },
- include: {
- program: true,
- partner: true,
- links: true,
- saleReward: true,
- },
- });
-
- const { program } = programEnrollment;
-
- const data = missedSaleEvents
- .map((e) => {
- const reward = determinePartnerReward({
- event: "sale",
- programEnrollment,
- context: {
- sale: {
- productId: e.metadata?.productId,
- },
- },
- });
- if (!reward) {
- return null;
- }
- return {
- id: createId({ prefix: "cm_" }),
- programId: program.id,
- partnerId: link.partnerId!,
- linkId: link.id,
- invoiceId: e.invoice_id,
- customerId: e.customer.id,
- eventId: e.eventId,
- amount: e.sale.amount,
- type: EventType.sale,
- quantity: 1,
- currency: "usd",
- createdAt: new Date(e.timestamp),
- earnings: calculateSaleEarnings({
- reward,
- sale: {
- quantity: 1,
- amount: e.sale.amount,
- },
- }),
- };
- })
- .filter(
- (c): c is NonNullable<typeof c> => c !== null,
- ) satisfies Prisma.CommissionCreateManyInput[];
-
- console.table(data, [
- "customerId",
- "amount",
- "earnings",
- "invoiceId",
- "createdAt",
- ]);
-
- // create commissions
- const res = await prisma.commission.createMany({
- data,
- skipDuplicates: true,
- });
- console.log(
- `Backfilled ${res.count} commissions for a total of ${currencyFormatter(res.count * 20_00)}`,
- );
-
- // sync total commissions for the partner in the program
- await syncTotalCommissions({
- partnerId: link.partnerId,
- programId: link.programId,
- });
-}
-
-main();
diff --git a/apps/web/scripts/migrations/backfill-link-partner-group-ids.ts b/apps/web/scripts/migrations/backfill-link-partner-group-ids.ts
deleted file mode 100644
index 17bd693b18..0000000000
--- a/apps/web/scripts/migrations/backfill-link-partner-group-ids.ts
+++ /dev/null
@@ -1,76 +0,0 @@
-import { includeProgramEnrollment } from "@/lib/api/links/include-program-enrollment";
-import { includeTags } from "@/lib/api/links/include-tags";
-import { recordLink } from "@/lib/tinybird";
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-
-const LINKS_PER_BATCH = 1000;
-const PR_MERGE_TIMESTAMP = new Date("2025-11-07T00:00:00Z");
-
-async function main() {
- let cursor: string | undefined = undefined;
-
- while (true) {
- // Find the program links
- const links = await prisma.link.findMany({
- where: {
- programId: {
- not: null,
- },
- partnerId: {
- not: null,
- },
- createdAt: {
- lte: PR_MERGE_TIMESTAMP,
- },
- },
- include: {
- ...includeTags,
- ...includeProgramEnrollment,
- },
- orderBy: {
- id: "asc",
- },
- take: LINKS_PER_BATCH,
- ...(cursor
- ? {
- cursor: { id: cursor },
- skip: 1,
- }
- : {}),
- });
-
- if (links.length === 0) {
- console.log("No links found, skipping...");
- break;
- }
-
- console.log(`Found ${links.length} links to process.`);
-
- cursor = links[links.length - 1].id;
-
- const linksWithGroupIds = links.filter(
- (link) => link.programEnrollment?.groupId,
- );
-
- if (linksWithGroupIds.length === 0) {
- console.log("No links with group ids found, skipping...");
- break;
- }
-
- const { successful_rows, quarantined_rows } =
- await recordLink(linksWithGroupIds);
-
- if (successful_rows !== linksWithGroupIds.length) {
- console.log(
- `Failed to record ${linksWithGroupIds.length - successful_rows} links.`,
- );
- }
-
- if (quarantined_rows !== 0) {
- console.log(`Quarantined ${quarantined_rows} links.`);
- }
- }
-}
-
-main();
diff --git a/apps/web/scripts/migrations/backfill-link-stats.ts b/apps/web/scripts/migrations/backfill-link-stats.ts
deleted file mode 100644
index 9c48faaa3f..0000000000
--- a/apps/web/scripts/migrations/backfill-link-stats.ts
+++ /dev/null
@@ -1,66 +0,0 @@
-import { prisma } from "@dub/prisma";
-import { Prisma } from "@dub/prisma/client";
-import "dotenv-flow/config";
-
-// script to backfill link cache for links with webhooks
-async function main() {
- const where: Prisma.LinkWhereInput = {
- programId: {
- not: null,
- },
- leads: {
- gt: 0,
- },
- lastLeadAt: null,
- };
-
- console.time("findMany");
- const links = await prisma.link.findMany({
- where,
- take: 5000,
- select: {
- id: true,
- shortLink: true,
- leads: true,
- lastLeadAt: true,
- customers: {
- select: {
- email: true,
- createdAt: true,
- },
- orderBy: {
- createdAt: "desc",
- },
- take: 1,
- },
- },
- });
- console.timeEnd("findMany");
-
- console.time("updateMany");
- for (const link of links) {
- if (link.customers.length === 0) {
- console.log(`No customers for ${link.shortLink}`);
- continue;
- }
-
- const res = await prisma.link.update({
- where: { id: link.id },
- data: {
- lastLeadAt: link.customers[0].createdAt,
- },
- });
- console.log(`Updated ${link.shortLink} with lastLeadAt: ${res.lastLeadAt}`);
- }
- console.timeEnd("updateMany");
-
- console.time("count");
- const remaining = await prisma.link.count({
- where,
- });
- console.timeEnd("count");
-
- console.log(`Updated ${links.length} links, ${remaining} remaining`);
-}
-
-main();
diff --git a/apps/web/scripts/migrations/backfill-missing-lead-commissions.ts b/apps/web/scripts/migrations/backfill-missing-lead-commissions.ts
deleted file mode 100644
index 15b7fea449..0000000000
--- a/apps/web/scripts/migrations/backfill-missing-lead-commissions.ts
+++ /dev/null
@@ -1,53 +0,0 @@
-// @ts-nocheck some weird typing issues below
-
-import { createId } from "@/lib/api/create-id";
-import { prisma } from "@dub/prisma";
-import { Prisma } from "@dub/prisma/client";
-import "dotenv-flow/config";
-import { getEvents } from "../../lib/analytics/get-events";
-
-async function main() {
- const link = await prisma.link.findUniqueOrThrow({
- where: {
- id: "link_xxx",
- },
- });
- const leadEvents = await getEvents({
- event: "leads",
- interval: "all",
- workspaceId: link.projectId!,
- linkId: link.id,
- sortBy: "timestamp",
- page: 1,
- limit: 1000,
- });
-
- const commissionsToCreate: Prisma.CommissionCreateManyInput[] = leadEvents
- .filter((e) => e.event === "lead")
- .map((e) => ({
- id: createId({ prefix: "cm_" }),
- programId: link.programId!,
- partnerId: link.partnerId!,
- rewardId: "rw_xxx",
- customerId: e.customer!.id,
- linkId: link.id,
- eventId: e.eventId,
- type: "lead",
- quantity: 1,
- amount: 0,
- currency: "usd",
- earnings: 2000,
- status: "pending",
- createdAt: new Date(e.timestamp),
- }));
-
- console.table(commissionsToCreate);
-
- await prisma.commission.createMany({
- data: commissionsToCreate,
- skipDuplicates: true,
- });
- console.log(`Created ${commissionsToCreate.length} commissions`);
-}
-
-main();
diff --git a/apps/web/scripts/migrations/backfill-missing-sales.ts b/apps/web/scripts/migrations/backfill-missing-sales.ts
deleted file mode 100644
index eb55512821..0000000000
--- a/apps/web/scripts/migrations/backfill-missing-sales.ts
+++ /dev/null
@@ -1,128 +0,0 @@
-import { getEvents } from "@/lib/analytics/get-events";
-import { getLeadEvent, recordSale } from "@/lib/tinybird";
-import { clickEventSchemaTB } from "@/lib/zod/schemas/clicks";
-import { prisma } from "@dub/prisma";
-import { nanoid } from "@dub/utils";
-import "dotenv-flow/config";
-import * as fs from "fs";
-import * as Papa from "papaparse";
-
-const workspaceId = "xxx";
-let customerSpend: {
- email: string;
- amount: number;
- date: string;
-}[] = [];
-
-async function main() {
- const customers = await prisma.customer.findMany({
- where: {
- projectId: workspaceId,
- },
- select: {
- id: true,
- email: true,
- externalId: true,
- },
- orderBy: {
- createdAt: "asc",
- },
- });
-
- Papa.parse(fs.createReadStream("xxx.csv", "utf-8"), {
- header: true,
- skipEmptyLines: true,
- step: (result: {
- data: {
- email: string;
- net_volume: string;
- created: string;
- };
- }) => {
- customerSpend.push({
- email: result.data.email,
- amount: parseFloat(result.data.net_volume),
- date: result.data.created,
- });
- },
- complete: async () => {
- const alreadyLogged = await getEvents({
- workspaceId,
- event: "sales",
- interval: "all",
- page: 1,
- limit: 1000,
- sortOrder: "desc",
- sortBy: "timestamp",
- os: undefined,
- device: undefined,
- browser: undefined,
- });
-
- const toBackfill = await Promise.all(
- customerSpend.map(async (cs) => {
- const customer = customers.find((c) => c.email === cs.email);
- if (!customer) return null;
- // if the sale has already been logged, skip
- if (
- alreadyLogged.some(
- (e) =>
- e !== null &&
- "customer" in e &&
- e.customer?.externalId === customer.externalId,
- )
- )
- return null;
-
- const eventId = nanoid(16);
-
- const leadEvent = await getLeadEvent({
- customerId: customer.id,
- });
-
- const clickData = clickEventSchemaTB
- .omit({ timestamp: true })
- .parse(leadEvent);
-
- const saleAmount = parseInt((cs.amount * 100).toFixed(0));
-
- const data = {
- ...clickData,
- timestamp: new Date(cs.date).toISOString(),
- event_id: eventId,
- event_name: "Subscription creation",
- customer_id: customer.id,
- payment_processor: "stripe",
- amount: saleAmount,
- currency: "usd",
- invoice_id: "",
- metadata: "",
- };
-
- const tbRes = await recordSale(data);
- console.log(tbRes);
- const prismaRes = prisma.link.update({
- where: {
- id: clickData.link_id,
- },
- data: {
- sales: {
- increment: 1,
- },
- saleAmount: {
- increment: saleAmount,
- },
- },
- });
- console.log(prismaRes);
-
- return data;
- }),
- ).then((res) => res.filter(Boolean));
-
- console.log(JSON.stringify(toBackfill, null, 2));
- },
- });
-}
-
-main();
diff --git a/apps/web/scripts/migrations/backfill-partner-groupid-logs.ts b/apps/web/scripts/migrations/backfill-partner-groupid-logs.ts
deleted file mode 100644
index 4556efd7c4..0000000000
--- a/apps/web/scripts/migrations/backfill-partner-groupid-logs.ts
+++ /dev/null
@@ -1,161 +0,0 @@
-import { buildProgramEnrollmentChangeSet } from "@/lib/api/activity-log/build-program-enrollment-change-set";
-import { trackActivityLog } from "@/lib/api/activity-log/track-activity-log";
-import { prisma } from "@dub/prisma";
-import { groupBy } from "@dub/utils";
-import { differenceInSeconds } from "date-fns";
-import "dotenv-flow/config";
-import * as fs from "fs";
-import * as Papa from "papaparse";
-
-let logsToBackfill: {
- programId: string;
- partnerId: string;
- groupId: string;
- timestamp: Date;
-}[] = [];
-
-// Remove the following before running this script:
-// - "server-only" from serialize-reward.ts
-// - import { logger } from "@/lib/axiom/server" from track-activity-log.ts
-async function main() {
- Papa.parse(fs.createReadStream("backfill_group_id_logs.csv", "utf-8"), {
- header: true,
- skipEmptyLines: true,
- step: (result: {
- data: {
- program_id: string;
- partner_id: string;
- partner_group_id: string;
- timestamp: string;
- };
- }) => {
- logsToBackfill.push({
- programId: result.data.program_id,
- partnerId: result.data.partner_id,
- groupId: result.data.partner_group_id,
- timestamp: new Date(result.data.timestamp),
- });
- },
- complete: async () => {
- const programs = await prisma.program.findMany({
- select: {
- id: true,
- slug: true,
- workspaceId: true,
- workspace: {
- select: {
- users: {
- select: {
- userId: true,
- },
- orderBy: {
- createdAt: "asc",
- },
- take: 1,
- },
- },
- },
- },
- });
-
- for (const program of programs) {
- const filteredLogs = logsToBackfill.filter(
- (log) => log.programId === program.id,
- );
- console.log(
- `Found ${filteredLogs.length} logs to backfill for program ${program.slug}`,
- );
-
- const groups = await prisma.partnerGroup.findMany({
- where: {
- id: {
- in: filteredLogs.map((log) => log.groupId),
- },
- },
- select: {
- id: true,
- name: true,
- },
- });
-
- const groupNameMap = new Map(
- groups.map((group) => [group.id, group.name]),
- );
-
- const activityLogHistory = await prisma.activityLog.findMany({
- where: {
- programId: program.id,
- resourceType: "partner",
- },
- });
-
- // group by and partner id and sort by timestamp to show chronological order
- const groupedLogs = Object.entries(
- groupBy(filteredLogs, (log) => log.partnerId),
- ).map(([partnerId, logs]) => ({
- partnerId,
- activityLogsToBackfill: logs
- .sort((a, b) => a.timestamp.getTime() - b.timestamp.getTime()) // sort by timestamp to show chronological order
- .map((log, idx) => {
- if (idx === 0) return null;
- if (
- activityLogHistory.find(
- (a) =>
- a.resourceId === log.partnerId &&
- differenceInSeconds(a.createdAt, log.timestamp) < 10,
- )
- ) {
- // console.log(
- // `Skipping log for partner ${log.partnerId} because it already exists in the activity log history`,
- // );
- return null;
- }
- return {
- workspaceId: program.workspaceId,
- programId: log.programId,
- resourceType: "partner" as const,
- resourceId: log.partnerId,
- action: "partner.groupChanged" as const,
- createdAt: log.timestamp,
- userId: program.workspace.users[0].userId,
- changeSet: buildProgramEnrollmentChangeSet({
- oldEnrollment: {
- partnerGroup: groupNameMap.get(logs[idx - 1].groupId)
- ? {
- id: logs[idx - 1].groupId,
- name: groupNameMap.get(logs[idx - 1].groupId)!,
- }
- : null,
- },
- newEnrollment: groupNameMap.get(log.groupId)
- ? {
- partnerGroup: groupNameMap.get(log.groupId)
- ? {
- id: log.groupId,
- name: groupNameMap.get(log.groupId)!,
- }
- : null,
- }
- : null,
- }),
- };
- })
- .filter((log): log is NonNullable<typeof log> => log !== null),
- }));
-
- const withChanges = groupedLogs.filter(
- (group) => group.activityLogsToBackfill.length,
- );
- console.log(
- `Found ${withChanges.length} partners with activity logs to backfill`,
- );
-
- await trackActivityLog(
- withChanges.flatMap((group) => group.activityLogsToBackfill),
- );
- }
- },
- });
-}
-
-main();
diff --git a/apps/web/scripts/migrations/backfill-partner-groups-verify.ts b/apps/web/scripts/migrations/backfill-partner-groups-verify.ts
deleted file mode 100644
index 6eaddc1ae5..0000000000
--- a/apps/web/scripts/migrations/backfill-partner-groups-verify.ts
+++ /dev/null
@@ -1,64 +0,0 @@
-import { prisma } from "@dub/prisma";
-import { deepEqual } from "@dub/utils";
-import "dotenv-flow/config";
-
-// verify that the rewards and discount are the same for all enrollments in a group
-async function main() {
- const groups = await prisma.programEnrollment.groupBy({
- by: [
- "programId",
- "groupId",
- "saleRewardId",
- "leadRewardId",
- "clickRewardId",
- "discountId",
- ],
- where: {
- status: {
- notIn: ["pending", "rejected", "banned"],
- },
- },
- });
-
- for (const group of groups) {
- if (!group.groupId) {
- console.log(`Some enrollments in ${group.programId} have no group id`);
- continue;
- }
-
- const groupData = await prisma.partnerGroup.findUnique({
- where: {
- id: group.groupId,
- },
- });
-
- if (!groupData) {
- console.log(`Group ${group.groupId} not found`);
- continue;
- }
-
- if (
- !deepEqual(
- {
- saleRewardId: groupData.saleRewardId,
- leadRewardId: groupData.leadRewardId,
- clickRewardId: groupData.clickRewardId,
- discountId: groupData.discountId,
- },
- {
- saleRewardId: group.saleRewardId,
- leadRewardId: group.leadRewardId,
- clickRewardId: group.clickRewardId,
- discountId: group.discountId,
- },
- )
- ) {
- console.log(`Group ${group.groupId} has different rewards`);
- continue;
- }
-
- console.log(`Group ${group.groupId} is valid`);
- }
-}
-
-main();
diff --git a/apps/web/scripts/migrations/backfill-plain-customers.ts b/apps/web/scripts/migrations/backfill-plain-customers.ts
deleted file mode 100644
index c9f86b82fc..0000000000
--- a/apps/web/scripts/migrations/backfill-plain-customers.ts
+++ /dev/null
@@ -1,46 +0,0 @@
-import { prisma } from "@dub/prisma";
-import { chunk } from "@dub/utils";
-import "dotenv-flow/config";
-import { syncUserPlanToPlain } from "../../lib/plain/sync-user-plan";
-
-async function main() {
- const users = await prisma.user.findMany({
- select: {
- id: true,
- name: true,
- email: true,
- },
- where: {
- // NOT: GENERIC_EMAIL_DOMAINS.map((domain) => ({
- // email: {
- // endsWith: `@${domain}`,
- // },
- // })),
- projects: {
- some: {
- project: {
- plan: {
- not: "free",
- },
- },
- role: "owner",
- },
- },
- },
- take: 50,
- orderBy: {
- createdAt: "asc",
- },
- });
-
- const chunks = chunk(users, 50);
- for (let i = 0; i < chunks.length; i++) {
- const chunk = chunks[i];
- await Promise.allSettled(chunk.map((user) => syncUserPlanToPlain(user)));
- console.log(
- `Backfilled ${chunk.length} users in batch ${i + 1} of ${chunks.length} (out of ${users.length} total users)`,
- );
- }
-}
-
-main();
diff --git a/apps/web/scripts/migrations/backfill-program-categories.ts b/apps/web/scripts/migrations/backfill-program-categories.ts
deleted file mode 100644
index dfa3e3c406..0000000000
--- a/apps/web/scripts/migrations/backfill-program-categories.ts
+++ /dev/null
@@ -1,244 +0,0 @@
-import { anthropic } from "@ai-sdk/anthropic";
-import { prisma } from "@dub/prisma";
-import { Category } from "@dub/prisma/client";
-import FireCrawlApp from "@mendable/firecrawl-js";
-import { generateText, Output } from "ai";
-import "dotenv-flow/config";
-import * as z from "zod/v4";
-
-const CategoryEnum = z.enum(Category);
-
-// AI response schema
-const categorizationSchema = z.object({
- categories: z.array(CategoryEnum).min(1).max(3),
- reasoning: z.string(),
-});
-
-// Result interface
-interface ProgramResult {
- programId: string;
- programName: string;
- categories: Category[];
- url?: string;
- error?: string;
-}
-
-if (!process.env.FIRECRAWL_API_KEY)
- throw new Error("FIRECRAWL_API_KEY is not set");
-
-// Initialize FireCrawl
-const firecrawl = new FireCrawlApp({
- apiKey: process.env.FIRECRAWL_API_KEY,
-});
-
-// Function to scrape website content
-async function scrapeWebsite(url: string) {
- try {
- const scrapeResult = await firecrawl.scrapeUrl(url, {
- formats: ["markdown"],
- onlyMainContent: true,
- parsePDF: false,
- maxAge: 14400000, // 4 hours cache
- excludeTags: ["img"],
- });
-
- if (!scrapeResult.success) {
- throw new Error(scrapeResult.error || "Failed to scrape");
- }
-
- return {
- content: scrapeResult.markdown || "",
- title: scrapeResult.metadata?.title || "",
- description: scrapeResult.metadata?.description || "",
- };
- } catch (error) {
- console.error(`Error scraping ${url}:`, error);
- return null;
- }
-}
-
-// Function to categorize content using AI
-async function categorizeProgram(
- programName: string,
- url: string,
- content: string,
- title: string,
- description: string,
-) {
- try {
- const prompt = `Analyze this website and categorize it into 1-3 most relevant categories.
-
-IMPORTANT: You must select categories from this EXACT list (case-sensitive):
-- Artificial_Intelligence
-- Development
-- Design
-- Productivity
-- Finance
-- Marketing
-- Ecommerce
-- Security
-- Education
-- Health
-- Consumer
-
-Category descriptions:
-- Artificial_Intelligence: AI/ML tools, chatbots, automation, machine learning platforms
-- Development: Code tools, APIs, developer platforms, programming resources
-- Design: Design tools, UI/UX, creative software, graphics
-- Productivity: Task management, collaboration, workflow tools, organization
-- Finance: Financial services, payments, accounting, investment, banking
-- Marketing: Marketing tools, analytics, advertising, social media, SEO
-- Ecommerce: Online stores, commerce platforms, marketplaces, retail
-- Security: Cybersecurity, privacy, protection tools, data security
-- Education: Learning platforms, courses, educational content, training
-- Health: Healthcare, fitness, wellness apps, medical services
-- Consumer: General consumer products/services that don't fit other categories
-
-CRITICAL: Only use the exact category names listed above. DO NOT create new categories or modify existing ones. Do not select "Entrepreneurship" or "Business" as a category.
-
-Website information:
-Name: ${programName}
-Website URL: ${url}
-Page Title: ${title}
-Meta Description: ${description}
-Website Content Preview: ${content.slice(0, 300)}...`;
-
- const { output } = await generateText({
- model: anthropic("claude-sonnet-4-6"),
- output: Output.object({
- schema: categorizationSchema,
- }),
- prompt,
- temperature: 0.4,
- });
-
- return categorizationSchema.parse(output).categories;
- } catch (error) {
- // console.error(`Error categorizing ${programName}:`, error);
-
- // If it's a validation error (invalid enum values), return empty array
- if (
- error?.name === "AI_NoObjectGeneratedError" ||
- error?.cause?.name === "AI_TypeValidationError"
- ) {
- console.log(
- ` Invalid categories returned for ${programName}, skipping categorization`,
- );
- return []; // Return empty array for invalid categories
- }
-
- return []; // Return empty array for any other errors too
- }
-}
-
-// Main processing function
-async function main() {
- console.log("Starting program categorization...");
-
- const programs = await prisma.program.findMany({
- select: {
- id: true,
- name: true,
- url: true,
- },
- where: {
- url: {
- not: null,
- },
- categories: {
- none: {},
- },
- addedToMarketplaceAt: {
- not: null,
- },
- },
- take: 10,
- });
-
- console.log(`Found ${programs.length} programs to categorize`);
-
- const results: ProgramResult[] = [];
-
- // Process each program
- for (let i = 0; i < programs.length; i++) {
- const program = programs[i];
- console.log(`\n[${i + 1}/${programs.length}] Processing: ${program.name}`);
-
- if (!program.url) {
- results.push({
- programId: program.id,
- programName: program.name,
- categories: [],
- error: "No URL provided",
- });
- continue;
- }
-
- // Scrape website
- console.log(`Scraping: ${program.url}`);
- const scraped = await scrapeWebsite(program.url);
-
- if (!scraped) {
- results.push({
- programId: program.id,
- programName: program.name,
- categories: [],
- url: program.url,
- error: "Failed to scrape website",
- });
- continue;
- }
-
- // Categorize with AI
- console.log(`Analyzing content...`);
- const categories = await categorizeProgram(
- program.name,
- program.url,
- scraped.content,
- scraped.title,
- scraped.description,
- );
-
- results.push({
- programId: program.id,
- programName: program.name,
- categories: categories as Category[],
- url: program.url,
- });
-
- console.log(
- ` Categories: ${categories.length > 0 ? categories.join(", ") : "None (invalid/failed categorization)"}`,
- );
-
- // Add delay to respect rate limits
- await new Promise((resolve) => setTimeout(resolve, 1000));
- }
-
- console.log(JSON.stringify(results, null, 2));
- console.log(`Completed categorization of ${results.length} programs`);
-
- const expanded = results.flatMap((result) =>
- result.categories.map((category) => ({
- programId: result.programId,
- category,
- })),
- );
-
- console.log("expanded", JSON.stringify(expanded, null, 2));
-
- if (!expanded.length) return;
-
- await prisma.programCategory.createMany({
- data: expanded,
- skipDuplicates: true,
- });
-}
-
-main()
- .catch((error) => {
- console.error("Script failed:", error);
- process.exit(1);
- })
- .finally(async () => {
- await prisma.$disconnect();
- });
diff --git a/apps/web/scripts/migrations/backfill-program-marketplace-descriptions.ts b/apps/web/scripts/migrations/backfill-program-marketplace-descriptions.ts
deleted file mode 100644
index ff0a67da53..0000000000
--- a/apps/web/scripts/migrations/backfill-program-marketplace-descriptions.ts
+++ /dev/null
@@ -1,118 +0,0 @@
-import { prisma } from "@dub/prisma";
-import FireCrawlApp from "@mendable/firecrawl-js";
-import "dotenv-flow/config";
-
-if (!process.env.FIRECRAWL_API_KEY)
- throw new Error("FIRECRAWL_API_KEY is not set");
-
-// Initialize FireCrawl
-const firecrawl = new FireCrawlApp({
- apiKey: process.env.FIRECRAWL_API_KEY,
-});
-
-// Function to scrape website content
-async function scrapeWebsite(url: string) {
- try {
- const scrapeResult = await firecrawl.scrapeUrl(url, {
- formats: ["markdown"],
- onlyMainContent: true,
- parsePDF: false,
- maxAge: 14400000, // 4 hours cache
- excludeTags: ["img"],
- });
-
- if (!scrapeResult.success) {
- throw new Error(scrapeResult.error || "Failed to scrape");
- }
-
- return {
- content: scrapeResult.markdown || "",
- title: scrapeResult.metadata?.title || "",
- description: scrapeResult.metadata?.description || "",
- };
- } catch (error) {
- console.error(`Error scraping ${url}:`, error);
- return null;
- }
-}
-
-// Main processing function
-async function main() {
- console.log("Starting program description backfill...");
-
- const programs = await prisma.program.findMany({
- select: {
- id: true,
- name: true,
- url: true,
- description: true,
- },
- where: {
- addedToMarketplaceAt: {
- not: null,
- },
- url: {
- not: null,
- },
- description: null,
- },
- });
-
- console.log(`Found ${programs.length} programs to backfill descriptions`);
-
- let successCount = 0;
- let errorCount = 0;
-
- // Process each program
- for (let i = 0; i < programs.length; i++) {
- const program = programs[i];
- console.log(`\n[${i + 1}/${programs.length}] Processing: ${program.name}`);
-
- if (!program.url) {
- console.log(` Skipping: No URL provided`);
- errorCount++;
- continue;
- }
-
- // Scrape website
- console.log(` Scraping: ${program.url}`);
- const scraped = await scrapeWebsite(program.url);
-
- if (!scraped) {
- console.log(` Error: Failed to scrape website`);
- errorCount++;
- continue;
- }
-
- if (!scraped.description) {
- console.log(` Error: Failed to generate description`);
- errorCount++;
- continue;
- }
-
- // Update program description
- await prisma.program.update({
- where: { id: program.id },
- data: { description: scraped.description },
- });
-
- console.log(
- ` βœ“ Updated description: ${scraped.description.substring(0, 100)}...`,
- );
- successCount++;
-
- // Add delay to respect rate limits
- await new Promise((resolve) => setTimeout(resolve, 1000));
- }
-
- console.log(`\nCompleted: ${successCount} successful, ${errorCount} errors`);
-}
-
-main()
- .catch((error) => {
- console.error("Script failed:", error);
- process.exit(1);
- })
- .finally(async () => {
- await prisma.$disconnect();
- });
diff --git a/apps/web/scripts/migrations/backfill-program-marketplace.ts b/apps/web/scripts/migrations/backfill-program-marketplace.ts
deleted file mode 100644
index c4a69532f0..0000000000
--- a/apps/web/scripts/migrations/backfill-program-marketplace.ts
+++ /dev/null
@@ -1,76 +0,0 @@
-import { DEFAULT_PARTNER_GROUP } from "@/lib/zod/schemas/groups";
-import { prisma } from "@dub/prisma";
-import FireCrawlApp from "@mendable/firecrawl-js";
-import "dotenv-flow/config";
-
-if (!process.env.FIRECRAWL_API_KEY)
- throw new Error("FIRECRAWL_API_KEY is not set");
-
-// Initialize FireCrawl
-const firecrawl = new FireCrawlApp({
- apiKey: process.env.FIRECRAWL_API_KEY,
-});
-
-// Function to scrape website content
-async function scrapeWebsite(url: string) {
- try {
- const scrapeResult = await firecrawl.scrapeUrl(url, {
- formats: ["markdown"],
- onlyMainContent: true,
- parsePDF: false,
- maxAge: 14400000, // 4 hours cache
- excludeTags: ["img"],
- });
-
- if (!scrapeResult.success) {
- throw new Error(scrapeResult.error || "Failed to scrape");
- }
-
- return {
- content: scrapeResult.markdown || "",
- title: scrapeResult.metadata?.title || "",
- description: scrapeResult.metadata?.description || "",
- };
- } catch (error) {
- console.error(`Error scraping ${url}:`, error);
- return null;
- }
-}
-
-async function main() {
- const programToAdd = await prisma.program.findUniqueOrThrow({
- where: {
- slug: "",
- groups: {
- some: {
- slug: DEFAULT_PARTNER_GROUP.slug,
- applicationFormPublishedAt: {
- not: null,
- },
- },
- },
- },
- });
-
- if (!programToAdd.url) {
- throw new Error("Program URL is not set");
- }
-
- const scraped = await scrapeWebsite(programToAdd.url);
-
- const res = await prisma.program.update({
- where: {
- id: programToAdd.id,
- },
- data: {
- addedToMarketplaceAt: new Date(),
- description: scraped?.description || null,
- },
- });
-
- console.log(
- `Added ${res.name} to the marketplace with description: ${res.description}`,
- );
-}
-
-main();
diff --git a/apps/web/scripts/migrations/backfill-referral-links.ts b/apps/web/scripts/migrations/backfill-referral-links.ts
deleted file mode 100644
index 18b892e552..0000000000
--- a/apps/web/scripts/migrations/backfill-referral-links.ts
+++ /dev/null
@@ -1,45 +0,0 @@
-import { prefixWorkspaceId } from "@/lib/api/workspaces/workspace-id";
-import { dub } from "@/lib/dub";
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-
-async function main() {
- const workspaces = await prisma.project
- .findMany({
- where: {
- slug: {
- in: ["dub", "steven", "acme"],
- },
- },
- select: {
- id: true,
- slug: true,
- },
- orderBy: {
- createdAt: "asc",
- },
- })
- .then((workspaces) =>
- workspaces.map((workspace) => ({
- id: prefixWorkspaceId(workspace.id),
- slug: workspace.slug,
- })),
- );
-
- console.table(workspaces.slice(0, 10));
-
- const res = await dub.links.createMany(
- workspaces.map((workspace) => ({
- domain: "refer.dub.co",
- key: workspace.slug,
- url: "https://dub.co",
- externalId: prefixWorkspaceId(workspace.id), // attaching the workspace ID as the externalId for easy updates later on: https://d.to/externalId
- tagIds: ["cm000srqx0004o6ldehod07zc"], // tagging these links with the "Referral links" tag
- trackConversion: true, // enable conversion tracking for these links
- })),
- );
-
- console.log(res);
-}
-
-main();
diff --git a/apps/web/scripts/migrations/backfill-reward-modifier-ids.ts b/apps/web/scripts/migrations/backfill-reward-modifier-ids.ts
deleted file mode 100644
index 9aec675b8b..0000000000
--- a/apps/web/scripts/migrations/backfill-reward-modifier-ids.ts
+++ /dev/null
@@ -1,56 +0,0 @@
-import { RewardConditions } from "@/lib/types";
-import { prisma } from "@dub/prisma";
-import { Prisma } from "@dub/prisma/client";
-import "dotenv-flow/config";
-import { v4 as uuid } from "uuid";
-
-async function main() {
- const rewards = await prisma.reward.findMany({
- where: {
- modifiers: {
- not: Prisma.DbNull,
- },
- },
- select: {
- id: true,
- modifiers: true,
- },
- });
-
- if (rewards.length === 0) {
- return;
- }
-
- for (const reward of rewards) {
- const modifiers = reward.modifiers as RewardConditions[];
-
- const hasMissingId = modifiers.some(
- (m: { id?: string }) => m && typeof m === "object" && m.id == null,
- );
-
- if (!hasMissingId) {
- console.log(
- `Reward ${reward.id} has no missing modifier IDs, skipping...`,
- );
- continue;
- }
-
- const updatedModifiers = modifiers.map((m: RewardConditions) => {
- return {
- ...m,
- id: m.id ?? uuid(),
- };
- });
-
- await prisma.reward.update({
- where: { id: reward.id },
- data: { modifiers: updatedModifiers },
- });
-
- console.log(
- `Updated reward ${reward.id} with ${updatedModifiers.length} modifiers`,
- );
- }
-}
-
-main();
diff --git a/apps/web/scripts/migrations/backfill-saml-sso.ts b/apps/web/scripts/migrations/backfill-saml-sso.ts
deleted file mode 100644
index 121f79294d..0000000000
--- a/apps/web/scripts/migrations/backfill-saml-sso.ts
+++ /dev/null
@@ -1,70 +0,0 @@
-import { isGenericEmail } from "@/lib/is-generic-email";
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-
-async function main() {
- const workspaces = await prisma.project.findMany({
- where: {
- ssoEmailDomain: null,
- plan: "enterprise",
- },
- select: {
- id: true,
- name: true,
- ssoEmailDomain: true,
- users: {
- where: {
- role: "owner",
- },
- orderBy: {
- createdAt: "asc",
- },
- take: 1,
- select: {
- user: {
- select: {
- email: true,
- },
- },
- },
- },
- },
- });
-
- const workspacesToUpdate: any[] = [];
-
- for (const workspace of workspaces) {
- const email = workspace.users[0]?.user?.email;
- const emailDomain = email ? email.split("@")[1]?.toLowerCase() : undefined;
-
- if (!emailDomain || (email && isGenericEmail(email))) {
- console.log(
- `Workspace ${workspace.name}'s email domain (${emailDomain}) is invalid or generic, skipping...`,
- );
- continue;
- }
-
- workspacesToUpdate.push({
- id: workspace.id,
- name: workspace.name,
- ssoEmailDomain: emailDomain,
- });
- }
-
- console.table(workspacesToUpdate);
-
- await Promise.allSettled(
- workspacesToUpdate.map((workspace) =>
- prisma.project.update({
- where: {
- id: workspace.id,
- },
- data: {
- ssoEmailDomain: workspace.ssoEmailDomain,
- },
- }),
- ),
- );
-}
-
-main();
diff --git a/apps/web/scripts/migrations/backfill-short-links.ts b/apps/web/scripts/migrations/backfill-short-links.ts
deleted file mode 100644
index 8bdb239051..0000000000
--- a/apps/web/scripts/migrations/backfill-short-links.ts
+++ /dev/null
@@ -1,54 +0,0 @@
-import { prisma } from "@dub/prisma";
-import { linkConstructorSimple } from "@dub/utils";
-import "dotenv-flow/config";
-
-async function main() {
- const batchSize = 500;
- let processedCount = 0;
-
- while (true) {
- const links = await prisma.link.findMany({
- where: {
- shortLink: "null",
- },
- select: {
- id: true,
- domain: true,
- key: true,
- },
- take: batchSize,
- });
-
- if (links.length === 0) {
- break;
- }
-
- const results = await Promise.allSettled(
- links.map((link) =>
- prisma.link.update({
- where: { id: link.id },
- data: {
- shortLink: linkConstructorSimple({
- domain: link.domain,
- key: link.key,
- }),
- },
- }),
- ),
- );
-
- processedCount += results.filter((r) => r.status === "fulfilled").length;
- console.log(`Processed ${processedCount} links`);
- }
-
- console.log("Backfill complete");
-}
-
-main()
- .catch((e) => {
- console.error(e);
- process.exit(1);
- })
- .finally(async () => {
- await prisma.$disconnect();
- });
diff --git a/apps/web/scripts/migrations/backfill-stripe-connect.ts b/apps/web/scripts/migrations/backfill-stripe-connect.ts
deleted file mode 100644
index a368e54a1e..0000000000
--- a/apps/web/scripts/migrations/backfill-stripe-connect.ts
+++ /dev/null
@@ -1,63 +0,0 @@
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-import { stripeConnectClient } from "../stripe/connect-client";
-
-async function main() {
- const partners = await prisma.partner.findMany({
- where: {
- stripeConnectId: null,
- email: {
- not: null,
- },
- },
- take: 5,
- orderBy: {
- createdAt: "asc",
- },
- });
-
- await Promise.allSettled(
- partners.map(async (partner) => {
- const [firstName, lastName] = partner.name.split(" ");
- const res = await stripeConnectClient.accounts.create({
- type: "express",
- business_type: "individual",
- email: partner.email!,
- country: partner.country!,
- individual: {
- first_name: firstName,
- last_name: lastName,
- email: partner.email!,
- },
- capabilities: {
- transfers: {
- requested: true,
- },
- ...(partner.country === "US" && {
- card_payments: {
- requested: true,
- },
- }),
- },
- ...(partner.country !== "US" && {
- tos_acceptance: { service_agreement: "recipient" },
- }),
- });
-
- console.log(
- `New Stripe Connect account created for ${partner.name}: ${res.id}`,
- );
-
- await prisma.partner.update({
- where: {
- id: partner.id,
- },
- data: {
- stripeConnectId: res.id,
- },
- });
- }),
- );
-}
-
-main();
diff --git a/apps/web/scripts/migrations/backfill-total-commissions.ts b/apps/web/scripts/migrations/backfill-total-commissions.ts
deleted file mode 100644
index 4b0cbe5430..0000000000
--- a/apps/web/scripts/migrations/backfill-total-commissions.ts
+++ /dev/null
@@ -1,64 +0,0 @@
-import { syncTotalCommissions } from "@/lib/api/partners/sync-total-commissions";
-import { prisma } from "@dub/prisma";
-import { Prisma } from "@dub/prisma/client";
-import "dotenv-flow/config";
-
-async function main() {
- const commissions = await prisma.commission.groupBy({
- by: ["partnerId", "programId"],
- where: {
- earnings: {
- gt: 0,
- },
- status: {
- in: ["pending", "processed", "paid"],
- },
- },
- _sum: {
- earnings: true,
- },
- orderBy: {
- _sum: {
- earnings: "desc",
- },
- },
- });
-
- console.table(
- `Found ${commissions.length} partner-program pairs with commissions`,
- );
-
- const where: Prisma.ProgramEnrollmentWhereInput = {
- OR: commissions.map(({ partnerId, programId }) => ({
- partnerId,
- programId,
- })),
- totalCommissions: 0,
- };
-
- const programEnrollmentsToUpdate = await prisma.programEnrollment.findMany({
- where,
- take: 100,
- });
-
- for (const programEnrollment of programEnrollmentsToUpdate) {
- await syncTotalCommissions({
- partnerId: programEnrollment.partnerId,
- programId: programEnrollment.programId,
- });
- }
-
- console.log(
- `Updated ${programEnrollmentsToUpdate.length} program enrollments`,
- );
-
- const remainingProgramEnrollments = await prisma.programEnrollment.count({
- where,
- });
-
- console.log(
- `${remainingProgramEnrollments} remaining program enrollments to update`,
- );
-}
-
-main();
diff --git a/apps/web/scripts/migrations/backfill-workspace-stripe-billing-fields.ts b/apps/web/scripts/migrations/backfill-workspace-stripe-billing-fields.ts
deleted file mode 100644
index 678e020c36..0000000000
--- a/apps/web/scripts/migrations/backfill-workspace-stripe-billing-fields.ts
+++ /dev/null
@@ -1,406 +0,0 @@
-/**
- * Backfills workspace (`Project`) billing fields from the platform Stripe account:
- * `planPeriod`, `subscriptionCanceledAt`, `billingCycleEndsAt`.
- *
- * Requires:
- * - `STRIPE_SECRET_KEY` (platform Stripe β€” same as webhooks)
- * - Database URL env vars as used elsewhere (`DATABASE_URL`, etc.)
- *
- *
- * Usage:
- * pnpm exec tsx scripts/migrations/backfill-workspace-stripe-billing-fields.ts
- * pnpm exec tsx scripts/migrations/backfill-workspace-stripe-billing-fields.ts --dry-run
- * pnpm exec tsx scripts/migrations/backfill-workspace-stripe-billing-fields.ts --limit 50
- * pnpm exec tsx scripts/migrations/backfill-workspace-stripe-billing-fields.ts --slug my-workspace --dry-run
- * pnpm exec tsx scripts/migrations/backfill-workspace-stripe-billing-fields.ts --project-id clxxx...
- */
-import { prisma } from "@dub/prisma";
-import type { PlanPeriod } from "@dub/prisma/client";
-import { chunk } from "@dub/utils";
-import "dotenv-flow/config";
-import Stripe from "stripe";
-import { getPlanPeriodFromStripeSubscription } from "../../app/(ee)/api/stripe/webhook/utils/stripe-plan-period";
-import { stripe } from "../../lib/stripe";
-import { getSubscriptionCancellationFields } from "../../lib/stripe/workspace-subscription-fields";
-
-const SUBSCRIPTION_LIST_LIMIT = 100;
-const PAGE_SIZE = 50;
-const PARALLEL_BATCH = 8;
-const BATCH_DELAY_MS = 150;
-
-const STATUS_PRIORITY = [
- "active",
- "trialing",
- "past_due",
- "unpaid",
- "canceled",
-] as const;
-
-function argValue(argv: string[], flag: string): string | undefined {
- const i = argv.indexOf(flag);
- if (i === -1) {
- return undefined;
- }
- const value = argv[i + 1];
- if (value == null || value === "" || value.startsWith("--")) {
- throw new Error(`${flag} requires a value`);
- }
- return value;
-}
-
-function parseArgs() {
- const argv = process.argv.slice(2);
-
- let limit: number | undefined;
- if (argv.includes("--limit")) {
- const rawLimit = argValue(argv, "--limit");
- const n = Number(rawLimit);
- if (!Number.isInteger(n) || n <= 0) {
- throw new Error("--limit must be a positive integer");
- }
- limit = n;
- }
-
- const slug = argv.includes("--slug") ? argValue(argv, "--slug") : undefined;
- const projectId = argv.includes("--project-id")
- ? argValue(argv, "--project-id")
- : undefined;
-
- return {
- dryRun: argv.includes("--dry-run"),
- limit,
- slug,
- projectId,
- };
-}
-
-function isStripeMissingCustomerError(e: unknown): boolean {
- if (e instanceof Stripe.errors.StripeInvalidRequestError) {
- return (
- e.code === "resource_missing" &&
- (e.param === "customer" || /no such customer/i.test(e.message))
- );
- }
- return e instanceof Error && /no such customer/i.test(e.message);
-}
-
-function pickSubscription(
- subscriptions: Stripe.Subscription[],
-): Stripe.Subscription | null {
- if (subscriptions.length === 0) {
- return null;
- }
- for (const status of STATUS_PRIORITY) {
- const found = subscriptions.find((s) => s.status === status);
- if (found) {
- return found;
- }
- }
- return [...subscriptions].sort((a, b) => b.created - a.created)[0] ?? null;
-}
-
-async function listCustomerSubscriptions(
- customerId: string,
-): Promise<Stripe.Subscription[]> {
- const subscriptions: Stripe.Subscription[] = [];
- let startingAfter: string | undefined;
-
- while (true) {
- const page = await stripe.subscriptions.list({
- customer: customerId,
- status: "all",
- limit: SUBSCRIPTION_LIST_LIMIT,
- ...(startingAfter ? { starting_after: startingAfter } : {}),
- });
-
- subscriptions.push(...page.data);
-
- if (!page.has_more) {
- return subscriptions;
- }
-
- startingAfter = page.data[page.data.length - 1]?.id;
- if (!startingAfter) {
- return subscriptions;
- }
- }
-}
-
-function sleep(ms: number) {
- return new Promise((resolve) => setTimeout(resolve, ms));
-}
-
-function sameInstant(
- a: Date | null | undefined,
- b: Date | null | undefined,
-): boolean {
- if (a == null && b == null) {
- return true;
- }
- if (a == null || b == null) {
- return false;
- }
- return a.getTime() === b.getTime();
-}
-
-type ProjectBillingRow = {
- id: string;
- slug: string;
- stripeId: string | null;
- planPeriod: PlanPeriod | null;
- subscriptionCanceledAt: Date | null;
- billingCycleEndsAt: Date | null;
-};
-
-type BackfillCounters = {
- updated: number;
- skippedNoSubscription: number;
- failed: number;
-};
-
-async function backfillOneProject(
- project: ProjectBillingRow,
- dryRun: boolean,
- counters: BackfillCounters,
-): Promise<void> {
- const customerId = project.stripeId;
- if (!customerId) {
- return;
- }
-
- try {
- const allSubscriptions = await listCustomerSubscriptions(customerId);
- const sub = pickSubscription(allSubscriptions);
- if (!sub) {
- console.log(
- `[skip] ${project.id} (${project.slug}): no subscription for customer ${customerId}`,
- );
- counters.skippedNoSubscription++;
- return;
- }
-
- const planPeriod: PlanPeriod | undefined =
- getPlanPeriodFromStripeSubscription(sub);
- const { subscriptionCanceledAt, billingCycleEndsAt } =
- getSubscriptionCancellationFields(sub);
-
- const nextData = {
- planPeriod,
- subscriptionCanceledAt,
- billingCycleEndsAt,
- };
-
- const planEqual =
- planPeriod === undefined || planPeriod === project.planPeriod;
- const noOp =
- planEqual &&
- sameInstant(subscriptionCanceledAt, project.subscriptionCanceledAt) &&
- sameInstant(billingCycleEndsAt, project.billingCycleEndsAt);
-
- if (dryRun) {
- console.log({
- id: project.id,
- slug: project.slug,
- dryRun: true,
- subscriptionId: sub.id,
- subStatus: sub.status,
- noOp,
- proposed: nextData,
- current: {
- planPeriod: project.planPeriod,
- subscriptionCanceledAt: project.subscriptionCanceledAt,
- billingCycleEndsAt: project.billingCycleEndsAt,
- },
- });
- return;
- }
-
- if (noOp) {
- return;
- }
-
- const data: {
- subscriptionCanceledAt: Date | null;
- billingCycleEndsAt: Date | null;
- planPeriod?: PlanPeriod;
- } = {
- subscriptionCanceledAt,
- billingCycleEndsAt,
- };
- if (planPeriod !== undefined) {
- data.planPeriod = planPeriod;
- }
-
- await prisma.project.update({
- where: { id: project.id },
- data,
- });
- counters.updated++;
- console.log(`[updated] ${project.id} (${project.slug}) sub=${sub.id}`);
- } catch (e) {
- if (isStripeMissingCustomerError(e)) {
- console.log(
- `[skip] ${project.id} (${project.slug}): Stripe customer not found (${customerId})`,
- );
- counters.skippedNoSubscription++;
- return;
- }
- counters.failed++;
- console.error(
- `[error] ${project.id} (${project.slug}):`,
- e instanceof Error ? e.message : e,
- );
- }
-}
-
-async function runProjectBatches(
- projects: ProjectBillingRow[],
- dryRun: boolean,
- counters: BackfillCounters,
- onBatchComplete: (batchSize: number) => void,
-): Promise<void> {
- const batches = chunk(projects, PARALLEL_BATCH);
- for (const batch of batches) {
- await Promise.all(
- batch.map((project) => backfillOneProject(project, dryRun, counters)),
- );
- onBatchComplete(batch.length);
- await sleep(BATCH_DELAY_MS);
- }
-}
-
-async function main() {
- const { dryRun, limit, slug, projectId } = parseArgs();
-
- if (slug != null && projectId != null) {
- throw new Error("Use only one of --slug or --project-id");
- }
-
- if (!process.env.STRIPE_SECRET_KEY) {
- throw new Error("STRIPE_SECRET_KEY is required");
- }
-
- let processed = 0;
- let cursor: { id: string } | undefined;
- const counters: BackfillCounters = {
- updated: 0,
- skippedNoSubscription: 0,
- failed: 0,
- };
-
- /** Single-record mode: no cursor pagination */
- const scopedWhere =
- slug != null
- ? { slug, stripeId: { not: null } }
- : projectId != null
- ? { id: projectId, stripeId: { not: null } }
- : { stripeId: { not: null }, billingCycleEndsAt: null };
-
- if (scopedWhere != null) {
- const projects = await prisma.project.findMany({
- where: scopedWhere,
- select: {
- id: true,
- slug: true,
- stripeId: true,
- planPeriod: true,
- subscriptionCanceledAt: true,
- billingCycleEndsAt: true,
- },
- });
-
- if (projects.length === 0) {
- console.log({
- done: true,
- dryRun,
- message: "No matching project (check slug/id and stripeId)",
- slug: slug ?? null,
- projectId: projectId ?? null,
- });
- return;
- }
-
- await runProjectBatches(projects, dryRun, counters, (n) => {
- processed += n;
- });
-
- console.log({
- done: true,
- dryRun,
- mode: "scoped",
- processed,
- updated: counters.updated,
- skippedNoSubscription: counters.skippedNoSubscription,
- failed: counters.failed,
- slug: slug ?? null,
- projectId: projectId ?? null,
- });
- if (counters.failed > 0) {
- throw new Error(
- `Backfill finished with ${counters.failed} project error(s); see [error] logs above.`,
- );
- }
- return;
- }
-
- while (true) {
- if (limit != null && processed >= limit) {
- break;
- }
-
- const take =
- limit != null ? Math.min(PAGE_SIZE, limit - processed) : PAGE_SIZE;
- if (take <= 0) {
- break;
- }
-
- const projects = await prisma.project.findMany({
- where: {
- stripeId: { not: null },
- },
- select: {
- id: true,
- slug: true,
- stripeId: true,
- planPeriod: true,
- subscriptionCanceledAt: true,
- billingCycleEndsAt: true,
- },
- take,
- skip: cursor ? 1 : 0,
- cursor,
- orderBy: { id: "asc" },
- });
-
- if (projects.length === 0) {
- break;
- }
-
- await runProjectBatches(projects, dryRun, counters, (n) => {
- processed += n;
- });
-
- cursor = { id: projects[projects.length - 1].id };
- }
-
- console.log({
- done: true,
- dryRun,
- mode: "full",
- processed,
- updated: counters.updated,
- skippedNoSubscription: counters.skippedNoSubscription,
- failed: counters.failed,
- limit: limit ?? null,
- });
- if (counters.failed > 0) {
- throw new Error(
- `Backfill finished with ${counters.failed} project error(s); see [error] logs above.`,
- );
- }
-}
-
-main().catch((e) => {
- console.error(e);
- process.exit(1);
-});
diff --git a/apps/web/scripts/migrations/migrate-links-to-workspaces.ts b/apps/web/scripts/migrations/migrate-links-to-workspaces.ts
deleted file mode 100644
index 8375ae9dcf..0000000000
--- a/apps/web/scripts/migrations/migrate-links-to-workspaces.ts
+++ /dev/null
@@ -1,141 +0,0 @@
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-
-const DUB_PROJECT_ID = "cl7pj5kq4006835rbjlt2ofka";
-const DUB_USER_ID = "cl7p1s07k000687rbuhpwqkqa";
-
-async function main() {
- const users = await prisma.link.groupBy({
- by: ["userId"],
- _count: {
- id: true,
- },
- where: {
- projectId: DUB_PROJECT_ID,
- AND: [
- {
- userId: {
- not: null,
- },
- },
- {
- userId: {
- not: DUB_USER_ID,
- },
- },
- ],
- },
- orderBy: {
- _count: {
- id: "desc",
- },
- },
- take: 500,
- });
-
- // const projectsToCreate = (await Promise.all(
- // users
- // .filter(({ userId }) => userId !== null)
- // .map(async (user) => {
- // const u = await prisma.user.findUnique({
- // where: {
- // id: user.userId!,
- // },
- // select: {
- // name: true,
- // email: true,
- // },
- // });
- // if (!u) {
- // return null;
- // }
-
- // const projectName =
- // capitalize(u.name ?? u.email?.split("@")[0]) + "'s Links";
-
- // const projectSlug = slugify(u.email ?? "");
-
- // return {
- // userId: user.userId,
- // name: u.name,
- // email: u.email,
- // links: user._count.id,
- // projectName,
- // projectSlug,
- // };
- // }),
- // ).then((users) => users.filter((user) => user !== null))) as {
- // userId: string;
- // name: string | null;
- // email: string | null;
- // links: number;
- // projectName: string;
- // projectSlug: string;
- // }[];
-
- // await prisma.project.createMany({
- // data: projectsToCreate.map(({ projectName, projectSlug }) => ({
- // name: projectName,
- // slug: projectSlug,
- // billingCycleStart: new Date().getDate(),
- // })),
- // skipDuplicates: true,
- // });
-
- // const finalProjects = (await Promise.all(
- // projectsToCreate.map(async (user) => {
- // const project = await prisma.project.findUnique({
- // where: {
- // slug: user.projectSlug,
- // },
- // });
- // return {
- // ...user,
- // projectId: project?.id,
- // };
- // }),
- // )) as {
- // userId: string;
- // name: string | null;
- // email: string | null;
- // links: number;
- // projectId: string;
- // projectName: string;
- // projectSlug: string;
- // }[];
-
- // const projectUsers = await prisma.projectUsers.createMany({
- // data: finalProjects.map(({ userId, projectId }) => ({
- // userId,
- // projectId,
- // role: "owner",
- // })),
- // skipDuplicates: true,
- // });
-
- // const links = await Promise.all(
- // finalProjects.map(async (user) => {
- // return await prisma.link.updateMany({
- // where: {
- // projectId: DUB_PROJECT_ID,
- // userId: user.userId,
- // },
- // data: {
- // projectId: user.projectId,
- // },
- // });
- // }),
- // );
-
- // const redisHash = await redis.hset(
- // "migrated_links_users",
- // finalProjects.reduce((acc, { userId, projectId }) => {
- // acc[userId] = projectId;
- // return acc;
- // }, {} as Record<string, string>),
- // );
-
- console.log({ users });
-}
-
-main();
diff --git a/apps/web/scripts/migrations/migrate-sales.ts b/apps/web/scripts/migrations/migrate-sales.ts
deleted file mode 100644
index 4be613a000..0000000000
--- a/apps/web/scripts/migrations/migrate-sales.ts
+++ /dev/null
@@ -1,47 +0,0 @@
-import { createId } from "@/lib/api/create-id";
-import { prisma } from "@dub/prisma";
-import { EventType } from "@dub/prisma/client";
-import "dotenv-flow/config";
-
-async function main() {
- // @ts-ignore (old sales table)
- const sales = await prisma.sale.findMany({
- select: {
- id: true,
- programId: true,
- partnerId: true,
- linkId: true,
- payoutId: true,
- invoiceId: true,
- customerId: true,
- eventId: true,
- amount: true,
- earnings: true,
- currency: true,
- status: true,
- createdAt: true,
- updatedAt: true,
- },
- take: 1000,
- skip: 2500,
- });
-
- if (!sales.length) {
- console.log("No sales found.");
- return;
- }
-
- await prisma.commission.createMany({
- data: sales.map((sale) => ({
- ...sale,
- id: createId({ prefix: "cm_" }),
- type: EventType.sale,
- quantity: 1,
- })),
- skipDuplicates: true,
- });
-
- console.log(`Migrated ${sales.length} sales.`);
-}
-
-main();
diff --git a/apps/web/scripts/migrations/remove-duplicate-notification-emails.ts b/apps/web/scripts/migrations/remove-duplicate-notification-emails.ts
deleted file mode 100644
index 20318d62cf..0000000000
--- a/apps/web/scripts/migrations/remove-duplicate-notification-emails.ts
+++ /dev/null
@@ -1,67 +0,0 @@
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-
-// one time script to make sure notificationEmail entries are unique by emailId
-async function main() {
- const emails = await prisma.notificationEmail.groupBy({
- by: ["emailId"],
- _count: {
- emailId: true,
- },
- orderBy: {
- _count: {
- emailId: "desc",
- },
- },
- });
-
- const emailsWithDuplicates = emails.filter(
- (email) => email._count.emailId > 1,
- );
-
- if (emailsWithDuplicates.length === 0) {
- console.log("No more duplicate emails found");
- return;
- }
-
- console.log(`Found ${emailsWithDuplicates.length} emails with duplicates`);
-
- const emailIdsToDelete: string[] = [];
- const notificationEmailIdsToPreserve: string[] = [];
-
- for (const e of emailsWithDuplicates.slice(0, 100)) {
- const finalEmail = await prisma.notificationEmail.findFirst({
- where: {
- emailId: e.emailId,
- },
- orderBy: {
- id: "desc",
- },
- });
- if (!finalEmail) {
- console.log(`No final email found for ${e.emailId}`);
- continue;
- }
- emailIdsToDelete.push(e.emailId);
- notificationEmailIdsToPreserve.push(finalEmail.id);
- }
-
- console.log({
- emailIdsToDelete,
- notificationEmailIdsToPreserve,
- });
-
- const res = await prisma.notificationEmail.deleteMany({
- where: {
- emailId: {
- in: emailIdsToDelete,
- },
- id: {
- notIn: notificationEmailIdsToPreserve,
- },
- },
- });
- console.log(`Deleted ${res.count} notification emails`);
-}
-
-main();
diff --git a/apps/web/scripts/migrations/restore-group-ids.ts b/apps/web/scripts/migrations/restore-group-ids.ts
deleted file mode 100644
index 431edc3e01..0000000000
--- a/apps/web/scripts/migrations/restore-group-ids.ts
+++ /dev/null
@@ -1,97 +0,0 @@
-import { includeProgramEnrollment } from "@/lib/api/links/include-program-enrollment";
-import { includeTags } from "@/lib/api/links/include-tags";
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-import { recordLink } from "../../lib/tinybird";
-
-// one time script to restore group ids for banned/deactivated program enrollments
-async function main() {
- while (true) {
- const programEnrollmentsToUpdate = await prisma.programEnrollment.findMany({
- where: {
- status: "deactivated",
- groupId: null,
- },
- include: {
- program: {
- select: {
- defaultGroupId: true,
- },
- },
- commissions: {
- take: 1,
- orderBy: {
- createdAt: "desc",
- },
- select: {
- reward: {
- select: {
- leadPartnerGroup: {
- select: {
- id: true,
- },
- },
- },
- },
- },
- },
- },
- take: 1000,
- });
-
- if (programEnrollmentsToUpdate.length === 0) {
- break;
- }
-
- const toUpdate = programEnrollmentsToUpdate.map((p) => ({
- programEnrollmentId: p.id,
- status: p.status,
- groupId:
- p.commissions[0]?.reward?.leadPartnerGroup?.id ??
- p.program.defaultGroupId!,
- }));
-
- // group by groupId
- const toUpdateGrouped = toUpdate.reduce(
- (acc, p) => {
- acc[p.groupId] = acc[p.groupId] || [];
- acc[p.groupId].push(p.programEnrollmentId);
- return acc;
- },
- {} as Record<string, string[]>,
- );
-
- for (const [groupId, programEnrollmentIds] of Object.entries(
- toUpdateGrouped,
- )) {
- const res = await prisma.programEnrollment.updateMany({
- where: {
- id: { in: programEnrollmentIds },
- },
- data: { groupId },
- });
- console.log(
- `Updated ${res.count} program enrollments for group ${groupId}`,
- );
- }
-
- const partnerLinks = await prisma.link.findMany({
- where: {
- programEnrollment: {
- id: {
- in: toUpdate.map((p) => p.programEnrollmentId),
- },
- },
- },
- include: {
- ...includeTags,
- ...includeProgramEnrollment,
- },
- });
-
- const tbRes = await recordLink(partnerLinks);
- console.log("tbRes", tbRes);
- }
-}
-
-main();
diff --git a/apps/web/scripts/migrations/update-discoverable-partners.ts b/apps/web/scripts/migrations/update-discoverable-partners.ts
deleted file mode 100644
index 06b168a0aa..0000000000
--- a/apps/web/scripts/migrations/update-discoverable-partners.ts
+++ /dev/null
@@ -1,119 +0,0 @@
-import { EXCLUDED_PROGRAM_IDS } from "@/lib/constants/partner-profile";
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-
-async function main() {
- const eligiblePartners = await prisma.partner.findMany({
- where: {
- programs: {
- some: {
- programId: {
- notIn: EXCLUDED_PROGRAM_IDS,
- },
- status: "approved",
- totalCommissions: {
- gte: 10_00,
- },
- },
- none: {
- status: "banned",
- },
- },
- // AND: [
- // {
- // OR: [
- // {
- // website: {
- // equals: "",
- // },
- // },
- // { website: null },
- // ],
- // },
- // {
- // OR: [
- // {
- // youtube: {
- // equals: "",
- // },
- // },
- // { youtube: null },
- // ],
- // },
- // {
- // OR: [
- // {
- // twitter: {
- // equals: "",
- // },
- // },
- // { twitter: null },
- // ],
- // },
- // {
- // OR: [
- // {
- // linkedin: {
- // equals: "",
- // },
- // },
- // { linkedin: null },
- // ],
- // },
- // {
- // OR: [
- // {
- // instagram: {
- // equals: "",
- // },
- // },
- // { instagram: null },
- // ],
- // },
- // {
- // OR: [
- // {
- // tiktok: {
- // equals: "",
- // },
- // },
- // { tiktok: null },
- // ],
- // },
- // ],
- },
- include: {
- programs: true,
- },
- });
-
- console.table(eligiblePartners, ["name", "email"]);
-
- const discoveredRes = await prisma.partner.updateMany({
- where: {
- discoverableAt: null,
- id: {
- in: eligiblePartners.map((partner) => partner.id),
- },
- },
- data: { discoverableAt: new Date() },
- });
- console.log(`Updated ${discoveredRes.count} partners to be discoverable`);
-
- const notDiscoveredRes = await prisma.partner.updateMany({
- where: {
- discoverableAt: {
- not: null,
- },
- id: {
- notIn: eligiblePartners.map((partner) => partner.id),
- },
- },
- data: { discoverableAt: null },
- });
- console.log(
- `Updated ${notDiscoveredRes.count} partners to be not discoverable`,
- );
-}
-
-main();
diff --git a/apps/web/scripts/move-links-to-folder.ts b/apps/web/scripts/move-links-to-folder.ts
deleted file mode 100644
index af71e8ec29..0000000000
--- a/apps/web/scripts/move-links-to-folder.ts
+++ /dev/null
@@ -1,59 +0,0 @@
-import { includeProgramEnrollment } from "@/lib/api/links/include-program-enrollment";
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-import { includeTags } from "../lib/api/links/include-tags";
-import { recordLink } from "../lib/tinybird";
-
-// Move the program links to a folder.
-async function main() {
- const workspaceId = "xxx";
- const folderId = "fold_xxx";
-
- const links = await prisma.link.findMany({
- where: {
- projectId: workspaceId,
- programId: {
- not: null,
- },
- folderId: null,
- },
- select: {
- id: true,
- },
- });
-
- if (links.length === 0) {
- console.log("No links found.");
- return;
- }
-
- console.log(links);
-
- await prisma.link.updateMany({
- where: {
- id: {
- in: links.map((link) => link.id),
- },
- },
- data: { folderId },
- });
-
- const updatedLinks = await prisma.link.findMany({
- where: {
- id: {
- in: links.map((link) => link.id),
- },
- },
- include: {
- ...includeTags,
- ...includeProgramEnrollment,
- },
- });
-
- console.table(updatedLinks);
-
- const res = await recordLink(updatedLinks);
- console.log(res);
-}
-
-main();
diff --git a/apps/web/scripts/programs/create-zero-commissions.ts b/apps/web/scripts/programs/create-zero-commissions.ts
new file mode 100644
index 0000000000..e194c0f795
--- /dev/null
+++ b/apps/web/scripts/programs/create-zero-commissions.ts
@@ -0,0 +1,57 @@
+import { createId } from "@/lib/api/create-id";
+import { prisma } from "@dub/prisma";
+import "dotenv-flow/config";
+
+// script to generate zero commissions for a program and partner
+// useful for making sure a partner that's in a no reward group
+// doesn't receive rewards for old referrals if they're moved to a reward group
+async function main() {
+ const programId = "prog_xxx";
+ const partnerId = "pn_xxx";
+
+ const programEnrollment = await prisma.programEnrollment.findUniqueOrThrow({
+ where: {
+ partnerId_programId: {
+ programId,
+ partnerId,
+ },
+ },
+ include: {
+ links: {
+ orderBy: {
+ createdAt: "desc",
+ },
+ take: 1,
+ },
+ },
+ });
+
+ const customers = await prisma.customer.findMany({
+ where: {
+ programId,
+ partnerId,
+ },
+ });
+
+ const commissions = await prisma.commission.createMany({
+ data: customers.map((customer) => ({
+ id: createId({ prefix: "cm_" }),
+ programId,
+ partnerId,
+ linkId: programEnrollment.links[0].id,
+ customerId: customer.id,
+ rewardId: programEnrollment.saleRewardId,
+ type: "sale",
+ amount: 0,
+ quantity: 1,
+ earnings: 0,
+ currency: "usd",
+ status: "paid",
+ createdAt: customer.createdAt,
+ })),
+ });
+
+ console.log(`Created ${commissions.count} commissions`);
+}
+
+main();
diff --git a/apps/web/scripts/remove-workspace-scopes.ts b/apps/web/scripts/remove-workspace-scopes.ts
deleted file mode 100644
index 437d0d92a8..0000000000
--- a/apps/web/scripts/remove-workspace-scopes.ts
+++ /dev/null
@@ -1,53 +0,0 @@
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-
-async function main() {
- // Find all restricted tokens that have the workspace.* scopes
- const tokens = await prisma.restrictedToken.findMany({
- where: {
- scopes: {
- contains: "workspaces.",
- },
- installationId: {
- not: null,
- },
- },
- select: {
- id: true,
- scopes: true,
- lastUsed: true,
- },
- take: 100,
- });
-
- console.table(tokens);
-
- // Remove the workspace.* scopes from the tokens
- const results = await Promise.allSettled(
- tokens.map((token) => {
- if (!token.scopes) return Promise.resolve(null);
-
- // Split by spaces, filter out "workspaces.read" and "workspaces.write", and join back
- const updatedScopes = token.scopes
- .split(" ")
- .filter((scope) => !scope.startsWith("workspaces."))
- .join(" ")
- .trim();
-
- return prisma.restrictedToken.update({
- where: {
- id: token.id,
- },
- data: {
- scopes: updatedScopes || null,
- },
- });
- }),
- );
-
- console.log(
- `Updated ${results.filter((r) => r.status === "fulfilled").length} tokens.`,
- );
-}
-
-main();
diff --git a/apps/web/scripts/restore-backup.ts b/apps/web/scripts/restore-backup.ts
deleted file mode 100644
index a2ad9a5e7c..0000000000
--- a/apps/web/scripts/restore-backup.ts
+++ /dev/null
@@ -1,31 +0,0 @@
-import { bulkCreateLinks } from "@/lib/api/links";
-import { ProcessedLinkProps } from "@/lib/types";
-import { redis } from "@/lib/upstash";
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-
-const domain = "xxx";
-
-async function main() {
- const restoredData = await redis.lrange<ProcessedLinkProps>(
- "restoredData",
- 0,
- -1,
- );
-
- if (restoredData.length === 0) {
- const links = await prisma.link.findMany({
- where: {
- domain,
- },
- });
- await redis.lpush("restoredData", links);
- } else {
- const response = await bulkCreateLinks({ links: restoredData });
- console.log(response);
- // delete restoredData from redis
- await redis.del("restoredData");
- }
-}
-
-main();
diff --git a/apps/web/scripts/revert-partner-payout-demo.ts b/apps/web/scripts/revert-partner-payout-demo.ts
deleted file mode 100644
index 36eb14027e..0000000000
--- a/apps/web/scripts/revert-partner-payout-demo.ts
+++ /dev/null
@@ -1,29 +0,0 @@
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-
-const payoutId = "po_z50ZzjiXigYwDGPEqhNATiJ2";
-
-async function main() {
- const res = await Promise.all([
- prisma.payout.update({
- where: {
- id: payoutId,
- },
- data: {
- status: "pending",
- },
- }),
- prisma.commission.updateMany({
- where: {
- payoutId,
- },
- data: {
- status: "processed",
- },
- }),
- ]);
-
- console.log(res);
-}
-
-main();
diff --git a/apps/web/scripts/seed-support-embeddings.ts b/apps/web/scripts/seed-support-embeddings.ts
deleted file mode 100644
index 45d3d579c2..0000000000
--- a/apps/web/scripts/seed-support-embeddings.ts
+++ /dev/null
@@ -1,110 +0,0 @@
-/**
- * Seed script for support embeddings.
- *
- *
- * Usage:
- * pnpm tsx scripts/seed-support-embeddings.ts # seed all articles
- * pnpm tsx scripts/seed-support-embeddings.ts --url <url> # re-seed a single article
- */
-
-import { fetchPlausiblePageviews } from "app/api/ai/sync-embeddings/fetch-plausible-pageviews";
-import "dotenv-flow/config";
-import { upsertDocsEmbeddings } from "../lib/ai/upsert-docs-embedding";
-
-/**
- * Fetch all article URLs from dub.co/llms.txt.
- */
-export async function fetchArticleUrls(): Promise<string[]> {
- const res = await fetch("https://dub.co/docs/llms.txt");
- if (!res.ok) throw new Error(`Failed to fetch llms.txt: ${res.status}`);
-
- const text = await res.text();
- const urls: string[] = [];
-
- for (const line of text.split("\n")) {
- const trimmed = line.trim();
- const linkMatch = trimmed.match(
- /\(?(https?:\/\/dub\.co\/(?:docs|help)[^\s)]*)\)?/,
- );
- const candidate = linkMatch ? linkMatch[1] : trimmed;
-
- if (candidate.startsWith("http")) {
- try {
- const parsed = new URL(candidate);
- const origin = "https://dub.co";
- const pathname = parsed.pathname.endsWith(".md")
- ? parsed.pathname.slice(0, -3)
- : parsed.pathname;
- const normalizedUrl = `${origin}${pathname}`;
- urls.push(normalizedUrl);
- } catch {
- // Invalid URL, skip
- }
- }
- }
-
- return [...new Set(urls)];
-}
-
-async function main() {
- const args = process.argv.slice(2);
- const urlFlagIdx = args.indexOf("--url");
-
- if (urlFlagIdx !== -1) {
- const url = args[urlFlagIdx + 1];
- if (!url) {
- console.error(
- "Error: --url requires a value (e.g. --url https://dub.co/docs/...)",
- );
- process.exit(1);
- }
- console.log(`Seeding single article: ${url}`);
- const pageviewsMap = await fetchPlausiblePageviews();
- const result = await upsertDocsEmbeddings(url, pageviewsMap);
- console.log(
- ` β†’ ${result.chunks} chunks${result.skipped ? " (skipped)" : ""}`,
- );
- console.log("Done.");
- return;
- }
-
- console.log("Fetching article list from llms.txt...");
- const urls = await fetchArticleUrls();
- console.log(`Found ${urls.length} articles to embed.\n`);
-
- console.log("Fetching pageviews from Plausible...");
- const pageviewsMap = await fetchPlausiblePageviews();
- console.log(`Loaded pageviews for ${pageviewsMap.size} pages.\n`);
-
- let success = 0;
- let skipped = 0;
- let failed = 0;
-
- for (const url of urls) {
- try {
- process.stdout.write(`Processing: ${url} ... `);
- const result = await upsertDocsEmbeddings(url, pageviewsMap);
- if (result.skipped) {
- process.stdout.write(`skipped\n`);
- skipped++;
- } else {
- process.stdout.write(`${result.chunks} chunks\n`);
- success++;
- }
- } catch (err) {
- process.stdout.write(`ERROR: ${err}\n`);
- failed++;
- }
-
- await new Promise((r) => setTimeout(r, 200));
- }
-
- console.log(
- `\nDone. Success: ${success}, Skipped: ${skipped}, Failed: ${failed}`,
- );
-}
-
-main().catch((err) => {
- console.error(err);
- process.exit(1);
-});
diff --git a/apps/web/scripts/sent-mail-reset.ts b/apps/web/scripts/sent-mail-reset.ts
deleted file mode 100644
index 2b2bc98f8d..0000000000
--- a/apps/web/scripts/sent-mail-reset.ts
+++ /dev/null
@@ -1,28 +0,0 @@
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-
-async function main() {
- while (true) {
- const users = await prisma.user.findMany({
- where: {
- sentMail: true,
- },
- take: 1000,
- });
- if (users.length === 0) {
- console.log("No more users to update");
- break;
- }
- await prisma.user.updateMany({
- where: {
- id: { in: users.map((user) => user.id) },
- },
- data: {
- sentMail: false,
- },
- });
- console.log(`Updated ${users.length} users to sentMail: false`);
- }
-}
-
-main();
diff --git a/apps/web/scripts/sitemap-importer.ts b/apps/web/scripts/sitemap-importer.ts
deleted file mode 100644
index a7fe17cbc8..0000000000
--- a/apps/web/scripts/sitemap-importer.ts
+++ /dev/null
@@ -1,20 +0,0 @@
-import "dotenv-flow/config";
-import { importTrackedSitemaps } from "../lib/sitemaps/import-tracked-sitemaps";
-
-const sitemapUrl = "https://dub.co/sitemap.xml";
-const domain = "site.dub.co";
-const projectId = "ws_xxx";
-
-async function main() {
- const { linksToCreate, createdLinks } = await importTrackedSitemaps({
- trackedSitemaps: [{ url: sitemapUrl }],
- domain,
- projectId,
- });
-
- console.table(linksToCreate);
- console.log(`Found ${linksToCreate.length} links to create`);
- console.log(`Created ${createdLinks.length} links`);
-}
-
-main().catch(console.error);
diff --git a/apps/web/scripts/sync-conversions.ts b/apps/web/scripts/sync-conversions.ts
deleted file mode 100644
index 0b043d8fdf..0000000000
--- a/apps/web/scripts/sync-conversions.ts
+++ /dev/null
@@ -1,44 +0,0 @@
-import { prisma } from "@dub/prisma";
-import { chunk } from "@dub/utils";
-import "dotenv-flow/config";
-import * as fs from "fs";
-import * as Papa from "papaparse";
-
-const data: { [key: number]: string[] } = {};
-
-async function main() {
- Papa.parse(fs.createReadStream("link_id_unique_customers.csv", "utf-8"), {
- header: true,
- step: (result: { data: { link_id: string; unique_customers: number } }) => {
- data[result.data.unique_customers] = (
- data[result.data.unique_customers] || []
- ).concat(result.data.link_id);
- },
- complete: async () => {
- const dataArray = Object.entries(data).map(([conversions, linkIds]) => ({
- conversions: parseInt(conversions),
- linkIds,
- }));
- console.table(dataArray);
-
- for (const { conversions, linkIds } of dataArray) {
- const chunks = chunk(linkIds, 1000);
- for (const chunk of chunks) {
- const res = await prisma.link.updateMany({
- where: {
- id: { in: chunk },
- },
- data: {
- conversions,
- },
- });
- console.log(
- `Updated ${res.count} links with ${conversions} conversions.`,
- );
- }
- }
- },
- });
-}
-
-main();
diff --git a/apps/web/scripts/sync-domain-clicks.ts b/apps/web/scripts/sync-domain-clicks.ts
deleted file mode 100644
index c3f72d7a0e..0000000000
--- a/apps/web/scripts/sync-domain-clicks.ts
+++ /dev/null
@@ -1,62 +0,0 @@
-// @ts-nocheck
-
-// TODO:
-// Fix the script
-
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-import * as fs from "fs";
-import * as Papa from "papaparse";
-
-const index = 1000;
-const domainClicks: { domain: string; clicks: string }[] = [];
-
-async function main() {
- Papa.parse(fs.createReadStream("sql.csv", "utf-8"), {
- header: true,
- step: (result: { data: { domain: string; clicks: string } }) => {
- domainClicks.push(result.data);
- },
- complete: async () => {
- const domainWithClicks = await prisma.domain.findMany({
- where: {
- clicks: {
- gt: 0,
- },
- },
- select: {
- slug: true,
- },
- });
- console.table(domainClicks.slice(0, 50));
- const domainClickToUpdate = domainClicks
- .filter((domainClick) => {
- const { domain } = domainClick;
- return domainWithClicks.find(({ slug }) => slug === domain);
- })
- .slice(index, index + 1000);
-
- await Promise.all(
- domainClickToUpdate.map(async (domainClick) => {
- const { domain, clicks } = domainClick;
- console.log(`Updating ${domain} with ${clicks} clicks.`);
- return prisma.domain.update({
- where: {
- slug: domain,
- },
- data: {
- clicks: parseInt(clicks),
- },
- });
- }),
- );
- console.log(
- `Done updating ${index} to ${
- index + domainClickToUpdate.length
- } domains.`,
- );
- },
- });
-}
-
-main();
diff --git a/apps/web/scripts/sync-expired-links.ts b/apps/web/scripts/sync-expired-links.ts
deleted file mode 100644
index 727b207d5f..0000000000
--- a/apps/web/scripts/sync-expired-links.ts
+++ /dev/null
@@ -1,54 +0,0 @@
-import { RedisLinkProps } from "@/lib/types";
-import { redis } from "@/lib/upstash";
-import { prisma } from "@dub/prisma";
-import { chunk } from "@dub/utils";
-import "dotenv-flow/config";
-
-async function main() {
- const allExpiredLinks = await prisma.link.findMany({
- where: {
- NOT: {
- expiresAt: null,
- },
- },
- select: {
- id: true,
- domain: true,
- key: true,
- expiresAt: true,
- },
- orderBy: {
- expiresAt: "asc",
- },
- skip: 2000,
- });
-
- let count = 0;
- const chunks = chunk(allExpiredLinks, 100);
-
- for (const chunk of chunks) {
- const redisLinks = await redis.mget<RedisLinkProps[]>(
- chunk.map((link) => `${link.domain}:${link.key}`),
- );
-
- const pipeline = redis.pipeline();
- redisLinks.forEach((link, idx) => {
- const { domain, key, expiresAt } = chunk[idx];
- // @ts-ignore (old version)
- const { expired, ...rest } = link || {};
- // WARNING: OLD VERSION OF REDIS IMPLEMENTATION, WE NOW USE HSET (hashes)
- pipeline.set(`${domain}:${key}`, {
- ...rest,
- expiresAt,
- });
- });
-
- await pipeline.exec();
-
- count += chunk.length;
-
- console.log(`Synced ${count} expired links...`);
- }
-}
-
-main();
diff --git a/apps/web/scripts/sync-limits.ts b/apps/web/scripts/sync-limits.ts
deleted file mode 100644
index bc47c61058..0000000000
--- a/apps/web/scripts/sync-limits.ts
+++ /dev/null
@@ -1,17 +0,0 @@
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-
-async function main() {
- const response = await prisma.project.updateMany({
- where: {
- plan: "business",
- },
- data: {
- domainsLimit: 40,
- },
- });
-
- console.log(response);
-}
-
-main();
diff --git a/apps/web/scripts/sync-link-clicks.ts b/apps/web/scripts/sync-link-clicks.ts
deleted file mode 100644
index 7e8fcbef79..0000000000
--- a/apps/web/scripts/sync-link-clicks.ts
+++ /dev/null
@@ -1,65 +0,0 @@
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-import * as fs from "fs";
-import * as Papa from "papaparse";
-
-const index = 24000;
-const linkClicks: { domain: string; key: string; clicks: string }[] = [];
-
-async function main() {
- Papa.parse(fs.createReadStream("sql.csv", "utf-8"), {
- header: true,
- step: (result: {
- data: { domain: string; key: string; clicks: string };
- }) => {
- linkClicks.push(result.data);
- },
- complete: async () => {
- const linksWithClicks = await prisma.link.findMany({
- where: {
- clicks: {
- gt: 0,
- },
- },
- select: {
- domain: true,
- key: true,
- },
- });
- console.table(linkClicks.slice(0, 50));
-
- const linkClicksToUpdate = linkClicks
- .filter((linkClick) => {
- const { domain, key } = linkClick;
- return linksWithClicks.find(
- (link) => link.domain === domain && link.key === key,
- );
- })
- .slice(index, index + 1000);
-
- await Promise.all(
- linkClicksToUpdate.map((linkClick) => {
- const { domain, key, clicks } = linkClick;
- console.log(`Updating ${domain}/${key} with ${clicks} clicks.`);
- return prisma.link.update({
- where: {
- domain_key: {
- domain,
- key,
- },
- },
- data: {
- clicks: parseInt(clicks),
- },
- });
- }),
- );
-
- console.log(
- `Done updating ${index} to ${index + linkClicksToUpdate.length} links.`,
- );
- },
- });
-}
-
-main();
diff --git a/apps/web/scripts/sync-links-metadata.ts b/apps/web/scripts/sync-links-metadata.ts
deleted file mode 100644
index 26d7cf052f..0000000000
--- a/apps/web/scripts/sync-links-metadata.ts
+++ /dev/null
@@ -1,58 +0,0 @@
-import { recordLink } from "@/lib/tinybird";
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-
-const count = 16;
-const limit = 20000;
-
-async function main() {
- const links = await prisma.link.findMany({
- include: {
- tags: {
- select: {
- tag: true,
- },
- },
- },
- orderBy: [
- {
- clicks: "desc",
- },
- {
- createdAt: "asc",
- },
- ],
- skip: 10000 + limit * count,
- take: limit,
- });
-
- // const links = await prisma.domain
- // .findMany({
- // orderBy: [
- // {
- // clicks: "desc",
- // },
- // {
- // createdAt: "asc",
- // },
- // ],
- // take: 5000,
- // })
- // .then((domains) => {
- // return domains.map((domain) => ({
- // id: domain.id,
- // domain: domain.slug,
- // key: "_root",
- // url: domain.target || "",
- // tags: [] as { tagId: string }[],
- // projectId: domain.projectId,
- // createdAt: domain.createdAt,
- // }));
- // });
-
- const res = await recordLink(links);
-
- console.log(res);
-}
-
-main();
diff --git a/apps/web/scripts/sync-tag-analytics.ts b/apps/web/scripts/sync-tag-analytics.ts
deleted file mode 100644
index d97e7c14ba..0000000000
--- a/apps/web/scripts/sync-tag-analytics.ts
+++ /dev/null
@@ -1,31 +0,0 @@
-import { recordLink } from "@/lib/tinybird";
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-
-async function main() {
- const links = await prisma.link.findMany({
- where: {
- tags: {
- some: {},
- },
- },
- include: {
- tags: {
- select: {
- tag: true,
- },
- },
- },
- orderBy: {
- createdAt: "asc",
- },
- skip: 0,
- take: 1000,
- });
-
- const res = await recordLink(links);
-
- console.log(res);
-}
-
-main();
diff --git a/apps/web/scripts/test-appsflyer.ts b/apps/web/scripts/test-appsflyer.ts
deleted file mode 100644
index 691ffa3696..0000000000
--- a/apps/web/scripts/test-appsflyer.ts
+++ /dev/null
@@ -1,28 +0,0 @@
-import { APP_DOMAIN_WITH_NGROK } from "@dub/utils";
-import "dotenv-flow/config";
-
-async function main() {
- const trackLeadSearchParams = new URLSearchParams({
- appId: "id123",
- partnerEventId: "lead",
- clickId: "OcDa0EUdcs2XcRU6",
- eventName: "Signup",
- customerExternalId: "af+customer+2",
- });
-
- const trackSaleSearchParams = new URLSearchParams({
- appId: "id123",
- partnerEventId: "sale",
- customerExternalId: "af+customer+2",
- amount: "2900",
- currency: "usd",
- eventName: "Purchase",
- });
-
- console.log({
- trackLead: `${APP_DOMAIN_WITH_NGROK}/api/appsflyer/webhook?${trackLeadSearchParams.toString()}`,
- trackSale: `${APP_DOMAIN_WITH_NGROK}/api/appsflyer/webhook?${trackSaleSearchParams.toString()}`,
- });
-}
-
-main();
diff --git a/apps/web/scripts/test-paypal-payouts.ts b/apps/web/scripts/test-paypal-payouts.ts
deleted file mode 100644
index 9e201e3332..0000000000
--- a/apps/web/scripts/test-paypal-payouts.ts
+++ /dev/null
@@ -1,62 +0,0 @@
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-import { createPaypalToken } from "../lib/paypal/create-paypal-token";
-import { paypalEnv } from "../lib/paypal/env";
-
-async function main() {
- const payouts = await prisma.payout.findMany({
- where: {
- id: "po_xxxxx",
- },
- include: {
- partner: true,
- program: true,
- },
- });
-
- // DON'T FORGET TO CHANGE TO PROD ENVS BEFORE RUNNING THIS SCRIPT
- const paypalAccessToken = await createPaypalToken();
-
- console.log({ paypalAccessToken });
-
- console.log("Creating PayPal batch payout with env", paypalEnv);
-
- const body = {
- sender_batch_header: {
- sender_batch_id: payouts[0].id,
- },
- items: payouts.map((payout) => ({
- recipient_type: "EMAIL",
- receiver: payout.partner.paypalEmail,
- sender_item_id: payout.id,
- note: `Dub Partners payout (${payout.program.name})`,
- amount: {
- value: (payout.amount / 100).toString(),
- currency: "USD",
- },
- })),
- };
-
- console.log(
- "Creating PayPal batch payout with body",
- JSON.stringify(body, null, 2),
- );
-
- const response = await fetch(
- `${paypalEnv.PAYPAL_API_HOST}/v1/payments/payouts`,
- {
- method: "POST",
- headers: {
- "Content-Type": "application/json",
- Authorization: `Bearer ${paypalAccessToken}`,
- },
- body: JSON.stringify(body),
- },
- );
-
- const data = await response.json();
-
- console.log("Completed PayPal batch payout with data", data);
-}
-
-main();
diff --git a/apps/web/scripts/trigger-update-partner-stats.ts b/apps/web/scripts/trigger-update-partner-stats.ts
deleted file mode 100644
index 6c9bc07f16..0000000000
--- a/apps/web/scripts/trigger-update-partner-stats.ts
+++ /dev/null
@@ -1,37 +0,0 @@
-import { prisma } from "@dub/prisma";
-import { chunk } from "@dub/utils";
-import "dotenv-flow/config";
-import { syncTotalCommissions } from "../lib/api/partners/sync-total-commissions";
-
-async function main() {
- const programEnrollments = await prisma.programEnrollment.findMany({
- where: {
- totalCommissions: {
- gt: 0,
- },
- },
- });
-
- console.table(`Found ${programEnrollments.length} program enrollments`);
- const chunks = chunk(programEnrollments, 100);
- for (let i = 0; i < chunks.length; i++) {
- const chunk = chunks[i];
- const res = await Promise.allSettled(
- chunk.map(async (programEnrollment) => {
- return await syncTotalCommissions({
- partnerId: programEnrollment.partnerId,
- programId: programEnrollment.programId,
- });
- }),
- );
- const successCount = res.filter((r) => r.status === "fulfilled").length;
- const errorCount = res.filter((r) => r.status === "rejected").length;
- console.log(
- `Processed chunk ${i + 1} of ${chunks.length} (${successCount} successes, ${errorCount} errors)`,
- );
- // sleep for 500ms
- await new Promise((resolve) => setTimeout(resolve, 500));
- }
-}
-
-main();
diff --git a/apps/web/scripts/unban-links.ts b/apps/web/scripts/unban-links.ts
deleted file mode 100644
index fa506316ec..0000000000
--- a/apps/web/scripts/unban-links.ts
+++ /dev/null
@@ -1,42 +0,0 @@
-import { prisma } from "@dub/prisma";
-import { LEGAL_WORKSPACE_ID } from "@dub/utils";
-import "dotenv-flow/config";
-import { linkCache } from "../lib/api/links/cache";
-
-async function main() {
- const links = await prisma.link.findMany({
- where: {
- projectId: LEGAL_WORKSPACE_ID,
- url: {
- contains: "xxxxxxx",
- },
- domain: "dub.sh",
- },
- select: {
- id: true,
- domain: true,
- key: true,
- },
- take: 1000,
- });
-
- const redisRes = await linkCache.expireMany(links);
-
- const prismaRes = await prisma.link.updateMany({
- where: {
- id: {
- in: links.map((link) => link.id),
- },
- },
- data: {
- projectId: "xxx",
- userId: "xxx",
- },
- });
-
- console.table(links);
- console.table(redisRes);
- console.table(prismaRes);
-}
-
-main();
diff --git a/apps/web/scripts/update-integrations.ts b/apps/web/scripts/update-integrations.ts
deleted file mode 100644
index ee93673aba..0000000000
--- a/apps/web/scripts/update-integrations.ts
+++ /dev/null
@@ -1,30 +0,0 @@
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-
-async function main() {
- const urls = [
- {
- slug: "raycast",
- installUrl: "https://www.raycast.com/dubinc/dub",
- },
- {
- slug: "zapier",
- installUrl: "https://zapier.com/apps/dub/integrations",
- },
- ];
-
- for (const { slug, installUrl } of urls) {
- await prisma.integration.update({
- where: {
- slug,
- },
- data: {
- installUrl,
- },
- });
-
- console.log(`Updated ${slug} integration with install URL: ${installUrl}`);
- }
-}
-
-main();
diff --git a/apps/web/scripts/update-link-owner.ts b/apps/web/scripts/update-link-owner.ts
deleted file mode 100644
index f8ec80cba8..0000000000
--- a/apps/web/scripts/update-link-owner.ts
+++ /dev/null
@@ -1,38 +0,0 @@
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-
-const projectSlug = "xxx";
-
-async function main() {
- const owner = await prisma.projectUsers.findMany({
- where: {
- project: {
- slug: projectSlug,
- },
- role: "owner",
- },
- select: {
- user: {
- select: {
- id: true,
- },
- },
- },
- });
-
- const updateOwner = await prisma.link.updateMany({
- where: {
- project: {
- slug: projectSlug,
- },
- userId: null,
- },
- data: {
- userId: owner[0].user.id,
- },
- });
-
- console.log(owner, updateOwner);
-}
-
-main();
diff --git a/apps/web/scripts/update-not-found.ts b/apps/web/scripts/update-not-found.ts
deleted file mode 100644
index f3a8fa01d3..0000000000
--- a/apps/web/scripts/update-not-found.ts
+++ /dev/null
@@ -1,17 +0,0 @@
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-
-async function main() {
- const domains = await prisma.domain.updateMany({
- where: {
- placeholder: "https://dub.co/help/article/dub-links",
- },
- data: {
- placeholder: null,
- },
- });
-
- console.log(domains);
-}
-
-main();
diff --git a/apps/web/scripts/update-payment-failed.ts b/apps/web/scripts/update-payment-failed.ts
deleted file mode 100644
index 3ef7e2d677..0000000000
--- a/apps/web/scripts/update-payment-failed.ts
+++ /dev/null
@@ -1,48 +0,0 @@
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-import * as fs from "fs";
-import * as Papa from "papaparse";
-
-const stripeCustomers: { stripeId: string; paymentFailedAt: string }[] = [];
-
-async function main() {
- Papa.parse(fs.createReadStream("unified_payments.csv", "utf-8"), {
- header: true,
- step: (result: { data: any }) => {
- stripeCustomers.push({
- stripeId: result.data["Customer ID"],
- paymentFailedAt: result.data["Created date (UTC)"],
- });
- },
- complete: async () => {
- const validWorkspaces = await prisma.project.findMany({
- where: {
- stripeId: {
- in: stripeCustomers.map((c) => c.stripeId),
- },
- plan: {
- not: "free",
- },
- },
- });
-
- const workspaceToUpdate = validWorkspaces.map((w) => {
- const stripeCustomer = stripeCustomers.find(
- (c) => c.stripeId === w.stripeId,
- );
- return {
- id: w.id,
- slug: w.slug,
- plan: w.plan,
- stripeId: w.stripeId,
- paymentFailedAt: stripeCustomer?.paymentFailedAt,
- };
- });
-
- console.log({ stripeCustomers: stripeCustomers.length });
- console.table(workspaceToUpdate.slice(0, 10));
- },
- });
-}
-
-main();
diff --git a/apps/web/scripts/update-payouts-limits.ts b/apps/web/scripts/update-payouts-limits.ts
deleted file mode 100644
index 2ebceaf6a3..0000000000
--- a/apps/web/scripts/update-payouts-limits.ts
+++ /dev/null
@@ -1,33 +0,0 @@
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-import * as fs from "fs";
-import * as Papa from "papaparse";
-
-const stripeIds: string[] = [];
-
-async function main() {
- Papa.parse(fs.createReadStream("business_monthly.csv", "utf-8"), {
- header: true,
- step: (result: { data: any }) => {
- stripeIds.push(result.data["Customer ID"]);
- },
- complete: async () => {
- const res = await prisma.project.updateMany({
- where: {
- // plan: "business",
- stripeId: {
- in: stripeIds,
- },
- },
- data: {
- // payoutsLimit: 0,
- payoutsLimit: 2_500_00,
- },
- });
-
- console.log({ res });
- },
- });
-}
-
-main();
diff --git a/apps/web/scripts/update-spam-links.ts b/apps/web/scripts/update-spam-links.ts
deleted file mode 100644
index bbfd5a1e08..0000000000
--- a/apps/web/scripts/update-spam-links.ts
+++ /dev/null
@@ -1,20 +0,0 @@
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-
-async function main() {
- const updateOwner = await prisma.link.updateMany({
- where: {
- url: {
- contains: "evirtualservices.co",
- },
- userId: null,
- },
- data: {
- userId: "clqei1lgc0000vsnzi01pbf47",
- },
- });
-
- console.log(updateOwner);
-}
-
-main();
diff --git a/apps/web/scripts/update-subscribers.ts b/apps/web/scripts/update-subscribers.ts
deleted file mode 100644
index c1d69b4282..0000000000
--- a/apps/web/scripts/update-subscribers.ts
+++ /dev/null
@@ -1,34 +0,0 @@
-import { prisma } from "@dub/prisma";
-import "dotenv-flow/config";
-
-async function main() {
- const partnerUsers = await prisma.user.findMany({
- where: {
- partners: {
- some: {},
- },
- projects: {
- some: {},
- },
- },
- orderBy: {
- createdAt: "asc",
- },
- skip: 200,
- });
-
- console.log(`Found ${partnerUsers.length} partner users`);
-
- for (const user of partnerUsers) {
- if (!user.email) {
- console.log(`Skipping ${user.id} because they have no email`);
- continue;
- }
-
- console.log(
- `Subscribed ${user.email} to partners.dub.co and unsubscribed from app.dub.co`,
- );
- }
-}
-
-main();
diff --git a/apps/web/scripts/update-user-notifications.ts b/apps/web/scripts/update-user-notifications.ts
deleted file mode 100644
index 23abfd0c67..0000000000
--- a/apps/web/scripts/update-user-notifications.ts
+++ /dev/null
@@ -1,39 +0,0 @@
-import { prisma } from "@dub/prisma";
-
-import "dotenv-flow/config";
-
-async function main() {
- const workspaceUsers = await prisma.projectUsers.findMany({
- where: {
- user: {
- isMachine: false,
- },
- notificationPreference: null,
- },
- select: {
- id: true,
- },
- take: 1000,
- skip: 0,
- });
-
- if (!workspaceUsers) {
- return;
- }
-
- const result = await prisma.notificationPreference.createMany({
- data: workspaceUsers.map((workspaceUser) => {
- return {
- projectUserId: workspaceUser.id,
- };
- }),
- });
-
- console.log(
- "Created notification preferences for",
- result.count,
- "workspace users",
- );
-}
-
-main();