2022-12-09 08:43:43 +01:00
|
|
|
import { Website, NextApiRequestQueryBody } from 'lib/types';
|
2022-12-02 05:53:37 +01:00
|
|
|
import { canViewWebsite, canUpdateWebsite, canDeleteWebsite } from 'lib/auth';
|
2022-10-04 02:17:53 +02:00
|
|
|
import { useAuth, useCors } from 'lib/middleware';
|
2022-11-19 03:49:58 +01:00
|
|
|
import { NextApiResponse } from 'next';
|
2022-11-23 00:06:52 +01:00
|
|
|
import { methodNotAllowed, ok, serverError, unauthorized } from 'next-basics';
|
2022-11-02 23:45:47 +01:00
|
|
|
import { deleteWebsite, getWebsite, updateWebsite } from 'queries';
|
2022-10-04 02:17:53 +02:00
|
|
|
|
2022-11-18 07:46:05 +01:00
|
|
|
export interface WebsiteRequestQuery {
|
2022-11-15 22:21:14 +01:00
|
|
|
id: string;
|
|
|
|
}
|
|
|
|
|
2022-11-18 07:46:05 +01:00
|
|
|
export interface WebsiteRequestBody {
|
2022-11-15 22:21:14 +01:00
|
|
|
name: string;
|
|
|
|
domain: string;
|
|
|
|
shareId: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default async (
|
2022-11-18 07:46:05 +01:00
|
|
|
req: NextApiRequestQueryBody<WebsiteRequestQuery, WebsiteRequestBody>,
|
2022-11-23 00:06:52 +01:00
|
|
|
res: NextApiResponse<Website>,
|
2022-11-15 22:21:14 +01:00
|
|
|
) => {
|
2022-10-12 22:11:44 +02:00
|
|
|
await useCors(req, res);
|
|
|
|
await useAuth(req, res);
|
2022-10-04 02:17:53 +02:00
|
|
|
|
2022-12-02 05:53:37 +01:00
|
|
|
const {
|
|
|
|
user: { id: userId },
|
|
|
|
} = req.auth;
|
2022-11-02 23:45:47 +01:00
|
|
|
const { id: websiteId } = req.query;
|
2022-10-04 02:17:53 +02:00
|
|
|
|
2022-10-12 22:11:44 +02:00
|
|
|
if (req.method === 'GET') {
|
2022-12-02 05:53:37 +01:00
|
|
|
if (!(await canViewWebsite(userId, websiteId))) {
|
2022-12-01 19:58:50 +01:00
|
|
|
return unauthorized(res);
|
|
|
|
}
|
|
|
|
|
2022-11-02 23:45:47 +01:00
|
|
|
const website = await getWebsite({ id: websiteId });
|
2022-10-04 02:17:53 +02:00
|
|
|
|
|
|
|
return ok(res, website);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.method === 'POST') {
|
2022-12-02 05:53:37 +01:00
|
|
|
if (!(await canUpdateWebsite(userId, websiteId))) {
|
2022-12-01 19:58:50 +01:00
|
|
|
return unauthorized(res);
|
|
|
|
}
|
|
|
|
|
2022-11-22 07:32:59 +01:00
|
|
|
const { name, domain, shareId } = req.body;
|
2022-10-04 02:17:53 +02:00
|
|
|
|
2022-10-13 00:52:31 +02:00
|
|
|
try {
|
2022-11-22 07:32:59 +01:00
|
|
|
await updateWebsite(websiteId, { name, domain, shareId });
|
2022-11-19 03:49:58 +01:00
|
|
|
} catch (e: any) {
|
2022-10-13 00:52:31 +02:00
|
|
|
if (e.message.includes('Unique constraint') && e.message.includes('share_id')) {
|
|
|
|
return serverError(res, 'That share ID is already taken.');
|
|
|
|
}
|
|
|
|
}
|
2022-10-04 02:17:53 +02:00
|
|
|
|
|
|
|
return ok(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.method === 'DELETE') {
|
2022-12-02 05:53:37 +01:00
|
|
|
if (!(await canDeleteWebsite(userId, websiteId))) {
|
|
|
|
return unauthorized(res);
|
|
|
|
}
|
|
|
|
|
2022-11-02 23:45:47 +01:00
|
|
|
await deleteWebsite(websiteId);
|
2022-10-04 02:17:53 +02:00
|
|
|
|
|
|
|
return ok(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
return methodNotAllowed(res);
|
|
|
|
};
|