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 ts errors

Steven Tey224bf188898f
diff --git a/apps/web/app/api/cron/import/csv/utils.ts b/apps/web/app/api/cron/import/csv/utils.ts
index cb44f32e8a..d39283efbb 100644
--- a/apps/web/app/api/cron/import/csv/utils.ts
+++ b/apps/web/app/api/cron/import/csv/utils.ts
@@ -1,7 +1,7 @@
-import { bulkCreateLinks, getRandomKey } from "@/lib/api/links";
+import { bulkCreateLinks, processLink } from "@/lib/api/links";
import { qstash } from "@/lib/cron";
import prisma from "@/lib/prisma";
-import { LinkProps } from "@/lib/types";
+import { LinkWithTagIdsProps, ProjectProps } from "@/lib/types";
import { redis } from "@/lib/upstash";
import { APP_DOMAIN_WITH_NGROK } from "@dub/utils";
import { sendEmail } from "emails";
@@ -19,20 +19,31 @@ export const importLinksFromCSV = async ({
count?: number;
}) => {
const [links, totalCount] = await Promise.all([
- redis.lrange<LinkProps>(`import:csv:${projectId}`, count, count + 100),
+ redis.lrange<LinkWithTagIdsProps>(
+ `import:csv:${projectId}`,
+ count,
+ count + 100,
+ ),
redis.llen(`import:csv:${projectId}`),
]);
+ const project = (await prisma.project.findUnique({
+ where: {
+ id: projectId,
+ },
+ })) as unknown as ProjectProps;
+
const processedLinks = await Promise.all(
- links.map(async (link) => ({
- ...link,
- key: link.key || (await getRandomKey(domain)),
- projectId,
- userId,
- })),
+ links.map(async (link) =>
+ processLink({ payload: link, project, userId, bulk: true }),
+ ),
);
- const importedLinks = await bulkCreateLinks({ links: processedLinks });
+ const validLinks = processedLinks
+ .filter(({ error }) => !error)
+ .map(({ link }) => link);
+
+ const importedLinks = await bulkCreateLinks({ links: validLinks });
count += importedLinks.length;
diff --git a/apps/web/app/api/links/bulk/route.ts b/apps/web/app/api/links/bulk/route.ts
index dae1ef71f0..bc698f93e9 100644
--- a/apps/web/app/api/links/bulk/route.ts
+++ b/apps/web/app/api/links/bulk/route.ts
@@ -46,7 +46,12 @@ export const POST = withAuth(
const processedLinks = await Promise.all(
links.map(async (link) =>
- processLink({ payload: link, project, session, bulk: true }),
+ processLink({
+ payload: link,
+ project,
+ userId: session.user.id,
+ bulk: true,
+ }),
),
);
diff --git a/apps/web/app/api/links/route.ts b/apps/web/app/api/links/route.ts
index e0578163bb..2c0bcea308 100644
--- a/apps/web/app/api/links/route.ts
+++ b/apps/web/app/api/links/route.ts
@@ -59,7 +59,7 @@ export const POST = withAuth(
const { link, error, status } = await processLink({
payload: body,
project,
- session,
+ userId: session.user.id,
});
if (error) {
diff --git a/apps/web/lib/api/links.ts b/apps/web/lib/api/links.ts
index 792481ad89..66e72a4585 100644
--- a/apps/web/lib/api/links.ts
+++ b/apps/web/lib/api/links.ts
@@ -20,7 +20,6 @@ import {
validKeyRegex,
} from "@dub/utils";
import cloudinary from "cloudinary";
-import { Session } from "../auth";
import { getLinkViaEdge } from "../planetscale";
import { recordLink } from "../tinybird";
import {
@@ -262,12 +261,12 @@ export function processKey(key: string) {
export async function processLink({
payload,
project,
- session,
+ userId,
bulk = false,
}: {
payload: LinkWithTagIdsProps;
project?: ProjectProps;
- session?: Session;
+ userId?: string;
bulk?: boolean;
}) {
let {
@@ -441,9 +440,9 @@ export async function processLink({
url: processedUrl,
// make sure projectId is set to the current project
projectId: project?.id || null,
- // if session is passed, set userId to the current user's id (we don't change the userId if it's already set, e.g. when editing a link)
- ...(session && {
- userId: session.user.id,
+ // if userId is passed, set it (we don't change the userId if it's already set, e.g. when editing a link)
+ ...(userId && {
+ userId,
}),
},
error: null,