Files
medreport_mrb2b/app/home/(user)/_lib/server/load-recommendations.ts
2025-10-23 16:48:27 +03:00

39 lines
1.0 KiB
TypeScript

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<string[]> {
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 [];
}
}