add doctor feedback
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { cache } from 'react';
|
||||
|
||||
import { AccountWithParams } from '@/packages/features/accounts/src/types/accounts';
|
||||
import { getLogger } from '@/packages/shared/src/logger';
|
||||
import { getSupabaseServerClient } from '@/packages/supabase/src/clients/server-client';
|
||||
|
||||
import {
|
||||
@@ -11,28 +12,52 @@ import {
|
||||
import { updateLifeStyle } from './ai-actions';
|
||||
|
||||
const failedResponse = {
|
||||
lifestyle: [],
|
||||
summary: null,
|
||||
response: {
|
||||
lifestyle: [],
|
||||
summary: null,
|
||||
},
|
||||
dateCreated: new Date().toISOString(),
|
||||
};
|
||||
|
||||
async function lifeStyleLoader(
|
||||
account: AccountWithParams,
|
||||
analysisResponses?: AnalysisResponses,
|
||||
): Promise<ILifeStyleResponse> {
|
||||
async function lifeStyleLoader({
|
||||
account,
|
||||
analysisResponses,
|
||||
isDoctorView = false,
|
||||
aiResponseTimestamp,
|
||||
}: {
|
||||
account: AccountWithParams | null;
|
||||
analysisResponses?: AnalysisResponses;
|
||||
isDoctorView?: boolean;
|
||||
aiResponseTimestamp: string;
|
||||
}): Promise<{ response: ILifeStyleResponse; dateCreated: string }> {
|
||||
const logger = await getLogger();
|
||||
if (!account?.personal_code) {
|
||||
return failedResponse;
|
||||
}
|
||||
|
||||
const supabaseClient = getSupabaseServerClient();
|
||||
const { data, error } = await supabaseClient
|
||||
const query = supabaseClient
|
||||
.schema('medreport')
|
||||
.from('ai_responses')
|
||||
.select('response')
|
||||
.select('response, latest_data_change')
|
||||
.eq('account_id', account.id)
|
||||
.eq('prompt_name', PROMPT_NAME.LIFE_STYLE)
|
||||
.order('latest_data_change', { ascending: false, nullsFirst: false })
|
||||
.limit(1)
|
||||
.maybeSingle();
|
||||
.eq('prompt_name', PROMPT_NAME.LIFE_STYLE);
|
||||
|
||||
if (isDoctorView) {
|
||||
logger.info(
|
||||
{ aiResponseTimestamp, accountId: account.id },
|
||||
'Attempting to receive life style row',
|
||||
);
|
||||
query.eq('latest_data_change', aiResponseTimestamp);
|
||||
} else {
|
||||
query
|
||||
.eq('is_visible_to_customer', true)
|
||||
.order('latest_data_change', { ascending: false, nullsFirst: false });
|
||||
}
|
||||
|
||||
const { data, error } = await query.limit(1).maybeSingle();
|
||||
|
||||
logger.info({ data: !!data }, 'Existing life style row');
|
||||
|
||||
if (error) {
|
||||
console.error('Error fetching AI response from DB: ', error);
|
||||
@@ -40,9 +65,19 @@ async function lifeStyleLoader(
|
||||
}
|
||||
|
||||
if (data?.response) {
|
||||
return JSON.parse(data.response as string);
|
||||
return {
|
||||
response: JSON.parse(data.response as string),
|
||||
dateCreated: data.latest_data_change,
|
||||
};
|
||||
} else {
|
||||
return await updateLifeStyle({ account, analysisResponses });
|
||||
const newLifeStyle = await updateLifeStyle({
|
||||
account,
|
||||
analysisResponses,
|
||||
isDoctorView,
|
||||
aiResponseTimestamp,
|
||||
});
|
||||
|
||||
return { response: newLifeStyle, dateCreated: aiResponseTimestamp };
|
||||
}
|
||||
}
|
||||
export const loadLifeStyle = cache(lifeStyleLoader);
|
||||
|
||||
Reference in New Issue
Block a user