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.

applied same changes to deleteProject

Steven Tey538a8498dc41
diff --git a/components/home/pricing.tsx b/components/home/pricing.tsx
index 5a520ed622..b674d4fb5a 100644
--- a/components/home/pricing.tsx
+++ b/components/home/pricing.tsx
@@ -161,14 +161,17 @@ const Pricing = () => {
$
{plan === "Pro"
? period === "yearly"
- ? nFormatter(PRO_TIERS[tier].price.yearly.amount, 1)
+ ? nFormatter(
+ PRO_TIERS[tier].price.yearly.amount / 12,
+ 1,
+ )
: PRO_TIERS[tier].price.monthly.amount
: 0}
</p>
</div>
)}
<p className="text-gray-500">
- per {period === "yearly" ? "year" : "month"}
+ per {period === "yearly" ? "month, billed annually" : "month"}
</p>
</div>
<div className="flex h-20 items-center justify-center border-t border-b border-gray-200 bg-gray-50">
diff --git a/lib/api/links.ts b/lib/api/links.ts
index d3939e055a..663fd7a019 100644
--- a/lib/api/links.ts
+++ b/lib/api/links.ts
@@ -321,23 +321,3 @@ export async function archiveLink(
},
});
}
-
-export async function changeDomain(projectId: string, domain: string, newDomain: string) {
- const links = await prisma.link.findMany({
- where: {
- project: {
- id: projectId
- }
- }
- });
- const pipeline = redis.pipeline();
- links.forEach(({ key }) => {
- pipeline.rename(`${domain}:clicks:${key}`, `${newDomain}:clicks:${key}`);
- pipeline.rename(`${domain}:${key}`, `${newDomain}:${key}`);
- });
- try {
- return await pipeline.exec();
- } catch (e) {
- return null;
- }
-}
diff --git a/lib/api/projects.ts b/lib/api/projects.ts
new file mode 100644
index 0000000000..8f19da569e
--- /dev/null
+++ b/lib/api/projects.ts
@@ -0,0 +1,47 @@
+import { redis } from "@/lib/upstash";
+
+export async function changeDomainForLinks(
+ projectId: string,
+ domain: string,
+ newDomain: string,
+) {
+ const links = await prisma.link.findMany({
+ where: {
+ project: {
+ id: projectId,
+ },
+ },
+ });
+ const pipeline = redis.pipeline();
+ pipeline.rename(`${domain}:root:clicks`, `${newDomain}:root:clicks`);
+ links.forEach(({ key }) => {
+ pipeline.rename(`${domain}:${key}`, `${newDomain}:${key}`);
+ pipeline.rename(`${domain}:clicks:${key}`, `${newDomain}:clicks:${key}`);
+ });
+ try {
+ return await pipeline.exec();
+ } catch (e) {
+ return null;
+ }
+}
+
+export async function deleteProjectLinks(domain: string) {
+ const links = await prisma.link.findMany({
+ where: {
+ project: {
+ domain,
+ },
+ },
+ });
+ const pipeline = redis.pipeline();
+ pipeline.del(`${domain}:root:clicks`);
+ links.forEach(({ key }) => {
+ pipeline.del(`${domain}:${key}`);
+ pipeline.del(`${domain}:clicks:${key}`);
+ });
+ try {
+ return await pipeline.exec();
+ } catch (e) {
+ return null;
+ }
+}
diff --git a/lib/cron/domains.tsx b/lib/cron/domains.tsx
index 7535610b55..b830ba677d 100644
--- a/lib/cron/domains.tsx
+++ b/lib/cron/domains.tsx
@@ -4,7 +4,8 @@ import ProjectDeleted from "emails/ProjectDeleted";
import { log } from "@/lib/utils";
import { removeDomain } from "@/lib/domains";
import prisma from "@/lib/prisma";
-import { deleteProject, redis } from "@/lib/upstash";
+import { deleteProjectLinks } from "@/lib/api/projects";
+import { redis } from "@/lib/upstash";
export const handleDomainUpdates = async (
projectSlug: string,
@@ -52,7 +53,7 @@ export const handleDomainUpdates = async (
},
}),
removeDomain(domain),
- deleteProject(domain),
+ deleteProjectLinks(domain),
log(
`Domain *${domain}* has been invalid for > 30 days, deleting.`,
"cron",
diff --git a/lib/upstash.ts b/lib/upstash.ts
index b9d66bdada..469e7bb300 100644
--- a/lib/upstash.ts
+++ b/lib/upstash.ts
@@ -76,92 +76,9 @@ export async function recordClick(
);
}
-/**
- * Get the links associated with a project
- **/
-export async function getLinksForProject(
- domain: string,
- userId?: string,
-): Promise<LinkProps[]> {
- /*
- This function is used to get all links for a project.
-
- Only applicable for dub.sh:
- - If a username is provided, it will only return links for that user.
- Otherwise, it will return all links for the project.
- */
- const keys = await redis.zrange<string[]>(
- `${domain}:links:timestamps${userId ? `:${userId}` : ""}`,
- 0,
- -1,
- {
- rev: true,
- },
- );
- if (!keys || keys.length === 0) return []; // no links for this project
- const metadata = (await redis.hmget(`${domain}:links`, ...keys)) as {
- [key: string]: Omit<LinkProps, "key">;
- };
- const links = keys.map((key) => ({
- key,
- ...metadata[key],
- }));
- return links;
-}
-
-/**
- * Get the number of links that a project has
- **/
-export async function getLinkCountForProject(slug: string) {
- return await redis.zcard(`${slug}:links:timestamps`);
-}
-
export async function getLinkClicksCount(domain: string, key: string) {
const start = Date.now() - 2629746000; // 30 days ago
return (
(await redis.zcount(`${domain}:clicks:${key}`, start, Date.now())) || 0
);
}
-
-export async function changeDomain(domain: string, newHostname: string) {
- const keys = await redis.zrange<string[]>(
- `${domain}:links:timestamps`,
- 0,
- -1,
- );
- const pipeline = redis.pipeline();
- pipeline.rename(`${domain}:links`, `${newHostname}:links`);
- pipeline.rename(
- `${domain}:links:timestamps`,
- `${newHostname}:links:timestamps`,
- );
- pipeline.rename(`${domain}:root:clicks`, `${newHostname}:root:clicks`);
- keys.forEach((key) => {
- pipeline.rename(`${domain}:clicks:${key}`, `${newHostname}:clicks:${key}`);
- });
- try {
- return await pipeline.exec();
- } catch (e) {
- return null;
- }
-}
-
-export async function deleteProject(domain: string) {
- const keys = await redis.zrange<string[]>(
- `${domain}:links:timestamps`,
- 0,
- -1,
- );
- const pipeline = redis.pipeline();
- pipeline.del(`${domain}:links`);
- pipeline.del(`${domain}:links:timestamps`);
- pipeline.del(`${domain}:root:clicks`);
- keys.forEach((key) => {
- pipeline.del(`${domain}:clicks:${key}`);
- });
- try {
- return await pipeline.exec();
- } catch (e) {
- return null;
- }
-}
diff --git a/pages/api/projects/[slug]/domains/[domain]/index.ts b/pages/api/projects/[slug]/domains/[domain]/index.ts
index c8fd167f41..b4a768848c 100644
--- a/pages/api/projects/[slug]/domains/[domain]/index.ts
+++ b/pages/api/projects/[slug]/domains/[domain]/index.ts
@@ -3,10 +3,11 @@ import { withProjectAuth } from "@/lib/auth";
import { addDomain, removeDomain } from "@/lib/domains";
import prisma from "@/lib/prisma";
import { validDomainRegex } from "@/lib/utils";
-import { changeDomain } from "@/lib/api/links";
+import { changeDomainForLinks } from "@/lib/api/projects";
+import { ProjectProps } from "@/lib/types";
export default withProjectAuth(
- async (req: NextApiRequest, res: NextApiResponse) => {
+ async (req: NextApiRequest, res: NextApiResponse, project: ProjectProps) => {
// PUT /api/projects/[slug]/domains/[domain] edit a project's domain
if (req.method === "PUT") {
const { slug, domain } = req.query as { slug: string; domain: string }; // slug is the domain
@@ -22,13 +23,6 @@ export default withProjectAuth(
}
if (domain !== newDomain) {
- // make sure domain doesn't exist
- const project = await prisma.project.findUnique({
- where: {
- domain: newDomain,
- },
- select: { id: true, slug: true },
- });
if (project && project.slug !== slug) {
return res.status(400).json({ error: "Domain already exists" });
}
@@ -36,7 +30,6 @@ export default withProjectAuth(
await Promise.all([
removeDomain(domain),
addDomain(newDomain),
- changeDomain(project.id, domain, newDomain),
prisma.project.update({
where: {
slug,
@@ -46,6 +39,7 @@ export default withProjectAuth(
domainVerified: false,
},
}),
+ changeDomainForLinks(project.id, domain, newDomain),
]);
return res.status(200).json({
diff --git a/pages/api/projects/[slug]/index.ts b/pages/api/projects/[slug]/index.ts
index 6da7954b3c..cbcbfc4ed3 100644
--- a/pages/api/projects/[slug]/index.ts
+++ b/pages/api/projects/[slug]/index.ts
@@ -3,7 +3,7 @@ import { withProjectAuth } from "@/lib/auth";
import { removeDomain } from "@/lib/domains";
import prisma from "@/lib/prisma";
import { ProjectProps } from "@/lib/types";
-import { deleteProject } from "@/lib/upstash";
+import { deleteProjectLinks } from "@/lib/api/projects";
export default withProjectAuth(
async (req: NextApiRequest, res: NextApiResponse, project: ProjectProps) => {
@@ -34,7 +34,7 @@ export default withProjectAuth(
},
}),
removeDomain(domain),
- deleteProject(domain),
+ deleteProjectLinks(domain),
]);
return res