mirror of
https://github.com/kremalicious/umami.git
synced 2024-11-15 01:35:17 +01:00
35 lines
896 B
JavaScript
35 lines
896 B
JavaScript
import { updateWebsite, createWebsite, getWebsiteById } from 'lib/queries';
|
|
import { useAuth } from 'lib/middleware';
|
|
import { uuid } from 'lib/crypto';
|
|
import { ok, unauthorized, methodNotAllowed } from 'lib/response';
|
|
|
|
export default async (req, res) => {
|
|
await useAuth(req, res);
|
|
|
|
const { user_id, is_admin } = req.auth;
|
|
const { website_id } = req.body;
|
|
|
|
if (req.method === 'POST') {
|
|
const { name, domain } = req.body;
|
|
|
|
if (website_id) {
|
|
const website = getWebsiteById(website_id);
|
|
|
|
if (website.user_id === user_id || is_admin) {
|
|
await updateWebsite(website_id, { name, domain });
|
|
|
|
return ok(res);
|
|
}
|
|
|
|
return unauthorized(res);
|
|
} else {
|
|
const website_uuid = uuid();
|
|
const website = await createWebsite(user_id, { website_uuid, name, domain });
|
|
|
|
return ok(res, website);
|
|
}
|
|
}
|
|
|
|
return methodNotAllowed(res);
|
|
};
|