2022-11-02 23:45:47 +01:00
|
|
|
import { createWebsite, getAllWebsites, getUserWebsites } from 'queries';
|
|
|
|
import { ok, methodNotAllowed, getRandomChars } from 'next-basics';
|
2022-11-02 16:57:52 +01:00
|
|
|
import { useAuth, useCors } from 'lib/middleware';
|
2022-10-04 02:17:53 +02:00
|
|
|
import { uuid } from 'lib/crypto';
|
2020-08-12 07:24:41 +02:00
|
|
|
|
|
|
|
export default async (req, res) => {
|
2022-11-02 16:57:52 +01:00
|
|
|
await useCors(req, res);
|
2020-08-12 07:24:41 +02:00
|
|
|
await useAuth(req, res);
|
|
|
|
|
2022-11-09 16:40:17 +01:00
|
|
|
const {
|
2022-11-09 21:03:24 +01:00
|
|
|
user: { id: userId, isAdmin },
|
2022-11-09 16:40:17 +01:00
|
|
|
} = req.auth;
|
2020-08-12 07:24:41 +02:00
|
|
|
|
|
|
|
if (req.method === 'GET') {
|
2022-11-02 23:45:47 +01:00
|
|
|
const { include_all } = req.query;
|
2020-09-11 08:55:29 +02:00
|
|
|
|
2022-11-08 20:55:02 +01:00
|
|
|
const websites =
|
|
|
|
isAdmin && include_all ? await getAllWebsites() : await getUserWebsites(userId);
|
2020-08-12 07:24:41 +02:00
|
|
|
|
|
|
|
return ok(res, websites);
|
|
|
|
}
|
|
|
|
|
2022-10-04 02:17:53 +02:00
|
|
|
if (req.method === 'POST') {
|
2022-11-02 23:45:47 +01:00
|
|
|
const { name, domain, enableShareUrl } = req.body;
|
2022-10-04 02:17:53 +02:00
|
|
|
|
2022-10-12 22:11:44 +02:00
|
|
|
const shareId = enableShareUrl ? getRandomChars(8) : null;
|
2022-11-08 20:55:02 +01:00
|
|
|
const website = await createWebsite(userId, { id: uuid(), name, domain, shareId });
|
2022-10-04 02:17:53 +02:00
|
|
|
|
|
|
|
return ok(res, website);
|
|
|
|
}
|
|
|
|
|
2020-08-12 07:24:41 +02:00
|
|
|
return methodNotAllowed(res);
|
|
|
|
};
|