Skip to content

🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.

refactor(api): remove deprecated site retrieval endpoint and related hooks

Bill Yangb0296ba6ce68
diff --git a/client/src/api/admin/sites.ts b/client/src/api/admin/sites.ts
index b26edd240..37de285ed 100644
--- a/client/src/api/admin/sites.ts
+++ b/client/src/api/admin/sites.ts
@@ -70,16 +70,6 @@ export type GetSitesFromOrgResponse = {
};
};
-export function useGetSites() {
- return useQuery<GetSitesResponse>({
- queryKey: ["get-sites"],
- queryFn: () => {
- return authedFetchWithError(`${BACKEND_URL}/get-sites`);
- },
- staleTime: Infinity,
- });
-}
-
export function useGetSitesFromOrg(organizationId?: string) {
return useQuery<GetSitesFromOrgResponse>({
queryKey: ["get-sites-from-org", organizationId],
@@ -93,10 +83,6 @@ export function useGetSitesFromOrg(organizationId?: string) {
});
}
-export async function getSites() {
- return authedFetchWithError<GetSitesResponse>(`${BACKEND_URL}/get-sites`);
-}
-
export function addSite(
domain: string,
name: string,
diff --git a/server/src/api/sites/getSites.ts b/server/src/api/sites/getSites.ts
deleted file mode 100644
index c1bc82beb..000000000
--- a/server/src/api/sites/getSites.ts
+++ /dev/null
@@ -1,118 +0,0 @@
-import { and, eq } from "drizzle-orm";
-import { FastifyReply, FastifyRequest } from "fastify";
-import clickhouse from "../../db/clickhouse/clickhouse.js";
-import { db } from "../../db/postgres/postgres.js";
-import { member } from "../../db/postgres/schema.js";
-import {
- getSitesUserHasAccessTo,
- getUserGodMode,
-} from "../../lib/auth-utils.js";
-import { IS_CLOUD, DEFAULT_EVENT_LIMIT } from "../../lib/const.js";
-import { processResults } from "../analytics/utils.js";
-import { getSubscriptionInner } from "../stripe/getSubscription.js";
-
-export async function getSites(req: FastifyRequest, reply: FastifyReply) {
- try {
- // Get sites the user has access to
- const sitesData = await getSitesUserHasAccessTo(req);
- const godMode = await getUserGodMode(req);
-
- // Only query for session counts if there are sites
- const sessionCountMap = new Map<number, number>();
-
- if (sitesData.length > 0) {
- // Extract site IDs that the user has access to
- const siteIds = sitesData.map((site) => site.siteId);
-
- // Query session counts only for the user's sites
- const sessionCountsResult = await clickhouse.query({
- query: `
- SELECT
- site_id,
- uniqExact(session_id) AS total_sessions
- FROM events
- WHERE timestamp >= now() - INTERVAL 1 DAY
- AND site_id IN (${siteIds.join(",")})
- GROUP BY site_id
- `,
- format: "JSONEachRow",
- });
-
- const sessionCounts = await processResults(sessionCountsResult);
-
- // Add session counts to map
- if (Array.isArray(sessionCounts)) {
- sessionCounts.forEach((row: any) => {
- if (
- row &&
- typeof row.site_id === "number" &&
- typeof row.total_sessions === "number"
- ) {
- sessionCountMap.set(Number(row.site_id), row.total_sessions);
- }
- });
- }
- }
-
- const enhancedSitesData = await Promise.all(
- sitesData.map(async (site) => {
- let isOwner = false;
- let ownerId = "";
-
- if (!IS_CLOUD) {
- return {
- ...site,
- monthlyEventCount: 0,
- eventLimit: Infinity,
- overMonthlyLimit: false,
- isOwner: true,
- sessionsLast24Hours: sessionCountMap.get(Number(site.siteId)) || 0,
- };
- }
- // Determine ownership if organization ID exists
- if (site.organizationId) {
- const orgOwnerResult = await db
- .select({ userId: member.userId })
- .from(member)
- .where(
- and(
- eq(member.organizationId, site.organizationId),
- eq(member.role, "owner")
- )
- )
- .limit(1);
-
- if (orgOwnerResult.length > 0) {
- ownerId = orgOwnerResult[0].userId;
- isOwner = ownerId === req.user?.id;
- }
- }
-
- const subscription = await getSubscriptionInner(site.organizationId);
-
- const monthlyEventCount = subscription?.monthlyEventCount || 0;
- const eventLimit = subscription?.eventLimit || DEFAULT_EVENT_LIMIT;
-
- return {
- ...site,
- monthlyEventCount,
- eventLimit,
- overMonthlyLimit: monthlyEventCount > eventLimit,
- isOwner,
- sessionsLast24Hours: sessionCountMap.get(site.siteId) || 0,
- };
- })
- );
-
- return reply
- .status(200)
- .send(
- enhancedSitesData.sort(
- (a, b) => b.sessionsLast24Hours - a.sessionsLast24Hours
- )
- );
- } catch (err) {
- console.error("Error in getSites:", err);
- return reply.status(500).send(String(err));
- }
-}
diff --git a/server/src/index.ts b/server/src/index.ts
index 3ef51bd24..e797b7ffe 100644
--- a/server/src/index.ts
+++ b/server/src/index.ts
@@ -43,7 +43,6 @@ import { deleteSite } from "./api/sites/deleteSite.js";
import { getSite } from "./api/sites/getSite.js";
import { getSiteHasData } from "./api/sites/getSiteHasData.js";
import { getSiteIsPublic } from "./api/sites/getSiteIsPublic.js";
-import { getSites } from "./api/sites/getSites.js";
import { getSitesFromOrg } from "./api/sites/getSitesFromOrg.js";
import { createCheckoutSession } from "./api/stripe/createCheckoutSession.js";
import { createPortalSession } from "./api/stripe/createPortalSession.js";
@@ -239,7 +238,6 @@ server.post("/change-site-public", changeSitePublic);
server.post("/change-site-salt", changeSiteSalt);
server.post("/change-site-block-bots", changeSiteBlockBots);
server.post("/delete-site/:id", deleteSite);
-server.get("/get-sites", getSites);
server.get("/get-sites-from-org/:organizationId", getSitesFromOrg);
server.get("/get-site/:id", getSite);
server.get(