import { cache } from 'react'; import { AccountWithParams } from '@/packages/features/accounts/src/types/accounts'; import { getSupabaseServerClient } from '@/packages/supabase/src/clients/server-client'; import { PROMPT_NAME } from '../../_components/ai/types'; export const loadRecommendations = cache(recommendationsLoader); async function recommendationsLoader( account: AccountWithParams | null, ): Promise { if (!account?.personal_code) { return []; } const supabaseClient = getSupabaseServerClient(); const { data, error } = await supabaseClient .schema('medreport') .from('ai_responses') .select('*') .eq('account_id', account.id) .eq('prompt_name', PROMPT_NAME.ANALYSIS_RECOMMENDATIONS) .order('latest_data_change', { ascending: false, nullsFirst: false }) .limit(1) .maybeSingle(); if (error) { console.error('Error fetching AI response from DB: ', error); return []; } if (data?.response) { return JSON.parse(data.response as string).recommended; } else { return []; } }