mirror of
https://github.com/kremalicious/umami.git
synced 2025-02-14 21:10:34 +01:00
Renamed id routes for API.
This commit is contained in:
parent
53a991176b
commit
4429198397
@ -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>
|
||||
|
@ -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>
|
||||
|
@ -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 />
|
||||
|
@ -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,
|
||||
|
@ -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,
|
||||
|
@ -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(() => {
|
||||
|
@ -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) {
|
||||
|
@ -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)
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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);
|
@ -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;
|
@ -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,
|
||||
|
@ -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);
|
@ -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))) {
|
@ -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))) {
|
@ -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))) {
|
@ -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))) {
|
@ -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;
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
@ -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) {
|
@ -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);
|
||||
}
|
@ -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))) {
|
@ -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') {
|
@ -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))) {
|
@ -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,
|
@ -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))) {
|
@ -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))) {
|
@ -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))) {
|
@ -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))) {
|
@ -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') {
|
@ -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') {
|
||||
|
@ -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(
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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,
|
||||
|
@ -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) {
|
||||
|
@ -134,7 +134,7 @@ async function clickhouseQuery(data: {
|
||||
|
||||
const message = {
|
||||
...args,
|
||||
website_id: websiteId,
|
||||
website_websiteId,
|
||||
session_id: sessionId,
|
||||
event_id: uuid(),
|
||||
country: country,
|
||||
|
Loading…
x
Reference in New Issue
Block a user