This commit is contained in:
Brian Cao 2022-10-31 23:42:37 -07:00
parent 246e4e5f4f
commit 17041efaae
73 changed files with 491 additions and 874 deletions

View file

@ -1,39 +1,39 @@
import { allowQuery } from 'lib/auth';
import { useAuth, useCors } from 'lib/middleware';
import { getRandomChars, methodNotAllowed, ok, serverError, unauthorized } from 'next-basics';
import { deleteWebsite, getAccount, getWebsite, updateWebsite } from 'queries';
import { deleteWebsite, getUser, getWebsite, updateWebsite } from 'queries';
import { TYPE_WEBSITE } from 'lib/constants';
export default async (req, res) => {
await useCors(req, res);
await useAuth(req, res);
const { id: websiteUuid } = req.query;
const { id } = req.query;
if (!(await allowQuery(req, TYPE_WEBSITE))) {
return unauthorized(res);
}
if (req.method === 'GET') {
const website = await getWebsite({ websiteUuid });
const website = await getWebsite({ id });
return ok(res, website);
}
if (req.method === 'POST') {
const { name, domain, owner, enableShareUrl, shareId } = req.body;
const { accountUuid } = req.auth;
let account;
const { userId } = req.auth;
let user;
if (accountUuid) {
account = await getAccount({ accountUuid });
if (userId) {
user = await getUser({ id: userId });
if (!account) {
return serverError(res, 'Account does not exist.');
if (!user) {
return serverError(res, 'User does not exist.');
}
}
const website = await getWebsite({ websiteUuid });
const website = await getWebsite({ id });
const newShareId = enableShareUrl ? website.shareId || getRandomChars(8) : null;
@ -43,9 +43,9 @@ export default async (req, res) => {
name,
domain,
shareId: shareId ? shareId : newShareId,
userId: account ? account.id : +owner || undefined,
userId: +owner || user.id,
},
{ websiteUuid },
{ id },
);
} catch (e) {
if (e.message.includes('Unique constraint') && e.message.includes('share_id')) {
@ -61,7 +61,7 @@ export default async (req, res) => {
return unauthorized(res);
}
await deleteWebsite(websiteUuid);
await deleteWebsite(id);
return ok(res);
}

View file

@ -94,7 +94,7 @@ export default async (req, res) => {
let domain;
if (type === 'referrer') {
const website = await getWebsite({ websiteUuid: websiteId });
const website = await getWebsite({ id: websiteId });
if (!website) {
return badRequest(res);

View file

@ -1,4 +1,4 @@
import { createWebsite, getAccount, getAllWebsites, getUserWebsites } from 'queries';
import { createWebsite, getUser, getAllWebsites, getUserWebsites } from 'queries';
import { ok, methodNotAllowed, unauthorized, getRandomChars } from 'next-basics';
import { useAuth } from 'lib/middleware';
import { uuid } from 'lib/crypto';
@ -8,14 +8,14 @@ export default async (req, res) => {
const { user_id, include_all } = req.query;
const { userId: currentUserId, isAdmin } = req.auth;
const accountUuid = user_id || req.auth.accountUuid;
let account;
const id = user_id || currentUserId;
let user;
if (accountUuid) {
account = await getAccount({ accountUuid });
if (id) {
user = await getUser({ id });
}
const userId = account ? account.id : user_id;
const userId = user ? user.id : user_id;
if (req.method === 'GET') {
if (userId && userId !== currentUserId && !isAdmin) {
@ -23,9 +23,7 @@ export default async (req, res) => {
}
const websites =
isAdmin && include_all
? await getAllWebsites()
: await getUserWebsites({ userId: account?.id });
isAdmin && include_all ? await getAllWebsites() : await getUserWebsites({ userId });
return ok(res, websites);
}
@ -33,15 +31,14 @@ export default async (req, res) => {
if (req.method === 'POST') {
const { name, domain, owner, enableShareUrl } = req.body;
const website_owner = account ? account.id : +owner;
const website_owner = user ? userId : +owner;
if (website_owner !== currentUserId && !isAdmin) {
return unauthorized(res);
}
const websiteUuid = uuid();
const shareId = enableShareUrl ? getRandomChars(8) : null;
const website = await createWebsite(website_owner, { websiteUuid, name, domain, shareId });
const website = await createWebsite(website_owner, { id: uuid(), name, domain, shareId });
return ok(res, website);
}