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.

4.2 KB
Raw Blame
import { prisma } from "@/lib/prisma";
import { isNotHostedImage, storage } from "@/lib/storage";
import { bulkUpdateLinksBodySchema } from "@/lib/zod/schemas/links";
import { R2_URL, getParamsFromURL, nanoid, truncate } from "@dub/utils";
import { Prisma } from "@prisma/client";
import { waitUntil } from "@vercel/functions";
import * as z from "zod/v4";
import { combineTagIds } from "../tags/combine-tag-ids";
import { includeTags } from "./include-tags";
import { propagateBulkLinkChanges } from "./propagate-bulk-link-changes";
import { transformLink } from "./utils";

export async function bulkUpdateLinks(
  // omit externalIds from params
  params: Omit<z.infer<typeof bulkUpdateLinksBodySchema>, "externalIds"> & {
    workspaceId: string;
  },
) {
  const { linkIds, data, workspaceId } = params;

  const {
    url,
    title,
    description,
    image,
    proxy,
    expiresAt,
    geo,
    testVariants,
    tagId,
    tagIds,
    tagNames,
    webhookIds,
    ...rest
  } = data;

  const combinedTagIds = combineTagIds({ tagId, tagIds });

  const imageUrlNonce = nanoid(7);

  const { utm_source, utm_medium, utm_campaign, utm_term, utm_content } =
    getParamsFromURL(url || "");

  const updatedLinks = await Promise.all(
    linkIds.map((linkId) =>
      prisma.link.update({
        where: {
          id: linkId,
        },
        data: {
          ...rest,
          url,
          proxy,
          title: title ? truncate(title, 120) : title,
          description: description ? truncate(description, 240) : description,
          image:
            proxy && image && isNotHostedImage(image)
              ? `${R2_URL}/images/${linkIds[0]}_${imageUrlNonce}`
              : image,
          expiresAt: expiresAt ? new Date(expiresAt) : expiresAt,
          geo: geo === null ? Prisma.DbNull : geo,
          testVariants: testVariants === null ? Prisma.DbNull : testVariants,

          // Only persist known UTM Link columns — ignore other query params
          ...(url && {
            utm_source: utm_source || null,
            utm_medium: utm_medium || null,
            utm_campaign: utm_campaign || null,
            utm_term: utm_term || null,
            utm_content: utm_content || null,
          }),
          // Associate tags by tagNames
          ...(tagNames &&
            workspaceId && {
              tags: {
                deleteMany: {},
                create: tagNames.map((tagName, idx) => ({
                  tag: {
                    connect: {
                      name_projectId: {
                        name: tagName,
                        projectId: workspaceId as string,
                      },
                    },
                  },
                  createdAt: new Date(new Date().getTime() + idx * 100), // increment by 100ms for correct order
                })),
              },
            }),

          // Associate tags by IDs (takes priority over tagNames)
          ...(combinedTagIds && {
            tags: {
              deleteMany: {},
              create: combinedTagIds.map((tagId, idx) => ({
                tagId,
                createdAt: new Date(new Date().getTime() + idx * 100), // increment by 100ms for correct order
              })),
            },
          }),

          // Associate webhooks
          ...(webhookIds && {
            webhooks: {
              deleteMany: {},
              create: webhookIds.map((webhookId) => ({
                webhookId,
              })),
            },
          }),
        },
        include: {
          ...includeTags,
          webhooks: webhookIds ? { select: { webhookId: true } } : false,
        },
      }),
    ),
  );

  waitUntil(
    Promise.all([
      // propagate changes to redis and tinybird
      propagateBulkLinkChanges({
        links: updatedLinks,
      }),
      // if proxy is true and image is not stored in R2, upload image to R2
      proxy &&
        image &&
        isNotHostedImage(image) &&
        storage.upload({
          key: `images/${linkIds[0]}_${imageUrlNonce}`,
          body: image,
          opts: {
            width: 1200,
            height: 630,
          },
        }),
    ]),
  );

  return updatedLinks.map((link) => transformLink(link));
}