umami/pages/api/websites/[id]/index.js
Brian Cao 78338205a3
Feat/um 62 prisma property names (#1562)
* checkpoint

* fix pg schema

* fix mysql schema

* change property names
2022-10-10 13:42:18 -07:00

69 lines
1.6 KiB
JavaScript

import { methodNotAllowed, ok, unauthorized, getRandomChars } from 'next-basics';
import { deleteWebsite, getAccount, getWebsite, updateWebsite } from 'queries';
import { allowQuery } from 'lib/auth';
import { useAuth, useCors } from 'lib/middleware';
import { validate } from 'uuid';
export default async (req, res) => {
const { id } = req.query;
const websiteId = +id;
const where = validate(id) ? { websiteUuid: id } : { id: +id };
if (req.method === 'GET') {
await useCors(req, res);
if (!(await allowQuery(req))) {
return unauthorized(res);
}
const website = await getWebsite(where);
return ok(res, website);
}
if (req.method === 'POST') {
await useAuth(req, res);
const { isAdmin: currentUserIsAdmin, userId: currentUserId, accountUuid } = req.auth;
const { name, domain, owner, enable_share_url } = req.body;
let account;
if (accountUuid) {
account = await getAccount({ accountUuid });
}
const website = await getWebsite(where);
const shareId = enable_share_url ? website.shareId || getRandomChars(8) : null;
if (website.userId !== currentUserId && !currentUserIsAdmin) {
return unauthorized(res);
}
await updateWebsite(
{
name,
domain,
shareId: shareId,
userId: account ? account.id : +owner,
},
where,
);
return ok(res);
}
if (req.method === 'DELETE') {
if (!(await allowQuery(req, true))) {
return unauthorized(res);
}
await deleteWebsite(websiteId);
return ok(res);
}
return methodNotAllowed(res);
};