Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Update link creation to support multiple tags

Tim Wilson5bcefb47fc2b
diff --git a/apps/web/lib/api/links.ts b/apps/web/lib/api/links.ts
index d7fe0943ff..264bb5bf97 100644
--- a/apps/web/lib/api/links.ts
+++ b/apps/web/lib/api/links.ts
@@ -20,7 +20,12 @@ import {
validKeyRegex,
} from "@dub/utils";
import cloudinary from "cloudinary";
-import { LinkProps, ProjectProps, RedisLinkProps } from "../types";
+import {
+ LinkProps,
+ LinkWithTagIdsProps,
+ ProjectProps,
+ RedisLinkProps,
+} from "../types";
import { Session } from "../auth";
import { getLinkViaEdge } from "../planetscale";
@@ -105,7 +110,7 @@ export async function getLinksCount({
showArchived?: boolean;
};
- const combinedTagIds = (tagIds ?? []).concat(tagId ?? []);
+ const combinedTagIds = combineTagIds({ tagId, tagIds });
const linksWhere = {
archived: showArchived ? undefined : false,
@@ -231,7 +236,7 @@ export async function processLink({
session,
bulk = false,
}: {
- payload: LinkProps;
+ payload: LinkWithTagIdsProps;
project?: ProjectProps;
session?: Session;
bulk?: boolean;
@@ -395,6 +400,25 @@ export async function processLink({
}
}
+ const combinedTagIds = combineTagIds(payload);
+ if (combinedTagIds.length) {
+ const validTagsCount = await prisma.tag.count({
+ where: {
+ id: {
+ in: combinedTagIds,
+ },
+ projectId: payload.projectId ?? undefined,
+ },
+ });
+ if (validTagsCount !== combinedTagIds.length) {
+ return {
+ link: payload,
+ error: "Invalid tags.",
+ status: 422,
+ };
+ }
+ }
+
// remove shortLink & qrCode attributes from payload since it's a polyfill
delete payload["shortLink"];
delete payload["qrCode"];
@@ -417,11 +441,24 @@ export async function processLink({
};
}
-export async function addLink(link: LinkProps) {
- const { domain, key, url, expiresAt, title, description, image, proxy, geo } =
- link;
+export async function addLink(link: LinkWithTagIdsProps) {
+ const {
+ domain,
+ key,
+ url,
+ expiresAt,
+ title,
+ description,
+ image,
+ proxy,
+ geo,
+ tagId,
+ tagIds,
+ } = link;
const uploadedImage = image && image.startsWith("data:image") ? true : false;
+ const combinedTagIds = combineTagIds({ tagId, tagIds });
+
const exists = await checkIfKeyExists(domain, key);
if (exists) return null;
@@ -442,6 +479,13 @@ export async function addLink(link: LinkProps) {
utm_content,
expiresAt: expiresAt ? new Date(expiresAt) : null,
geo: geo || undefined,
+ ...(combinedTagIds?.length && {
+ tags: {
+ createMany: {
+ data: combinedTagIds.map((tagId) => ({ tagId })),
+ },
+ },
+ }),
},
});
@@ -472,11 +516,12 @@ export async function addLink(link: LinkProps) {
};
}
-export async function bulkCreateLinks(links: LinkProps[]) {
+export async function bulkCreateLinks(links: LinkWithTagIdsProps[]) {
if (links.length === 0) return [];
await prisma.link.createMany({
- data: links.map((link) => {
+ data: links.map(({ tagId, tagIds, ...link }) => {
+ const combinedTagIds = combineTagIds({ tagId, tagIds });
const { utm_source, utm_medium, utm_campaign, utm_term, utm_content } =
getParamsFromURL(link.url);
return {
@@ -490,6 +535,13 @@ export async function bulkCreateLinks(links: LinkProps[]) {
utm_content,
expiresAt: link.expiresAt ? new Date(link.expiresAt) : null,
geo: link.geo || undefined,
+ ...(combinedTagIds?.length && {
+ tags: {
+ createMany: {
+ data: combinedTagIds.map((tagId) => ({ tagId })),
+ },
+ },
+ }),
};
}),
skipDuplicates: true,
@@ -706,3 +758,13 @@ export async function archiveLink(
},
});
}
+
+function combineTagIds({
+ tagId,
+ tagIds,
+}: {
+ tagId?: string | null;
+ tagIds?: string[];
+}) {
+ return (tagIds ?? []).concat(tagId ?? []);
+}
diff --git a/apps/web/lib/types.ts b/apps/web/lib/types.ts
index 2287375900..781ea30878 100644
--- a/apps/web/lib/types.ts
+++ b/apps/web/lib/types.ts
@@ -1,6 +1,12 @@
import { DirectorySyncProviders } from "@boxyhq/saml-jackson";
+import { Link } from "@prisma/client";
+
+export type LinkProps = Link;
+
+export interface LinkWithTagIdsProps extends LinkProps {
+ tagIds?: string[];
+}
-export { type Link as LinkProps } from "@prisma/client";
export interface SimpleLinkProps {
domain: string;
key: string;