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.

some more changes

Steven Teyf4f91dd0aa08
diff --git a/apps/web/app/api/edge/stats/[endpoint]/route.ts b/apps/web/app/api/edge/stats/[endpoint]/route.ts
index 7386cb8568..5f1dffc819 100644
--- a/apps/web/app/api/edge/stats/[endpoint]/route.ts
+++ b/apps/web/app/api/edge/stats/[endpoint]/route.ts
@@ -1,11 +1,12 @@
import { DEMO_LINK_ID, DUB_PROJECT_ID, getSearchParams } from "@dub/utils";
import { isBlacklistedReferrer } from "@/lib/edge-config";
-import { getLinkViaEdge } from "@/lib/planetscale";
+import { getLinkViaEdge, getProjectViaEdge } from "@/lib/planetscale";
import { getStats } from "@/lib/stats";
import { ratelimit } from "@/lib/upstash";
import { LOCALHOST_IP } from "@dub/utils";
import { ipAddress } from "@vercel/edge";
import { NextResponse, type NextRequest } from "next/server";
+import { exceededLimitError } from "@/lib/api/errors";
export const runtime = "edge";
@@ -49,9 +50,23 @@ export const GET = async (
status: 403,
});
}
- // return 403 if interval is 90d or all
- if (interval === "all" || interval === "90d") {
- return new Response(`Require higher plan`, { status: 403 });
+ const project =
+ link?.projectId && (await getProjectViaEdge(link.projectId));
+ if (
+ !project ||
+ (project.plan === "free" && (interval === "all" || interval === "90d"))
+ ) {
+ return new Response("Unauthorized: Need higher plan.", { status: 403 });
+ }
+ if (project.usage > project.usageLimit) {
+ return new Response(
+ exceededLimitError({
+ plan: project.plan,
+ limit: project.usageLimit,
+ type: "clicks",
+ }),
+ { status: 403 },
+ );
}
}
diff --git a/apps/web/lib/planetscale.ts b/apps/web/lib/planetscale.ts
index c6bd17c015..80af070edf 100644
--- a/apps/web/lib/planetscale.ts
+++ b/apps/web/lib/planetscale.ts
@@ -1,5 +1,5 @@
import { connect } from "@planetscale/database";
-import { DomainProps } from "./types";
+import { DomainProps, ProjectProps } from "./types";
export const pscale_config = {
url: process.env.DATABASE_URL,
@@ -48,3 +48,15 @@ export const getDomainViaEdge = async (domain: string) => {
? (rows[0] as DomainProps)
: null;
};
+
+export const getProjectViaEdge = async (projectId: string) => {
+ if (!process.env.DATABASE_URL) return null;
+
+ const { rows } =
+ (await conn.execute("SELECT * FROM Project WHERE id = ?", [projectId])) ||
+ {};
+
+ return rows && Array.isArray(rows) && rows.length > 0
+ ? (rows[0] as ProjectProps)
+ : null;
+};
diff --git a/apps/web/ui/stats/toggle.tsx b/apps/web/ui/stats/toggle.tsx
index 8b92902468..c6c2519bf1 100644
--- a/apps/web/ui/stats/toggle.tsx
+++ b/apps/web/ui/stats/toggle.tsx
@@ -50,8 +50,7 @@ export default function Toggle() {
const scrolled = useScroll(80);
const { name, plan, logo } = useProject();
- // TODO: change this to allDomains after #545 merges
- const { allProjectDomains, primaryDomain } = useDomains();
+ const { allDomains, primaryDomain } = useDomains();
const isPublicStatsPage = basePath.startsWith("/stats");
@@ -89,9 +88,9 @@ export default function Toggle() {
<h2 className="text-lg font-semibold text-gray-800">
{primaryDomain}
</h2>
- {allProjectDomains && allProjectDomains.length > 1 && (
+ {allDomains && allDomains.length > 1 && (
<Tooltip
- content={<DomainsFilterTooltip domains={allProjectDomains} />}
+ content={<DomainsFilterTooltip domains={allDomains} />}
side="bottom"
>
<div className="group flex cursor-pointer items-center">
@@ -99,7 +98,7 @@ export default function Toggle() {
variant="gray"
className="border-gray-300 transition-all group-hover:bg-gray-200"
>
- +{allProjectDomains.length - 1}
+ +{allDomains.length - 1}
</Badge>
</div>
</Tooltip>