Merge branch 'dev' into feat/um-66-update-clickhouse-schema-uuid

This commit is contained in:
Brian Cao 2022-10-10 16:20:07 -07:00
commit ec6454aead
65 changed files with 436 additions and 441 deletions

View file

@ -27,7 +27,7 @@ export default async (req, res) => {
const events = await getEventMetrics(websiteId, website_uuid, startDate, endDate, tz, unit, {
url,
event_name,
eventName: event_name,
});
return ok(res, events);

View file

@ -1,4 +1,4 @@
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
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';
@ -8,7 +8,7 @@ export default async (req, res) => {
const { id } = req.query;
const websiteId = +id;
const where = validate(id) ? { website_uuid: id } : { website_id: +id };
const where = validate(id) ? { websiteUuid: id } : { id: +id };
if (req.method === 'GET') {
await useCors(req, res);
@ -25,17 +25,19 @@ export default async (req, res) => {
if (req.method === 'POST') {
await useAuth(req, res);
const { is_admin: currentUserIsAdmin, user_id: currentUserId, account_uuid } = req.auth;
const { name, domain, owner, share_id } = req.body;
const { isAdmin: currentUserIsAdmin, userId: currentUserId, accountUuid } = req.auth;
const { name, domain, owner, enable_share_url } = req.body;
let account;
if (account_uuid) {
account = await getAccount({ account_uuid });
if (accountUuid) {
account = await getAccount({ accountUuid });
}
const website = await getWebsite(where);
if (website.user_id !== currentUserId && !currentUserIsAdmin) {
const shareId = enable_share_url ? website.shareId || getRandomChars(8) : null;
if (website.userId !== currentUserId && !currentUserIsAdmin) {
return unauthorized(res);
}
@ -43,8 +45,8 @@ export default async (req, res) => {
{
name,
domain,
share_id: share_id || null,
user_id: account ? account.id : +owner,
shareId: shareId,
userId: account ? account.id : +owner,
},
where,
);

View file

@ -1,4 +1,4 @@
import { getWebsiteStats } from 'queries';
import { getWebsiteStats, getWebsiteById } from 'queries';
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
import { allowQuery } from 'lib/auth';
import { useCors } from 'lib/middleware';
@ -11,10 +11,10 @@ export default async (req, res) => {
return unauthorized(res);
}
const { id, start_at, end_at, url, referrer, os, browser, device, country } = req.query;
const { website_id, start_at, end_at, url, referrer, os, browser, device, country } = req.query;
const websiteId = +id;
const website_uuid = id;
const websiteId = +website_id;
let websiteUuid = (await getWebsiteById(websiteId)).websiteUuid;
const startDate = new Date(+start_at);
const endDate = new Date(+end_at);
@ -22,7 +22,7 @@ export default async (req, res) => {
const prevStartDate = new Date(+start_at - distance);
const prevEndDate = new Date(+end_at - distance);
const metrics = await getWebsiteStats(websiteId, website_uuid, {
const metrics = await getWebsiteStats(websiteUuid, {
start_at: startDate,
end_at: endDate,
filters: {
@ -34,7 +34,7 @@ export default async (req, res) => {
country,
},
});
const prevPeriod = await getWebsiteStats(websiteId, website_uuid, {
const prevPeriod = await getWebsiteStats(websiteUuid, {
start_at: prevStartDate,
end_at: prevEndDate,
filters: {

View file

@ -6,44 +6,41 @@ import { uuid } from 'lib/crypto';
export default async (req, res) => {
await useAuth(req, res);
const { user_id: current_user_id, is_admin, account_uuid } = req.auth;
const { userId: currentUserId, isAdmin, accountUuid } = req.auth;
const { user_id, include_all } = req.query;
let account;
if (account_uuid) {
account = await getAccount({ account_uuid });
if (accountUuid) {
account = await getAccount({ accountUuid: accountUuid });
}
const userId = account ? account.user_id : +user_id;
const userId = account ? account.id : +user_id;
if (req.method === 'GET') {
if (userId && userId !== current_user_id && !is_admin) {
if (userId && userId !== currentUserId && !isAdmin) {
return unauthorized(res);
}
const websites =
is_admin && include_all
isAdmin && include_all
? await getAllWebsites()
: await getUserWebsites(userId || current_user_id);
: await getUserWebsites(userId || currentUserId);
return ok(res, websites);
}
if (req.method === 'POST') {
await useAuth(req, res);
const { is_admin: currentUserIsAdmin, user_id: currentUserId } = req.auth;
const { name, domain, owner, enable_share_url } = req.body;
const website_owner = account ? account.user_id : +owner;
const website_owner = account ? account.id : +owner;
if (website_owner !== currentUserId && !currentUserIsAdmin) {
if (website_owner !== currentUserId && !isAdmin) {
return unauthorized(res);
}
const website_uuid = uuid();
const share_id = enable_share_url ? getRandomChars(8) : null;
const website = await createWebsite(website_owner, { website_uuid, name, domain, share_id });
const websiteUuid = uuid();
const shareId = enable_share_url ? getRandomChars(8) : null;
const website = await createWebsite(website_owner, { websiteUuid, name, domain, shareId });
return ok(res, website);
}