From 1ba28ece8b21881768bf143c5af102d23bed941c Mon Sep 17 00:00:00 2001 From: Mike Cao Date: Thu, 6 Feb 2025 11:22:00 -0800 Subject: [PATCH] Fixed queries again. --- src/app/api/reports/goals/route.ts | 2 +- src/app/api/reports/revenue/route.ts | 4 ++-- src/app/api/users/[userId]/usage/route.ts | 4 ++-- .../websites/[websiteId]/event-data/events/route.ts | 2 +- src/app/api/websites/[websiteId]/metrics/route.ts | 8 +++++++- .../[websiteId]/session-data/values/route.ts | 2 +- src/components/hooks/useApi.ts | 12 ++++++------ src/lib/fetch.ts | 13 ++++++------- 8 files changed, 26 insertions(+), 21 deletions(-) diff --git a/src/app/api/reports/goals/route.ts b/src/app/api/reports/goals/route.ts index b419c569..5a2f6bd0 100644 --- a/src/app/api/reports/goals/route.ts +++ b/src/app/api/reports/goals/route.ts @@ -2,7 +2,7 @@ import { z } from 'zod'; import { canViewWebsite } from '@/lib/auth'; import { unauthorized, json } from '@/lib/response'; import { parseRequest } from '@/lib/request'; -import { getGoals } from '@/queries/analytics/reports/getGoals'; +import { getGoals } from '@/queries/sql/reports/getGoals'; import { reportParms } from '@/lib/schema'; export async function POST(request: Request) { diff --git a/src/app/api/reports/revenue/route.ts b/src/app/api/reports/revenue/route.ts index db7d036b..f8f4041f 100644 --- a/src/app/api/reports/revenue/route.ts +++ b/src/app/api/reports/revenue/route.ts @@ -3,8 +3,8 @@ import { canViewWebsite } from '@/lib/auth'; import { unauthorized, json } from '@/lib/response'; import { parseRequest } from '@/lib/request'; import { reportParms, timezoneParam } from '@/lib/schema'; -import { getRevenue } from '@/queries/analytics/reports/getRevenue'; -import { getRevenueValues } from '@/queries/analytics/reports/getRevenueValues'; +import { getRevenue } from '@/queries/sql/reports/getRevenue'; +import { getRevenueValues } from '@/queries/sql/reports/getRevenueValues'; export async function GET(request: Request) { const { auth, query, error } = await parseRequest(request); diff --git a/src/app/api/users/[userId]/usage/route.ts b/src/app/api/users/[userId]/usage/route.ts index 168ae42e..e6ff217d 100644 --- a/src/app/api/users/[userId]/usage/route.ts +++ b/src/app/api/users/[userId]/usage/route.ts @@ -1,8 +1,8 @@ import { z } from 'zod'; import { json, unauthorized } from '@/lib/response'; import { getAllUserWebsitesIncludingTeamOwner } from '@/queries/prisma/website'; -import { getEventUsage } from '@/queries/analytics/events/getEventUsage'; -import { getEventDataUsage } from '@/queries/analytics/events/getEventDataUsage'; +import { getEventUsage } from '@/queries/sql/events/getEventUsage'; +import { getEventDataUsage } from '@/queries/sql/events/getEventDataUsage'; import { parseRequest } from '@/lib/request'; export async function GET(request: Request, { params }: { params: Promise<{ userId: string }> }) { diff --git a/src/app/api/websites/[websiteId]/event-data/events/route.ts b/src/app/api/websites/[websiteId]/event-data/events/route.ts index 3e32aa83..aec7b471 100644 --- a/src/app/api/websites/[websiteId]/event-data/events/route.ts +++ b/src/app/api/websites/[websiteId]/event-data/events/route.ts @@ -2,7 +2,7 @@ import { z } from 'zod'; import { parseRequest } from '@/lib/request'; import { unauthorized, json } from '@/lib/response'; import { canViewWebsite } from '@/lib/auth'; -import { getEventDataEvents } from '@/queries/analytics/events/getEventDataEvents'; +import { getEventDataEvents } from '@/queries/sql/events/getEventDataEvents'; export async function GET( request: Request, diff --git a/src/app/api/websites/[websiteId]/metrics/route.ts b/src/app/api/websites/[websiteId]/metrics/route.ts index 17c8676f..50bfc387 100644 --- a/src/app/api/websites/[websiteId]/metrics/route.ts +++ b/src/app/api/websites/[websiteId]/metrics/route.ts @@ -3,7 +3,7 @@ import { canViewWebsite } from '@/lib/auth'; import { SESSION_COLUMNS, EVENT_COLUMNS, FILTER_COLUMNS, OPERATORS } from '@/lib/constants'; import { getRequestFilters, getRequestDateRange, parseRequest } from '@/lib/request'; import { json, unauthorized, badRequest } from '@/lib/response'; -import { getPageviewMetrics, getSessionMetrics } from '@/queries'; +import { getPageviewMetrics, getSessionMetrics, getChannelMetrics } from '@/queries'; import { filterParams } from '@/lib/schema'; export async function GET( @@ -78,5 +78,11 @@ export async function GET( return json(data); } + if (type === 'channel') { + const data = await getChannelMetrics(websiteId, filters); + + return json(data); + } + return badRequest(); } diff --git a/src/app/api/websites/[websiteId]/session-data/values/route.ts b/src/app/api/websites/[websiteId]/session-data/values/route.ts index 1c12f00d..93e91775 100644 --- a/src/app/api/websites/[websiteId]/session-data/values/route.ts +++ b/src/app/api/websites/[websiteId]/session-data/values/route.ts @@ -2,7 +2,7 @@ import { z } from 'zod'; import { parseRequest } from '@/lib/request'; import { unauthorized, json } from '@/lib/response'; import { canViewWebsite } from '@/lib/auth'; -import { getEventDataEvents } from '@/queries/analytics/events/getEventDataEvents'; +import { getEventDataEvents } from '@/queries/sql/events/getEventDataEvents'; export async function GET( request: Request, diff --git a/src/components/hooks/useApi.ts b/src/components/hooks/useApi.ts index 7684f4e4..be386a29 100644 --- a/src/components/hooks/useApi.ts +++ b/src/components/hooks/useApi.ts @@ -27,8 +27,8 @@ export function useApi() { }; const basePath = process.env.basePath; - const getUrl = (url: string, basePath = '') => { - return url.startsWith('http') ? url : `${basePath}/api${url}`; + const getUrl = (url: string) => { + return url.startsWith('http') ? url : `${basePath || ''}/api${url}`; }; const getHeaders = (headers: any = {}) => { @@ -38,7 +38,7 @@ export function useApi() { return { get: useCallback( async (url: string, params: object = {}, headers: object = {}) => { - return httpGet(getUrl(url, basePath), params, getHeaders(headers)) + return httpGet(getUrl(url), params, getHeaders(headers)) .then(handleResponse) .catch(handleError); }, @@ -47,7 +47,7 @@ export function useApi() { post: useCallback( async (url: string, params: object = {}, headers: object = {}) => { - return httpPost(getUrl(url, basePath), params, getHeaders(headers)) + return httpPost(getUrl(url), params, getHeaders(headers)) .then(handleResponse) .catch(handleError); }, @@ -56,7 +56,7 @@ export function useApi() { put: useCallback( async (url: string, params: object = {}, headers: object = {}) => { - return httpPut(getUrl(url, basePath), params, getHeaders(headers)) + return httpPut(getUrl(url), params, getHeaders(headers)) .then(handleResponse) .catch(handleError); }, @@ -65,7 +65,7 @@ export function useApi() { del: useCallback( async (url: string, params: object = {}, headers: object = {}) => { - return httpDelete(getUrl(url, basePath), params, getHeaders(headers)) + return httpDelete(getUrl(url), params, getHeaders(headers)) .then(handleResponse) .catch(handleError); }, diff --git a/src/lib/fetch.ts b/src/lib/fetch.ts index e5068b29..754da56f 100644 --- a/src/lib/fetch.ts +++ b/src/lib/fetch.ts @@ -1,7 +1,7 @@ import { buildUrl } from '@/lib/url'; export async function request(method: string, url: string, body?: string, headers: object = {}) { - const res = await fetch(url, { + return fetch(url, { method, cache: 'no-cache', headers: { @@ -10,22 +10,21 @@ export async function request(method: string, url: string, body?: string, header ...headers, }, body, - }); - return res.json(); + }).then(res => res.json()); } -export function httpGet(url: string, params: object = {}, headers: object = {}) { +export async function httpGet(url: string, params: object = {}, headers: object = {}) { return request('GET', buildUrl(url, params), undefined, headers); } -export function httpDelete(url: string, params: object = {}, headers: object = {}) { +export async function httpDelete(url: string, params: object = {}, headers: object = {}) { return request('DELETE', buildUrl(url, params), undefined, headers); } -export function httpPost(url: string, params: object = {}, headers: object = {}) { +export async function httpPost(url: string, params: object = {}, headers: object = {}) { return request('POST', url, JSON.stringify(params), headers); } -export function httpPut(url: string, params: object = {}, headers: object = {}) { +export async function httpPut(url: string, params: object = {}, headers: object = {}) { return request('PUT', url, JSON.stringify(params), headers); }