diff --git a/lib/auth.js b/lib/auth.js index ae66ce81..fc34b93b 100644 --- a/lib/auth.js +++ b/lib/auth.js @@ -52,7 +52,7 @@ export function isValidToken(token, validation) { export async function allowQuery(req, type) { const { id } = req.query; - const { id: userId, isAdmin, shareToken } = req.auth ?? {}; + const { userId, isAdmin, shareToken } = req.auth ?? {}; if (isAdmin) { return true; diff --git a/pages/api/realtime/init.js b/pages/api/realtime/init.js index 6b980a0d..1c3265c0 100644 --- a/pages/api/realtime/init.js +++ b/pages/api/realtime/init.js @@ -10,7 +10,7 @@ export default async (req, res) => { if (req.method === 'GET') { const { userId } = req.auth; - const websites = await getUserWebsites({ userId }); + const websites = await getUserWebsites(userId); const ids = websites.map(({ id }) => id); const token = createToken({ websites: ids }, secret()); const data = await getRealtimeData(ids, subMinutes(new Date(), 30)); diff --git a/pages/api/websites/index.js b/pages/api/websites/index.js index 32d5e264..8c947a4f 100644 --- a/pages/api/websites/index.js +++ b/pages/api/websites/index.js @@ -7,12 +7,13 @@ export default async (req, res) => { await useCors(req, res); await useAuth(req, res); - const { id, isAdmin } = req.auth; + const { userId, isAdmin } = req.auth; if (req.method === 'GET') { const { include_all } = req.query; - const websites = isAdmin && include_all ? await getAllWebsites() : await getUserWebsites(id); + const websites = + isAdmin && include_all ? await getAllWebsites() : await getUserWebsites(userId); return ok(res, websites); } @@ -21,7 +22,7 @@ export default async (req, res) => { const { name, domain, enableShareUrl } = req.body; const shareId = enableShareUrl ? getRandomChars(8) : null; - const website = await createWebsite(id, { id: uuid(), name, domain, shareId }); + const website = await createWebsite(userId, { id: uuid(), name, domain, shareId }); return ok(res, website); } diff --git a/queries/admin/user/deleteUser.js b/queries/admin/user/deleteUser.js index a0a8a34f..ed4cf51b 100644 --- a/queries/admin/user/deleteUser.js +++ b/queries/admin/user/deleteUser.js @@ -8,19 +8,25 @@ export async function deleteUser(userId) { where: { userId }, }); + let websiteIds = []; + + if (websites.length > 0) { + websiteIds = websites.map(a => a.id); + } + return client .$transaction([ client.pageview.deleteMany({ - where: { session: { website: { userId } } }, + where: { websiteId: { in: websiteIds } }, }), client.eventData.deleteMany({ - where: { event: { session: { website: { userId } } } }, + where: { event: { websiteId: { in: websiteIds } } }, }), client.event.deleteMany({ - where: { session: { website: { userId } } }, + where: { websiteId: { in: websiteIds } }, }), client.session.deleteMany({ - where: { website: { userId } }, + where: { websiteId: { in: websiteIds } }, }), client.website.deleteMany({ where: { userId }, diff --git a/queries/admin/website/deleteWebsite.js b/queries/admin/website/deleteWebsite.js index 241af156..8ab4f2bc 100644 --- a/queries/admin/website/deleteWebsite.js +++ b/queries/admin/website/deleteWebsite.js @@ -6,16 +6,16 @@ export async function deleteWebsite(id) { return transaction([ client.pageview.deleteMany({ - where: { session: { website: { id } } }, + where: { websiteId: id }, }), client.eventData.deleteMany({ - where: { event: { session: { website: { id } } } }, + where: { event: { websiteId: id } }, }), client.event.deleteMany({ - where: { session: { website: { id } } }, + where: { websiteId: id }, }), client.session.deleteMany({ - where: { website: { id } }, + where: { websiteId: id }, }), client.website.delete({ where: { id }, diff --git a/queries/admin/website/resetWebsite.js b/queries/admin/website/resetWebsite.js index 68cae61e..7da3183a 100644 --- a/queries/admin/website/resetWebsite.js +++ b/queries/admin/website/resetWebsite.js @@ -5,16 +5,16 @@ export async function resetWebsite(id) { return transaction([ client.pageview.deleteMany({ - where: { session: { website: { id } } }, + where: { websiteId: id }, }), client.eventData.deleteMany({ - where: { event: { session: { website: { id } } } }, + where: { event: { websiteId: id } }, }), client.event.deleteMany({ - where: { session: { website: { id } } }, + where: { websiteId: id }, }), client.session.deleteMany({ - where: { website: { id } }, + where: { websiteId: id }, }), ]); } diff --git a/queries/analytics/event/getEvents.js b/queries/analytics/event/getEvents.js index dde19992..81a187ce 100644 --- a/queries/analytics/event/getEvents.js +++ b/queries/analytics/event/getEvents.js @@ -12,10 +12,8 @@ export function getEvents(...args) { function relationalQuery(websites, start_at) { return prisma.client.event.findMany({ where: { - website: { - id: { - in: websites, - }, + websiteId: { + in: websites, }, createdAt: { gte: start_at, diff --git a/queries/analytics/pageview/getPageviews.js b/queries/analytics/pageview/getPageviews.js index e3b5cbc4..6dbb4b30 100644 --- a/queries/analytics/pageview/getPageviews.js +++ b/queries/analytics/pageview/getPageviews.js @@ -12,10 +12,8 @@ export async function getPageviews(...args) { async function relationalQuery(websites, start_at) { return prisma.client.pageview.findMany({ where: { - website: { - id: { - in: websites, - }, + websiteId: { + in: websites, }, createdAt: { gte: start_at, diff --git a/queries/analytics/session/getSessions.js b/queries/analytics/session/getSessions.js index a806c16d..c5fed485 100644 --- a/queries/analytics/session/getSessions.js +++ b/queries/analytics/session/getSessions.js @@ -14,10 +14,8 @@ async function relationalQuery(websites, start_at) { where: { ...(websites && websites.length > 0 ? { - website: { - id: { - in: websites, - }, + websiteId: { + in: websites, }, } : {}),