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.

add script

Steven Tey6f622998b9f8
diff --git a/apps/web/app/api/links/bulk/route.ts b/apps/web/app/api/links/bulk/route.ts
index 024be9e033..99ca1174f9 100644
--- a/apps/web/app/api/links/bulk/route.ts
+++ b/apps/web/app/api/links/bulk/route.ts
@@ -3,57 +3,56 @@ import { withAuth } from "@/lib/auth";
import { NextResponse } from "next/server";
// POST /api/links/bulk – bulk create up to 100 links
-export const POST = withAuth(async ({ req, headers, session, project }) => {
- if (!project) {
- return new Response(
- "Missing project. Bulk link creation is only available for custom domain projects.",
- { status: 400, headers },
- );
- }
- let links = [];
- try {
- links = await req.json();
- if (!Array.isArray(links)) {
- throw new Error("Invalid request body.");
+export const POST = withAuth(
+ async ({ req, headers, session, project }) => {
+ if (!project) {
+ return new Response(
+ "Missing project. Bulk link creation is only available for custom domain projects.",
+ { status: 400, headers },
+ );
+ }
+ let links = [];
+ try {
+ links = await req.json();
+ if (!Array.isArray(links)) {
+ throw new Error("Invalid request body.");
+ }
+ } catch (e) {
+ return new Response("Invalid request body.", { status: 400, headers });
+ }
+ if (links.length === 0) {
+ return new Response("No links created.", { status: 304, headers });
+ }
+ if (links.length > 100) {
+ return new Response("You can only create up to 100 links at a time.", {
+ status: 400,
+ headers,
+ });
}
- } catch (e) {
- return new Response("Invalid request body.", { status: 400, headers });
- }
- if (!project || project.plan === "free") {
- return new Response(
- "You can only use bulk link creation on a Pro plan and above.",
- { status: 403, headers },
- );
- }
- if (links.length === 0) {
- return new Response("No links created.", { status: 304, headers });
- }
- if (links.length > 100) {
- return new Response("You can only create up to 100 links at a time.", {
- status: 400,
- headers,
- });
- }
- const processedLinks = await Promise.all(
- links.map(async (link) =>
- processLink({ payload: link, project, session, bulk: true }),
- ),
- );
+ const processedLinks = await Promise.all(
+ links.map(async (link) =>
+ processLink({ payload: link, project, session, bulk: true }),
+ ),
+ );
- const validLinks = processedLinks
- .filter(({ error }) => !error)
- .map(({ link }) => link);
+ const validLinks = processedLinks
+ .filter(({ error }) => !error)
+ .map(({ link }) => link);
- const errors = processedLinks
- .filter(({ error }) => error)
- .map(({ link, error }) => ({
- link,
- error,
- }));
+ const errors = processedLinks
+ .filter(({ error }) => error)
+ .map(({ link, error }) => ({
+ link,
+ error,
+ }));
- const validLinksResponse =
- validLinks.length > 0 ? await bulkCreateLinks(validLinks) : [];
+ const validLinksResponse =
+ validLinks.length > 0 ? await bulkCreateLinks(validLinks) : [];
- return NextResponse.json([...validLinksResponse, ...errors]);
-});
+ return NextResponse.json([...validLinksResponse, ...errors]);
+ },
+ {
+ requiredPlan: ["pro", "enterprise"],
+ },
+);
diff --git a/apps/web/scripts/bulk-delete-links.ts b/apps/web/scripts/bulk-delete-links.ts
new file mode 100644
index 0000000000..56a1894d07
--- /dev/null
+++ b/apps/web/scripts/bulk-delete-links.ts
@@ -0,0 +1,29 @@
+import "dotenv-flow/config";
+import prisma from "@/lib/prisma";
+import { redis } from "./utils";
+
+async function main() {
+ const links = await prisma.link.findMany({
+ where: {
+ domain: "steven.blue",
+ },
+ });
+
+ const pipeline = redis.pipeline();
+ links.forEach(({ domain, key }) => {
+ pipeline.del(`${domain}:${key}`);
+ });
+
+ const response = await Promise.all([
+ pipeline.exec(),
+ prisma.link.deleteMany({
+ where: {
+ domain: "steven.blue",
+ },
+ }),
+ ]);
+
+ console.log(JSON.stringify(response, null, 2));
+}
+
+main();