This commit is contained in:
Mike Cao committed 2026-06-09 23:07:24 -07:00
commit 8227589ad9
35 files changed
+308 -145

No files matched your search

+32 -2
View File
@@ -3,9 +3,34 @@ import { uuid } from '@/lib/crypto';
import { parseRequest } from '@/lib/request';
import { json, unauthorized } from '@/lib/response';
import { pagingParams, reportSchema, reportTypeParam } from '@/lib/schema';
import { canUpdateWebsite, canViewAuthenticatedWebsite } from '@/permissions';
import {
canUpdateWebsite,
canViewAuthenticatedWebsite,
canViewWebsiteSection,
} from '@/permissions';
import type { ShareSection } from '@/permissions';
import { createReport, getReports } from '@/queries/prisma';
function getReportSection(type?: z.infer<typeof reportTypeParam>): ShareSection | null {
switch (type) {
case 'attribution':
case 'breakdown':
case 'performance':
case 'retention':
case 'revenue':
case 'utm':
return type;
case 'funnel':
return 'funnels';
case 'goal':
return 'goals';
case 'journey':
return 'journeys';
default:
return null;
}
}
export async function GET(request: Request) {
const schema = z.object({
websiteId: z.uuid(),
@@ -26,7 +51,12 @@ export async function GET(request: Request) {
search,
};
if (!(await canViewAuthenticatedWebsite(auth, websiteId))) {
const section = getReportSection(type);
const canView = section
? await canViewWebsiteSection(auth, websiteId, section)
: await canViewAuthenticatedWebsite(auth, websiteId);
if (!canView) {
return unauthorized();
}
@@ -3,7 +3,7 @@ import { parseEventPropertyFilters } from '@/lib/params';
import { getQueryFilters, parseRequest } from '@/lib/request';
import { json, unauthorized } from '@/lib/response';
import { filterParams, timezoneParam, unitParam } from '@/lib/schema';
import { canViewAuthenticatedWebsite } from '@/permissions';
import { canViewWebsiteSection } from '@/permissions';
import { getEventDataArraySeries } from '@/queries/sql/events/getEventDataArraySeries';
export async function GET(
@@ -28,7 +28,7 @@ export async function GET(
const { websiteId } = await params;
if (!(await canViewAuthenticatedWebsite(auth, websiteId))) {
if (!(await canViewWebsiteSection(auth, websiteId, 'events'))) {
return unauthorized();
}
@@ -3,7 +3,7 @@ import { parseEventPropertyFilters } from '@/lib/params';
import { getQueryFilters, parseRequest } from '@/lib/request';
import { json, unauthorized } from '@/lib/response';
import { filterParams, timezoneParam } from '@/lib/schema';
import { canViewAuthenticatedWebsite } from '@/permissions';
import { canViewWebsiteSection } from '@/permissions';
import { getEventDataDateSeries } from '@/queries/sql/events/getEventDataDateSeries';
export async function GET(
@@ -27,7 +27,7 @@ export async function GET(
const { websiteId } = await params;
if (!(await canViewAuthenticatedWebsite(auth, websiteId))) {
if (!(await canViewWebsiteSection(auth, websiteId, 'events'))) {
return unauthorized();
}
@@ -3,7 +3,7 @@ import { parseEventPropertyFilters } from '@/lib/params';
import { getQueryFilters, parseRequest } from '@/lib/request';
import { json, unauthorized } from '@/lib/response';
import { filterParams, timezoneParam, unitParam } from '@/lib/schema';
import { canViewAuthenticatedWebsite } from '@/permissions';
import { canViewWebsiteSection } from '@/permissions';
import { getEventDataNumericSeries } from '@/queries/sql/events/getEventDataNumericSeries';
export async function GET(
@@ -29,7 +29,7 @@ export async function GET(
const { websiteId } = await params;
if (!(await canViewAuthenticatedWebsite(auth, websiteId))) {
if (!(await canViewWebsiteSection(auth, websiteId, 'events'))) {
return unauthorized();
}
@@ -3,7 +3,7 @@ import { parseEventPropertyFilters } from '@/lib/params';
import { getQueryFilters, parseRequest } from '@/lib/request';
import { json, unauthorized } from '@/lib/response';
import { filterParams } from '@/lib/schema';
import { canViewAuthenticatedWebsite } from '@/permissions';
import { canViewWebsiteSection } from '@/permissions';
import { getEventDataNumericStats } from '@/queries/sql/events/getEventDataNumericStats';
export async function GET(
@@ -26,7 +26,7 @@ export async function GET(
const { websiteId } = await params;
if (!(await canViewAuthenticatedWebsite(auth, websiteId))) {
if (!(await canViewWebsiteSection(auth, websiteId, 'events'))) {
return unauthorized();
}
@@ -3,7 +3,7 @@ import { parseEventPropertyFilters } from '@/lib/params';
import { getQueryFilters, parseRequest } from '@/lib/request';
import { json, unauthorized } from '@/lib/response';
import { filterParams, timezoneParam, unitParam } from '@/lib/schema';
import { canViewAuthenticatedWebsite } from '@/permissions';
import { canViewWebsiteSection } from '@/permissions';
import { getEventDataPropertySeries } from '@/queries/sql/events/getEventDataPropertySeries';
export async function GET(
@@ -28,7 +28,7 @@ export async function GET(
const { websiteId } = await params;
if (!(await canViewAuthenticatedWebsite(auth, websiteId))) {
if (!(await canViewWebsiteSection(auth, websiteId, 'events'))) {
return unauthorized();
}
@@ -3,7 +3,7 @@ import { parseEventPropertyFilters } from '@/lib/params';
import { getQueryFilters, parseRequest } from '@/lib/request';
import { json, unauthorized } from '@/lib/response';
import { filterParams, pagingParams, timezoneParam, unitParam } from '@/lib/schema';
import { canViewAuthenticatedWebsite } from '@/permissions';
import { canViewWebsiteSection } from '@/permissions';
import { getEventDataPivot } from '@/queries/sql/events/getEventDataPivot';
export async function GET(
@@ -28,7 +28,7 @@ export async function GET(
const { websiteId } = await params;
if (!(await canViewAuthenticatedWebsite(auth, websiteId))) {
if (!(await canViewWebsiteSection(auth, websiteId, 'events'))) {
return unauthorized();
}
@@ -1,6 +1,6 @@
import { parseRequest } from '@/lib/request';
import { json, unauthorized } from '@/lib/response';
import { canViewAuthenticatedWebsite } from '@/permissions';
import { canViewWebsiteSection } from '@/permissions';
import { getEventDataById } from '@/queries/sql/events/getEventDataById';
export async function GET(
@@ -15,7 +15,7 @@ export async function GET(
const { websiteId, eventId } = await params;
if (!(await canViewAuthenticatedWebsite(auth, websiteId))) {
if (!(await canViewWebsiteSection(auth, websiteId, 'events'))) {
return unauthorized();
}
@@ -2,7 +2,7 @@ import { z } from 'zod';
import { getQueryFilters, parseRequest } from '@/lib/request';
import { json, unauthorized } from '@/lib/response';
import { filterParams } from '@/lib/schema';
import { canViewAuthenticatedWebsite } from '@/permissions';
import { canViewWebsiteSection } from '@/permissions';
import { getEventDataEvents } from '@/queries/sql/events/getEventDataEvents';
export async function GET(
@@ -23,7 +23,7 @@ export async function GET(
const { websiteId } = await params;
if (!(await canViewAuthenticatedWebsite(auth, websiteId))) {
if (!(await canViewWebsiteSection(auth, websiteId, 'events'))) {
return unauthorized();
}
@@ -2,7 +2,7 @@ import { z } from 'zod';
import { getQueryFilters, parseRequest } from '@/lib/request';
import { json, unauthorized } from '@/lib/response';
import { filterParams } from '@/lib/schema';
import { canViewAuthenticatedWebsite } from '@/permissions';
import { canViewWebsiteSection } from '@/permissions';
import { getEventDataFields } from '@/queries/sql';
export async function GET(
@@ -23,7 +23,7 @@ export async function GET(
const { websiteId } = await params;
if (!(await canViewAuthenticatedWebsite(auth, websiteId))) {
if (!(await canViewWebsiteSection(auth, websiteId, 'events'))) {
return unauthorized();
}
@@ -2,7 +2,7 @@ import { z } from 'zod';
import { getQueryFilters, parseRequest } from '@/lib/request';
import { json, unauthorized } from '@/lib/response';
import { filterParams } from '@/lib/schema';
import { canViewAuthenticatedWebsite } from '@/permissions';
import { canViewWebsiteSection } from '@/permissions';
import { getEventDataProperties } from '@/queries/sql';
export async function GET(
@@ -23,7 +23,7 @@ export async function GET(
const { websiteId } = await params;
if (!(await canViewAuthenticatedWebsite(auth, websiteId))) {
if (!(await canViewWebsiteSection(auth, websiteId, 'events'))) {
return unauthorized();
}
@@ -2,7 +2,7 @@ import { z } from 'zod';
import { getQueryFilters, parseRequest } from '@/lib/request';
import { json, unauthorized } from '@/lib/response';
import { filterParams, pagingParams } from '@/lib/schema';
import { canViewAuthenticatedWebsite } from '@/permissions';
import { canViewWebsiteSection } from '@/permissions';
import { getEventData } from '@/queries/sql/events/getEventData';
export async function GET(
@@ -24,7 +24,7 @@ export async function GET(
const { websiteId } = await params;
if (!(await canViewAuthenticatedWebsite(auth, websiteId))) {
if (!(await canViewWebsiteSection(auth, websiteId, 'events'))) {
return unauthorized();
}
@@ -2,7 +2,7 @@ import { z } from 'zod';
import { getQueryFilters, parseRequest } from '@/lib/request';
import { json, unauthorized } from '@/lib/response';
import { filterParams } from '@/lib/schema';
import { canViewAuthenticatedWebsite } from '@/permissions';
import { canViewWebsiteSection } from '@/permissions';
import { getEventDataStats } from '@/queries/sql';
export async function GET(
@@ -23,7 +23,7 @@ export async function GET(
const { websiteId } = await params;
if (!(await canViewAuthenticatedWebsite(auth, websiteId))) {
if (!(await canViewWebsiteSection(auth, websiteId, 'events'))) {
return unauthorized();
}
@@ -2,7 +2,7 @@ import { z } from 'zod';
import { getQueryFilters, parseRequest } from '@/lib/request';
import { json, unauthorized } from '@/lib/response';
import { filterParams } from '@/lib/schema';
import { canViewAuthenticatedWebsite } from '@/permissions';
import { canViewWebsiteSection } from '@/permissions';
import { getEventDataValues } from '@/queries/sql';
export async function GET(
@@ -26,7 +26,7 @@ export async function GET(
const { websiteId } = await params;
if (!(await canViewAuthenticatedWebsite(auth, websiteId))) {
if (!(await canViewWebsiteSection(auth, websiteId, 'events'))) {
return unauthorized();
}
@@ -2,7 +2,7 @@ import { z } from 'zod';
import { parseRequest } from '@/lib/request';
import { json, notFound, ok, unauthorized } from '@/lib/response';
import { anyObjectParam, segmentTypeParam } from '@/lib/schema';
import { canDeleteWebsite, canUpdateWebsite, canViewAuthenticatedWebsite } from '@/permissions';
import { canDeleteWebsite, canUpdateWebsite, canViewSharedWebsiteFilters } from '@/permissions';
import { deleteSegment, getWebsiteSegment, updateSegment } from '@/queries/prisma';
export async function GET(
@@ -17,7 +17,7 @@ export async function GET(
const { websiteId, segmentId } = await params;
if (!(await canViewAuthenticatedWebsite(auth, websiteId))) {
if (!(await canViewSharedWebsiteFilters(auth, websiteId))) {
return unauthorized();
}
@@ -3,7 +3,7 @@ import { uuid } from '@/lib/crypto';
import { getQueryFilters, parseRequest } from '@/lib/request';
import { json, unauthorized } from '@/lib/response';
import { searchParams, segmentParamSchema, segmentTypeParam } from '@/lib/schema';
import { canUpdateWebsite, canViewAuthenticatedWebsite } from '@/permissions';
import { canUpdateWebsite, canViewSharedWebsiteFilters } from '@/permissions';
import { createSegment, getWebsiteSegments } from '@/queries/prisma';
export async function GET(
@@ -24,7 +24,7 @@ export async function GET(
const { websiteId } = await params;
const { type } = query;
if (websiteId && !(await canViewAuthenticatedWebsite(auth, websiteId))) {
if (websiteId && !(await canViewSharedWebsiteFilters(auth, websiteId))) {
return unauthorized();
}
@@ -3,7 +3,7 @@ import { parsePropertyFilters } from '@/lib/params';
import { getQueryFilters, parseRequest } from '@/lib/request';
import { json, unauthorized } from '@/lib/response';
import { filterParams, pagingParams, timezoneParam, unitParam } from '@/lib/schema';
import { canViewAuthenticatedWebsite } from '@/permissions';
import { canViewWebsiteSection } from '@/permissions';
import { getSessionDataPivot } from '@/queries/sql/sessions/getSessionDataPivot';
export async function GET(
@@ -28,7 +28,7 @@ export async function GET(
const { websiteId } = await params;
if (!(await canViewAuthenticatedWebsite(auth, websiteId))) {
if (!(await canViewWebsiteSection(auth, websiteId, 'sessions'))) {
return unauthorized();
}
@@ -3,7 +3,7 @@ import { parsePropertyFilters } from '@/lib/params';
import { getQueryFilters, parseRequest } from '@/lib/request';
import { json, unauthorized } from '@/lib/response';
import { filterParams, timezoneParam, unitParam } from '@/lib/schema';
import { canViewAuthenticatedWebsite } from '@/permissions';
import { canViewWebsiteSection } from '@/permissions';
import { getSessionDataArraySeries } from '@/queries/sql';
export async function GET(
@@ -27,7 +27,7 @@ export async function GET(
const { websiteId } = await params;
if (!(await canViewAuthenticatedWebsite(auth, websiteId))) {
if (!(await canViewWebsiteSection(auth, websiteId, 'sessions'))) {
return unauthorized();
}
@@ -3,7 +3,7 @@ import { parsePropertyFilters } from '@/lib/params';
import { getQueryFilters, parseRequest } from '@/lib/request';
import { json, unauthorized } from '@/lib/response';
import { filterParams, timezoneParam } from '@/lib/schema';
import { canViewAuthenticatedWebsite } from '@/permissions';
import { canViewWebsiteSection } from '@/permissions';
import { getSessionDataDateSeries } from '@/queries/sql';
export async function GET(
@@ -26,7 +26,7 @@ export async function GET(
const { websiteId } = await params;
if (!(await canViewAuthenticatedWebsite(auth, websiteId))) {
if (!(await canViewWebsiteSection(auth, websiteId, 'sessions'))) {
return unauthorized();
}
@@ -3,7 +3,7 @@ import { parsePropertyFilters } from '@/lib/params';
import { getQueryFilters, parseRequest } from '@/lib/request';
import { json, unauthorized } from '@/lib/response';
import { filterParams, timezoneParam, unitParam } from '@/lib/schema';
import { canViewAuthenticatedWebsite } from '@/permissions';
import { canViewWebsiteSection } from '@/permissions';
import { getSessionDataNumericSeries } from '@/queries/sql';
export async function GET(
@@ -28,7 +28,7 @@ export async function GET(
const { websiteId } = await params;
if (!(await canViewAuthenticatedWebsite(auth, websiteId))) {
if (!(await canViewWebsiteSection(auth, websiteId, 'sessions'))) {
return unauthorized();
}
@@ -3,7 +3,7 @@ import { parsePropertyFilters } from '@/lib/params';
import { getQueryFilters, parseRequest } from '@/lib/request';
import { json, unauthorized } from '@/lib/response';
import { filterParams, timezoneParam } from '@/lib/schema';
import { canViewAuthenticatedWebsite } from '@/permissions';
import { canViewWebsiteSection } from '@/permissions';
import { getSessionDataNumericStats } from '@/queries/sql';
export async function GET(
@@ -26,7 +26,7 @@ export async function GET(
const { websiteId } = await params;
if (!(await canViewAuthenticatedWebsite(auth, websiteId))) {
if (!(await canViewWebsiteSection(auth, websiteId, 'sessions'))) {
return unauthorized();
}
@@ -3,7 +3,7 @@ import { parsePropertyFilters } from '@/lib/params';
import { getQueryFilters, parseRequest } from '@/lib/request';
import { json, unauthorized } from '@/lib/response';
import { filterParams } from '@/lib/schema';
import { canViewAuthenticatedWebsite } from '@/permissions';
import { canViewWebsiteSection } from '@/permissions';
import { getSessionDataProperties } from '@/queries/sql';
export async function GET(
@@ -25,7 +25,7 @@ export async function GET(
const { websiteId } = await params;
if (!(await canViewAuthenticatedWebsite(auth, websiteId))) {
if (!(await canViewWebsiteSection(auth, websiteId, 'sessions'))) {
return unauthorized();
}
@@ -3,7 +3,7 @@ import { parsePropertyFilters } from '@/lib/params';
import { getQueryFilters, parseRequest } from '@/lib/request';
import { json, unauthorized } from '@/lib/response';
import { filterParams, timezoneParam, unitParam } from '@/lib/schema';
import { canViewAuthenticatedWebsite } from '@/permissions';
import { canViewWebsiteSection } from '@/permissions';
import { getSessionDataPropertySeries } from '@/queries/sql';
export async function GET(
@@ -27,7 +27,7 @@ export async function GET(
const { websiteId } = await params;
if (!(await canViewAuthenticatedWebsite(auth, websiteId))) {
if (!(await canViewWebsiteSection(auth, websiteId, 'sessions'))) {
return unauthorized();
}
@@ -3,7 +3,7 @@ import { parsePropertyFilters } from '@/lib/params';
import { getQueryFilters, parseRequest } from '@/lib/request';
import { json, unauthorized } from '@/lib/response';
import { filterParams, timezoneParam, unitParam } from '@/lib/schema';
import { canViewAuthenticatedWebsite } from '@/permissions';
import { canViewWebsiteSection } from '@/permissions';
import { getSessionDataActivityStats } from '@/queries/sql/sessions/getSessionDataActivityStats';
export async function GET(
@@ -27,7 +27,7 @@ export async function GET(
const { websiteId } = await params;
if (!(await canViewAuthenticatedWebsite(auth, websiteId))) {
if (!(await canViewWebsiteSection(auth, websiteId, 'sessions'))) {
return unauthorized();
}
@@ -2,7 +2,7 @@ import { z } from 'zod';
import { getQueryFilters, parseRequest } from '@/lib/request';
import { json, unauthorized } from '@/lib/response';
import { filterParams } from '@/lib/schema';
import { canViewAuthenticatedWebsite } from '@/permissions';
import { canViewWebsiteSection } from '@/permissions';
import { getSessionDataValues } from '@/queries/sql';
export async function GET(
@@ -25,7 +25,7 @@ export async function GET(
const { websiteId } = await params;
if (!(await canViewAuthenticatedWebsite(auth, websiteId))) {
if (!(await canViewWebsiteSection(auth, websiteId, 'sessions'))) {
return unauthorized();
}
@@ -1,7 +1,7 @@
import { z } from 'zod';
import { getQueryFilters, parseRequest } from '@/lib/request';
import { json, unauthorized } from '@/lib/response';
import { canViewAuthenticatedWebsite } from '@/permissions';
import { canViewWebsiteSection } from '@/permissions';
import { getSessionActivity } from '@/queries/sql';
export async function GET(
@@ -21,7 +21,14 @@ export async function GET(
const { websiteId, sessionId } = await params;
if (!(await canViewAuthenticatedWebsite(auth, websiteId))) {
if (
!(await canViewWebsiteSection(auth, websiteId, [
'sessions',
'events',
'realtime',
'revenue',
]))
) {
return unauthorized();
}
@@ -1,6 +1,6 @@
import { parseRequest } from '@/lib/request';
import { json, unauthorized } from '@/lib/response';
import { canViewAuthenticatedWebsite } from '@/permissions';
import { canViewWebsiteSection } from '@/permissions';
import { getSessionData } from '@/queries/sql';
export async function GET(
@@ -15,7 +15,14 @@ export async function GET(
const { websiteId, sessionId } = await params;
if (!(await canViewAuthenticatedWebsite(auth, websiteId))) {
if (
!(await canViewWebsiteSection(auth, websiteId, [
'sessions',
'events',
'realtime',
'revenue',
]))
) {
return unauthorized();
}
@@ -1,6 +1,6 @@
import { parseRequest } from '@/lib/request';
import { json, unauthorized } from '@/lib/response';
import { canViewAuthenticatedWebsite } from '@/permissions';
import { canViewWebsiteSection } from '@/permissions';
import { getWebsiteSession } from '@/queries/sql';
export async function GET(
@@ -15,7 +15,14 @@ export async function GET(
const { websiteId, sessionId } = await params;
if (!(await canViewAuthenticatedWebsite(auth, websiteId))) {
if (
!(await canViewWebsiteSection(auth, websiteId, [
'sessions',
'events',
'realtime',
'revenue',
]))
) {
return unauthorized();
}
@@ -24,7 +24,7 @@ export async function GET(
const { websiteId } = await params;
if (!(await canViewWebsiteSection(auth, websiteId, 'sessions'))) {
if (!(await canViewWebsiteSection(auth, websiteId, ['overview', 'sessions']))) {
return unauthorized();
}
+13 -2
View File
@@ -5,6 +5,7 @@ import { createContext, type ReactNode, useEffect } from 'react';
import { useShareTokenQuery } from '@/components/hooks';
import { ENTITY_TYPE } from '@/lib/constants';
import type { ShareParameters, WhiteLabel } from '@/lib/types';
import { setShareData, useApp } from '@/store/app';
export interface ShareData {
shareId: string;
@@ -22,6 +23,8 @@ export interface ShareData {
export const ShareContext = createContext<ShareData>(null);
const selector = (state: { shareToken: { token?: string } | null }) => state.shareToken;
const ALL_SECTION_IDS = [
'overview',
'events',
@@ -55,8 +58,10 @@ export function ShareProvider({ slug, children }: { slug: string; children: Reac
const { share, isLoading, isFetching } = useShareTokenQuery(slug);
const router = useRouter();
const pathname = usePathname();
const shareToken = useApp(selector);
const path = getSharePath(pathname);
const isWebsiteShare = share?.shareType === ENTITY_TYPE.website;
const isShareReady = !!share?.token && shareToken?.token === share.token;
const allowedSections =
isWebsiteShare && share?.parameters
@@ -75,11 +80,17 @@ export function ShareProvider({ slug, children }: { slug: string; children: Reac
}
}, [shouldRedirect, slug, allowedSections, router]);
if (isFetching && isLoading) {
useEffect(() => {
return () => {
setShareData(null, null);
};
}, [slug]);
if ((isFetching && isLoading) || (share && !isShareReady)) {
return <Loading placement="absolute" />;
}
if (!share || shouldRedirect) {
if (!share || !isShareReady || shouldRedirect) {
return null;
}
@@ -1,18 +1,24 @@
import { setShareData } from '@/store/app';
import { useEffect } from 'react';
import { setShareData, useApp } from '@/store/app';
import { useApi } from '../useApi';
export function useShareTokenQuery(slug: string) {
const { get, useQuery } = useApi();
const shareId = useApp(state => state.share?.shareId);
const shareToken = useApp(state => state.shareToken?.token);
const query = useQuery({
queryKey: ['share', slug],
queryFn: async () => {
const data = await get(`/share/${slug}`);
setShareData(data, { token: data?.token });
return data;
},
queryFn: async () => get(`/share/${slug}`),
});
useEffect(() => {
if (
query.data?.token &&
(shareId !== query.data.shareId || shareToken !== query.data.token)
) {
setShareData(query.data, { token: query.data.token });
}
}, [query.data, shareId, shareToken]);
return { share: query.data, ...query };
}
+4 -8
View File
@@ -1,5 +1,4 @@
import { useMutation, useQuery } from '@tanstack/react-query';
import { usePathname } from 'next/navigation';
import { useCallback } from 'react';
import { getApiUrl } from '@/lib/api-url';
import { getClientAuthToken } from '@/lib/client';
@@ -7,8 +6,6 @@ import { SHARE_CONTEXT_HEADER, SHARE_TOKEN_HEADER } from '@/lib/constants';
import { type FetchResponse, httpDelete, httpGet, httpPost, httpPut } from '@/lib/fetch';
import { useApp } from '@/store/app';
const selector = (state: { shareToken: { token?: string } }) => state.shareToken;
async function handleResponse(res: FetchResponse): Promise<any> {
if (!res.ok) {
const { message, code, status } = res?.data?.error || {};
@@ -19,13 +16,12 @@ async function handleResponse(res: FetchResponse): Promise<any> {
}
export function useApi() {
const shareToken = useApp(selector);
const pathname = usePathname();
const isSharePath = pathname?.startsWith('/share');
const shareId = useApp(state => state.share?.shareId);
const shareToken = useApp(state => state.shareToken?.token);
const shareHeaders =
isSharePath && shareToken?.token
? { [SHARE_TOKEN_HEADER]: shareToken.token, [SHARE_CONTEXT_HEADER]: '1' }
shareId && shareToken
? { [SHARE_TOKEN_HEADER]: shareToken, [SHARE_CONTEXT_HEADER]: '1' }
: {};
const defaultHeaders = {
+148
View File
@@ -0,0 +1,148 @@
import { expect, test, vi } from 'vitest';
import { ENTITY_TYPE } from '@/lib/constants';
import {
canViewSharedWebsite,
canViewSharedWebsiteFilters,
canViewWebsiteSection,
} from './share';
vi.mock('./website', () => ({
canViewWebsite: vi.fn(),
}));
test('canViewWebsiteSection allows board shares for included websites', async () => {
await expect(
canViewWebsiteSection(
{
shareToken: {
shareType: ENTITY_TYPE.board,
websiteIds: ['website-1'],
parameters: {},
},
},
'website-1',
'goals',
),
).resolves.toBe(true);
});
test('canViewWebsiteSection respects section flags on website shares', async () => {
await expect(
canViewWebsiteSection(
{
shareToken: {
shareType: ENTITY_TYPE.website,
websiteId: 'website-1',
parameters: {
overview: true,
goals: false,
},
},
},
'website-1',
'goals',
),
).resolves.toBe(false);
});
test('canViewWebsiteSection allows any requested enabled section', async () => {
await expect(
canViewWebsiteSection(
{
shareToken: {
shareType: ENTITY_TYPE.website,
websiteId: 'website-1',
parameters: {
overview: true,
compare: false,
},
},
},
'website-1',
['overview', 'compare'],
),
).resolves.toBe(true);
});
test('canViewSharedWebsite allows board shares for included websites', async () => {
await expect(
canViewSharedWebsite(
{
shareToken: {
shareType: ENTITY_TYPE.board,
websiteIds: ['website-1'],
parameters: {},
},
},
'website-1',
),
).resolves.toBe(true);
});
test('canViewSharedWebsiteFilters requires allowFilter for share tokens', async () => {
await expect(
canViewSharedWebsiteFilters(
{
shareToken: {
shareType: ENTITY_TYPE.website,
websiteId: 'website-1',
parameters: {
allowFilter: false,
},
},
},
'website-1',
),
).resolves.toBe(false);
await expect(
canViewSharedWebsiteFilters(
{
shareToken: {
shareType: ENTITY_TYPE.website,
websiteId: 'website-1',
parameters: {
allowFilter: true,
},
},
},
'website-1',
),
).resolves.toBe(true);
});
test('canViewWebsiteSection allows pixel shares for the shared entity id', async () => {
await expect(
canViewWebsiteSection(
{
shareToken: {
shareType: ENTITY_TYPE.pixel,
pixelId: 'pixel-1',
parameters: {
overview: true,
},
},
},
'pixel-1',
'overview',
),
).resolves.toBe(true);
});
test('canViewWebsiteSection allows link shares for the shared entity id', async () => {
await expect(
canViewWebsiteSection(
{
shareToken: {
shareType: ENTITY_TYPE.link,
linkId: 'link-1',
parameters: {
overview: true,
},
},
},
'link-1',
'overview',
),
).resolves.toBe(true);
});
+21 -9
View File
@@ -40,7 +40,14 @@ type ShareSectionInput = ShareSection | ShareSection[];
function shareTokenIncludesWebsite(auth: Auth | null | undefined, websiteId: string) {
const { shareToken } = auth || {};
return shareToken?.websiteId === websiteId || shareToken?.websiteIds?.includes(websiteId);
return (
shareToken?.websiteId === websiteId ||
shareToken?.pixelId === websiteId ||
shareToken?.linkId === websiteId ||
shareToken?.websiteIds?.includes(websiteId) ||
shareToken?.pixelIds?.includes(websiteId) ||
shareToken?.linkIds?.includes(websiteId)
);
}
export async function canViewWebsiteSection(
@@ -54,11 +61,7 @@ export async function canViewWebsiteSection(
const { shareToken } = auth || {};
if (
!shareToken ||
shareToken.shareType !== ENTITY_TYPE.website ||
!shareTokenIncludesWebsite(auth, websiteId)
) {
if (!shareToken || !shareTokenIncludesWebsite(auth, websiteId)) {
return false;
}
@@ -79,11 +82,20 @@ export async function canViewSharedWebsite(auth: Auth | null | undefined, websit
return canViewWebsite(auth, websiteId);
}
return shareTokenIncludesWebsite(auth, websiteId);
}
export async function canViewSharedWebsiteFilters(
auth: Auth | null | undefined,
websiteId: string,
) {
if (auth?.user) {
return canViewWebsite(auth, websiteId);
}
const { shareToken } = auth || {};
return (
shareToken?.shareType === ENTITY_TYPE.website && shareTokenIncludesWebsite(auth, websiteId)
);
return shareTokenIncludesWebsite(auth, websiteId) && shareToken?.parameters?.allowFilter !== false;
}
export async function canViewAuthenticatedWebsite(