2022-10-04 02:17:53 +02:00
|
|
|
import { allowQuery } from 'lib/auth';
|
|
|
|
import { useAuth, useCors } from 'lib/middleware';
|
2022-10-13 00:52:31 +02:00
|
|
|
import { getRandomChars, methodNotAllowed, ok, serverError, unauthorized } from 'next-basics';
|
2022-10-12 22:11:44 +02:00
|
|
|
import { deleteWebsite, getAccount, getWebsite, updateWebsite } from 'queries';
|
2022-10-25 19:45:56 +02:00
|
|
|
import { TYPE_WEBSITE } from 'lib/constants';
|
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-25 19:45:56 +02:00
|
|
|
const { id: websiteUuid } = req.query;
|
2022-10-04 02:17:53 +02:00
|
|
|
|
2022-10-12 22:11:44 +02:00
|
|
|
if (req.method === 'GET') {
|
2023-01-19 00:09:49 +01:00
|
|
|
if (!(await allowQuery(req, TYPE_WEBSITE))) {
|
|
|
|
return unauthorized(res);
|
|
|
|
}
|
|
|
|
|
2022-10-25 19:45:56 +02:00
|
|
|
const website = await getWebsite({ websiteUuid });
|
2022-10-04 02:17:53 +02:00
|
|
|
|
|
|
|
return ok(res, website);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.method === 'POST') {
|
2023-01-19 00:09:49 +01:00
|
|
|
if (!(await allowQuery(req, TYPE_WEBSITE, false))) {
|
|
|
|
return unauthorized(res);
|
|
|
|
}
|
|
|
|
|
2022-10-12 22:11:44 +02:00
|
|
|
const { name, domain, owner, enableShareUrl, shareId } = req.body;
|
|
|
|
const { accountUuid } = req.auth;
|
2022-10-30 00:50:29 +02:00
|
|
|
|
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-13 00:52:31 +02:00
|
|
|
|
|
|
|
if (!account) {
|
|
|
|
return serverError(res, 'Account does not exist.');
|
|
|
|
}
|
2022-10-04 02:17:53 +02:00
|
|
|
}
|
|
|
|
|
2022-10-25 19:45:56 +02:00
|
|
|
const website = await getWebsite({ websiteUuid });
|
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-13 00:52:31 +02:00
|
|
|
try {
|
|
|
|
await updateWebsite(
|
|
|
|
{
|
|
|
|
name,
|
|
|
|
domain,
|
|
|
|
shareId: shareId ? shareId : newShareId,
|
2022-10-31 18:38:37 +01:00
|
|
|
userId: +owner || account.id,
|
2022-10-13 00:52:31 +02:00
|
|
|
},
|
2022-10-25 19:45:56 +02:00
|
|
|
{ websiteUuid },
|
2022-10-13 00:52:31 +02:00
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
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') {
|
2023-01-19 00:09:49 +01:00
|
|
|
if (!(await allowQuery(req, TYPE_WEBSITE, false))) {
|
2022-10-04 02:17:53 +02:00
|
|
|
return unauthorized(res);
|
|
|
|
}
|
|
|
|
|
2022-10-25 19:45:56 +02:00
|
|
|
await deleteWebsite(websiteUuid);
|
2022-10-04 02:17:53 +02:00
|
|
|
|
|
|
|
return ok(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
return methodNotAllowed(res);
|
|
|
|
};
|