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.

fix bulk create links

Steven Tey31d470aa6a08
diff --git a/apps/web/app/api/links/bulk/route.ts b/apps/web/app/api/links/bulk/route.ts
index bc698f93e9..f02368c51b 100644
--- a/apps/web/app/api/links/bulk/route.ts
+++ b/apps/web/app/api/links/bulk/route.ts
@@ -73,6 +73,5 @@ export const POST = withAuth(
},
{
needNotExceededLinks: true,
- requiredPlan: ["pro", "business", "enterprise"],
},
);
diff --git a/apps/web/lib/api/links.ts b/apps/web/lib/api/links.ts
index 366ef18994..472176a215 100644
--- a/apps/web/lib/api/links.ts
+++ b/apps/web/lib/api/links.ts
@@ -595,10 +595,7 @@ export async function bulkCreateLinks({
linkTags.push(
...combinedTagIds.map((tagId) => ({ tagId, linkId: data.id })),
);
- return {
- ...data,
- tagIds: combinedTagIds,
- };
+ return data;
}),
)) as LinkWithTagIdsProps[];
}
@@ -648,6 +645,17 @@ export async function bulkCreateLinks({
data: linkTags.filter(({ tagId }) => validTagIds.includes(tagId)),
skipDuplicates: true,
}),
+ // update links usage
+ prisma.project.update({
+ where: {
+ id: createdLinks[0].projectId!, // this will always be present
+ },
+ data: {
+ linksUsage: {
+ increment: createdLinks.length,
+ },
+ },
+ }),
]);
return createdLinks.map((link) => {
diff --git a/apps/web/lib/auth/index.ts b/apps/web/lib/auth/index.ts
index da2466adae..d009498f61 100644
--- a/apps/web/lib/auth/index.ts
+++ b/apps/web/lib/auth/index.ts
@@ -367,7 +367,6 @@ export const withAuth =
// plan checks
if (!requiredPlan.includes(project.plan)) {
- // return res.status(403).end("Unauthorized: Need higher plan.");
return new Response("Unauthorized: Need higher plan.", {
status: 403,
headers,
diff --git a/apps/web/scripts/restore-backup.ts b/apps/web/scripts/restore-backup.ts
index cfa2003325..ad662c6eaa 100644
--- a/apps/web/scripts/restore-backup.ts
+++ b/apps/web/scripts/restore-backup.ts
@@ -1,62 +1,31 @@
import { bulkCreateLinks } from "@/lib/api/links";
+import prisma from "@/lib/prisma";
import { LinkWithTagIdsProps } from "@/lib/types";
import "dotenv-flow/config";
-import * as fs from "fs";
-import * as Papa from "papaparse";
+import { redis } from "@/lib/upstash";
const domain = "xxx";
-const csvLinks: LinkWithTagIdsProps[] = [];
async function main() {
- // const links = await prisma.link.findMany({
- // where: {
- // domain,
- // },
- // });
+ const restoredData = await redis.lrange<LinkWithTagIdsProps>(
+ "restoredData",
+ 0,
+ -1,
+ );
- // // save to csv
- // const csv = Papa.unparse(links, {
- // header: true,
- // });
- // fs.writeFileSync("backup-links.csv", csv);
-
- // read from csv
- Papa.parse(fs.createReadStream("backup-links.csv", "utf-8"), {
- header: true,
- skipEmptyLines: true,
- step: (result: { data: any }) => {
- csvLinks.push({
- ...result.data,
- archived: result.data.archived === "true",
- proxy: result.data.proxy === "true",
- rewrite: result.data.rewrite === "true",
- publicStats: result.data.publicStats === "true",
- checkDisabled: result.data.checkDisabled === "true",
- clicks: parseInt(result.data.clicks),
- lastChecked:
- result.data.lastChecked.length > 0 ? result.data.lastChecked : null,
- lastClicked:
- result.data.lastClicked.length > 0 ? result.data.lastClicked : null,
- tagIds: [],
- });
- },
- complete: async () => {
- console.log(csvLinks.length);
- console.table(csvLinks.slice(0, 10), [
- "id",
- "domain",
- "key",
- "url",
- "archived",
- "proxy",
- "rewrite",
- "publicStats",
- "checkDisabled",
- "clicks",
- ]);
- await bulkCreateLinks({ links: csvLinks });
- },
- });
+ 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();