2022-10-04 02:17:53 +02:00
|
|
|
import { allowQuery } from 'lib/auth';
|
|
|
|
import { useAuth, useCors } from 'lib/middleware';
|
2022-10-12 04:37:38 +02:00
|
|
|
import { getRandomChars, methodNotAllowed, ok, unauthorized } from 'next-basics';
|
2022-10-12 22:11:44 +02:00
|
|
|
import { deleteWebsite, getAccount, getWebsite, updateWebsite } from 'queries';
|
2022-10-04 02:17:53 +02:00
|
|
|
|
|
|
|
export default async (req, res) => {
|
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-10-12 22:11:44 +02:00
|
|
|
const { id: websiteId } = req.query;
|
2022-10-04 02:17:53 +02:00
|
|
|
|
2022-10-12 22:11:44 +02:00
|
|
|
if (!(await allowQuery(req))) {
|
|
|
|
return unauthorized(res);
|
|
|
|
}
|
2022-10-04 02:17:53 +02:00
|
|
|
|
2022-10-12 22:11:44 +02:00
|
|
|
if (req.method === 'GET') {
|
|
|
|
const website = await getWebsite({ websiteUuid: websiteId });
|
2022-10-04 02:17:53 +02:00
|
|
|
|
|
|
|
return ok(res, website);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.method === 'POST') {
|
2022-10-12 22:11:44 +02:00
|
|
|
const { name, domain, owner, enableShareUrl, shareId } = req.body;
|
|
|
|
const { accountUuid } = req.auth;
|
2022-10-08 02:13:03 +02:00
|
|
|
let account;
|
2022-10-04 02:17:53 +02:00
|
|
|
|
2022-10-10 22:42:18 +02:00
|
|
|
if (accountUuid) {
|
|
|
|
account = await getAccount({ accountUuid });
|
2022-10-04 02:17:53 +02:00
|
|
|
}
|
|
|
|
|
2022-10-12 22:11:44 +02:00
|
|
|
const website = await getWebsite({ websiteUuid: websiteId });
|
2022-10-10 22:42:18 +02:00
|
|
|
|
2022-10-12 22:11:44 +02:00
|
|
|
const newShareId = enableShareUrl ? website.shareId || getRandomChars(8) : null;
|
2022-10-04 02:17:53 +02:00
|
|
|
|
2022-10-08 02:13:03 +02:00
|
|
|
await updateWebsite(
|
|
|
|
{
|
|
|
|
name,
|
|
|
|
domain,
|
2022-10-12 22:11:44 +02:00
|
|
|
shareId: shareId ? shareId : newShareId,
|
2022-10-10 22:42:18 +02:00
|
|
|
userId: account ? account.id : +owner,
|
2022-10-08 02:13:03 +02:00
|
|
|
},
|
2022-10-12 04:37:38 +02:00
|
|
|
{ websiteUuid: websiteId },
|
2022-10-08 02:13:03 +02:00
|
|
|
);
|
2022-10-04 02:17:53 +02:00
|
|
|
|
|
|
|
return ok(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.method === 'DELETE') {
|
|
|
|
if (!(await allowQuery(req, true))) {
|
|
|
|
return unauthorized(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
await deleteWebsite(websiteId);
|
|
|
|
|
|
|
|
return ok(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
return methodNotAllowed(res);
|
|
|
|
};
|