Skip to content

armgit / umami

Public Imported from GitHub
0

Umami is a privacy-first web and product analytics platform. Traffic, campaigns, behavior, conversions, and revenue in one place. No cookies, no surveillance, self-hosted or in the cloud.

Renamed id routes for API.

Mike Cao442919839703
diff --git a/src/app/(main)/reports/ReportsTable.tsx b/src/app/(main)/reports/ReportsTable.tsx
index d82832718..09f8b3fc6 100644
--- a/src/app/(main)/reports/ReportsTable.tsx
+++ b/src/app/(main)/reports/ReportsTable.tsx
@@ -1,6 +1,6 @@
import { GridColumn, GridTable, Icon, Icons, Text, useBreakpoint } from 'react-basics';
import LinkButton from 'components/common/LinkButton';
-import { useMessages, useLogin } from 'components/hooks';
+import { useMessages, useLogin, useNavigation } from 'components/hooks';
import { REPORT_TYPES } from 'lib/constants';
import ReportDeleteButton from './ReportDeleteButton';
@@ -8,6 +8,7 @@ export function ReportsTable({ data = [], showDomain }: { data: any[]; showDomai
const { formatMessage, labels } = useMessages();
const { user } = useLogin();
const breakpoint = useBreakpoint();
+ const { renderTeamUrl } = useNavigation();
return (
<GridTable data={data} cardMode={['xs', 'sm', 'md'].includes(breakpoint)}>
@@ -33,7 +34,7 @@ export function ReportsTable({ data = [], showDomain }: { data: any[]; showDomai
{(user.id === userId || user.id === website?.userId) && (
<ReportDeleteButton reportId={id} reportName={name} />
)}
- <LinkButton href={`/reports/${id}`}>
+ <LinkButton href={renderTeamUrl(`/reports/${id}`)}>
<Icon>
<Icons.ArrowRight />
</Icon>
diff --git a/src/app/(main)/settings/teams/[teamId]/TeamWebsitesTable.tsx b/src/app/(main)/settings/teams/[teamId]/TeamWebsitesTable.tsx
index 1a819646b..2fe74b65a 100644
--- a/src/app/(main)/settings/teams/[teamId]/TeamWebsitesTable.tsx
+++ b/src/app/(main)/settings/teams/[teamId]/TeamWebsitesTable.tsx
@@ -17,7 +17,7 @@ export function TeamWebsitesTable({
<GridColumn name="domain" label={formatMessage(labels.domain)} />
<GridColumn name="action" label=" " alignment="end">
{row => {
- const { id: websiteId } = row;
+ const { websiteId } = row;
return (
<Link href={`/websites/${websiteId}`}>
<Button>
diff --git a/src/app/(main)/teams/[teamId]/reports/TeamReports.tsx b/src/app/(main)/teams/[teamId]/reports/TeamReports.tsx
index 0fbde593f..ee20aa183 100644
--- a/src/app/(main)/teams/[teamId]/reports/TeamReports.tsx
+++ b/src/app/(main)/teams/[teamId]/reports/TeamReports.tsx
@@ -1,16 +1,17 @@
'use client';
import Link from 'next/link';
import { Button, Flexbox, Icon, Icons, Text } from 'react-basics';
-import { useMessages } from 'components/hooks';
+import { useMessages, useNavigation } from 'components/hooks';
import ReportsDataTable from 'app/(main)/reports/ReportsDataTable';
export function TeamReports({ websiteId }) {
const { formatMessage, labels } = useMessages();
+ const { renderTeamUrl } = useNavigation();
return (
<>
<Flexbox alignItems="center" justifyContent="end">
- <Link href={`/reports/create`}>
+ <Link href={renderTeamUrl(`/reports/create`)}>
<Button variant="primary">
<Icon>
<Icons.Plus />
diff --git a/src/components/hooks/index.js b/src/components/hooks/index.ts
similarity index 100%
rename from src/components/hooks/index.js
rename to src/components/hooks/index.ts
diff --git a/src/components/hooks/queries/useWebsiteMetrics.ts b/src/components/hooks/queries/useWebsiteMetrics.ts
index c7a719b12..8e7d00db9 100644
--- a/src/components/hooks/queries/useWebsiteMetrics.ts
+++ b/src/components/hooks/queries/useWebsiteMetrics.ts
@@ -4,7 +4,7 @@ import { UseQueryOptions } from '@tanstack/react-query';
export function useWebsiteMetrics(
websiteId: string,
params?: { [key: string]: any },
- options?: Omit<UseQueryOptions, 'queryKey' | 'queryFn'>,
+ options?: Omit<UseQueryOptions & { onDataLoad?: (data: any) => void }, 'queryKey' | 'queryFn'>,
) {
const { get, useQuery } = useApi();
@@ -25,6 +25,8 @@ export function useWebsiteMetrics(
...filters,
});
+ options?.onDataLoad?.(data);
+
return data;
},
...options,
diff --git a/src/components/input/DateFilter.tsx b/src/components/input/DateFilter.tsx
index 1fabc735f..847a32184 100644
--- a/src/components/input/DateFilter.tsx
+++ b/src/components/input/DateFilter.tsx
@@ -11,10 +11,9 @@ export interface DateFilterProps {
value: string;
startDate: Date;
endDate: Date;
- offset: number;
+ offset?: number;
className?: string;
onChange?: (value: string) => void;
- selectedUnit?: string;
showAllTime?: boolean;
alignment?: 'start' | 'center' | 'end';
}
@@ -23,7 +22,7 @@ export function DateFilter({
startDate,
endDate,
value,
- offset,
+ offset = 0,
className,
onChange,
showAllTime = false,
diff --git a/src/components/metrics/MetricsTable.tsx b/src/components/metrics/MetricsTable.tsx
index 19d3e7fc2..6713c6299 100644
--- a/src/components/metrics/MetricsTable.tsx
+++ b/src/components/metrics/MetricsTable.tsx
@@ -1,19 +1,21 @@
import { ReactNode, useMemo, useState } from 'react';
import { Loading, Icon, Text, SearchField } from 'react-basics';
import classNames from 'classnames';
-import { percentFilter } from 'lib/filters';
-import { useDateRange } from 'components/hooks';
-import { useNavigation } from 'components/hooks';
import ErrorMessage from 'components/common/ErrorMessage';
import LinkButton from 'components/common/LinkButton';
-import ListTable, { ListTableProps } from './ListTable';
import { DEFAULT_ANIMATION_DURATION } from 'lib/constants';
+import { percentFilter } from 'lib/filters';
+import {
+ useDateRange,
+ useNavigation,
+ useWebsiteMetrics,
+ useMessages,
+ useLocale,
+ useFormat,
+} from 'components/hooks';
import Icons from 'components/icons';
-import { useMessages } from 'components/hooks';
-import { useLocale } from 'components/hooks';
-import useFormat from 'components//hooks/useFormat';
+import ListTable, { ListTableProps } from './ListTable';
import styles from './MetricsTable.module.css';
-import useWebsiteMetrics from 'components/hooks/queries/useWebsiteMetrics';
export interface MetricsTableProps extends ListTableProps {
websiteId: string;
@@ -67,7 +69,7 @@ export function MetricsTable({
region,
city,
},
- { retryDelay: delay || DEFAULT_ANIMATION_DURATION },
+ { retryDelay: delay || DEFAULT_ANIMATION_DURATION, onDataLoad },
);
const filteredData = useMemo(() => {
diff --git a/src/lib/query.ts b/src/lib/query.ts
index 88ce62d4c..b1dd5cbe1 100644
--- a/src/lib/query.ts
+++ b/src/lib/query.ts
@@ -3,7 +3,7 @@ import { getAllowedUnits, getMinimumUnit } from './date';
import { getWebsiteDateRange } from '../queries';
export async function parseDateRangeQuery(req: NextApiRequest) {
- const { id: websiteId, startAt, endAt, unit } = req.query;
+ const { websiteId, startAt, endAt, unit } = req.query;
// All-time
if (+startAt === 0 && +endAt === 1) {
diff --git a/src/pages/api/admin/users.ts b/src/pages/api/admin/users.ts
index 63f27727f..0eecb447d 100644
--- a/src/pages/api/admin/users.ts
+++ b/src/pages/api/admin/users.ts
@@ -9,9 +9,9 @@ import * as yup from 'yup';
export interface UsersRequestQuery extends SearchFilter {}
export interface UsersRequestBody {
+ userId: string;
username: string;
password: string;
- id: string;
role: Role;
}
@@ -20,9 +20,9 @@ const schema = {
...pageInfo,
}),
POST: yup.object().shape({
+ userId: yup.string().uuid(),
username: yup.string().max(255).required(),
password: yup.string().required(),
- id: yup.string().uuid(),
role: yup
.string()
.matches(/admin|user|view-only/i)
diff --git a/src/pages/api/event-data/stats.ts b/src/pages/api/event-data/stats.ts
index a12ad92f8..a2f9a1135 100644
--- a/src/pages/api/event-data/stats.ts
+++ b/src/pages/api/event-data/stats.ts
@@ -22,7 +22,7 @@ const schema = {
export default async (
req: NextApiRequestQueryBody<EventDataStatsRequestQuery>,
- res: NextApiResponse<any>,
+ res: NextApiResponse,
) => {
await useCors(req, res);
await useAuth(req, res);
diff --git a/src/pages/api/me/password.ts b/src/pages/api/me/password.ts
index 24cd25179..2ba91d863 100644
--- a/src/pages/api/me/password.ts
+++ b/src/pages/api/me/password.ts
@@ -12,10 +12,6 @@ import {
import { getUser, updateUser } from 'queries';
import * as yup from 'yup';
-export interface UserPasswordRequestQuery {
- id: string;
-}
-
export interface UserPasswordRequestBody {
currentPassword: string;
newPassword: string;
@@ -29,7 +25,7 @@ const schema = {
};
export default async (
- req: NextApiRequestQueryBody<UserPasswordRequestQuery, UserPasswordRequestBody>,
+ req: NextApiRequestQueryBody<any, UserPasswordRequestBody>,
res: NextApiResponse<User>,
) => {
if (process.env.CLOUD_MODE) {
@@ -40,10 +36,10 @@ export default async (
await useValidate(schema, req, res);
const { currentPassword, newPassword } = req.body;
- const { id } = req.auth.user;
+ const { id: userId } = req.auth.user;
if (req.method === 'POST') {
- const user = await getUser(id, { includePassword: true });
+ const user = await getUser(userId, { includePassword: true });
if (!checkPassword(currentPassword, user.password)) {
return badRequest(res, 'Current password is incorrect');
@@ -51,7 +47,7 @@ export default async (
const password = hashPassword(newPassword);
- const updated = await updateUser({ password }, { id });
+ const updated = await updateUser(userId, { password });
return ok(res, updated);
}
diff --git a/src/pages/api/me/teams.ts b/src/pages/api/me/teams.ts
index b0803277d..d068e0d3d 100644
--- a/src/pages/api/me/teams.ts
+++ b/src/pages/api/me/teams.ts
@@ -1,30 +1,23 @@
import { useCors, useValidate } from 'lib/middleware';
-import { NextApiRequestQueryBody, SearchFilter } from 'lib/types';
+import { NextApiRequestQueryBody } from 'lib/types';
import { pageInfo } from 'lib/schema';
import { NextApiResponse } from 'next';
import { methodNotAllowed } from 'next-basics';
-import userTeams from 'pages/api/users/[id]/teams';
+import userTeams from 'pages/api/users/[userId]/teams';
import * as yup from 'yup';
-export interface MyTeamsRequestQuery extends SearchFilter {
- id: string;
-}
-
const schema = {
GET: yup.object().shape({
...pageInfo,
}),
};
-export default async (
- req: NextApiRequestQueryBody<MyTeamsRequestQuery, any>,
- res: NextApiResponse,
-) => {
+export default async (req: NextApiRequestQueryBody, res: NextApiResponse) => {
await useCors(req, res);
await useValidate(schema, req, res);
if (req.method === 'GET') {
- req.query.id = req.auth.user.id;
+ req.query.userId = req.auth.user.id;
return userTeams(req, res);
}
diff --git a/src/pages/api/me/websites.ts b/src/pages/api/me/websites.ts
index e5a9ad328..7a1139910 100644
--- a/src/pages/api/me/websites.ts
+++ b/src/pages/api/me/websites.ts
@@ -1,31 +1,24 @@
import { useAuth, useCors, useValidate } from 'lib/middleware';
-import { NextApiRequestQueryBody, SearchFilter } from 'lib/types';
+import { NextApiRequestQueryBody } from 'lib/types';
import { pageInfo } from 'lib/schema';
import { NextApiResponse } from 'next';
import { methodNotAllowed } from 'next-basics';
-import userWebsites from 'pages/api/users/[id]/websites';
+import userWebsites from 'pages/api/users/[userId]/websites';
import * as yup from 'yup';
-export interface MyWebsitesRequestQuery extends SearchFilter {
- id: string;
-}
-
const schema = {
GET: yup.object().shape({
...pageInfo,
}),
};
-export default async (
- req: NextApiRequestQueryBody<MyWebsitesRequestQuery, any>,
- res: NextApiResponse,
-) => {
+export default async (req: NextApiRequestQueryBody, res: NextApiResponse) => {
await useCors(req, res);
await useAuth(req, res);
await useValidate(schema, req, res);
if (req.method === 'GET') {
- req.query.id = req.auth.user.id;
+ req.query.userId = req.auth.user.id;
return userWebsites(req, res);
}
diff --git a/src/pages/api/realtime/[id].ts b/src/pages/api/realtime/[websiteId].ts
similarity index 91%
rename from src/pages/api/realtime/[id].ts
rename to src/pages/api/realtime/[websiteId].ts
index 0edd589d8..d315bf18a 100644
--- a/src/pages/api/realtime/[id].ts
+++ b/src/pages/api/realtime/[websiteId].ts
@@ -9,13 +9,13 @@ import * as yup from 'yup';
import { REALTIME_RANGE } from 'lib/constants';
export interface RealtimeRequestQuery {
- id: string;
+ websiteId: string;
startAt: number;
}
const schema = {
GET: yup.object().shape({
- id: yup.string().uuid().required(),
+ websiteId: yup.string().uuid().required(),
startAt: yup.number().integer().required(),
}),
};
@@ -28,7 +28,7 @@ export default async (
await useValidate(schema, req, res);
if (req.method === 'GET') {
- const { id: websiteId, startAt } = req.query;
+ const { websiteId, startAt } = req.query;
if (!(await canViewWebsite(req.auth, websiteId))) {
return unauthorized(res);
diff --git a/src/pages/api/reports/[id].ts b/src/pages/api/reports/[reportId].ts
similarity index 92%
rename from src/pages/api/reports/[id].ts
rename to src/pages/api/reports/[reportId].ts
index db86631f7..9c1135148 100644
--- a/src/pages/api/reports/[id].ts
+++ b/src/pages/api/reports/[reportId].ts
@@ -7,7 +7,7 @@ import { deleteReport, getReport, updateReport } from 'queries';
import * as yup from 'yup';
export interface ReportRequestQuery {
- id: string;
+ reportId: string;
}
export interface ReportRequestBody {
@@ -20,10 +20,10 @@ export interface ReportRequestBody {
const schema: YupRequest = {
GET: yup.object().shape({
- id: yup.string().uuid().required(),
+ reportId: yup.string().uuid().required(),
}),
POST: yup.object().shape({
- id: yup.string().uuid().required(),
+ reportId: yup.string().uuid().required(),
websiteId: yup.string().uuid().required(),
type: yup
.string()
@@ -36,7 +36,7 @@ const schema: YupRequest = {
.test('len', 'Must not exceed 6000 characters.', val => JSON.stringify(val).length < 6000),
}),
DELETE: yup.object().shape({
- id: yup.string().uuid().required(),
+ reportId: yup.string().uuid().required(),
}),
};
@@ -48,7 +48,7 @@ export default async (
await useAuth(req, res);
await useValidate(schema, req, res);
- const { id: reportId } = req.query;
+ const { reportId } = req.query;
const {
user: { id: userId },
} = req.auth;
diff --git a/src/pages/api/reports/index.ts b/src/pages/api/reports/index.ts
index 64a293264..86032ef51 100644
--- a/src/pages/api/reports/index.ts
+++ b/src/pages/api/reports/index.ts
@@ -1,14 +1,12 @@
import { uuid } from 'lib/crypto';
import { useAuth, useCors, useValidate } from 'lib/middleware';
-import { NextApiRequestQueryBody, SearchFilter } from 'lib/types';
+import { NextApiRequestQueryBody } from 'lib/types';
import { pageInfo } from 'lib/schema';
import { NextApiResponse } from 'next';
import { methodNotAllowed, ok } from 'next-basics';
import { createReport, getReports } from 'queries';
import * as yup from 'yup';
-export interface ReportsRequestQuery extends SearchFilter {}
-
export interface ReportRequestBody {
websiteId: string;
name: string;
@@ -60,11 +58,18 @@ export default async (
const data = await getReports(
{
where: {
- userId: !(websiteId && teamId) ? userId : undefined,
- websiteId,
+ website: {
+ id: websiteId,
+ userId: !websiteId && !teamId ? userId : undefined,
+ teamId,
+ },
},
include: {
- website: true,
+ website: {
+ select: {
+ domain: true,
+ },
+ },
},
},
filters,
diff --git a/src/pages/api/share/[id].ts b/src/pages/api/share/[shareId].ts
similarity index 89%
rename from src/pages/api/share/[id].ts
rename to src/pages/api/share/[shareId].ts
index 27688e234..26ac4cdce 100644
--- a/src/pages/api/share/[id].ts
+++ b/src/pages/api/share/[shareId].ts
@@ -7,17 +7,17 @@ import { getSharedWebsite } from 'queries';
import * as yup from 'yup';
export interface ShareRequestQuery {
- id: string;
+ shareId: string;
}
export interface ShareResponse {
- id: string;
+ shareId: string;
token: string;
}
const schema = {
GET: yup.object().shape({
- id: yup.string().required(),
+ shareId: yup.string().required(),
}),
};
@@ -27,7 +27,7 @@ export default async (
) => {
await useValidate(schema, req, res);
- const { id: shareId } = req.query;
+ const { shareId } = req.query;
if (req.method === 'GET') {
const website = await getSharedWebsite(shareId);
diff --git a/src/pages/api/teams/[id]/index.ts b/src/pages/api/teams/[teamId]/index.ts
similarity index 92%
rename from src/pages/api/teams/[id]/index.ts
rename to src/pages/api/teams/[teamId]/index.ts
index 17ca675ae..b731ee0cb 100644
--- a/src/pages/api/teams/[id]/index.ts
+++ b/src/pages/api/teams/[teamId]/index.ts
@@ -8,7 +8,7 @@ import { deleteTeam, getTeam, updateTeam } from 'queries';
import * as yup from 'yup';
export interface TeamRequestQuery {
- id: string;
+ teamId: string;
}
export interface TeamRequestBody {
@@ -18,7 +18,7 @@ export interface TeamRequestBody {
const schema = {
GET: yup.object().shape({
- id: yup.string().uuid().required(),
+ teamId: yup.string().uuid().required(),
}),
POST: yup.object().shape({
id: yup.string().uuid().required(),
@@ -26,7 +26,7 @@ const schema = {
accessCode: yup.string().max(50),
}),
DELETE: yup.object().shape({
- id: yup.string().uuid().required(),
+ teamId: yup.string().uuid().required(),
}),
};
@@ -37,7 +37,7 @@ export default async (
await useAuth(req, res);
await useValidate(schema, req, res);
- const { id: teamId } = req.query;
+ const { teamId } = req.query;
if (req.method === 'GET') {
if (!(await canViewTeam(req.auth, teamId))) {
diff --git a/src/pages/api/teams/[id]/users/[userId].ts b/src/pages/api/teams/[teamId]/users/[userId].ts
similarity index 94%
rename from src/pages/api/teams/[id]/users/[userId].ts
rename to src/pages/api/teams/[teamId]/users/[userId].ts
index d6ddb09ec..da6218b13 100644
--- a/src/pages/api/teams/[id]/users/[userId].ts
+++ b/src/pages/api/teams/[teamId]/users/[userId].ts
@@ -7,7 +7,7 @@ import { deleteTeamUser, getTeamUser, updateTeamUser } from 'queries';
import * as yup from 'yup';
export interface TeamUserRequestQuery {
- id: string;
+ teamId: string;
userId: string;
}
@@ -17,7 +17,7 @@ export interface TeamUserRequestBody {
const schema = {
DELETE: yup.object().shape({
- id: yup.string().uuid().required(),
+ teamId: yup.string().uuid().required(),
userId: yup.string().uuid().required(),
}),
POST: yup.object().shape({
@@ -35,7 +35,7 @@ export default async (
await useAuth(req, res);
await useValidate(schema, req, res);
- const { id: teamId, userId } = req.query;
+ const { teamId, userId } = req.query;
if (req.method === 'POST') {
if (!(await canUpdateTeam(req.auth, teamId))) {
diff --git a/src/pages/api/teams/[id]/users/index.ts b/src/pages/api/teams/[teamId]/users/index.ts
similarity index 95%
rename from src/pages/api/teams/[id]/users/index.ts
rename to src/pages/api/teams/[teamId]/users/index.ts
index 2bae34ac5..b79df9435 100644
--- a/src/pages/api/teams/[id]/users/index.ts
+++ b/src/pages/api/teams/[teamId]/users/index.ts
@@ -8,7 +8,7 @@ import { createTeamUser, getTeamUser, getTeamUsers } from 'queries';
import * as yup from 'yup';
export interface TeamUserRequestQuery extends SearchFilter {
- id: string;
+ teamId: string;
}
export interface TeamUserRequestBody {
@@ -18,7 +18,7 @@ export interface TeamUserRequestBody {
const schema = {
GET: yup.object().shape({
- id: yup.string().uuid().required(),
+ teamId: yup.string().uuid().required(),
...pageInfo,
}),
POST: yup.object().shape({
@@ -37,7 +37,7 @@ export default async (
await useAuth(req, res);
await useValidate(schema, req, res);
- const { id: teamId } = req.query;
+ const { teamId } = req.query;
if (req.method === 'GET') {
if (!(await canViewTeam(req.auth, teamId))) {
diff --git a/src/pages/api/teams/[id]/websites/index.ts b/src/pages/api/teams/[teamId]/websites/index.ts
similarity index 94%
rename from src/pages/api/teams/[id]/websites/index.ts
rename to src/pages/api/teams/[teamId]/websites/index.ts
index b5e152107..2f6fe3254 100644
--- a/src/pages/api/teams/[id]/websites/index.ts
+++ b/src/pages/api/teams/[teamId]/websites/index.ts
@@ -9,7 +9,7 @@ import { createWebsite, getTeamWebsites } from 'queries';
import { uuid } from 'lib/crypto';
export interface TeamWebsiteRequestQuery extends SearchFilter {
- id: string;
+ teamId: string;
}
export interface TeamWebsiteRequestBody {
@@ -20,7 +20,7 @@ export interface TeamWebsiteRequestBody {
const schema = {
GET: yup.object().shape({
- id: yup.string().uuid().required(),
+ teamId: yup.string().uuid().required(),
...pageInfo,
}),
POST: yup.object().shape({
@@ -37,7 +37,7 @@ export default async (
await useAuth(req, res);
await useValidate(schema, req, res);
- const { id: teamId } = req.query;
+ const { teamId } = req.query;
if (req.method === 'GET') {
if (!(await canViewTeam(req.auth, teamId))) {
diff --git a/src/pages/api/teams/join.ts b/src/pages/api/teams/join.ts
index ff4768d88..a9943f648 100644
--- a/src/pages/api/teams/join.ts
+++ b/src/pages/api/teams/join.ts
@@ -6,6 +6,7 @@ import { NextApiResponse } from 'next';
import { methodNotAllowed, notFound, ok } from 'next-basics';
import { createTeamUser, findTeam, getTeamUser } from 'queries';
import * as yup from 'yup';
+
export interface TeamsJoinRequestBody {
accessCode: string;
}
diff --git a/src/pages/api/users/[id]/index.ts b/src/pages/api/users/[userId]/index.ts
similarity index 80%
rename from src/pages/api/users/[id]/index.ts
rename to src/pages/api/users/[userId]/index.ts
index 495d41ccf..ca46ff160 100644
--- a/src/pages/api/users/[id]/index.ts
+++ b/src/pages/api/users/[userId]/index.ts
@@ -1,16 +1,17 @@
+import * as yup from 'yup';
import { canDeleteUser, canUpdateUser, canViewUser } from 'lib/auth';
import { useAuth, useValidate } from 'lib/middleware';
import { NextApiRequestQueryBody, Role, User } from 'lib/types';
import { NextApiResponse } from 'next';
import { badRequest, hashPassword, methodNotAllowed, ok, unauthorized } from 'next-basics';
import { deleteUser, getUser, getUserByUsername, updateUser } from 'queries';
-import * as yup from 'yup';
export interface UserRequestQuery {
- id: string;
+ userId: string;
}
export interface UserRequestBody {
+ userId: string;
username: string;
password: string;
role: Role;
@@ -18,10 +19,10 @@ export interface UserRequestBody {
const schema = {
GET: yup.object().shape({
- id: yup.string().uuid().required(),
+ userId: yup.string().uuid().required(),
}),
POST: yup.object().shape({
- id: yup.string().uuid().required(),
+ userId: yup.string().uuid().required(),
username: yup.string().max(255),
password: yup.string(),
role: yup.string().matches(/admin|user|view-only/i),
@@ -36,28 +37,28 @@ export default async (
await useValidate(schema, req, res);
const {
- user: { id: userId, isAdmin },
+ user: { isAdmin },
} = req.auth;
- const { id } = req.query;
+ const userId: string = req.query.userId;
if (req.method === 'GET') {
- if (!(await canViewUser(req.auth, id))) {
+ if (!(await canViewUser(req.auth, userId))) {
return unauthorized(res);
}
- const user = await getUser(id);
+ const user = await getUser(userId);
return ok(res, user);
}
if (req.method === 'POST') {
- if (!(await canUpdateUser(req.auth, id))) {
+ if (!(await canUpdateUser(req.auth, userId))) {
return unauthorized(res);
}
const { username, password, role } = req.body;
- const user = await getUser(id);
+ const user = await getUser(userId);
const data: any = {};
@@ -83,7 +84,7 @@ export default async (
}
}
- const updated = await updateUser(data, { id });
+ const updated = await updateUser(userId, data);
return ok(res, updated);
}
@@ -93,11 +94,11 @@ export default async (
return unauthorized(res);
}
- if (id === userId) {
+ if (userId === req.auth.user.id) {
return badRequest(res, 'You cannot delete yourself.');
}
- await deleteUser(id);
+ await deleteUser(userId);
return ok(res);
}
diff --git a/src/pages/api/users/[id]/teams.ts b/src/pages/api/users/[userId]/teams.ts
similarity index 100%
rename from src/pages/api/users/[id]/teams.ts
rename to src/pages/api/users/[userId]/teams.ts
diff --git a/src/pages/api/users/[id]/usage.ts b/src/pages/api/users/[userId]/usage.ts
similarity index 100%
rename from src/pages/api/users/[id]/usage.ts
rename to src/pages/api/users/[userId]/usage.ts
diff --git a/src/pages/api/users/[id]/websites.ts b/src/pages/api/users/[userId]/websites.ts
similarity index 67%
rename from src/pages/api/users/[id]/websites.ts
rename to src/pages/api/users/[userId]/websites.ts
index afd93fe1f..e316dafb9 100644
--- a/src/pages/api/users/[id]/websites.ts
+++ b/src/pages/api/users/[userId]/websites.ts
@@ -1,36 +1,27 @@
import { useAuth, useCors, useValidate } from 'lib/middleware';
-import { NextApiRequestQueryBody, SearchFilter } from 'lib/types';
+import { NextApiRequestQueryBody } from 'lib/types';
import { pageInfo } from 'lib/schema';
import { NextApiResponse } from 'next';
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
import { getUserWebsites } from 'queries';
import * as yup from 'yup';
-export interface UserWebsitesRequestQuery extends SearchFilter {
- id: string;
- includeTeams?: boolean;
- onlyTeams?: boolean;
-}
-
const schema = {
GET: yup.object().shape({
- id: yup.string().uuid().required(),
+ userId: yup.string().uuid().required(),
includeTeams: yup.boolean(),
onlyTeams: yup.boolean(),
...pageInfo,
}),
};
-export default async (
- req: NextApiRequestQueryBody<UserWebsitesRequestQuery>,
- res: NextApiResponse,
-) => {
+export default async (req: NextApiRequestQueryBody, res: NextApiResponse) => {
await useCors(req, res);
await useAuth(req, res);
await useValidate(schema, req, res);
const { user } = req.auth;
- const { id: userId, page = 1, pageSize, query = '', ...rest } = req.query;
+ const { userId, page = 1, pageSize, query = '', ...rest } = req.query;
if (req.method === 'GET') {
if (!user.isAdmin && user.id !== userId) {
diff --git a/src/pages/api/websites/[id]/active.ts b/src/pages/api/websites/[websiteId]/active.ts
similarity index 77%
rename from src/pages/api/websites/[id]/active.ts
rename to src/pages/api/websites/[websiteId]/active.ts
index ef631a0e8..d87a7818e 100644
--- a/src/pages/api/websites/[id]/active.ts
+++ b/src/pages/api/websites/[websiteId]/active.ts
@@ -7,12 +7,12 @@ import { getActiveVisitors } from 'queries';
import * as yup from 'yup';
export interface WebsiteActiveRequestQuery {
- id: string;
+ websiteId: string;
}
const schema = {
GET: yup.object().shape({
- id: yup.string().uuid().required(),
+ websiteId: yup.string().uuid().required(),
}),
};
@@ -24,14 +24,14 @@ export default async (
await useAuth(req, res);
await useValidate(schema, req, res);
- const { id: websiteId } = req.query;
+ const { websiteId } = req.query;
if (req.method === 'GET') {
- if (!(await canViewWebsite(req.auth, websiteId))) {
+ if (!(await canViewWebsite(req.auth, websiteId as string))) {
return unauthorized(res);
}
- const result = await getActiveVisitors(websiteId);
+ const result = await getActiveVisitors(websiteId as string);
return ok(res, result);
}
diff --git a/src/pages/api/websites/[id]/daterange.ts b/src/pages/api/websites/[websiteId]/daterange.ts
similarity index 90%
rename from src/pages/api/websites/[id]/daterange.ts
rename to src/pages/api/websites/[websiteId]/daterange.ts
index 5bf76a91a..1aeb76cbe 100644
--- a/src/pages/api/websites/[id]/daterange.ts
+++ b/src/pages/api/websites/[websiteId]/daterange.ts
@@ -7,12 +7,12 @@ import { getWebsiteDateRange } from 'queries';
import * as yup from 'yup';
export interface WebsiteDateRangeRequestQuery {
- id: string;
+ websiteId: string;
}
const schema = {
GET: yup.object().shape({
- id: yup.string().uuid().required(),
+ websiteId: yup.string().uuid().required(),
}),
};
@@ -24,7 +24,7 @@ export default async (
await useAuth(req, res);
await useValidate(schema, req, res);
- const { id: websiteId } = req.query;
+ const { websiteId } = req.query;
if (req.method === 'GET') {
if (!(await canViewWebsite(req.auth, websiteId))) {
diff --git a/src/pages/api/websites/[id]/events.ts b/src/pages/api/websites/[websiteId]/events.ts
similarity index 92%
rename from src/pages/api/websites/[id]/events.ts
rename to src/pages/api/websites/[websiteId]/events.ts
index 05a651ab6..b4d465a4b 100644
--- a/src/pages/api/websites/[id]/events.ts
+++ b/src/pages/api/websites/[websiteId]/events.ts
@@ -9,7 +9,7 @@ import { getEventMetrics } from 'queries';
import * as yup from 'yup';
export interface WebsiteEventsRequestQuery {
- id: string;
+ websiteId: string;
startAt: string;
endAt: string;
unit?: string;
@@ -19,7 +19,7 @@ export interface WebsiteEventsRequestQuery {
const schema = {
GET: yup.object().shape({
- id: yup.string().uuid().required(),
+ websiteId: yup.string().uuid().required(),
startAt: yup.number().integer().required(),
endAt: yup.number().integer().moreThan(yup.ref('startAt')).required(),
unit: UnitTypeTest,
@@ -36,7 +36,7 @@ export default async (
await useAuth(req, res);
await useValidate(schema, req, res);
- const { id: websiteId, timezone, url } = req.query;
+ const { websiteId, timezone, url } = req.query;
const { startDate, endDate, unit } = await parseDateRangeQuery(req);
if (req.method === 'GET') {
diff --git a/src/pages/api/websites/[id]/index.ts b/src/pages/api/websites/[websiteId]/index.ts
similarity index 93%
rename from src/pages/api/websites/[id]/index.ts
rename to src/pages/api/websites/[websiteId]/index.ts
index 795449193..627484c7e 100644
--- a/src/pages/api/websites/[id]/index.ts
+++ b/src/pages/api/websites/[websiteId]/index.ts
@@ -7,7 +7,7 @@ import { deleteWebsite, getWebsite, updateWebsite } from 'queries';
import { SHARE_ID_REGEX } from 'lib/constants';
export interface WebsiteRequestQuery {
- id: string;
+ websiteId: string;
}
export interface WebsiteRequestBody {
@@ -20,10 +20,10 @@ import * as yup from 'yup';
const schema = {
GET: yup.object().shape({
- id: yup.string().uuid().required(),
+ websiteId: yup.string().uuid().required(),
}),
POST: yup.object().shape({
- id: yup.string().uuid().required(),
+ websiteId: yup.string().uuid().required(),
name: yup.string(),
domain: yup.string(),
shareId: yup.string().matches(SHARE_ID_REGEX, { excludeEmptyString: true }).nullable(),
@@ -38,7 +38,7 @@ export default async (
await useAuth(req, res);
await useValidate(schema, req, res);
- const { id: websiteId } = req.query;
+ const { websiteId } = req.query;
if (req.method === 'GET') {
if (!(await canViewWebsite(req.auth, websiteId))) {
diff --git a/src/pages/api/websites/[id]/metrics.ts b/src/pages/api/websites/[websiteId]/metrics.ts
similarity index 97%
rename from src/pages/api/websites/[id]/metrics.ts
rename to src/pages/api/websites/[websiteId]/metrics.ts
index 91cc7f7d4..1062960cc 100644
--- a/src/pages/api/websites/[id]/metrics.ts
+++ b/src/pages/api/websites/[websiteId]/metrics.ts
@@ -9,7 +9,7 @@ import { parseDateRangeQuery } from 'lib/query';
import * as yup from 'yup';
export interface WebsiteMetricsRequestQuery {
- id: string;
+ websiteId: string;
type: string;
startAt: number;
endAt: number;
@@ -30,7 +30,7 @@ export interface WebsiteMetricsRequestQuery {
const schema = {
GET: yup.object().shape({
- id: yup.string().uuid().required(),
+ websiteId: yup.string().uuid().required(),
type: yup.string().required(),
startAt: yup.number().required(),
endAt: yup.number().required(),
@@ -59,7 +59,7 @@ export default async (
await useValidate(schema, req, res);
const {
- id: websiteId,
+ websiteId,
type,
url,
referrer,
diff --git a/src/pages/api/websites/[id]/pageviews.ts b/src/pages/api/websites/[websiteId]/pageviews.ts
similarity index 90%
rename from src/pages/api/websites/[id]/pageviews.ts
rename to src/pages/api/websites/[websiteId]/pageviews.ts
index 7356c5042..23ea335db 100644
--- a/src/pages/api/websites/[id]/pageviews.ts
+++ b/src/pages/api/websites/[websiteId]/pageviews.ts
@@ -7,7 +7,7 @@ import { methodNotAllowed, ok, unauthorized } from 'next-basics';
import { getPageviewStats, getSessionStats } from 'queries';
export interface WebsitePageviewRequestQuery {
- id: string;
+ websiteId: string;
startAt: number;
endAt: number;
unit?: string;
@@ -27,7 +27,7 @@ import { TimezoneTest, UnitTypeTest } from 'lib/yup';
import * as yup from 'yup';
const schema = {
GET: yup.object().shape({
- id: yup.string().uuid().required(),
+ websiteId: yup.string().uuid().required(),
startAt: yup.number().required(),
endAt: yup.number().required(),
unit: UnitTypeTest,
@@ -52,19 +52,8 @@ export default async (
await useAuth(req, res);
await useValidate(schema, req, res);
- const {
- id: websiteId,
- timezone,
- url,
- referrer,
- title,
- os,
- browser,
- device,
- country,
- region,
- city,
- } = req.query;
+ const { websiteId, timezone, url, referrer, title, os, browser, device, country, region, city } =
+ req.query;
if (req.method === 'GET') {
if (!(await canViewWebsite(req.auth, websiteId))) {
diff --git a/src/pages/api/websites/[id]/reports.ts b/src/pages/api/websites/[websiteId]/reports.ts
similarity index 91%
rename from src/pages/api/websites/[id]/reports.ts
rename to src/pages/api/websites/[websiteId]/reports.ts
index f5a47eb92..e03424306 100644
--- a/src/pages/api/websites/[id]/reports.ts
+++ b/src/pages/api/websites/[websiteId]/reports.ts
@@ -8,12 +8,12 @@ import { getWebsiteReports } from 'queries';
import { pageInfo } from 'lib/schema';
export interface ReportsRequestQuery extends SearchFilter {
- id: string;
+ websiteId: string;
}
const schema = {
GET: yup.object().shape({
- id: yup.string().uuid().required(),
+ websiteId: yup.string().uuid().required(),
...pageInfo,
}),
};
@@ -26,7 +26,7 @@ export default async (
await useAuth(req, res);
await useValidate(schema, req, res);
- const { id: websiteId } = req.query;
+ const { websiteId } = req.query;
if (req.method === 'GET') {
if (!(await canViewWebsite(req.auth, websiteId))) {
diff --git a/src/pages/api/websites/[id]/reset.ts b/src/pages/api/websites/[websiteId]/reset.ts
similarity index 89%
rename from src/pages/api/websites/[id]/reset.ts
rename to src/pages/api/websites/[websiteId]/reset.ts
index 647574111..82e769dc2 100644
--- a/src/pages/api/websites/[id]/reset.ts
+++ b/src/pages/api/websites/[websiteId]/reset.ts
@@ -7,12 +7,12 @@ import { resetWebsite } from 'queries';
import * as yup from 'yup';
export interface WebsiteResetRequestQuery {
- id: string;
+ websiteId: string;
}
const schema = {
POST: yup.object().shape({
- id: yup.string().uuid().required(),
+ websiteId: yup.string().uuid().required(),
}),
};
@@ -24,7 +24,7 @@ export default async (
await useAuth(req, res);
await useValidate(schema, req, res);
- const { id: websiteId } = req.query;
+ const { websiteId } = req.query;
if (req.method === 'POST') {
if (!(await canUpdateWebsite(req.auth, websiteId))) {
diff --git a/src/pages/api/websites/[id]/stats.ts b/src/pages/api/websites/[websiteId]/stats.ts
similarity index 95%
rename from src/pages/api/websites/[id]/stats.ts
rename to src/pages/api/websites/[websiteId]/stats.ts
index 4e8d2a88b..129bfca6d 100644
--- a/src/pages/api/websites/[id]/stats.ts
+++ b/src/pages/api/websites/[websiteId]/stats.ts
@@ -8,7 +8,7 @@ import { parseDateRangeQuery } from 'lib/query';
import { getWebsiteStats } from 'queries';
export interface WebsiteStatsRequestQuery {
- id: string;
+ websiteId: string;
startAt: number;
endAt: number;
url?: string;
@@ -27,7 +27,7 @@ export interface WebsiteStatsRequestQuery {
import * as yup from 'yup';
const schema = {
GET: yup.object().shape({
- id: yup.string().uuid().required(),
+ websiteId: yup.string().uuid().required(),
startAt: yup.number().required(),
endAt: yup.number().required(),
url: yup.string(),
@@ -53,7 +53,7 @@ export default async (
await useValidate(schema, req, res);
const {
- id: websiteId,
+ websiteId,
url,
referrer,
title,
@@ -65,7 +65,7 @@ export default async (
country,
region,
city,
- } = req.query;
+ }: any & { websiteId: string } = req.query;
if (req.method === 'GET') {
if (!(await canViewWebsite(req.auth, websiteId))) {
diff --git a/src/pages/api/websites/[id]/values.ts b/src/pages/api/websites/[websiteId]/values.ts
similarity index 93%
rename from src/pages/api/websites/[id]/values.ts
rename to src/pages/api/websites/[websiteId]/values.ts
index 64fad1f27..9cf2dba8b 100644
--- a/src/pages/api/websites/[id]/values.ts
+++ b/src/pages/api/websites/[websiteId]/values.ts
@@ -8,7 +8,7 @@ import { getValues } from 'queries';
import { parseDateRangeQuery } from 'lib/query';
export interface ValuesRequestQuery {
- id: string;
+ websiteId: string;
startAt: number;
endAt: number;
}
@@ -16,7 +16,7 @@ export interface ValuesRequestQuery {
import * as yup from 'yup';
const schema = {
GET: yup.object().shape({
- id: yup.string().uuid().required(),
+ websiteId: yup.string().uuid().required(),
startAt: yup.number().required(),
endAt: yup.number().required(),
}),
@@ -27,7 +27,7 @@ export default async (req: NextApiRequestQueryBody<ValuesRequestQuery>, res: Nex
await useAuth(req, res);
await useValidate(schema, req, res);
- const { id: websiteId, type } = req.query;
+ const { websiteId, type } = req.query;
const { startDate, endDate } = await parseDateRangeQuery(req);
if (req.method === 'GET') {
diff --git a/src/pages/api/websites/index.ts b/src/pages/api/websites/index.ts
index d0295b875..9aabad650 100644
--- a/src/pages/api/websites/index.ts
+++ b/src/pages/api/websites/index.ts
@@ -5,7 +5,7 @@ import { NextApiRequestQueryBody, SearchFilter } from 'lib/types';
import { NextApiResponse } from 'next';
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
import { createWebsite } from 'queries';
-import userWebsites from 'pages/api/users/[id]/websites';
+import userWebsites from 'pages/api/users/[userId]/websites';
import * as yup from 'yup';
import { pageInfo } from 'lib/schema';
@@ -41,11 +41,11 @@ export default async (
} = req.auth;
if (req.method === 'GET') {
- if (!req.query.id) {
- req.query.id = userId;
+ if (!req.query.userId) {
+ req.query.userId = userId;
}
- return userWebsites(req as any, res);
+ return userWebsites(req, res);
}
if (req.method === 'POST') {
diff --git a/src/queries/admin/report.ts b/src/queries/admin/report.ts
index d996bcbf4..92b04ff83 100644
--- a/src/queries/admin/report.ts
+++ b/src/queries/admin/report.ts
@@ -84,7 +84,7 @@ export async function getReports(
],
};
- return prisma.pagedQuery('report', { where }, filters);
+ return prisma.pagedQuery('report', { ...criteria, where }, filters);
}
export async function getUserReports(
diff --git a/src/queries/admin/teamUser.ts b/src/queries/admin/teamUser.ts
index 4153449a7..e082c94ce 100644
--- a/src/queries/admin/teamUser.ts
+++ b/src/queries/admin/teamUser.ts
@@ -50,6 +50,17 @@ export async function createTeamUser(
});
}
+export async function updateTeamUser(teamUserId: string, data: any): Promise<TeamUser> {
+ return prisma.client.teamUser.update(
+ {
+ where: {
+ id: teamUserId,
+ },
+ },
+ data,
+ );
+}
+
export async function deleteTeamUser(teamId: string, userId: string): Promise<TeamUser> {
const { client } = prisma;
diff --git a/src/queries/admin/user.ts b/src/queries/admin/user.ts
index da8de255b..f9eb23a41 100644
--- a/src/queries/admin/user.ts
+++ b/src/queries/admin/user.ts
@@ -112,12 +112,11 @@ export async function createUser(data: {
});
}
-export async function updateUser(
- data: Prisma.UserUpdateInput,
- where: Prisma.UserWhereUniqueInput,
-): Promise<User> {
+export async function updateUser(userId: string, data: Prisma.UserUpdateInput): Promise<User> {
return prisma.client.user.update({
- where,
+ where: {
+ id: userId,
+ },
data,
select: {
id: true,
diff --git a/src/queries/admin/website.ts b/src/queries/admin/website.ts
index 3024caf1f..984770ae1 100644
--- a/src/queries/admin/website.ts
+++ b/src/queries/admin/website.ts
@@ -138,7 +138,7 @@ export async function updateWebsite(
): Promise<Website> {
return prisma.client.website.update({
where: {
- id: websiteId,
+ websiteId,
},
data,
});
@@ -160,7 +160,7 @@ export async function resetWebsite(
where: { websiteId },
}),
client.website.update({
- where: { id: websiteId },
+ where: { websiteId },
data: {
resetAt: new Date(),
},
@@ -200,10 +200,10 @@ export async function deleteWebsite(
data: {
deletedAt: new Date(),
},
- where: { id: websiteId },
+ where: { websiteId },
})
: client.website.delete({
- where: { id: websiteId },
+ where: { websiteId },
}),
]).then(async data => {
if (cache.enabled) {
diff --git a/src/queries/analytics/events/saveEvent.ts b/src/queries/analytics/events/saveEvent.ts
index a6457d7e6..0996fa0d1 100644
--- a/src/queries/analytics/events/saveEvent.ts
+++ b/src/queries/analytics/events/saveEvent.ts
@@ -134,7 +134,7 @@ async function clickhouseQuery(data: {
const message = {
...args,
- website_id: websiteId,
+ website_websiteId,
session_id: sessionId,
event_id: uuid(),
country: country,