Merge branch 'dev' into hosts-support

This commit is contained in:
Mike Cao 2024-06-18 23:02:14 -07:00 committed by GitHub
commit d1559c3a98
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
281 changed files with 7555 additions and 1973 deletions

View file

@ -10,7 +10,8 @@ import * as yup from 'yup';
export interface WebsitesRequestQuery extends PageParams {
userId?: string;
includeTeams?: boolean;
includeOwnedTeams?: boolean;
includeAllTeams?: boolean;
}
export interface WebsitesRequestBody {
@ -43,27 +44,42 @@ export default async (
return unauthorized(res);
}
const { userId, includeOwnedTeams } = req.query;
const { userId, includeOwnedTeams, includeAllTeams } = req.query;
const websites = await getWebsites(
{
where: {
OR: [
...(userId && [{ userId }]),
...(userId &&
includeOwnedTeams && [
{
team: {
deletedAt: null,
teamUser: {
some: {
role: ROLES.teamOwner,
userId,
...(userId && includeOwnedTeams
? [
{
team: {
deletedAt: null,
teamUser: {
some: {
role: ROLES.teamOwner,
userId,
},
},
},
},
},
]),
]
: []),
...(userId && includeAllTeams
? [
{
team: {
deletedAt: null,
teamUser: {
some: {
userId,
},
},
},
},
]
: []),
],
},
include: {

View file

@ -27,7 +27,7 @@ const schema: YupRequest = {
websiteId: yup.string().uuid().required(),
type: yup
.string()
.matches(/funnel|insights|retention|utm/i)
.matches(/funnel|insights|retention|utm|goals|journey/i)
.required(),
name: yup.string().max(200).required(),
description: yup.string().max(500),

View file

@ -0,0 +1,84 @@
import { canViewWebsite } from 'lib/auth';
import { useAuth, useCors, useValidate } from 'lib/middleware';
import { NextApiRequestQueryBody } from 'lib/types';
import { TimezoneTest } from 'lib/yup';
import { NextApiResponse } from 'next';
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
import { getGoals } from 'queries/analytics/reports/getGoals';
import * as yup from 'yup';
export interface RetentionRequestBody {
websiteId: string;
dateRange: { startDate: string; endDate: string; timezone: string };
goals: { type: string; value: string; goal: number }[];
}
const schema = {
POST: yup.object().shape({
websiteId: yup.string().uuid().required(),
dateRange: yup
.object()
.shape({
startDate: yup.date().required(),
endDate: yup.date().required(),
timezone: TimezoneTest,
})
.required(),
goals: yup
.array()
.of(
yup.object().shape({
type: yup
.string()
.matches(/url|event|event-data/i)
.required(),
value: yup.string().required(),
goal: yup.number().required(),
operator: yup
.string()
.matches(/count|sum|average/i)
.when('type', {
is: 'eventData',
then: yup.string().required(),
}),
property: yup.string().when('type', {
is: 'eventData',
then: yup.string().required(),
}),
}),
)
.min(1)
.required(),
}),
};
export default async (
req: NextApiRequestQueryBody<any, RetentionRequestBody>,
res: NextApiResponse,
) => {
await useCors(req, res);
await useAuth(req, res);
await useValidate(schema, req, res);
if (req.method === 'POST') {
const {
websiteId,
dateRange: { startDate, endDate },
goals,
} = req.body;
if (!(await canViewWebsite(req.auth, websiteId))) {
return unauthorized(res);
}
const data = await getGoals(websiteId, {
startDate: new Date(startDate),
endDate: new Date(endDate),
goals,
});
return ok(res, data);
}
return methodNotAllowed(res);
};

View file

@ -27,7 +27,7 @@ const schema = {
name: yup.string().max(200).required(),
type: yup
.string()
.matches(/funnel|insights|retention|utm/i)
.matches(/funnel|insights|retention|utm|goals|journey/i)
.required(),
description: yup.string().max(500),
parameters: yup
@ -66,11 +66,29 @@ export default async (
const data = await getReports(
{
where: {
userId: !teamId && !websiteId ? userId : undefined,
websiteId,
website: {
teamId,
},
OR: [
...(websiteId ? [{ websiteId }] : []),
...(teamId
? [
{
website: {
deletedAt: null,
teamId,
},
},
]
: []),
...(userId && !websiteId && !teamId
? [
{
website: {
deletedAt: null,
userId,
},
},
]
: []),
],
},
include: {
website: {

View file

@ -0,0 +1,66 @@
import { canViewWebsite } from 'lib/auth';
import { useAuth, useCors, useValidate } from 'lib/middleware';
import { NextApiRequestQueryBody } from 'lib/types';
import { NextApiResponse } from 'next';
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
import { getJourney } from 'queries';
import * as yup from 'yup';
export interface RetentionRequestBody {
websiteId: string;
dateRange: { startDate: string; endDate: string };
steps: number;
startStep?: string;
endStep?: string;
}
const schema = {
POST: yup.object().shape({
websiteId: yup.string().uuid().required(),
dateRange: yup
.object()
.shape({
startDate: yup.date().required(),
endDate: yup.date().required(),
})
.required(),
steps: yup.number().min(3).max(7).required(),
startStep: yup.string(),
endStep: yup.string(),
}),
};
export default async (
req: NextApiRequestQueryBody<any, RetentionRequestBody>,
res: NextApiResponse,
) => {
await useCors(req, res);
await useAuth(req, res);
await useValidate(schema, req, res);
if (req.method === 'POST') {
const {
websiteId,
dateRange: { startDate, endDate },
steps,
startStep,
endStep,
} = req.body;
if (!(await canViewWebsite(req.auth, websiteId))) {
return unauthorized(res);
}
const data = await getJourney(websiteId, {
startDate: new Date(startDate),
endDate: new Date(endDate),
steps,
startStep,
endStep,
});
return ok(res, data);
}
return methodNotAllowed(res);
};

View file

@ -87,7 +87,7 @@ export default async (req: NextApiRequestCollect, res: NextApiResponse) => {
if (req.method === 'POST') {
if (!process.env.DISABLE_BOT_CHECK && isbot(req.headers['user-agent'])) {
return ok(res);
return ok(res, { beep: 'boop' });
}
await useValidate(schema, req, res);
@ -144,7 +144,6 @@ export default async (req: NextApiRequestCollect, res: NextApiResponse) => {
eventData: data,
...session,
sessionId: session.id,
visitId: session.visitId,
});
}

View file

@ -2,11 +2,11 @@ import { useAuth, useCors, useValidate } from 'lib/middleware';
import { NextApiRequestQueryBody } from 'lib/types';
import { NextApiResponse } from 'next';
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
import { getAllWebsites, getEventDataUsage, getEventUsage } from 'queries';
import { getAllUserWebsitesIncludingTeamOwner, getEventDataUsage, getEventUsage } from 'queries';
import * as yup from 'yup';
export interface UserUsageRequestQuery {
id: string;
userId: string;
startAt: string;
endAt: string;
}
@ -24,7 +24,7 @@ export interface UserUsageRequestResponse {
const schema = {
GET: yup.object().shape({
id: yup.string().uuid().required(),
userId: yup.string().uuid().required(),
startAt: yup.number().integer().required(),
endAt: yup.number().integer().min(yup.ref<number>('startAt')).required(),
}),
@ -45,12 +45,12 @@ export default async (
return unauthorized(res);
}
const { id: userId, startAt, endAt } = req.query;
const { userId, startAt, endAt } = req.query;
const startDate = new Date(+startAt);
const endDate = new Date(+endAt);
const websites = await getAllWebsites(userId);
const websites = await getAllUserWebsitesIncludingTeamOwner(userId);
const websiteIds = websites.map(a => a.id);
@ -62,6 +62,7 @@ export default async (
websiteName: a.name,
websiteEventUsage: websiteEventUsage.find(b => a.id === b.websiteId)?.count || 0,
eventDataUsage: eventDataUsage.find(b => a.id === b.websiteId)?.count || 0,
deletedAt: a.deletedAt,
}));
const usage = websiteUsage.reduce(
@ -74,9 +75,13 @@ export default async (
{ websiteEventUsage: 0, eventDataUsage: 0 },
);
const filteredWebsiteUsage = websiteUsage.filter(
a => !a.deletedAt && (a.websiteEventUsage > 0 || a.eventDataUsage > 0),
);
return ok(res, {
...usage,
websites: websiteUsage,
websites: filteredWebsiteUsage,
});
}

View file

@ -9,7 +9,6 @@ import * as yup from 'yup';
const schema = {
GET: yup.object().shape({
userId: yup.string().uuid().required(),
teamId: yup.string().uuid(),
...pageInfo,
}),
};

View file

@ -1,3 +1,4 @@
import * as yup from 'yup';
import { canViewWebsite } from 'lib/auth';
import { useAuth, useCors, useValidate } from 'lib/middleware';
import { getRequestFilters, getRequestDateRange } from 'lib/request';
@ -5,6 +6,8 @@ import { NextApiRequestQueryBody, WebsitePageviews } from 'lib/types';
import { NextApiResponse } from 'next';
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
import { getPageviewStats, getSessionStats } from 'queries';
import { TimezoneTest, UnitTypeTest } from 'lib/yup';
import { getCompareDate } from 'lib/date';
export interface WebsitePageviewRequestQuery {
websiteId: string;
@ -22,10 +25,9 @@ export interface WebsitePageviewRequestQuery {
country?: string;
region: string;
city?: string;
compare?: string;
}
import { TimezoneTest, UnitTypeTest } from 'lib/yup';
import * as yup from 'yup';
const schema = {
GET: yup.object().shape({
websiteId: yup.string().uuid().required(),
@ -43,6 +45,7 @@ const schema = {
country: yup.string(),
region: yup.string(),
city: yup.string(),
compare: yup.string(),
}),
};
@ -54,7 +57,7 @@ export default async (
await useAuth(req, res);
await useValidate(schema, req, res);
const { websiteId, timezone } = req.query;
const { websiteId, timezone, compare } = req.query;
if (req.method === 'GET') {
if (!(await canViewWebsite(req.auth, websiteId))) {
@ -76,6 +79,40 @@ export default async (
getSessionStats(websiteId, filters),
]);
if (compare) {
const { startDate: compareStartDate, endDate: compareEndDate } = getCompareDate(
compare,
startDate,
endDate,
);
const [comparePageviews, compareSessions] = await Promise.all([
getPageviewStats(websiteId, {
...filters,
startDate: compareStartDate,
endDate: compareEndDate,
}),
getSessionStats(websiteId, {
...filters,
startDate: compareStartDate,
endDate: compareEndDate,
}),
]);
return ok(res, {
pageviews,
sessions,
startDate,
endDate,
compare: {
pageviews: comparePageviews,
sessions: compareSessions,
startDate: compareStartDate,
endDate: compareEndDate,
},
});
}
return ok(res, { pageviews, sessions });
}

View file

@ -1,4 +1,4 @@
import { subMinutes, differenceInMinutes } from 'date-fns';
import * as yup from 'yup';
import { NextApiResponse } from 'next';
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
import { canViewWebsite } from 'lib/auth';
@ -6,6 +6,7 @@ import { useAuth, useCors, useValidate } from 'lib/middleware';
import { NextApiRequestQueryBody, WebsiteStats } from 'lib/types';
import { getRequestFilters, getRequestDateRange } from 'lib/request';
import { getWebsiteStats } from 'queries';
import { getCompareDate } from 'lib/date';
export interface WebsiteStatsRequestQuery {
websiteId: string;
@ -23,9 +24,9 @@ export interface WebsiteStatsRequestQuery {
country?: string;
region?: string;
city?: string;
compare?: string;
}
import * as yup from 'yup';
const schema = {
GET: yup.object().shape({
websiteId: yup.string().uuid().required(),
@ -43,6 +44,7 @@ const schema = {
country: yup.string(),
region: yup.string(),
city: yup.string(),
compare: yup.string(),
}),
};
@ -54,7 +56,7 @@ export default async (
await useAuth(req, res);
await useValidate(schema, req, res);
const { websiteId } = req.query;
const { websiteId, compare } = req.query;
if (req.method === 'GET') {
if (!(await canViewWebsite(req.auth, websiteId))) {
@ -62,9 +64,11 @@ export default async (
}
const { startDate, endDate } = await getRequestDateRange(req);
const diff = differenceInMinutes(endDate, startDate);
const prevStartDate = subMinutes(startDate, diff);
const prevEndDate = subMinutes(endDate, diff);
const { startDate: compareStartDate, endDate: compareEndDate } = getCompareDate(
compare,
startDate,
endDate,
);
const filters = getRequestFilters(req);
@ -72,14 +76,14 @@ export default async (
const prevPeriod = await getWebsiteStats(websiteId, {
...filters,
startDate: prevStartDate,
endDate: prevEndDate,
startDate: compareStartDate,
endDate: compareEndDate,
});
const stats = Object.keys(metrics[0]).reduce((obj, key) => {
obj[key] = {
value: Number(metrics[0][key]) || 0,
change: Number(metrics[0][key]) - Number(prevPeriod[0][key]) || 0,
prev: Number(prevPeriod[0][key]) || 0,
};
return obj;
}, {});