diff --git a/app/auth/membership-confirmation/page.tsx b/app/auth/membership-confirmation/page.tsx index 3cf00c8..c6aef9b 100644 --- a/app/auth/membership-confirmation/page.tsx +++ b/app/auth/membership-confirmation/page.tsx @@ -5,6 +5,10 @@ import pathsConfig from '@/config/paths.config'; import { getSupabaseServerClient } from '@kit/supabase/server-client'; import { withI18n } from '~/lib/i18n/with-i18n'; +import { + PAGE_VIEW_ACTION, + createPageViewLog, +} from '~/lib/services/audit/pageView.service'; import MembershipConfirmationNotification from './_components/membership-confirmation-notification'; @@ -18,6 +22,10 @@ async function MembershipConfirmation() { if (!user?.id) { redirect(pathsConfig.app.home); } + await createPageViewLog({ + accountId: user.id, + action: PAGE_VIEW_ACTION.REGISTRATION_SUCCESS, + }); return ; } diff --git a/app/home/(user)/(dashboard)/analysis-results/page.tsx b/app/home/(user)/(dashboard)/analysis-results/page.tsx index 60c43d5..f96f28d 100644 --- a/app/home/(user)/(dashboard)/analysis-results/page.tsx +++ b/app/home/(user)/(dashboard)/analysis-results/page.tsx @@ -1,5 +1,7 @@ import Link from 'next/link'; +import { redirect } from 'next/navigation'; +import { loadCurrentUserAccount } from '@/app/home/(user)/_lib/server/load-user-account'; import { createI18nServerInstance } from '@/lib/i18n/i18n.server'; import { withI18n } from '@/lib/i18n/with-i18n'; @@ -7,14 +9,16 @@ import { Trans } from '@kit/ui/makerkit/trans'; import { PageBody } from '@kit/ui/page'; import { Button } from '@kit/ui/shadcn/button'; +import pathsConfig from '~/config/paths.config'; +import { getAnalysisElements } from '~/lib/services/analysis-element.service'; +import { + PAGE_VIEW_ACTION, + createPageViewLog, +} from '~/lib/services/audit/pageView.service'; +import { getAnalysisOrders } from '~/lib/services/order.service'; + import { loadUserAnalysis } from '../../_lib/server/load-user-analysis'; import Analysis from './_components/analysis'; -import pathsConfig from '~/config/paths.config'; -import { redirect } from 'next/navigation'; -import { getAnalysisOrders } from '~/lib/services/order.service'; -import { getAnalysisElements } from '~/lib/services/analysis-element.service'; -import { loadCurrentUserAccount } from '@/app/home/(user)/_lib/server/load-user-account'; -import { createPageViewLog } from '~/lib/services/audit/pageView.service'; export const generateMetadata = async () => { const i18n = await createI18nServerInstance(); @@ -26,13 +30,15 @@ export const generateMetadata = async () => { }; async function AnalysisResultsPage() { - const account = await loadCurrentUserAccount() + const account = await loadCurrentUserAccount(); if (!account) { throw new Error('Account not found'); } const analysisResponses = await loadUserAnalysis(); - const analysisResponseElements = analysisResponses?.flatMap(({ elements }) => elements); + const analysisResponseElements = analysisResponses?.flatMap( + ({ elements }) => elements, + ); const analysisOrders = await getAnalysisOrders().catch(() => null); @@ -42,29 +48,46 @@ async function AnalysisResultsPage() { await createPageViewLog({ accountId: account.id, - action: 'VIEW_ANALYSIS_RESULTS', + action: PAGE_VIEW_ACTION.VIEW_ANALYSIS_RESULTS, }); const analysisElementIds = [ - ...new Set(analysisOrders?.flatMap((order) => order.analysis_element_ids).filter(Boolean) as number[]), + ...new Set( + analysisOrders + ?.flatMap((order) => order.analysis_element_ids) + .filter(Boolean) as number[], + ), ]; - const analysisElements = await getAnalysisElements({ ids: analysisElementIds }); - const analysisElementsWithResults = analysisResponseElements - ?.sort((a, b) => { - if (!a.response_time || !b.response_time) { - return 0; - } - return new Date(b.response_time).getTime() - new Date(a.response_time).getTime(); - }) - .map((results) => ({ results })) ?? []; - const analysisElementsWithoutResults = analysisElements - .filter((element) => !analysisElementsWithResults?.some(({ results }) => results.analysis_element_original_id === element.analysis_id_original)); + const analysisElements = await getAnalysisElements({ + ids: analysisElementIds, + }); + const analysisElementsWithResults = + analysisResponseElements + ?.sort((a, b) => { + if (!a.response_time || !b.response_time) { + return 0; + } + return ( + new Date(b.response_time).getTime() - + new Date(a.response_time).getTime() + ); + }) + .map((results) => ({ results })) ?? []; + const analysisElementsWithoutResults = analysisElements.filter( + (element) => + !analysisElementsWithResults?.some( + ({ results }) => + results.analysis_element_original_id === element.analysis_id_original, + ), + ); - const hasNoAnalysisElements = analysisElementsWithResults.length === 0 && analysisElementsWithoutResults.length === 0; + const hasNoAnalysisElements = + analysisElementsWithResults.length === 0 && + analysisElementsWithoutResults.length === 0; return ( -
+

@@ -85,16 +108,27 @@ async function AnalysisResultsPage() {

{analysisElementsWithResults.map(({ results }) => { - const analysisElement = analysisElements.find((element) => element.analysis_id_original === results.analysis_element_original_id); + const analysisElement = analysisElements.find( + (element) => + element.analysis_id_original === + results.analysis_element_original_id, + ); if (!analysisElement) { return null; } return ( - + ); })} {analysisElementsWithoutResults.map((element) => ( - + ))} {hasNoAnalysisElements && (
diff --git a/lib/services/audit/pageView.service.ts b/lib/services/audit/pageView.service.ts index 719db6d..ea5aaca 100644 --- a/lib/services/audit/pageView.service.ts +++ b/lib/services/audit/pageView.service.ts @@ -1,11 +1,16 @@ import { getSupabaseServerClient } from '@kit/supabase/server-client'; +export enum PAGE_VIEW_ACTION { + VIEW_ANALYSIS_RESULTS = 'VIEW_ANALYSIS_RESULTS', + REGISTRATION_SUCCESS = 'REGISTRATION_SUCCESS', +} + export const createPageViewLog = async ({ accountId, action, }: { accountId: string; - action: 'VIEW_ANALYSIS_RESULTS'; + action: PAGE_VIEW_ACTION; }) => { try { const supabase = getSupabaseServerClient(); @@ -32,4 +37,4 @@ export const createPageViewLog = async ({ } catch (error) { console.error('Failed to insert page view log', error); } -} +};