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 utils

Kiran Ka4881da99074
diff --git a/apps/web/app/api/cron/domains/transfer/route.ts b/apps/web/app/api/cron/domains/transfer/route.ts
index c2ca3c9194..37757f7b81 100644
--- a/apps/web/app/api/cron/domains/transfer/route.ts
+++ b/apps/web/app/api/cron/domains/transfer/route.ts
@@ -1,13 +1,13 @@
-import { getAnalytics } from "@/lib/analytics";
import { qstash, receiver } from "@/lib/cron";
import prisma from "@/lib/prisma";
-import { formatRedisLink, redis } from "@/lib/upstash";
import z from "@/lib/zod";
import { APP_DOMAIN_WITH_NGROK, log } from "@dub/utils";
-import { Link } from "@prisma/client";
-import { sendEmail } from "emails";
-import DomainTransferred from "emails/domain-transferred";
import { NextResponse } from "next/server";
+import {
+ domainTransferredEmail,
+ updateLinksInRedis,
+ updateUsage,
+} from "./utils";
const schema = z.object({
currentWorkspaceId: z.string(),
@@ -66,48 +66,10 @@ export async function POST(req: Request) {
// No more links to transfer
if (hasMoreLinks === 0) {
- const currentWorkspace = await prisma.project.findUnique({
- where: {
- id: currentWorkspaceId,
- },
- select: {
- name: true,
- slug: true,
- users: {
- where: {
- role: "owner",
- },
- select: {
- user: {
- select: {
- email: true,
- },
- },
- },
- },
- },
- });
-
- const newWorkspace = await prisma.project.findUniqueOrThrow({
- where: {
- id: newWorkspaceId,
- },
- select: {
- name: true,
- slug: true,
- },
- });
-
- const ownerEmail = currentWorkspace?.users[0]?.user?.email!;
-
- sendEmail({
- subject: "Domain transfer completed",
- email: ownerEmail,
- react: DomainTransferred({
- email: ownerEmail,
- domain,
- newWorkspace,
- }),
+ await domainTransferredEmail({
+ domain,
+ currentWorkspaceId,
+ newWorkspaceId,
});
}
@@ -134,89 +96,3 @@ export async function POST(req: Request) {
return NextResponse.json({ error: error.message });
}
}
-
-// Update links in the redis
-const updateLinksInRedis = async ({
- newWorkspaceId,
- domain,
- links,
-}: {
- newWorkspaceId: string;
- domain: string;
- links: Link[];
-}) => {
- const pipeline = redis.pipeline();
-
- const formatedLinks = await Promise.all(
- links.map(async (link) => {
- return {
- ...(await formatRedisLink(link)),
- projectId: newWorkspaceId,
- key: link.key.toLowerCase(),
- };
- }),
- );
-
- formatedLinks.map((formatedLink) => {
- const { key, ...rest } = formatedLink;
-
- pipeline.hset(domain, {
- [formatedLink.key]: rest,
- });
- });
-
- await pipeline.exec();
-};
-
-// Update links & click usage
-const updateUsage = async ({
- currentWorkspaceId,
- newWorkspaceId,
- links,
-}: {
- currentWorkspaceId: string;
- newWorkspaceId: string;
- links: Link[];
-}) => {
- const linkClicks: number[] = await Promise.all(
- links.map((link) =>
- getAnalytics({
- linkId: link.id,
- endpoint: "clicks",
- interval: "30d",
- }),
- ),
- );
-
- const totalClicks = linkClicks.reduce((acc, curr) => acc + curr, 0);
- const linksCount = links.length;
-
- await Promise.all([
- prisma.project.update({
- where: {
- id: currentWorkspaceId,
- },
- data: {
- usage: {
- decrement: totalClicks,
- },
- linksUsage: {
- decrement: linksCount,
- },
- },
- }),
- prisma.project.update({
- where: {
- id: newWorkspaceId,
- },
- data: {
- usage: {
- increment: totalClicks,
- },
- linksUsage: {
- increment: linksCount,
- },
- },
- }),
- ]);
-};
diff --git a/apps/web/app/api/cron/domains/transfer/utils.ts b/apps/web/app/api/cron/domains/transfer/utils.ts
new file mode 100644
index 0000000000..3611c1dd89
--- /dev/null
+++ b/apps/web/app/api/cron/domains/transfer/utils.ts
@@ -0,0 +1,145 @@
+import { getAnalytics } from "@/lib/analytics";
+import prisma from "@/lib/prisma";
+import { formatRedisLink, redis } from "@/lib/upstash";
+import { Link } from "@prisma/client";
+import { sendEmail } from "emails";
+import DomainTransferred from "emails/domain-transferred";
+
+// Update links in the redis
+export const updateLinksInRedis = async ({
+ newWorkspaceId,
+ domain,
+ links,
+}: {
+ newWorkspaceId: string;
+ domain: string;
+ links: Link[];
+}) => {
+ const pipeline = redis.pipeline();
+
+ const formatedLinks = await Promise.all(
+ links.map(async (link) => {
+ return {
+ ...(await formatRedisLink(link)),
+ projectId: newWorkspaceId,
+ key: link.key.toLowerCase(),
+ };
+ }),
+ );
+
+ formatedLinks.map((formatedLink) => {
+ const { key, ...rest } = formatedLink;
+
+ pipeline.hset(domain, {
+ [formatedLink.key]: rest,
+ });
+ });
+
+ await pipeline.exec();
+};
+
+// Update links & click usage
+export const updateUsage = async ({
+ currentWorkspaceId,
+ newWorkspaceId,
+ links,
+}: {
+ currentWorkspaceId: string;
+ newWorkspaceId: string;
+ links: Link[];
+}) => {
+ const linkClicks: number[] = await Promise.all(
+ links.map((link) =>
+ getAnalytics({
+ linkId: link.id,
+ endpoint: "clicks",
+ interval: "30d",
+ }),
+ ),
+ );
+
+ const totalClicks = linkClicks.reduce((acc, curr) => acc + curr, 0);
+ const linksCount = links.length;
+
+ await Promise.all([
+ prisma.project.update({
+ where: {
+ id: currentWorkspaceId,
+ },
+ data: {
+ usage: {
+ decrement: totalClicks,
+ },
+ linksUsage: {
+ decrement: linksCount,
+ },
+ },
+ }),
+ prisma.project.update({
+ where: {
+ id: newWorkspaceId,
+ },
+ data: {
+ usage: {
+ increment: totalClicks,
+ },
+ linksUsage: {
+ increment: linksCount,
+ },
+ },
+ }),
+ ]);
+};
+
+// Send email to the owner after the domain transfer is completed
+export const domainTransferredEmail = async ({
+ domain,
+ currentWorkspaceId,
+ newWorkspaceId,
+}: {
+ domain: string;
+ currentWorkspaceId: string;
+ newWorkspaceId: string;
+}) => {
+ const currentWorkspace = await prisma.project.findUnique({
+ where: {
+ id: currentWorkspaceId,
+ },
+ select: {
+ users: {
+ where: {
+ role: "owner",
+ },
+ select: {
+ user: {
+ select: {
+ email: true,
+ },
+ },
+ },
+ },
+ },
+ });
+
+ const newWorkspace = await prisma.project.findUniqueOrThrow({
+ where: {
+ id: newWorkspaceId,
+ },
+ select: {
+ name: true,
+ slug: true,
+ },
+ });
+
+ const ownerEmail = currentWorkspace?.users[0]?.user?.email!;
+
+ sendEmail({
+ subject: "Domain transfer completed",
+ email: ownerEmail,
+ react: DomainTransferred({
+ email: ownerEmail,
+ domain,
+ newWorkspace,
+ }),
+ });
+};