Fix Typo.
parent
5aa8187e42
commit
f5eb974d8d
|
@ -0,0 +1,87 @@
|
||||||
|
import { badRequest, hashPassword, methodNotAllowed, ok, unauthorized } from 'next-basics';
|
||||||
|
import { getTeam, deleteTeam, updateTeam } from 'queries';
|
||||||
|
import { useAuth } from 'lib/middleware';
|
||||||
|
import { NextApiResponse } from 'next';
|
||||||
|
import { NextApiRequestQueryBody } from 'interface/api/nextApi';
|
||||||
|
import { Team } from '@prisma/client';
|
||||||
|
|
||||||
|
export interface TeamRequestQuery {
|
||||||
|
id: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface TeamRequestBody {
|
||||||
|
username: string;
|
||||||
|
password: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default async (
|
||||||
|
req: NextApiRequestQueryBody<TeamRequestQuery, TeamRequestBody>,
|
||||||
|
res: NextApiResponse<Team>,
|
||||||
|
) => {
|
||||||
|
await useAuth(req, res);
|
||||||
|
|
||||||
|
const {
|
||||||
|
user: { id: userId, isAdmin },
|
||||||
|
} = req.auth;
|
||||||
|
const { id } = req.query;
|
||||||
|
|
||||||
|
if (req.method === 'GET') {
|
||||||
|
if (id !== userId && !isAdmin) {
|
||||||
|
return unauthorized(res);
|
||||||
|
}
|
||||||
|
|
||||||
|
const user = await getTeam({ id });
|
||||||
|
|
||||||
|
return ok(res, user);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.method === 'POST') {
|
||||||
|
const { username, password } = req.body;
|
||||||
|
|
||||||
|
if (id !== userId && !isAdmin) {
|
||||||
|
return unauthorized(res);
|
||||||
|
}
|
||||||
|
|
||||||
|
const user = await getTeam({ id });
|
||||||
|
|
||||||
|
const data: any = {};
|
||||||
|
|
||||||
|
if (password) {
|
||||||
|
data.password = hashPassword(password);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only admin can change these fields
|
||||||
|
if (isAdmin) {
|
||||||
|
data.username = username;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check when username changes
|
||||||
|
if (data.username && user.username !== data.username) {
|
||||||
|
const userByTeamname = await getTeam({ username });
|
||||||
|
|
||||||
|
if (userByTeamname) {
|
||||||
|
return badRequest(res, 'Team already exists');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const updated = await updateTeam(data, { id });
|
||||||
|
|
||||||
|
return ok(res, updated);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.method === 'DELETE') {
|
||||||
|
if (id === userId) {
|
||||||
|
return badRequest(res, 'You cannot delete your own user.');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isAdmin) {
|
||||||
|
return unauthorized(res);
|
||||||
|
}
|
||||||
|
|
||||||
|
await deleteTeam(id);
|
||||||
|
|
||||||
|
return ok(res);
|
||||||
|
}
|
||||||
|
|
||||||
|
return methodNotAllowed(res);
|
||||||
|
};
|
|
@ -5,17 +5,17 @@ import { NextApiResponse } from 'next';
|
||||||
import { NextApiRequestQueryBody } from 'interface/api/nextApi';
|
import { NextApiRequestQueryBody } from 'interface/api/nextApi';
|
||||||
import { User } from 'interface/api/models';
|
import { User } from 'interface/api/models';
|
||||||
|
|
||||||
export interface UserReqeustQuery {
|
export interface UserRequestQuery {
|
||||||
id: string;
|
id: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface UserReqeustBody {
|
export interface UserRequestBody {
|
||||||
username: string;
|
username: string;
|
||||||
password: string;
|
password: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default async (
|
export default async (
|
||||||
req: NextApiRequestQueryBody<UserReqeustQuery, UserReqeustBody>,
|
req: NextApiRequestQueryBody<UserRequestQuery, UserRequestBody>,
|
||||||
res: NextApiResponse<User>,
|
res: NextApiResponse<User>,
|
||||||
) => {
|
) => {
|
||||||
await useAuth(req, res);
|
await useAuth(req, res);
|
||||||
|
|
|
@ -7,18 +7,18 @@ import { NextApiRequestQueryBody } from 'interface/api/nextApi';
|
||||||
import { NextApiResponse } from 'next';
|
import { NextApiResponse } from 'next';
|
||||||
import { Website } from 'interface/api/models';
|
import { Website } from 'interface/api/models';
|
||||||
|
|
||||||
export interface WebsiteReqeustQuery {
|
export interface WebsiteRequestQuery {
|
||||||
id: string;
|
id: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface WebsiteReqeustBody {
|
export interface WebsiteRequestBody {
|
||||||
name: string;
|
name: string;
|
||||||
domain: string;
|
domain: string;
|
||||||
shareId: string;
|
shareId: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default async (
|
export default async (
|
||||||
req: NextApiRequestQueryBody<WebsiteReqeustQuery, WebsiteReqeustBody>,
|
req: NextApiRequestQueryBody<WebsiteRequestQuery, WebsiteRequestBody>,
|
||||||
res: NextApiResponse<Website | any>,
|
res: NextApiResponse<Website | any>,
|
||||||
) => {
|
) => {
|
||||||
await useCors(req, res);
|
await useCors(req, res);
|
||||||
|
|
|
@ -36,7 +36,7 @@ function getColumn(type) {
|
||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface WebsiteMetricsReqeustQuery {
|
export interface WebsiteMetricsRequestQuery {
|
||||||
id: string;
|
id: string;
|
||||||
type: string;
|
type: string;
|
||||||
start_at: number;
|
start_at: number;
|
||||||
|
@ -50,7 +50,7 @@ export interface WebsiteMetricsReqeustQuery {
|
||||||
}
|
}
|
||||||
|
|
||||||
export default async (
|
export default async (
|
||||||
req: NextApiRequestQueryBody<WebsiteMetricsReqeustQuery>,
|
req: NextApiRequestQueryBody<WebsiteMetricsRequestQuery>,
|
||||||
res: NextApiResponse<WebsiteMetric[]>,
|
res: NextApiResponse<WebsiteMetric[]>,
|
||||||
) => {
|
) => {
|
||||||
await useCors(req, res);
|
await useCors(req, res);
|
||||||
|
|
|
@ -10,7 +10,7 @@ import { getPageviewStats } from 'queries';
|
||||||
|
|
||||||
const unitTypes = ['year', 'month', 'hour', 'day'];
|
const unitTypes = ['year', 'month', 'hour', 'day'];
|
||||||
|
|
||||||
export interface WebsitePageviewReqeustQuery {
|
export interface WebsitePageviewRequestQuery {
|
||||||
id: string;
|
id: string;
|
||||||
websiteId: string;
|
websiteId: string;
|
||||||
start_at: number;
|
start_at: number;
|
||||||
|
@ -26,7 +26,7 @@ export interface WebsitePageviewReqeustQuery {
|
||||||
}
|
}
|
||||||
|
|
||||||
export default async (
|
export default async (
|
||||||
req: NextApiRequestQueryBody<WebsitePageviewReqeustQuery>,
|
req: NextApiRequestQueryBody<WebsitePageviewRequestQuery>,
|
||||||
res: NextApiResponse<WebsitePageviews>,
|
res: NextApiResponse<WebsitePageviews>,
|
||||||
) => {
|
) => {
|
||||||
await useCors(req, res);
|
await useCors(req, res);
|
||||||
|
|
|
@ -6,12 +6,12 @@ import { TYPE_WEBSITE } from 'lib/constants';
|
||||||
import { NextApiRequestQueryBody } from 'interface/api/nextApi';
|
import { NextApiRequestQueryBody } from 'interface/api/nextApi';
|
||||||
import { NextApiResponse } from 'next';
|
import { NextApiResponse } from 'next';
|
||||||
|
|
||||||
export interface WebsiteResetReqeustQuery {
|
export interface WebsiteResetRequestQuery {
|
||||||
id: string;
|
id: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default async (
|
export default async (
|
||||||
req: NextApiRequestQueryBody<WebsiteResetReqeustQuery>,
|
req: NextApiRequestQueryBody<WebsiteResetRequestQuery>,
|
||||||
res: NextApiResponse,
|
res: NextApiResponse,
|
||||||
) => {
|
) => {
|
||||||
await useCors(req, res);
|
await useCors(req, res);
|
||||||
|
|
|
@ -7,7 +7,7 @@ import { WebsiteStats } from 'interface/api/models';
|
||||||
import { NextApiRequestQueryBody } from 'interface/api/nextApi';
|
import { NextApiRequestQueryBody } from 'interface/api/nextApi';
|
||||||
import { NextApiResponse } from 'next';
|
import { NextApiResponse } from 'next';
|
||||||
|
|
||||||
export interface WebsiteStatsReqeustQuery {
|
export interface WebsiteStatsRequestQuery {
|
||||||
id: string;
|
id: string;
|
||||||
type: string;
|
type: string;
|
||||||
start_at: number;
|
start_at: number;
|
||||||
|
@ -21,7 +21,7 @@ export interface WebsiteStatsReqeustQuery {
|
||||||
}
|
}
|
||||||
|
|
||||||
export default async (
|
export default async (
|
||||||
req: NextApiRequestQueryBody<WebsiteStatsReqeustQuery>,
|
req: NextApiRequestQueryBody<WebsiteStatsRequestQuery>,
|
||||||
res: NextApiResponse<WebsiteStats>,
|
res: NextApiResponse<WebsiteStats>,
|
||||||
) => {
|
) => {
|
||||||
await useCors(req, res);
|
await useCors(req, res);
|
||||||
|
|
|
@ -5,18 +5,18 @@ import { NextApiResponse } from 'next';
|
||||||
import { getRandomChars, methodNotAllowed, ok } from 'next-basics';
|
import { getRandomChars, methodNotAllowed, ok } from 'next-basics';
|
||||||
import { createWebsiteByUser, getAllWebsites, getWebsitesByUserId } from 'queries';
|
import { createWebsiteByUser, getAllWebsites, getWebsitesByUserId } from 'queries';
|
||||||
|
|
||||||
export interface WebsitesReqeustQuery {
|
export interface WebsitesRequestQuery {
|
||||||
include_all?: boolean;
|
include_all?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface WebsitesReqeustBody {
|
export interface WebsitesRequestBody {
|
||||||
name: string;
|
name: string;
|
||||||
domain: string;
|
domain: string;
|
||||||
enableShareUrl: boolean;
|
enableShareUrl: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default async (
|
export default async (
|
||||||
req: NextApiRequestQueryBody<WebsitesReqeustQuery, WebsitesReqeustBody>,
|
req: NextApiRequestQueryBody<WebsitesRequestQuery, WebsitesRequestBody>,
|
||||||
res: NextApiResponse,
|
res: NextApiResponse,
|
||||||
) => {
|
) => {
|
||||||
await useCors(req, res);
|
await useCors(req, res);
|
||||||
|
|
Loading…
Reference in New Issue