27
app/doctor/_components/analysis-fallback.tsx
Normal file
27
app/doctor/_components/analysis-fallback.tsx
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
'use server';
|
||||||
|
|
||||||
|
import React from 'react';
|
||||||
|
|
||||||
|
import { Spinner } from '@kit/ui/makerkit/spinner';
|
||||||
|
import { Trans } from '@kit/ui/makerkit/trans';
|
||||||
|
import { Progress } from '@kit/ui/shadcn/progress';
|
||||||
|
|
||||||
|
import { withI18n } from '~/lib/i18n/with-i18n';
|
||||||
|
|
||||||
|
const AnalysisFallback = ({
|
||||||
|
progress,
|
||||||
|
progressTextKey,
|
||||||
|
}: {
|
||||||
|
progress: number;
|
||||||
|
progressTextKey: string;
|
||||||
|
}) => {
|
||||||
|
return (
|
||||||
|
<div className="flex flex-col items-center justify-center gap-4 py-10">
|
||||||
|
<Trans i18nKey={progressTextKey} />
|
||||||
|
<Spinner />
|
||||||
|
<Progress value={progress} />
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default withI18n(AnalysisFallback);
|
||||||
@@ -16,6 +16,7 @@ import { zodResolver } from '@hookform/resolvers/zod';
|
|||||||
import { useQueryClient } from '@tanstack/react-query';
|
import { useQueryClient } from '@tanstack/react-query';
|
||||||
import { useForm } from 'react-hook-form';
|
import { useForm } from 'react-hook-form';
|
||||||
|
|
||||||
|
import { Spinner } from '@kit/ui/makerkit/spinner';
|
||||||
import { Trans } from '@kit/ui/makerkit/trans';
|
import { Trans } from '@kit/ui/makerkit/trans';
|
||||||
import { Button } from '@kit/ui/shadcn/button';
|
import { Button } from '@kit/ui/shadcn/button';
|
||||||
import {
|
import {
|
||||||
@@ -32,12 +33,21 @@ const AnalysisFeedback = ({
|
|||||||
feedback,
|
feedback,
|
||||||
patient,
|
patient,
|
||||||
order,
|
order,
|
||||||
|
aiDoctorFeedback,
|
||||||
|
timestamp,
|
||||||
|
recommendations,
|
||||||
|
isRecommendationsEdited,
|
||||||
}: {
|
}: {
|
||||||
feedback?: DoctorFeedback;
|
feedback?: DoctorFeedback;
|
||||||
patient: Patient;
|
patient: Patient;
|
||||||
order: Order;
|
order: Order;
|
||||||
|
aiDoctorFeedback?: string;
|
||||||
|
timestamp?: string;
|
||||||
|
recommendations: string[];
|
||||||
|
isRecommendationsEdited: boolean;
|
||||||
}) => {
|
}) => {
|
||||||
const [isDraftSubmitting, setIsDraftSubmitting] = useState(false);
|
const [isDraftSubmitting, setIsDraftSubmitting] = useState(false);
|
||||||
|
const [isSubmittingFeedback, setIsSubmittingFeedback] = useState(false);
|
||||||
const [isConfirmOpen, setIsConfirmOpen] = useState(false);
|
const [isConfirmOpen, setIsConfirmOpen] = useState(false);
|
||||||
const { data: user } = useUser();
|
const { data: user } = useUser();
|
||||||
const queryClient = useQueryClient();
|
const queryClient = useQueryClient();
|
||||||
@@ -46,7 +56,7 @@ const AnalysisFeedback = ({
|
|||||||
resolver: zodResolver(doctorAnalysisFeedbackFormSchema),
|
resolver: zodResolver(doctorAnalysisFeedbackFormSchema),
|
||||||
reValidateMode: 'onChange',
|
reValidateMode: 'onChange',
|
||||||
defaultValues: {
|
defaultValues: {
|
||||||
feedbackValue: feedback?.value ?? '',
|
feedbackValue: feedback?.value ?? aiDoctorFeedback ?? '',
|
||||||
userId: patient.userId,
|
userId: patient.userId,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
@@ -71,23 +81,30 @@ const AnalysisFeedback = ({
|
|||||||
data: DoctorAnalysisFeedbackForm,
|
data: DoctorAnalysisFeedbackForm,
|
||||||
status: 'DRAFT' | 'COMPLETED',
|
status: 'DRAFT' | 'COMPLETED',
|
||||||
) => {
|
) => {
|
||||||
|
setIsConfirmOpen(false);
|
||||||
|
setIsSubmittingFeedback(true);
|
||||||
|
|
||||||
const result = await giveFeedbackAction({
|
const result = await giveFeedbackAction({
|
||||||
...data,
|
...data,
|
||||||
analysisOrderId: order.analysisOrderId,
|
analysisOrderId: order.analysisOrderId,
|
||||||
status,
|
status,
|
||||||
|
patientId: patient.userId,
|
||||||
|
timestamp,
|
||||||
|
recommendations,
|
||||||
|
isRecommendationsEdited,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!result.success) {
|
if (!result.success) {
|
||||||
return toast.error(<Trans i18nKey="common:genericServerError" />);
|
return toast.error(<Trans i18nKey="common:genericServerError" />);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setIsSubmittingFeedback(false);
|
||||||
|
|
||||||
queryClient.invalidateQueries({
|
queryClient.invalidateQueries({
|
||||||
predicate: (query) => query.queryKey.includes('doctor-jobs'),
|
predicate: (query) => query.queryKey.includes('doctor-jobs'),
|
||||||
});
|
});
|
||||||
|
|
||||||
toast.success(<Trans i18nKey={'doctor:updateFeedbackSuccess'} />);
|
return toast.success(<Trans i18nKey={'doctor:updateFeedbackSuccess'} />);
|
||||||
|
|
||||||
return setIsConfirmOpen(false);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const confirmComplete = form.handleSubmit(async (data) => {
|
const confirmComplete = form.handleSubmit(async (data) => {
|
||||||
@@ -96,10 +113,6 @@ const AnalysisFeedback = ({
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<h3>
|
|
||||||
<Trans i18nKey="doctor:feedback" />
|
|
||||||
</h3>
|
|
||||||
<p>{feedback?.value ?? '-'}</p>
|
|
||||||
{!isReadOnly && (
|
{!isReadOnly && (
|
||||||
<Form {...form}>
|
<Form {...form}>
|
||||||
<form className="space-y-4 lg:w-1/2">
|
<form className="space-y-4 lg:w-1/2">
|
||||||
@@ -109,7 +122,11 @@ const AnalysisFeedback = ({
|
|||||||
render={({ field }) => (
|
render={({ field }) => (
|
||||||
<FormItem>
|
<FormItem>
|
||||||
<FormControl>
|
<FormControl>
|
||||||
<Textarea {...field} disabled={isReadOnly} />
|
<Textarea
|
||||||
|
className="min-h-[200px]"
|
||||||
|
{...field}
|
||||||
|
disabled={isDraftSubmitting || isSubmittingFeedback}
|
||||||
|
/>
|
||||||
</FormControl>
|
</FormControl>
|
||||||
<FormMessage />
|
<FormMessage />
|
||||||
</FormItem>
|
</FormItem>
|
||||||
@@ -136,7 +153,11 @@ const AnalysisFeedback = ({
|
|||||||
}
|
}
|
||||||
className="xs:w-1/4 w-full"
|
className="xs:w-1/4 w-full"
|
||||||
>
|
>
|
||||||
<Trans i18nKey="common:save" />
|
{isDraftSubmitting || form.formState.isSubmitting ? (
|
||||||
|
<Spinner />
|
||||||
|
) : (
|
||||||
|
<Trans i18nKey="common:save" />
|
||||||
|
)}
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
|
import { useMemo, useState } from 'react';
|
||||||
|
|
||||||
import { useQueryClient } from '@tanstack/react-query';
|
import { useQueryClient } from '@tanstack/react-query';
|
||||||
import { capitalize } from 'lodash';
|
import { capitalize } from 'lodash';
|
||||||
|
|
||||||
@@ -23,20 +25,40 @@ import { bmiFromMetric } from '~/lib/utils';
|
|||||||
import AnalysisFeedback from './analysis-feedback';
|
import AnalysisFeedback from './analysis-feedback';
|
||||||
import DoctorAnalysisWrapper from './doctor-analysis-wrapper';
|
import DoctorAnalysisWrapper from './doctor-analysis-wrapper';
|
||||||
import DoctorJobSelect from './doctor-job-select';
|
import DoctorJobSelect from './doctor-job-select';
|
||||||
|
import DoctorRecommendedAnalyses from './doctor-recommended-analyses';
|
||||||
|
|
||||||
export default function AnalysisView({
|
export default function AnalysisView({
|
||||||
patient,
|
patient,
|
||||||
order,
|
order,
|
||||||
analyses,
|
analyses,
|
||||||
feedback,
|
feedback,
|
||||||
|
aiDoctorFeedback,
|
||||||
|
recommendations,
|
||||||
|
availableAnalyses,
|
||||||
|
timestamp,
|
||||||
}: {
|
}: {
|
||||||
patient: Patient;
|
patient: Patient;
|
||||||
order: Order;
|
order: Order;
|
||||||
analyses: AnalysisResponse[];
|
analyses: AnalysisResponse[];
|
||||||
feedback?: DoctorFeedback;
|
feedback?: DoctorFeedback;
|
||||||
|
aiDoctorFeedback?: string;
|
||||||
|
recommendations?: string[];
|
||||||
|
availableAnalyses?: string[];
|
||||||
|
timestamp?: string;
|
||||||
}) {
|
}) {
|
||||||
const { data: user } = useUser();
|
const { data: user } = useUser();
|
||||||
const queryClient = useQueryClient();
|
const queryClient = useQueryClient();
|
||||||
|
const [recommendedAnalyses, setRecommendedAnalyses] = useState<string[]>(
|
||||||
|
recommendations ?? [],
|
||||||
|
);
|
||||||
|
const isRecommendationsEdited = useMemo(() => {
|
||||||
|
if (recommendedAnalyses.length !== recommendations?.length) return true;
|
||||||
|
const sa = new Set(recommendedAnalyses),
|
||||||
|
sb = new Set(recommendations);
|
||||||
|
if (sa.size !== sb.size) return true;
|
||||||
|
for (const v of sa) if (!sb.has(v)) return true;
|
||||||
|
return false;
|
||||||
|
}, [recommendations, recommendedAnalyses]);
|
||||||
|
|
||||||
const languageNames = useCurrentLocaleLanguageNames();
|
const languageNames = useCurrentLocaleLanguageNames();
|
||||||
|
|
||||||
@@ -154,7 +176,28 @@ export default function AnalysisView({
|
|||||||
})}
|
})}
|
||||||
</div>
|
</div>
|
||||||
{order.isPackage && (
|
{order.isPackage && (
|
||||||
<AnalysisFeedback order={order} patient={patient} feedback={feedback} />
|
<>
|
||||||
|
<h3>
|
||||||
|
<Trans i18nKey="doctor:feedback" />
|
||||||
|
</h3>
|
||||||
|
<p>{feedback?.value ?? '-'}</p>
|
||||||
|
<div className="flex flex-col gap-4 lg:flex-row">
|
||||||
|
<AnalysisFeedback
|
||||||
|
order={order}
|
||||||
|
patient={patient}
|
||||||
|
feedback={feedback}
|
||||||
|
aiDoctorFeedback={aiDoctorFeedback}
|
||||||
|
timestamp={timestamp}
|
||||||
|
recommendations={recommendedAnalyses}
|
||||||
|
isRecommendationsEdited={isRecommendationsEdited}
|
||||||
|
/>
|
||||||
|
<DoctorRecommendedAnalyses
|
||||||
|
recommendedAnalyses={recommendedAnalyses}
|
||||||
|
availableAnalyses={availableAnalyses}
|
||||||
|
setRecommendedAnalyses={setRecommendedAnalyses}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</>
|
||||||
)}
|
)}
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
|
|||||||
53
app/doctor/_components/doctor-recommended-analyses.tsx
Normal file
53
app/doctor/_components/doctor-recommended-analyses.tsx
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
'use client';
|
||||||
|
|
||||||
|
import React, { Dispatch, SetStateAction } from 'react';
|
||||||
|
|
||||||
|
import { Trans } from '@kit/ui/makerkit/trans';
|
||||||
|
import { Button } from '@kit/ui/shadcn/button';
|
||||||
|
|
||||||
|
const DoctorRecommendedAnalyses = ({
|
||||||
|
recommendedAnalyses,
|
||||||
|
availableAnalyses,
|
||||||
|
setRecommendedAnalyses,
|
||||||
|
}: {
|
||||||
|
recommendedAnalyses?: string[];
|
||||||
|
availableAnalyses?: string[];
|
||||||
|
setRecommendedAnalyses: Dispatch<SetStateAction<string[]>>;
|
||||||
|
}) => {
|
||||||
|
if (availableAnalyses?.length === 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<h5>
|
||||||
|
<Trans i18nKey="doctor:recommendedAnalyses" />
|
||||||
|
</h5>
|
||||||
|
<div className="mt-4 flex gap-2">
|
||||||
|
{availableAnalyses?.map((analysis, index) => {
|
||||||
|
return (
|
||||||
|
<Button
|
||||||
|
size="sm"
|
||||||
|
key={`${index}-analysis-feedback-list`}
|
||||||
|
variant={
|
||||||
|
recommendedAnalyses?.includes(analysis) ? 'default' : 'outline'
|
||||||
|
}
|
||||||
|
type="button"
|
||||||
|
onClick={() =>
|
||||||
|
setRecommendedAnalyses((prev: string[]) =>
|
||||||
|
prev.includes(analysis)
|
||||||
|
? prev.filter((x) => x !== analysis)
|
||||||
|
: [...prev, analysis],
|
||||||
|
)
|
||||||
|
}
|
||||||
|
>
|
||||||
|
{analysis}
|
||||||
|
</Button>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default DoctorRecommendedAnalyses;
|
||||||
@@ -0,0 +1,90 @@
|
|||||||
|
'use server';
|
||||||
|
|
||||||
|
import React from 'react';
|
||||||
|
|
||||||
|
import { AnalysisResponses } from '@/app/home/(user)/_components/ai/types';
|
||||||
|
import { OrderAnalysisCard } from '@/app/home/(user)/_components/order-analyses-cards';
|
||||||
|
import { loadLifeStyle } from '@/app/home/(user)/_lib/server/load-life-style';
|
||||||
|
import { loadRecommendations } from '@/app/home/(user)/_lib/server/load-recommendations';
|
||||||
|
import { AccountWithParams } from '@/packages/features/accounts/src/types/accounts';
|
||||||
|
import { AnalysisResultDetails } from '@/packages/features/doctor/src/lib/server/schema/doctor-analysis-detail-view.schema';
|
||||||
|
|
||||||
|
import { withI18n } from '~/lib/i18n/with-i18n';
|
||||||
|
|
||||||
|
import {
|
||||||
|
loadDoctorFeedback,
|
||||||
|
prepareFeedback,
|
||||||
|
} from '../_lib/server/load-doctor-feedback';
|
||||||
|
import AnalysisView from './analysis-view';
|
||||||
|
|
||||||
|
async function NewAnalysisRecommendationsLoader({
|
||||||
|
analysisResultDetails,
|
||||||
|
account,
|
||||||
|
analysisResponses,
|
||||||
|
currentAIResponseTimestamp,
|
||||||
|
analyses,
|
||||||
|
patient,
|
||||||
|
}: {
|
||||||
|
currentAIResponseTimestamp: string;
|
||||||
|
account: AccountWithParams | null;
|
||||||
|
analysisResponses: AnalysisResponses;
|
||||||
|
analysisResultDetails: AnalysisResultDetails;
|
||||||
|
analyses: OrderAnalysisCard[];
|
||||||
|
patient: AccountWithParams | null;
|
||||||
|
}) {
|
||||||
|
if (!analysisResultDetails.order.isPackage) {
|
||||||
|
return (
|
||||||
|
<AnalysisView
|
||||||
|
patient={analysisResultDetails.patient}
|
||||||
|
order={analysisResultDetails.order}
|
||||||
|
analyses={analysisResultDetails.analysisResponse}
|
||||||
|
feedback={analysisResultDetails.doctorFeedback}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const [lifeStyle, recommendations, aiFeedback] = await Promise.all([
|
||||||
|
loadLifeStyle({
|
||||||
|
account: patient,
|
||||||
|
analysisResponses,
|
||||||
|
isDoctorView: true,
|
||||||
|
aiResponseTimestamp: currentAIResponseTimestamp,
|
||||||
|
}),
|
||||||
|
loadRecommendations({
|
||||||
|
account: patient,
|
||||||
|
analysisResponses,
|
||||||
|
aiResponseTimestamp: currentAIResponseTimestamp,
|
||||||
|
isDoctorView: true,
|
||||||
|
analyses,
|
||||||
|
}),
|
||||||
|
loadDoctorFeedback(
|
||||||
|
analysisResultDetails.patient,
|
||||||
|
analysisResultDetails.analysisResponse,
|
||||||
|
currentAIResponseTimestamp,
|
||||||
|
),
|
||||||
|
]);
|
||||||
|
|
||||||
|
const feedback = prepareFeedback({
|
||||||
|
aiResponse: aiFeedback,
|
||||||
|
recommendations,
|
||||||
|
lifeStyleSummary: lifeStyle.response.summary,
|
||||||
|
patientName: analysisResultDetails.patient.firstName,
|
||||||
|
doctorName: `${account?.name} ${account?.last_name}`,
|
||||||
|
aiResponseTimestamp: currentAIResponseTimestamp,
|
||||||
|
});
|
||||||
|
|
||||||
|
return (
|
||||||
|
<AnalysisView
|
||||||
|
patient={analysisResultDetails.patient}
|
||||||
|
order={analysisResultDetails.order}
|
||||||
|
analyses={analysisResultDetails.analysisResponse}
|
||||||
|
feedback={analysisResultDetails.doctorFeedback}
|
||||||
|
aiDoctorFeedback={feedback}
|
||||||
|
availableAnalyses={analyses.map((analysis) => analysis.title)}
|
||||||
|
recommendations={recommendations}
|
||||||
|
timestamp={currentAIResponseTimestamp}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export default withI18n(NewAnalysisRecommendationsLoader);
|
||||||
55
app/doctor/_components/prepare-ai-parameters.tsx
Normal file
55
app/doctor/_components/prepare-ai-parameters.tsx
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
'use server';
|
||||||
|
|
||||||
|
import React, { Suspense } from 'react';
|
||||||
|
|
||||||
|
import { loadAnalyses } from '@/app/home/(user)/_lib/server/load-analyses';
|
||||||
|
import {
|
||||||
|
loadCurrentUserAccount,
|
||||||
|
loadUserAccount,
|
||||||
|
} from '@/app/home/(user)/_lib/server/load-user-account';
|
||||||
|
import { AnalysisResultDetails } from '@/packages/features/doctor/src/lib/server/schema/doctor-analysis-detail-view.schema';
|
||||||
|
import { createUserAnalysesApi } from '@/packages/features/user-analyses/src/server/api';
|
||||||
|
import { getSupabaseServerClient } from '@/packages/supabase/src/clients/server-client';
|
||||||
|
|
||||||
|
import { withI18n } from '~/lib/i18n/with-i18n';
|
||||||
|
import { getLatestResponseTime } from '~/lib/utils';
|
||||||
|
|
||||||
|
import AnalysisFallback from './analysis-fallback';
|
||||||
|
import NewAnalysisRecommendationsLoader from './new-analysis-recommendations-loader';
|
||||||
|
|
||||||
|
async function PrepareAIParameters({
|
||||||
|
analysisResultDetails,
|
||||||
|
}: {
|
||||||
|
analysisResultDetails: AnalysisResultDetails;
|
||||||
|
}) {
|
||||||
|
const { analyses } = await loadAnalyses();
|
||||||
|
const { account: doctorAccount } = await loadCurrentUserAccount();
|
||||||
|
const patientAccount = await loadUserAccount(
|
||||||
|
analysisResultDetails.patient.userId,
|
||||||
|
);
|
||||||
|
const client = getSupabaseServerClient();
|
||||||
|
const userAnalysesApi = createUserAnalysesApi(client);
|
||||||
|
const analysisResponses = await userAnalysesApi.getAllUserAnalysisResponses(
|
||||||
|
patientAccount.id,
|
||||||
|
);
|
||||||
|
const currentAIResponseTimestamp = getLatestResponseTime(analysisResponses);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Suspense
|
||||||
|
fallback={
|
||||||
|
<AnalysisFallback progress={66} progressTextKey="doctor:loadFeedback" />
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<NewAnalysisRecommendationsLoader
|
||||||
|
account={doctorAccount}
|
||||||
|
currentAIResponseTimestamp={currentAIResponseTimestamp}
|
||||||
|
analysisResponses={analysisResponses}
|
||||||
|
analysisResultDetails={analysisResultDetails}
|
||||||
|
analyses={analyses}
|
||||||
|
patient={patientAccount}
|
||||||
|
/>
|
||||||
|
</Suspense>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export default withI18n(PrepareAIParameters);
|
||||||
98
app/doctor/_lib/server/load-doctor-feedback.ts
Normal file
98
app/doctor/_lib/server/load-doctor-feedback.ts
Normal file
@@ -0,0 +1,98 @@
|
|||||||
|
import { cache } from 'react';
|
||||||
|
|
||||||
|
import { PROMPT_NAME } from '@/app/home/(user)/_components/ai/types';
|
||||||
|
import { generateDoctorFeedback } from '@/app/home/(user)/_lib/server/ai-actions';
|
||||||
|
import {
|
||||||
|
AnalysisResponse,
|
||||||
|
Patient,
|
||||||
|
} from '@/packages/features/doctor/src/lib/server/schema/doctor-analysis-detail-view.schema';
|
||||||
|
import { getLogger } from '@/packages/shared/src/logger';
|
||||||
|
import { getSupabaseServerClient } from '@/packages/supabase/src/clients/server-client';
|
||||||
|
|
||||||
|
export const loadDoctorFeedback = cache(doctorFeedbackLoader);
|
||||||
|
|
||||||
|
const PLACEHOLDER = {
|
||||||
|
ANALYSES: 'SOOVITATUD_ANALYYSID_PLACEHOLDER',
|
||||||
|
LIFE_STYLE_SUMMARY: 'ELUSTIILI_KOKKUVOTTE_PLACEHOLDER',
|
||||||
|
PATIENT_NAME: 'PATSIENDI_NIMI_PLACEHOLDER',
|
||||||
|
DOCTOR_NAME: 'ARSTI_NIMI_PLACEHOLDER',
|
||||||
|
ANALYSES_DATE: 'ANALYYSI_KUUPAEV_PLACEHOLDER',
|
||||||
|
};
|
||||||
|
|
||||||
|
export const prepareFeedback = ({
|
||||||
|
aiResponse,
|
||||||
|
recommendations,
|
||||||
|
lifeStyleSummary,
|
||||||
|
patientName,
|
||||||
|
doctorName,
|
||||||
|
aiResponseTimestamp,
|
||||||
|
}: {
|
||||||
|
aiResponse: string;
|
||||||
|
recommendations?: string[];
|
||||||
|
lifeStyleSummary: string | null;
|
||||||
|
patientName: string;
|
||||||
|
doctorName: string;
|
||||||
|
aiResponseTimestamp: string;
|
||||||
|
}) => {
|
||||||
|
const recommendationsList = recommendations
|
||||||
|
? recommendations.map((analysis) => `${analysis}`).join('\n')
|
||||||
|
: '';
|
||||||
|
const feedback = aiResponse
|
||||||
|
.replace(PLACEHOLDER.ANALYSES, recommendationsList)
|
||||||
|
.replace(PLACEHOLDER.LIFE_STYLE_SUMMARY, lifeStyleSummary ?? '')
|
||||||
|
.replace(PLACEHOLDER.PATIENT_NAME, patientName)
|
||||||
|
.replace(PLACEHOLDER.DOCTOR_NAME, doctorName)
|
||||||
|
.replace(
|
||||||
|
PLACEHOLDER.ANALYSES_DATE,
|
||||||
|
new Date(aiResponseTimestamp).toLocaleString(),
|
||||||
|
);
|
||||||
|
|
||||||
|
return feedback;
|
||||||
|
};
|
||||||
|
|
||||||
|
async function doctorFeedbackLoader(
|
||||||
|
patient: Patient | null,
|
||||||
|
analysisResponses: AnalysisResponse[],
|
||||||
|
aiResponseTimestamp: string,
|
||||||
|
): Promise<string> {
|
||||||
|
const logger = await getLogger();
|
||||||
|
if (!patient?.personalCode) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
const supabaseClient = getSupabaseServerClient();
|
||||||
|
|
||||||
|
logger.info(
|
||||||
|
{
|
||||||
|
aiResponseTimestamp,
|
||||||
|
patientId: patient.userId,
|
||||||
|
},
|
||||||
|
'Attempting to receive existing doctor feedback',
|
||||||
|
);
|
||||||
|
|
||||||
|
const { data, error } = await supabaseClient
|
||||||
|
.schema('medreport')
|
||||||
|
.from('ai_responses')
|
||||||
|
.select('*')
|
||||||
|
.eq('account_id', patient.userId)
|
||||||
|
.eq('prompt_name', PROMPT_NAME.FEEDBACK)
|
||||||
|
.eq('latest_data_change', aiResponseTimestamp)
|
||||||
|
.limit(1)
|
||||||
|
.maybeSingle();
|
||||||
|
|
||||||
|
logger.info({ data: !!data }, 'Existing doctor feedback');
|
||||||
|
|
||||||
|
if (error) {
|
||||||
|
console.error('Error fetching AI response from DB: ', error);
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (data?.response) {
|
||||||
|
return data.response as string;
|
||||||
|
} else {
|
||||||
|
return await generateDoctorFeedback({
|
||||||
|
patient,
|
||||||
|
analysisResponses,
|
||||||
|
aiResponseTimestamp,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,15 +1,17 @@
|
|||||||
import { cache } from 'react';
|
import { Suspense, cache } from 'react';
|
||||||
|
|
||||||
import { getAnalysisResultsForDoctor } from '@kit/doctor/services/doctor-analysis.service';
|
import { getAnalysisResultsForDoctor } from '@kit/doctor/services/doctor-analysis.service';
|
||||||
import { PageBody, PageHeader } from '@kit/ui/page';
|
import { PageBody, PageHeader } from '@kit/ui/page';
|
||||||
|
|
||||||
|
import { withI18n } from '~/lib/i18n/with-i18n';
|
||||||
import {
|
import {
|
||||||
DoctorPageViewAction,
|
DoctorPageViewAction,
|
||||||
createDoctorPageViewLog,
|
createDoctorPageViewLog,
|
||||||
} from '~/lib/services/audit/doctorPageView.service';
|
} from '~/lib/services/audit/doctorPageView.service';
|
||||||
|
|
||||||
import AnalysisView from '../../_components/analysis-view';
|
import AnalysisFallback from '../../_components/analysis-fallback';
|
||||||
import { DoctorGuard } from '../../_components/doctor-guard';
|
import { DoctorGuard } from '../../_components/doctor-guard';
|
||||||
|
import PrepareAiParameters from '../../_components/prepare-ai-parameters';
|
||||||
|
|
||||||
async function AnalysisPage({
|
async function AnalysisPage({
|
||||||
params,
|
params,
|
||||||
@@ -37,16 +39,20 @@ async function AnalysisPage({
|
|||||||
<>
|
<>
|
||||||
<PageHeader />
|
<PageHeader />
|
||||||
<PageBody className="px-12">
|
<PageBody className="px-12">
|
||||||
<AnalysisView
|
<Suspense
|
||||||
patient={analysisResultDetails.patient}
|
fallback={
|
||||||
order={analysisResultDetails.order}
|
<AnalysisFallback
|
||||||
analyses={analysisResultDetails.analysisResponse}
|
progress={33}
|
||||||
feedback={analysisResultDetails.doctorFeedback}
|
progressTextKey="doctor:loadParameters"
|
||||||
/>
|
/>
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<PrepareAiParameters analysisResultDetails={analysisResultDetails} />
|
||||||
|
</Suspense>
|
||||||
</PageBody>
|
</PageBody>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export default DoctorGuard(AnalysisPage);
|
export default DoctorGuard(withI18n(AnalysisPage));
|
||||||
const loadResult = cache(getAnalysisResultsForDoctor);
|
const loadResult = cache(getAnalysisResultsForDoctor);
|
||||||
|
|||||||
77
app/home/(user)/(dashboard)/life-style/page.tsx
Normal file
77
app/home/(user)/(dashboard)/life-style/page.tsx
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
import React from 'react';
|
||||||
|
|
||||||
|
import { createUserAnalysesApi } from '@/packages/features/user-analyses/src/server/api';
|
||||||
|
import { getSupabaseServerClient } from '@/packages/supabase/src/clients/server-client';
|
||||||
|
|
||||||
|
import { PageBody } from '@kit/ui/makerkit/page';
|
||||||
|
import { Trans } from '@kit/ui/makerkit/trans';
|
||||||
|
import { Skeleton } from '@kit/ui/shadcn/skeleton';
|
||||||
|
|
||||||
|
import { createI18nServerInstance } from '~/lib/i18n/i18n.server';
|
||||||
|
import { withI18n } from '~/lib/i18n/with-i18n';
|
||||||
|
import {
|
||||||
|
PageViewAction,
|
||||||
|
createPageViewLog,
|
||||||
|
} from '~/lib/services/audit/pageView.service';
|
||||||
|
import { getLatestResponseTime } from '~/lib/utils';
|
||||||
|
|
||||||
|
import { HomeLayoutPageHeader } from '../../_components/home-page-header';
|
||||||
|
import { loadLifeStyle } from '../../_lib/server/load-life-style';
|
||||||
|
import { loadCurrentUserAccount } from '../../_lib/server/load-user-account';
|
||||||
|
|
||||||
|
export async function generateMetadata() {
|
||||||
|
const { t } = await createI18nServerInstance();
|
||||||
|
|
||||||
|
return {
|
||||||
|
title: t('common:lifeStyle.title'),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
async function LifeStylePage() {
|
||||||
|
const { account } = await loadCurrentUserAccount();
|
||||||
|
if (!account) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
const client = getSupabaseServerClient();
|
||||||
|
const userAnalysesApi = createUserAnalysesApi(client);
|
||||||
|
const analysisResponses = await userAnalysesApi.getAllUserAnalysisResponses();
|
||||||
|
const currentAIResponseTimestamp = getLatestResponseTime(analysisResponses);
|
||||||
|
const { response } = await loadLifeStyle({
|
||||||
|
account,
|
||||||
|
analysisResponses,
|
||||||
|
aiResponseTimestamp: currentAIResponseTimestamp,
|
||||||
|
});
|
||||||
|
|
||||||
|
await createPageViewLog({
|
||||||
|
accountId: account.id,
|
||||||
|
action: PageViewAction.VIEW_LIFE_STYLE,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.lifestyle) {
|
||||||
|
return <Skeleton className="mt-10 h-10 w-full" />;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<HomeLayoutPageHeader
|
||||||
|
title={<Trans i18nKey={'common:lifeStyle.title'} />}
|
||||||
|
description=""
|
||||||
|
/>
|
||||||
|
|
||||||
|
<PageBody>
|
||||||
|
<div className="mt-8">
|
||||||
|
{response.lifestyle.map(({ title, description }, index) => (
|
||||||
|
<React.Fragment key={`${index}-${title}`}>
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
<h3>{title}</h3>
|
||||||
|
</div>
|
||||||
|
<p className="font-regular py-4">{description}</p>
|
||||||
|
</React.Fragment>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</PageBody>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export default withI18n(LifeStylePage);
|
||||||
@@ -37,8 +37,8 @@ async function OrderAnalysisPage() {
|
|||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<HomeLayoutPageHeader
|
<HomeLayoutPageHeader
|
||||||
title={<Trans i18nKey={'order-analysis:title'} />}
|
title={<Trans i18nKey="order-analysis:title" />}
|
||||||
description={<Trans i18nKey={'order-analysis:description'} />}
|
description={<Trans i18nKey="order-analysis:description" />}
|
||||||
/>
|
/>
|
||||||
<PageBody>
|
<PageBody>
|
||||||
<OrderAnalysesCards analyses={analyses} countryCode={countryCode} />
|
<OrderAnalysesCards analyses={analyses} countryCode={countryCode} />
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
import { Suspense } from 'react';
|
|
||||||
|
|
||||||
import { redirect } from 'next/navigation';
|
import { redirect } from 'next/navigation';
|
||||||
|
|
||||||
import { toTitleCase } from '@/lib/utils';
|
import { toTitleCase } from '@/lib/utils';
|
||||||
@@ -12,11 +10,9 @@ import { createUserAnalysesApi } from '@kit/user-analyses/api';
|
|||||||
import { createI18nServerInstance } from '~/lib/i18n/i18n.server';
|
import { createI18nServerInstance } from '~/lib/i18n/i18n.server';
|
||||||
import { withI18n } from '~/lib/i18n/with-i18n';
|
import { withI18n } from '~/lib/i18n/with-i18n';
|
||||||
|
|
||||||
|
import AIBlocks from '../_components/ai/ai-blocks';
|
||||||
import Dashboard from '../_components/dashboard';
|
import Dashboard from '../_components/dashboard';
|
||||||
import DashboardCards from '../_components/dashboard-cards';
|
import DashboardCards from '../_components/dashboard-cards';
|
||||||
import Recommendations from '../_components/recommendations';
|
|
||||||
import RecommendationsSkeleton from '../_components/recommendations-skeleton';
|
|
||||||
import { isValidOpenAiEnv } from '../_lib/server/is-valid-open-ai-env';
|
|
||||||
import { loadCurrentUserAccount } from '../_lib/server/load-user-account';
|
import { loadCurrentUserAccount } from '../_lib/server/load-user-account';
|
||||||
|
|
||||||
export const generateMetadata = async () => {
|
export const generateMetadata = async () => {
|
||||||
@@ -33,7 +29,10 @@ async function UserHomePage() {
|
|||||||
|
|
||||||
const { account } = await loadCurrentUserAccount();
|
const { account } = await loadCurrentUserAccount();
|
||||||
const api = createUserAnalysesApi(client);
|
const api = createUserAnalysesApi(client);
|
||||||
|
const userAnalysesApi = createUserAnalysesApi(client);
|
||||||
|
|
||||||
const bmiThresholds = await api.fetchBmiThresholds();
|
const bmiThresholds = await api.fetchBmiThresholds();
|
||||||
|
const analysisResponses = await userAnalysesApi.getAllUserAnalysisResponses();
|
||||||
|
|
||||||
if (!account) {
|
if (!account) {
|
||||||
redirect('/');
|
redirect('/');
|
||||||
@@ -53,16 +52,13 @@ async function UserHomePage() {
|
|||||||
/>
|
/>
|
||||||
<PageBody>
|
<PageBody>
|
||||||
<Dashboard account={account} bmiThresholds={bmiThresholds} />
|
<Dashboard account={account} bmiThresholds={bmiThresholds} />
|
||||||
{(await isValidOpenAiEnv()) && (
|
|
||||||
<>
|
<h4>
|
||||||
<h4>
|
<Trans i18nKey="dashboard:recommendations.title" />
|
||||||
<Trans i18nKey="dashboard:recommendations.title" />
|
</h4>
|
||||||
</h4>
|
<div className="mt-4 grid gap-6 sm:grid-cols-3">
|
||||||
<Suspense fallback={<RecommendationsSkeleton />}>
|
<AIBlocks account={account} analysisResponses={analysisResponses} />
|
||||||
<Recommendations account={account} />
|
</div>
|
||||||
</Suspense>
|
|
||||||
</>
|
|
||||||
)}
|
|
||||||
</PageBody>
|
</PageBody>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
|
|||||||
52
app/home/(user)/_components/ai/ai-blocks.tsx
Normal file
52
app/home/(user)/_components/ai/ai-blocks.tsx
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
'use server';
|
||||||
|
|
||||||
|
import React, { Suspense } from 'react';
|
||||||
|
|
||||||
|
import { AccountWithParams } from '@/packages/features/accounts/src/types/accounts';
|
||||||
|
|
||||||
|
import { isValidOpenAiEnv } from '../../_lib/server/is-valid-open-ai-env';
|
||||||
|
import LifeStyleCard from './life-style-card';
|
||||||
|
import OrderAnalysesPackageCard from './order-analyses-package-card';
|
||||||
|
import Recommendations from './recommendations';
|
||||||
|
import RecommendationsSkeleton from './recommendations-skeleton';
|
||||||
|
import { AnalysisResponses } from './types';
|
||||||
|
|
||||||
|
const AIBlocks = async ({
|
||||||
|
account,
|
||||||
|
analysisResponses,
|
||||||
|
}: {
|
||||||
|
account: AccountWithParams;
|
||||||
|
analysisResponses?: AnalysisResponses;
|
||||||
|
}) => {
|
||||||
|
const isOpenAiAvailable = await isValidOpenAiEnv();
|
||||||
|
|
||||||
|
if (!isOpenAiAvailable) {
|
||||||
|
return <OrderAnalysesPackageCard />;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (analysisResponses?.length === 0) {
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<OrderAnalysesPackageCard />
|
||||||
|
<Suspense fallback={<RecommendationsSkeleton amount={1} />}>
|
||||||
|
<LifeStyleCard
|
||||||
|
account={account}
|
||||||
|
analysisResponses={analysisResponses}
|
||||||
|
/>
|
||||||
|
</Suspense>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Suspense fallback={<RecommendationsSkeleton />}>
|
||||||
|
<LifeStyleCard account={account} analysisResponses={analysisResponses} />
|
||||||
|
<Recommendations
|
||||||
|
account={account}
|
||||||
|
analysisResponses={analysisResponses}
|
||||||
|
/>
|
||||||
|
</Suspense>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default AIBlocks;
|
||||||
56
app/home/(user)/_components/ai/life-style-card.tsx
Normal file
56
app/home/(user)/_components/ai/life-style-card.tsx
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
'use server';
|
||||||
|
|
||||||
|
import React from 'react';
|
||||||
|
|
||||||
|
import Link from 'next/link';
|
||||||
|
|
||||||
|
import { AccountWithParams } from '@/packages/features/accounts/src/types/accounts';
|
||||||
|
import { pathsConfig } from '@/packages/shared/src/config';
|
||||||
|
import { ChevronRight } from 'lucide-react';
|
||||||
|
|
||||||
|
import { Trans } from '@kit/ui/makerkit/trans';
|
||||||
|
import { Button } from '@kit/ui/shadcn/button';
|
||||||
|
import { Card, CardHeader } from '@kit/ui/shadcn/card';
|
||||||
|
|
||||||
|
import { getLatestResponseTime } from '~/lib/utils';
|
||||||
|
|
||||||
|
import { loadLifeStyle } from '../../_lib/server/load-life-style';
|
||||||
|
import { AnalysisResponses } from './types';
|
||||||
|
|
||||||
|
const LifeStyleCard = async ({
|
||||||
|
account,
|
||||||
|
analysisResponses,
|
||||||
|
}: {
|
||||||
|
account: AccountWithParams;
|
||||||
|
analysisResponses?: AnalysisResponses;
|
||||||
|
}) => {
|
||||||
|
const aiResponseTimestamp = getLatestResponseTime(analysisResponses);
|
||||||
|
const { response, dateCreated } = await loadLifeStyle({
|
||||||
|
account,
|
||||||
|
analysisResponses,
|
||||||
|
aiResponseTimestamp,
|
||||||
|
});
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Card variant="gradient-success" className="flex flex-col justify-between">
|
||||||
|
<CardHeader className="flex-row justify-between">
|
||||||
|
<div>
|
||||||
|
<span className="text-xs">
|
||||||
|
{new Date(dateCreated).toLocaleString()}
|
||||||
|
</span>
|
||||||
|
<h5 className="flex flex-col">
|
||||||
|
<Trans i18nKey="dashboard:heroCard.lifeStyle.title" />
|
||||||
|
</h5>
|
||||||
|
</div>
|
||||||
|
<Link href={pathsConfig.app.lifeStyle}>
|
||||||
|
<Button size="icon" variant="outline" className="px-2 text-black">
|
||||||
|
<ChevronRight className="size-4 stroke-2" />
|
||||||
|
</Button>
|
||||||
|
</Link>
|
||||||
|
</CardHeader>
|
||||||
|
<span className="text-primary p-4 text-sm">{response.summary}</span>
|
||||||
|
</Card>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default LifeStyleCard;
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
import React from 'react';
|
||||||
|
|
||||||
|
import Link from 'next/link';
|
||||||
|
|
||||||
|
import { pathsConfig } from '@/packages/shared/src/config';
|
||||||
|
import { ChevronRight, HeartPulse } from 'lucide-react';
|
||||||
|
|
||||||
|
import { Trans } from '@kit/ui/makerkit/trans';
|
||||||
|
import { Button } from '@kit/ui/shadcn/button';
|
||||||
|
import {
|
||||||
|
Card,
|
||||||
|
CardDescription,
|
||||||
|
CardFooter,
|
||||||
|
CardHeader,
|
||||||
|
} from '@kit/ui/shadcn/card';
|
||||||
|
|
||||||
|
const OrderAnalysesPackageCard = () => {
|
||||||
|
return (
|
||||||
|
<Card
|
||||||
|
variant="gradient-success"
|
||||||
|
className="xs:w-1/2 flex w-full flex-col justify-between sm:w-auto"
|
||||||
|
>
|
||||||
|
<CardHeader className="flex-row sm:pb-0">
|
||||||
|
<div
|
||||||
|
className={
|
||||||
|
'bg-primary/10 mb-6 flex size-8 items-center-safe justify-center-safe rounded-full text-white'
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<HeartPulse className="size-4 fill-green-500" />
|
||||||
|
</div>
|
||||||
|
<div className="bg-warning ml-auto flex size-8 items-center-safe justify-center-safe rounded-full text-white">
|
||||||
|
<Link href={pathsConfig.app.orderAnalysisPackage}>
|
||||||
|
<Button size="icon" variant="outline" className="px-2 text-black">
|
||||||
|
<ChevronRight className="size-4 stroke-2" />
|
||||||
|
</Button>
|
||||||
|
</Link>
|
||||||
|
</div>
|
||||||
|
</CardHeader>
|
||||||
|
<CardFooter className="flex flex-col items-start gap-2">
|
||||||
|
<h5>
|
||||||
|
<Trans i18nKey="dashboard:heroCard.orderPackage.title" />
|
||||||
|
</h5>
|
||||||
|
<CardDescription className="text-primary">
|
||||||
|
<Trans i18nKey="dashboard:heroCard.orderPackage.description" />
|
||||||
|
</CardDescription>
|
||||||
|
</CardFooter>
|
||||||
|
</Card>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default OrderAnalysesPackageCard;
|
||||||
67
app/home/(user)/_components/ai/recommendations-skeleton.tsx
Normal file
67
app/home/(user)/_components/ai/recommendations-skeleton.tsx
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
import React from 'react';
|
||||||
|
|
||||||
|
import { InfoTooltip } from '@/packages/shared/src/components/ui/info-tooltip';
|
||||||
|
|
||||||
|
import { Button } from '@kit/ui/shadcn/button';
|
||||||
|
import {
|
||||||
|
Card,
|
||||||
|
CardDescription,
|
||||||
|
CardFooter,
|
||||||
|
CardHeader,
|
||||||
|
} from '@kit/ui/shadcn/card';
|
||||||
|
import { Skeleton } from '@kit/ui/skeleton';
|
||||||
|
|
||||||
|
const RecommendationsSkeleton = ({ amount = 2 }: { amount?: number }) => {
|
||||||
|
const emptyData = [
|
||||||
|
{
|
||||||
|
title: '1',
|
||||||
|
description: '',
|
||||||
|
subtitle: '',
|
||||||
|
variant: { id: '' },
|
||||||
|
price: 1,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
return Array.from({ length: amount }, (_, index) => {
|
||||||
|
const { title, description, subtitle } = emptyData[0]!;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Skeleton key={title + index}>
|
||||||
|
<Card>
|
||||||
|
<CardHeader className="flex-row">
|
||||||
|
<div
|
||||||
|
className={
|
||||||
|
'mb-6 flex size-8 items-center-safe justify-center-safe'
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
<div className="ml-auto flex size-8 items-center-safe justify-center-safe">
|
||||||
|
<Button size="icon" className="px-2" />
|
||||||
|
</div>
|
||||||
|
</CardHeader>
|
||||||
|
<CardFooter className="flex">
|
||||||
|
<div className="flex flex-1 flex-col items-start">
|
||||||
|
<h5>
|
||||||
|
{title}
|
||||||
|
{description && (
|
||||||
|
<>
|
||||||
|
{' '}
|
||||||
|
<InfoTooltip
|
||||||
|
content={
|
||||||
|
<div className="flex flex-col gap-2">
|
||||||
|
<span>{description}</span>
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
</h5>
|
||||||
|
{subtitle && <CardDescription>{subtitle}</CardDescription>}
|
||||||
|
</div>
|
||||||
|
<div className="flex flex-col items-end gap-2 self-end text-sm"></div>
|
||||||
|
</CardFooter>
|
||||||
|
</Card>
|
||||||
|
</Skeleton>
|
||||||
|
);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
export default RecommendationsSkeleton;
|
||||||
41
app/home/(user)/_components/ai/recommendations.tsx
Normal file
41
app/home/(user)/_components/ai/recommendations.tsx
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
'use server';
|
||||||
|
|
||||||
|
import React from 'react';
|
||||||
|
|
||||||
|
import { AccountWithParams } from '@/packages/features/accounts/src/types/accounts';
|
||||||
|
|
||||||
|
import { getLatestResponseTime } from '~/lib/utils';
|
||||||
|
|
||||||
|
import { loadAnalyses } from '../../_lib/server/load-analyses';
|
||||||
|
import { loadRecommendations } from '../../_lib/server/load-recommendations';
|
||||||
|
import OrderAnalysesCards from '../order-analyses-cards';
|
||||||
|
import { AnalysisResponses } from './types';
|
||||||
|
|
||||||
|
export default async function Recommendations({
|
||||||
|
account,
|
||||||
|
analysisResponses,
|
||||||
|
}: {
|
||||||
|
account: AccountWithParams;
|
||||||
|
analysisResponses?: AnalysisResponses;
|
||||||
|
}) {
|
||||||
|
const { analyses, countryCode } = await loadAnalyses();
|
||||||
|
|
||||||
|
const currentAIResponseTimestamp = getLatestResponseTime(analysisResponses);
|
||||||
|
const analysisRecommendations = await loadRecommendations({
|
||||||
|
account,
|
||||||
|
analyses,
|
||||||
|
analysisResponses,
|
||||||
|
aiResponseTimestamp: currentAIResponseTimestamp,
|
||||||
|
});
|
||||||
|
const orderAnalyses = analyses.filter((analysis) =>
|
||||||
|
analysisRecommendations.includes(analysis.title),
|
||||||
|
);
|
||||||
|
|
||||||
|
if (orderAnalyses.length === 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<OrderAnalysesCards analyses={orderAnalyses} countryCode={countryCode} />
|
||||||
|
);
|
||||||
|
}
|
||||||
19
app/home/(user)/_components/ai/types.ts
Normal file
19
app/home/(user)/_components/ai/types.ts
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
import { Database } from '@/packages/supabase/src/database.types';
|
||||||
|
|
||||||
|
export interface ILifeStyleResponse {
|
||||||
|
lifestyle: {
|
||||||
|
title: string;
|
||||||
|
description: string;
|
||||||
|
score: 0 | 1 | 2;
|
||||||
|
}[];
|
||||||
|
summary: string | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export enum PROMPT_NAME {
|
||||||
|
LIFE_STYLE = 'Life Style',
|
||||||
|
ANALYSIS_RECOMMENDATIONS = 'Analysis Recommendations',
|
||||||
|
FEEDBACK = 'Doctor Feedback',
|
||||||
|
}
|
||||||
|
|
||||||
|
export type AnalysisResponses =
|
||||||
|
Database['medreport']['Functions']['get_latest_analysis_response_elements_for_current_user']['Returns'];
|
||||||
@@ -82,8 +82,8 @@ export function HomeMobileNavigation(props: {
|
|||||||
const hasDoctorRole =
|
const hasDoctorRole =
|
||||||
personalAccountData?.application_role === ApplicationRoleEnum.Doctor;
|
personalAccountData?.application_role === ApplicationRoleEnum.Doctor;
|
||||||
|
|
||||||
return hasDoctorRole && hasTotpFactor;
|
return hasDoctorRole;
|
||||||
}, [user, personalAccountData, hasTotpFactor]);
|
}, [personalAccountData]);
|
||||||
|
|
||||||
const cartQuantityTotal =
|
const cartQuantityTotal =
|
||||||
props.cart?.items?.reduce((acc, item) => acc + item.quantity, 0) ?? 0;
|
props.cart?.items?.reduce((acc, item) => acc + item.quantity, 0) ?? 0;
|
||||||
|
|||||||
@@ -57,72 +57,64 @@ export default function OrderAnalysesCards({
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return analyses.map(({ title, variant, description, subtitle, price }) => {
|
||||||
<div className="xs:grid-cols-3 mt-4 grid gap-6">
|
const formattedPrice =
|
||||||
{analyses.map(({ title, variant, description, subtitle, price }) => {
|
typeof price === 'number'
|
||||||
const formattedPrice =
|
? formatCurrency({
|
||||||
typeof price === 'number'
|
currencyCode: 'eur',
|
||||||
? formatCurrency({
|
locale: language,
|
||||||
currencyCode: 'eur',
|
value: price,
|
||||||
locale: language,
|
})
|
||||||
value: price,
|
: null;
|
||||||
})
|
return (
|
||||||
: null;
|
<Card
|
||||||
return (
|
key={title}
|
||||||
<Card
|
variant="gradient-success"
|
||||||
key={title}
|
className="flex flex-col justify-between"
|
||||||
variant="gradient-success"
|
>
|
||||||
className="flex flex-col justify-between"
|
<CardHeader className="flex-row">
|
||||||
>
|
<div className="bg-primary/10 mb-6 flex size-8 items-center-safe justify-center-safe rounded-full text-white">
|
||||||
<CardHeader className="flex-row">
|
<HeartPulse className="size-4 fill-green-500" />
|
||||||
<div
|
</div>
|
||||||
className={
|
<div className="bg-warning ml-auto flex size-8 items-center-safe justify-center-safe rounded-full text-white">
|
||||||
'bg-primary/10 mb-6 flex size-8 items-center-safe justify-center-safe rounded-full text-white'
|
<Button
|
||||||
}
|
size="icon"
|
||||||
>
|
variant="outline"
|
||||||
<HeartPulse className="size-4 fill-green-500" />
|
className="px-2 text-black"
|
||||||
</div>
|
onClick={() => handleSelect(variant.id)}
|
||||||
<div className="bg-warning ml-auto flex size-8 items-center-safe justify-center-safe rounded-full text-white">
|
>
|
||||||
<Button
|
{variantAddingToCart === variant.id ? (
|
||||||
size="icon"
|
<Loader2 className="size-4 animate-spin stroke-2" />
|
||||||
variant="outline"
|
) : (
|
||||||
className="px-2 text-black"
|
<ShoppingCart className="size-4 stroke-2" />
|
||||||
onClick={() => handleSelect(variant.id)}
|
)}
|
||||||
>
|
</Button>
|
||||||
{variantAddingToCart === variant.id ? (
|
</div>
|
||||||
<Loader2 className="size-4 animate-spin stroke-2" />
|
</CardHeader>
|
||||||
) : (
|
<CardFooter className="flex gap-2">
|
||||||
<ShoppingCart className="size-4 stroke-2" />
|
<div className="flex flex-1 flex-col items-start gap-2">
|
||||||
)}
|
<h5>
|
||||||
</Button>
|
{title}
|
||||||
</div>
|
{description && (
|
||||||
</CardHeader>
|
<>
|
||||||
<CardFooter className="flex gap-2">
|
<InfoTooltip
|
||||||
<div className="flex flex-1 flex-col items-start gap-2">
|
content={
|
||||||
<h5>
|
<div className="flex flex-col gap-2">
|
||||||
{title}
|
<span>{formattedPrice}</span>
|
||||||
{description && (
|
<span>{description}</span>
|
||||||
<>
|
</div>
|
||||||
<InfoTooltip
|
}
|
||||||
content={
|
/>
|
||||||
<div className="flex flex-col gap-2">
|
</>
|
||||||
<span>{formattedPrice}</span>
|
)}
|
||||||
<span>{description}</span>
|
</h5>
|
||||||
</div>
|
{subtitle && <CardDescription>{subtitle}</CardDescription>}
|
||||||
}
|
</div>
|
||||||
/>
|
<div className="flex flex-col items-end gap-2 self-end text-sm">
|
||||||
</>
|
<span>{formattedPrice}</span>
|
||||||
)}
|
</div>
|
||||||
</h5>
|
</CardFooter>
|
||||||
{subtitle && <CardDescription>{subtitle}</CardDescription>}
|
</Card>
|
||||||
</div>
|
);
|
||||||
<div className="flex flex-col items-end gap-2 self-end text-sm">
|
});
|
||||||
<span>{formattedPrice}</span>
|
|
||||||
</div>
|
|
||||||
</CardFooter>
|
|
||||||
</Card>
|
|
||||||
);
|
|
||||||
})}
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,74 +0,0 @@
|
|||||||
import React from 'react';
|
|
||||||
|
|
||||||
import { InfoTooltip } from '@/packages/shared/src/components/ui/info-tooltip';
|
|
||||||
|
|
||||||
import { Button } from '@kit/ui/shadcn/button';
|
|
||||||
import {
|
|
||||||
Card,
|
|
||||||
CardDescription,
|
|
||||||
CardFooter,
|
|
||||||
CardHeader,
|
|
||||||
} from '@kit/ui/shadcn/card';
|
|
||||||
import { Skeleton } from '@kit/ui/skeleton';
|
|
||||||
|
|
||||||
const RecommendationsSkeleton = () => {
|
|
||||||
const emptyData = [
|
|
||||||
{
|
|
||||||
title: '1',
|
|
||||||
description: '',
|
|
||||||
subtitle: '',
|
|
||||||
variant: { id: '' },
|
|
||||||
price: 1,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
title: '2',
|
|
||||||
description: '',
|
|
||||||
subtitle: '',
|
|
||||||
variant: { id: '' },
|
|
||||||
price: 1,
|
|
||||||
},
|
|
||||||
];
|
|
||||||
return (
|
|
||||||
<div className="xs:grid-cols-3 mt-4 grid gap-6">
|
|
||||||
{emptyData.map(({ title, description, subtitle }) => (
|
|
||||||
<Skeleton key={title}>
|
|
||||||
<Card>
|
|
||||||
<CardHeader className="flex-row">
|
|
||||||
<div
|
|
||||||
className={
|
|
||||||
'mb-6 flex size-8 items-center-safe justify-center-safe'
|
|
||||||
}
|
|
||||||
/>
|
|
||||||
<div className="ml-auto flex size-8 items-center-safe justify-center-safe">
|
|
||||||
<Button size="icon" className="px-2" />
|
|
||||||
</div>
|
|
||||||
</CardHeader>
|
|
||||||
<CardFooter className="flex">
|
|
||||||
<div className="flex flex-1 flex-col items-start">
|
|
||||||
<h5>
|
|
||||||
{title}
|
|
||||||
{description && (
|
|
||||||
<>
|
|
||||||
{' '}
|
|
||||||
<InfoTooltip
|
|
||||||
content={
|
|
||||||
<div className="flex flex-col gap-2">
|
|
||||||
<span>{description}</span>
|
|
||||||
</div>
|
|
||||||
}
|
|
||||||
/>
|
|
||||||
</>
|
|
||||||
)}
|
|
||||||
</h5>
|
|
||||||
{subtitle && <CardDescription>{subtitle}</CardDescription>}
|
|
||||||
</div>
|
|
||||||
<div className="flex flex-col items-end gap-2 self-end text-sm"></div>
|
|
||||||
</CardFooter>
|
|
||||||
</Card>
|
|
||||||
</Skeleton>
|
|
||||||
))}
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
export default RecommendationsSkeleton;
|
|
||||||
@@ -1,30 +0,0 @@
|
|||||||
'use server';
|
|
||||||
|
|
||||||
import React from 'react';
|
|
||||||
|
|
||||||
import { AccountWithParams } from '@/packages/features/accounts/src/types/accounts';
|
|
||||||
|
|
||||||
import { loadAnalyses } from '../_lib/server/load-analyses';
|
|
||||||
import { loadRecommendations } from '../_lib/server/load-recommendations';
|
|
||||||
import OrderAnalysesCards from './order-analyses-cards';
|
|
||||||
|
|
||||||
export default async function Recommendations({
|
|
||||||
account,
|
|
||||||
}: {
|
|
||||||
account: AccountWithParams;
|
|
||||||
}) {
|
|
||||||
const { analyses, countryCode } = await loadAnalyses();
|
|
||||||
|
|
||||||
const analysisRecommendations = await loadRecommendations(analyses, account);
|
|
||||||
const orderAnalyses = analyses.filter((analysis) =>
|
|
||||||
analysisRecommendations.includes(analysis.title),
|
|
||||||
);
|
|
||||||
|
|
||||||
if (orderAnalyses.length === 0) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
|
||||||
<OrderAnalysesCards analyses={orderAnalyses} countryCode={countryCode} />
|
|
||||||
);
|
|
||||||
}
|
|
||||||
324
app/home/(user)/_lib/server/ai-actions.ts
Normal file
324
app/home/(user)/_lib/server/ai-actions.ts
Normal file
@@ -0,0 +1,324 @@
|
|||||||
|
'use server';
|
||||||
|
|
||||||
|
import { AccountWithParams } from '@/packages/features/accounts/src/types/accounts';
|
||||||
|
import {
|
||||||
|
AnalysisResponse,
|
||||||
|
Patient,
|
||||||
|
} from '@/packages/features/doctor/src/lib/server/schema/doctor-analysis-detail-view.schema';
|
||||||
|
import { getLogger } from '@/packages/shared/src/logger';
|
||||||
|
import { getSupabaseServerClient } from '@/packages/supabase/src/clients/server-client';
|
||||||
|
import OpenAI from 'openai';
|
||||||
|
|
||||||
|
import PersonalCode from '~/lib/utils';
|
||||||
|
|
||||||
|
import {
|
||||||
|
AnalysisResponses,
|
||||||
|
ILifeStyleResponse,
|
||||||
|
PROMPT_NAME,
|
||||||
|
} from '../../_components/ai/types';
|
||||||
|
import { OrderAnalysisCard } from '../../_components/order-analyses-cards';
|
||||||
|
|
||||||
|
export async function updateLifeStyle({
|
||||||
|
account,
|
||||||
|
analysisResponses,
|
||||||
|
isDoctorView = false,
|
||||||
|
aiResponseTimestamp,
|
||||||
|
}: {
|
||||||
|
account: AccountWithParams;
|
||||||
|
analysisResponses?: AnalysisResponses;
|
||||||
|
isDoctorView?: boolean;
|
||||||
|
aiResponseTimestamp: string;
|
||||||
|
}): Promise<ILifeStyleResponse> {
|
||||||
|
const LIFE_STYLE_PROMPT_ID = process.env.PROMPT_ID_LIFE_STYLE;
|
||||||
|
if (!LIFE_STYLE_PROMPT_ID || !account?.personal_code) {
|
||||||
|
return {
|
||||||
|
lifestyle: [],
|
||||||
|
summary: null,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const openAIClient = new OpenAI();
|
||||||
|
const supabaseClient = getSupabaseServerClient();
|
||||||
|
const { gender, age } = PersonalCode.parsePersonalCode(account.personal_code);
|
||||||
|
const weight = account.accountParams?.weight || 'unknown';
|
||||||
|
const height = account.accountParams?.height || 'unknown';
|
||||||
|
const isSmoker = !!account.accountParams?.isSmoker;
|
||||||
|
const cholesterol =
|
||||||
|
analysisResponses
|
||||||
|
?.find((ar) => ar.analysis_name_lab === 'Kolesterool')
|
||||||
|
?.response_value.toString() || 'unknown';
|
||||||
|
const ldl =
|
||||||
|
analysisResponses
|
||||||
|
?.find((ar) => ar.analysis_name_lab === 'LDL kolesterool')
|
||||||
|
?.response_value.toString() || 'unknown';
|
||||||
|
const hdl =
|
||||||
|
analysisResponses
|
||||||
|
?.find((ar) => ar.analysis_name_lab === 'HDL kolesterool')
|
||||||
|
?.response_value.toString() || 'unknown';
|
||||||
|
const vitamind =
|
||||||
|
analysisResponses
|
||||||
|
?.find((ar) => ar.analysis_name_lab === 'Vitamiin D (25-OH)')
|
||||||
|
?.response_value.toString() || 'unknown';
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await openAIClient.responses.create({
|
||||||
|
store: false,
|
||||||
|
prompt: {
|
||||||
|
id: LIFE_STYLE_PROMPT_ID,
|
||||||
|
variables: {
|
||||||
|
gender: gender.value,
|
||||||
|
age: age.toString(),
|
||||||
|
weight: weight.toString(),
|
||||||
|
height: height.toString(),
|
||||||
|
cholesterol,
|
||||||
|
ldl,
|
||||||
|
hdl,
|
||||||
|
vitamind,
|
||||||
|
is_smoker: isSmoker.toString(),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
await supabaseClient
|
||||||
|
.schema('medreport')
|
||||||
|
.from('ai_responses')
|
||||||
|
.insert({
|
||||||
|
account_id: account.id,
|
||||||
|
prompt_name: PROMPT_NAME.LIFE_STYLE,
|
||||||
|
prompt_id: LIFE_STYLE_PROMPT_ID,
|
||||||
|
input: JSON.stringify({
|
||||||
|
gender: gender.value,
|
||||||
|
age: age.toString(),
|
||||||
|
weight: weight.toString(),
|
||||||
|
cholesterol,
|
||||||
|
ldl,
|
||||||
|
hdl,
|
||||||
|
vitamind,
|
||||||
|
is_smoker: isSmoker.toString(),
|
||||||
|
}),
|
||||||
|
latest_data_change: aiResponseTimestamp,
|
||||||
|
response: response.output_text,
|
||||||
|
is_visible_to_customer: !isDoctorView,
|
||||||
|
});
|
||||||
|
|
||||||
|
const json = JSON.parse(response.output_text);
|
||||||
|
|
||||||
|
return json;
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error calling OpenAI: ', error);
|
||||||
|
return {
|
||||||
|
lifestyle: [],
|
||||||
|
summary: null,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function updateRecommendations({
|
||||||
|
analyses,
|
||||||
|
analysisResponses,
|
||||||
|
account,
|
||||||
|
aiResponseTimestamp,
|
||||||
|
}: {
|
||||||
|
analyses: OrderAnalysisCard[];
|
||||||
|
analysisResponses?: AnalysisResponses;
|
||||||
|
account: AccountWithParams;
|
||||||
|
aiResponseTimestamp: string;
|
||||||
|
}) {
|
||||||
|
const RECOMMENDATIONS_PROMPT_IT =
|
||||||
|
process.env.PROMPT_ID_ANALYSIS_RECOMMENDATIONS;
|
||||||
|
|
||||||
|
if (!RECOMMENDATIONS_PROMPT_IT || !account?.personal_code) {
|
||||||
|
console.error('No prompt ID for analysis recommendations');
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
const openAIClient = new OpenAI();
|
||||||
|
const supabaseClient = getSupabaseServerClient();
|
||||||
|
|
||||||
|
const { gender, age } = PersonalCode.parsePersonalCode(account.personal_code);
|
||||||
|
const weight = account.accountParams?.weight || 'unknown';
|
||||||
|
|
||||||
|
const formattedAnalysisResponses = analysisResponses?.map(
|
||||||
|
({
|
||||||
|
analysis_name_lab,
|
||||||
|
response_value,
|
||||||
|
norm_upper,
|
||||||
|
norm_lower,
|
||||||
|
norm_status,
|
||||||
|
}) => ({
|
||||||
|
name: analysis_name_lab,
|
||||||
|
value: response_value,
|
||||||
|
normUpper: norm_upper,
|
||||||
|
normLower: norm_lower,
|
||||||
|
normStatus: norm_status,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
const formattedAnalyses = analyses.map(({ description, title }) => ({
|
||||||
|
description,
|
||||||
|
title,
|
||||||
|
}));
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await openAIClient.responses.create({
|
||||||
|
store: false,
|
||||||
|
prompt: {
|
||||||
|
id: RECOMMENDATIONS_PROMPT_IT,
|
||||||
|
variables: {
|
||||||
|
analyses: JSON.stringify(formattedAnalyses),
|
||||||
|
results: JSON.stringify(formattedAnalysisResponses),
|
||||||
|
gender: gender.value,
|
||||||
|
age: age.toString(),
|
||||||
|
weight: weight.toString(),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
await supabaseClient
|
||||||
|
.schema('medreport')
|
||||||
|
.from('ai_responses')
|
||||||
|
.insert({
|
||||||
|
account_id: account.id,
|
||||||
|
prompt_name: PROMPT_NAME.ANALYSIS_RECOMMENDATIONS,
|
||||||
|
prompt_id: RECOMMENDATIONS_PROMPT_IT,
|
||||||
|
input: JSON.stringify({
|
||||||
|
analyses: formattedAnalyses,
|
||||||
|
results: formattedAnalysisResponses,
|
||||||
|
gender,
|
||||||
|
age,
|
||||||
|
weight,
|
||||||
|
}),
|
||||||
|
latest_data_change: aiResponseTimestamp,
|
||||||
|
response: response.output_text,
|
||||||
|
is_visible_to_customer: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
const json = JSON.parse(response.output_text);
|
||||||
|
|
||||||
|
return json.recommended;
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error getting recommendations: ', error);
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function generateDoctorFeedback({
|
||||||
|
patient,
|
||||||
|
analysisResponses,
|
||||||
|
aiResponseTimestamp,
|
||||||
|
}: {
|
||||||
|
patient: Patient;
|
||||||
|
analysisResponses: AnalysisResponse[];
|
||||||
|
aiResponseTimestamp: string;
|
||||||
|
}): Promise<string> {
|
||||||
|
const DOCTOR_FEEDBACK_PROMPT_ID = process.env.PROMPT_ID_DOCTOR_FEEDBACK;
|
||||||
|
|
||||||
|
if (!DOCTOR_FEEDBACK_PROMPT_ID) {
|
||||||
|
console.error('No secrets for doctor feedback');
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
const openAIClient = new OpenAI();
|
||||||
|
const supabaseClient = getSupabaseServerClient();
|
||||||
|
|
||||||
|
const formattedAnalysisResponses = analysisResponses?.map(
|
||||||
|
({
|
||||||
|
analysis_name,
|
||||||
|
response_value,
|
||||||
|
norm_upper,
|
||||||
|
norm_lower,
|
||||||
|
norm_status,
|
||||||
|
}) => ({
|
||||||
|
name: analysis_name,
|
||||||
|
value: response_value,
|
||||||
|
normUpper: norm_upper,
|
||||||
|
normLower: norm_lower,
|
||||||
|
normStatus: norm_status,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await openAIClient.responses.create({
|
||||||
|
store: false,
|
||||||
|
prompt: {
|
||||||
|
id: DOCTOR_FEEDBACK_PROMPT_ID,
|
||||||
|
variables: {
|
||||||
|
analysesresults: JSON.stringify(formattedAnalysisResponses),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
await supabaseClient
|
||||||
|
.schema('medreport')
|
||||||
|
.from('ai_responses')
|
||||||
|
.insert({
|
||||||
|
account_id: patient.userId,
|
||||||
|
prompt_name: PROMPT_NAME.FEEDBACK,
|
||||||
|
prompt_id: DOCTOR_FEEDBACK_PROMPT_ID,
|
||||||
|
input: JSON.stringify({
|
||||||
|
analysesresults: formattedAnalysisResponses,
|
||||||
|
}),
|
||||||
|
latest_data_change: aiResponseTimestamp,
|
||||||
|
response: response.output_text,
|
||||||
|
});
|
||||||
|
|
||||||
|
return response.output_text;
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error getting doctor feedback: ', error);
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function confirmPatientAIResponses(
|
||||||
|
patientId: string,
|
||||||
|
aiResponseTimestamp: string,
|
||||||
|
recommendations: string[],
|
||||||
|
isRecommendationsEdited: boolean,
|
||||||
|
) {
|
||||||
|
const logger = await getLogger();
|
||||||
|
const supabaseClient = getSupabaseServerClient();
|
||||||
|
|
||||||
|
const { error } = await supabaseClient
|
||||||
|
.schema('medreport')
|
||||||
|
.from('ai_responses')
|
||||||
|
.update({
|
||||||
|
is_visible_to_customer: true,
|
||||||
|
})
|
||||||
|
.eq('latest_data_change', aiResponseTimestamp)
|
||||||
|
.eq('account_id', patientId)
|
||||||
|
.eq('prompt_name', PROMPT_NAME.LIFE_STYLE);
|
||||||
|
|
||||||
|
if (error) {
|
||||||
|
logger.error(
|
||||||
|
{ error, patientId, aiResponseTimestamp },
|
||||||
|
'Failed updating life style',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const { error: _error } = await supabaseClient
|
||||||
|
.schema('medreport')
|
||||||
|
.from('ai_responses')
|
||||||
|
.update({
|
||||||
|
is_visible_to_customer: true,
|
||||||
|
...(isRecommendationsEdited && {
|
||||||
|
response: JSON.stringify({
|
||||||
|
why: 'This was edited by doctor',
|
||||||
|
recommended: recommendations,
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
.eq('latest_data_change', aiResponseTimestamp)
|
||||||
|
.eq('account_id', patientId)
|
||||||
|
.eq('prompt_name', PROMPT_NAME.ANALYSIS_RECOMMENDATIONS);
|
||||||
|
|
||||||
|
if (_error) {
|
||||||
|
logger.error(
|
||||||
|
{
|
||||||
|
error,
|
||||||
|
aiResponseTimestamp,
|
||||||
|
patientId,
|
||||||
|
isRecommendationsEdited,
|
||||||
|
},
|
||||||
|
'Failed updating analysis recommendations',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
83
app/home/(user)/_lib/server/load-life-style.ts
Normal file
83
app/home/(user)/_lib/server/load-life-style.ts
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
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 {
|
||||||
|
AnalysisResponses,
|
||||||
|
ILifeStyleResponse,
|
||||||
|
PROMPT_NAME,
|
||||||
|
} from '../../_components/ai/types';
|
||||||
|
import { updateLifeStyle } from './ai-actions';
|
||||||
|
|
||||||
|
const failedResponse = {
|
||||||
|
response: {
|
||||||
|
lifestyle: [],
|
||||||
|
summary: null,
|
||||||
|
},
|
||||||
|
dateCreated: new Date().toISOString(),
|
||||||
|
};
|
||||||
|
|
||||||
|
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 query = supabaseClient
|
||||||
|
.schema('medreport')
|
||||||
|
.from('ai_responses')
|
||||||
|
.select('response, latest_data_change')
|
||||||
|
.eq('account_id', account.id)
|
||||||
|
.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);
|
||||||
|
return failedResponse;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (data?.response) {
|
||||||
|
return {
|
||||||
|
response: JSON.parse(data.response as string),
|
||||||
|
dateCreated: data.latest_data_change,
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
const newLifeStyle = await updateLifeStyle({
|
||||||
|
account,
|
||||||
|
analysisResponses,
|
||||||
|
isDoctorView,
|
||||||
|
aiResponseTimestamp,
|
||||||
|
});
|
||||||
|
|
||||||
|
return { response: newLifeStyle, dateCreated: aiResponseTimestamp };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
export const loadLifeStyle = cache(lifeStyleLoader);
|
||||||
@@ -1,140 +1,74 @@
|
|||||||
import { cache } from 'react';
|
import { cache } from 'react';
|
||||||
|
|
||||||
import { AccountWithParams } from '@/packages/features/accounts/src/types/accounts';
|
import { AccountWithParams } from '@/packages/features/accounts/src/types/accounts';
|
||||||
import { createUserAnalysesApi } from '@/packages/features/user-analyses/src/server/api';
|
import { getLogger } from '@/packages/shared/src/logger';
|
||||||
import { getSupabaseServerClient } from '@/packages/supabase/src/clients/server-client';
|
import { getSupabaseServerClient } from '@/packages/supabase/src/clients/server-client';
|
||||||
import { Database } from '@/packages/supabase/src/database.types';
|
|
||||||
import OpenAI from 'openai';
|
|
||||||
|
|
||||||
import PersonalCode from '~/lib/utils';
|
|
||||||
|
|
||||||
|
import { AnalysisResponses, PROMPT_NAME } from '../../_components/ai/types';
|
||||||
import { OrderAnalysisCard } from '../../_components/order-analyses-cards';
|
import { OrderAnalysisCard } from '../../_components/order-analyses-cards';
|
||||||
|
import { updateRecommendations } from './ai-actions';
|
||||||
|
|
||||||
export const loadRecommendations = cache(recommendationsLoader);
|
export const loadRecommendations = cache(recommendationsLoader);
|
||||||
|
|
||||||
type AnalysisResponses =
|
async function recommendationsLoader({
|
||||||
Database['medreport']['Functions']['get_latest_analysis_response_elements_for_current_user']['Returns'];
|
account,
|
||||||
|
isDoctorView = false,
|
||||||
const getLatestResponseTime = (items: AnalysisResponses) => {
|
analyses,
|
||||||
if (!items?.length) return null;
|
analysisResponses,
|
||||||
|
aiResponseTimestamp,
|
||||||
let latest = null;
|
}: {
|
||||||
for (const it of items) {
|
account: AccountWithParams | null;
|
||||||
const d = new Date(it.response_time);
|
isDoctorView?: boolean;
|
||||||
const t = d.getTime();
|
analyses: OrderAnalysisCard[];
|
||||||
if (!Number.isNaN(t) && (latest === null || t > latest.getTime())) {
|
analysisResponses?: AnalysisResponses;
|
||||||
latest = d;
|
aiResponseTimestamp: string;
|
||||||
}
|
}): Promise<string[]> {
|
||||||
}
|
const logger = await getLogger();
|
||||||
return latest;
|
|
||||||
};
|
|
||||||
|
|
||||||
async function recommendationsLoader(
|
|
||||||
analyses: OrderAnalysisCard[],
|
|
||||||
account: AccountWithParams | null,
|
|
||||||
): Promise<string[]> {
|
|
||||||
if (!process.env.OPENAI_API_KEY) {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
if (!account?.personal_code) {
|
if (!account?.personal_code) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
const supabaseClient = getSupabaseServerClient();
|
const supabaseClient = getSupabaseServerClient();
|
||||||
const userAnalysesApi = createUserAnalysesApi(supabaseClient);
|
|
||||||
const analysisResponses = await userAnalysesApi.getAllUserAnalysisResponses();
|
|
||||||
const analysesRecommendationsPromptId =
|
|
||||||
process.env.PROMPT_ID_ANALYSIS_RECOMMENDATIONS;
|
|
||||||
const latestResponseTime = getLatestResponseTime(analysisResponses);
|
|
||||||
const latestISO = latestResponseTime
|
|
||||||
? new Date(latestResponseTime).toISOString()
|
|
||||||
: new Date('2025').toISOString();
|
|
||||||
|
|
||||||
if (!analysesRecommendationsPromptId) {
|
const query = supabaseClient
|
||||||
console.error('No prompt ID for analysis recommendations');
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
const previouslyRecommended = await supabaseClient
|
|
||||||
.schema('medreport')
|
.schema('medreport')
|
||||||
.from('ai_responses')
|
.from('ai_responses')
|
||||||
.select('*')
|
.select('*')
|
||||||
.eq('account_id', account.id)
|
.eq('account_id', account.id)
|
||||||
.eq('prompt_id', analysesRecommendationsPromptId)
|
.eq('prompt_name', PROMPT_NAME.ANALYSIS_RECOMMENDATIONS);
|
||||||
.eq('latest_data_change', latestISO);
|
|
||||||
|
|
||||||
if (previouslyRecommended.data?.[0]?.response) {
|
logger.info(
|
||||||
return JSON.parse(previouslyRecommended.data[0].response as string)
|
{ accountId: account.id, isDoctorView, aiResponseTimestamp },
|
||||||
.recommended;
|
'Attempting to receive existing recommendations',
|
||||||
|
);
|
||||||
|
|
||||||
|
if (isDoctorView) {
|
||||||
|
query.eq('latest_data_change', aiResponseTimestamp);
|
||||||
|
} else {
|
||||||
|
query
|
||||||
|
.eq('is_visible_to_customer', true)
|
||||||
|
.order('latest_data_change', { ascending: false, nullsFirst: false });
|
||||||
}
|
}
|
||||||
|
|
||||||
const openAIClient = new OpenAI();
|
const { data, error } = await query.limit(1).maybeSingle();
|
||||||
const { gender, age } = PersonalCode.parsePersonalCode(account.personal_code);
|
|
||||||
const weight = account.accountParams?.weight || 'unknown';
|
|
||||||
|
|
||||||
const formattedAnalysisResponses = analysisResponses.map(
|
logger.info({ data: !!data }, 'Existing recommendations');
|
||||||
({
|
|
||||||
analysis_name_lab,
|
|
||||||
response_value,
|
|
||||||
norm_upper,
|
|
||||||
norm_lower,
|
|
||||||
norm_status,
|
|
||||||
}) => ({
|
|
||||||
name: analysis_name_lab,
|
|
||||||
value: response_value,
|
|
||||||
normUpper: norm_upper,
|
|
||||||
normLower: norm_lower,
|
|
||||||
normStatus: norm_status,
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
const formattedAnalyses = analyses.map(({ description, title }) => ({
|
|
||||||
description,
|
|
||||||
title,
|
|
||||||
}));
|
|
||||||
|
|
||||||
let response;
|
if (error) {
|
||||||
|
console.error('Error fetching AI response from DB: ', error);
|
||||||
try {
|
|
||||||
response = await openAIClient.responses.create({
|
|
||||||
store: false,
|
|
||||||
prompt: {
|
|
||||||
id: analysesRecommendationsPromptId,
|
|
||||||
variables: {
|
|
||||||
analyses: JSON.stringify(formattedAnalyses),
|
|
||||||
results: JSON.stringify(formattedAnalysisResponses),
|
|
||||||
gender: gender.value,
|
|
||||||
age: age.toString(),
|
|
||||||
weight: weight.toString(),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Error calling OpenAI: ', error);
|
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
const json = JSON.parse(response.output_text);
|
if (data?.response) {
|
||||||
|
return JSON.parse(data.response as string).recommended;
|
||||||
try {
|
} else {
|
||||||
await supabaseClient
|
if (isDoctorView) {
|
||||||
.schema('medreport')
|
return await updateRecommendations({
|
||||||
.from('ai_responses')
|
account,
|
||||||
.insert({
|
analyses,
|
||||||
account_id: account.id,
|
analysisResponses,
|
||||||
prompt_name: 'Analysis Recommendations',
|
aiResponseTimestamp,
|
||||||
prompt_id: analysesRecommendationsPromptId,
|
|
||||||
input: JSON.stringify({
|
|
||||||
analyses: formattedAnalyses,
|
|
||||||
results: formattedAnalysisResponses,
|
|
||||||
gender,
|
|
||||||
age,
|
|
||||||
weight,
|
|
||||||
}),
|
|
||||||
latest_data_change: latestISO,
|
|
||||||
response: response.output_text,
|
|
||||||
});
|
});
|
||||||
} catch (error) {
|
}
|
||||||
console.error('Error saving AI response: ', error);
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
return json.recommended;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ export enum PageViewAction {
|
|||||||
VIEW_ORDER_ANALYSIS = 'VIEW_ORDER_ANALYSIS',
|
VIEW_ORDER_ANALYSIS = 'VIEW_ORDER_ANALYSIS',
|
||||||
VIEW_TEAM_ACCOUNT_DASHBOARD = 'VIEW_TEAM_ACCOUNT_DASHBOARD',
|
VIEW_TEAM_ACCOUNT_DASHBOARD = 'VIEW_TEAM_ACCOUNT_DASHBOARD',
|
||||||
VIEW_TTO_SERVICE_BOOKING = 'VIEW_TTO_SERVICE_BOOKING',
|
VIEW_TTO_SERVICE_BOOKING = 'VIEW_TTO_SERVICE_BOOKING',
|
||||||
|
VIEW_LIFE_STYLE = 'VIEW_LIFE_STYLE',
|
||||||
}
|
}
|
||||||
|
|
||||||
export const createPageViewLog = async ({
|
export const createPageViewLog = async ({
|
||||||
|
|||||||
15
lib/utils.ts
15
lib/utils.ts
@@ -1,3 +1,4 @@
|
|||||||
|
import { AnalysisResponses } from '@/app/home/(user)/_components/ai/types';
|
||||||
import { type ClassValue, clsx } from 'clsx';
|
import { type ClassValue, clsx } from 'clsx';
|
||||||
import Isikukood, { Gender } from 'isikukood';
|
import Isikukood, { Gender } from 'isikukood';
|
||||||
import { twMerge } from 'tailwind-merge';
|
import { twMerge } from 'tailwind-merge';
|
||||||
@@ -148,3 +149,17 @@ export const findProductTypeIdByHandle = (
|
|||||||
) => {
|
) => {
|
||||||
return productTypes.find(({ metadata }) => metadata?.handle === handle)?.id;
|
return productTypes.find(({ metadata }) => metadata?.handle === handle)?.id;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export function getLatestResponseTime(items?: AnalysisResponses) {
|
||||||
|
if (!items?.length) return new Date().toISOString();
|
||||||
|
|
||||||
|
let latest = null;
|
||||||
|
for (const it of items) {
|
||||||
|
const d = new Date(it.response_time);
|
||||||
|
const t = d.getTime();
|
||||||
|
if (!Number.isNaN(t) && (latest === null || t > latest.getTime())) {
|
||||||
|
latest = d;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return latest ? new Date(latest).toISOString() : new Date().toISOString();
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
import { revalidatePath } from 'next/cache';
|
import { revalidatePath } from 'next/cache';
|
||||||
|
|
||||||
|
import { confirmPatientAIResponses } from '@/app/home/(user)/_lib/server/ai-actions';
|
||||||
|
|
||||||
import { enhanceAction } from '@kit/next/actions';
|
import { enhanceAction } from '@kit/next/actions';
|
||||||
import { getLogger } from '@kit/shared/logger';
|
import { getLogger } from '@kit/shared/logger';
|
||||||
|
|
||||||
@@ -104,11 +106,19 @@ export const giveFeedbackAction = doctorAction(
|
|||||||
userId,
|
userId,
|
||||||
analysisOrderId,
|
analysisOrderId,
|
||||||
status,
|
status,
|
||||||
|
patientId,
|
||||||
|
timestamp,
|
||||||
|
recommendations,
|
||||||
|
isRecommendationsEdited,
|
||||||
}: {
|
}: {
|
||||||
feedbackValue: string;
|
feedbackValue: string;
|
||||||
userId: DoctorAnalysisFeedbackTable['user_id'];
|
userId: DoctorAnalysisFeedbackTable['user_id'];
|
||||||
analysisOrderId: DoctorAnalysisFeedbackTable['analysis_order_id'];
|
analysisOrderId: DoctorAnalysisFeedbackTable['analysis_order_id'];
|
||||||
status: DoctorAnalysisFeedbackTable['status'];
|
status: DoctorAnalysisFeedbackTable['status'];
|
||||||
|
patientId: string;
|
||||||
|
timestamp?: string;
|
||||||
|
recommendations: string[];
|
||||||
|
isRecommendationsEdited: boolean;
|
||||||
}) => {
|
}) => {
|
||||||
const logger = await getLogger();
|
const logger = await getLogger();
|
||||||
const isCompleted = status === 'COMPLETED';
|
const isCompleted = status === 'COMPLETED';
|
||||||
@@ -122,6 +132,19 @@ export const giveFeedbackAction = doctorAction(
|
|||||||
await submitFeedback(analysisOrderId, userId, feedbackValue, status);
|
await submitFeedback(analysisOrderId, userId, feedbackValue, status);
|
||||||
logger.info({ analysisOrderId }, `Successfully submitted feedback`);
|
logger.info({ analysisOrderId }, `Successfully submitted feedback`);
|
||||||
|
|
||||||
|
if (timestamp) {
|
||||||
|
logger.info(
|
||||||
|
{ timestamp, patientId },
|
||||||
|
'Attempting to update patient ai responses',
|
||||||
|
);
|
||||||
|
await confirmPatientAIResponses(
|
||||||
|
patientId,
|
||||||
|
timestamp,
|
||||||
|
recommendations,
|
||||||
|
isRecommendationsEdited,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
revalidateDoctorAnalysis();
|
revalidateDoctorAnalysis();
|
||||||
|
|
||||||
if (isCompleted) {
|
if (isCompleted) {
|
||||||
|
|||||||
@@ -41,7 +41,8 @@ export const PatientSchema = z.object({
|
|||||||
email: z.string().nullable(),
|
email: z.string().nullable(),
|
||||||
height: z.number().optional().nullable(),
|
height: z.number().optional().nullable(),
|
||||||
weight: z.number().optional().nullable(),
|
weight: z.number().optional().nullable(),
|
||||||
preferred_locale: z.string().nullable(),
|
preferred_locale: z.enum(['en', 'et', 'ru']).nullable(),
|
||||||
|
isSmoker: z.boolean().optional(),
|
||||||
});
|
});
|
||||||
export type Patient = z.infer<typeof PatientSchema>;
|
export type Patient = z.infer<typeof PatientSchema>;
|
||||||
|
|
||||||
|
|||||||
@@ -26,6 +26,10 @@ export const doctorAnalysisFeedbackSchema = z.object({
|
|||||||
userId: z.string().uuid(),
|
userId: z.string().uuid(),
|
||||||
analysisOrderId: z.number(),
|
analysisOrderId: z.number(),
|
||||||
status: FeedbackStatus,
|
status: FeedbackStatus,
|
||||||
|
patientId: z.string(),
|
||||||
|
timestamp: z.string().optional(),
|
||||||
|
recommendations: z.array(z.string()),
|
||||||
|
isRecommendationsEdited: z.boolean(),
|
||||||
});
|
});
|
||||||
|
|
||||||
export type DoctorAnalysisFeedback = z.infer<
|
export type DoctorAnalysisFeedback = z.infer<
|
||||||
|
|||||||
@@ -424,7 +424,7 @@ export async function getAnalysisResultsForDoctor(
|
|||||||
.from('accounts')
|
.from('accounts')
|
||||||
.select(
|
.select(
|
||||||
`primary_owner_user_id, id, name, last_name, personal_code, phone, email, preferred_locale,
|
`primary_owner_user_id, id, name, last_name, personal_code, phone, email, preferred_locale,
|
||||||
accountParams:account_params(height,weight)`,
|
accountParams:account_params(height,weight,is_smoker)`,
|
||||||
)
|
)
|
||||||
.eq('is_personal_account', true)
|
.eq('is_personal_account', true)
|
||||||
.eq('primary_owner_user_id', userId)
|
.eq('primary_owner_user_id', userId)
|
||||||
@@ -529,6 +529,7 @@ export async function getAnalysisResultsForDoctor(
|
|||||||
email,
|
email,
|
||||||
height: accountParams?.height,
|
height: accountParams?.height,
|
||||||
weight: accountParams?.weight,
|
weight: accountParams?.weight,
|
||||||
|
isSmoker: accountParams?.is_smoker || false,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,6 +17,6 @@ export default function medusaError(error: any): never {
|
|||||||
throw new Error('No response received: ' + error.request);
|
throw new Error('No response received: ' + error.request);
|
||||||
} else {
|
} else {
|
||||||
// Something happened in setting up the request that triggered an Error
|
// Something happened in setting up the request that triggered an Error
|
||||||
throw new Error('Error setting up the request: ' + error.message);
|
throw new Error('Error setting up the request: ' + { error });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -434,7 +434,9 @@ class UserAnalysesApi {
|
|||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
async getAllUserAnalysisResponses(): Promise<
|
async getAllUserAnalysisResponses(
|
||||||
|
userId?: string,
|
||||||
|
): Promise<
|
||||||
Database['medreport']['Functions']['get_latest_analysis_response_elements_for_current_user']['Returns']
|
Database['medreport']['Functions']['get_latest_analysis_response_elements_for_current_user']['Returns']
|
||||||
> {
|
> {
|
||||||
const {
|
const {
|
||||||
@@ -448,7 +450,7 @@ class UserAnalysesApi {
|
|||||||
const { data, error } = await this.client
|
const { data, error } = await this.client
|
||||||
.schema('medreport')
|
.schema('medreport')
|
||||||
.rpc('get_latest_analysis_response_elements_for_current_user', {
|
.rpc('get_latest_analysis_response_elements_for_current_user', {
|
||||||
p_user_id: user.id,
|
p_user_id: userId ?? user.id,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
|
|||||||
@@ -40,6 +40,7 @@ const PathsSchema = z.object({
|
|||||||
completedJobs: z.string().min(1),
|
completedJobs: z.string().min(1),
|
||||||
openJobs: z.string().min(1),
|
openJobs: z.string().min(1),
|
||||||
analysisDetails: z.string().min(1),
|
analysisDetails: z.string().min(1),
|
||||||
|
lifeStyle: z.string().min(1),
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -83,6 +84,7 @@ const pathsConfig = PathsSchema.parse({
|
|||||||
completedJobs: '/doctor/completed-jobs',
|
completedJobs: '/doctor/completed-jobs',
|
||||||
openJobs: '/doctor/open-jobs',
|
openJobs: '/doctor/open-jobs',
|
||||||
analysisDetails: 'doctor/analysis',
|
analysisDetails: 'doctor/analysis',
|
||||||
|
lifeStyle: '/home/life-style',
|
||||||
},
|
},
|
||||||
} satisfies z.infer<typeof PathsSchema>);
|
} satisfies z.infer<typeof PathsSchema>);
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import {
|
import {
|
||||||
|
Apple,
|
||||||
FileLineChart,
|
FileLineChart,
|
||||||
HeartPulse,
|
HeartPulse,
|
||||||
LineChart,
|
LineChart,
|
||||||
@@ -53,6 +54,12 @@ const routes = [
|
|||||||
Icon: <Stethoscope className={iconClasses} />,
|
Icon: <Stethoscope className={iconClasses} />,
|
||||||
end: true,
|
end: true,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
label: 'common:routes.lifeStyle',
|
||||||
|
path: pathsConfig.app.lifeStyle,
|
||||||
|
Icon: <Apple className={iconClasses} />,
|
||||||
|
end: true,
|
||||||
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
] satisfies z.infer<typeof NavigationConfigSchema>['routes'];
|
] satisfies z.infer<typeof NavigationConfigSchema>['routes'];
|
||||||
|
|||||||
@@ -623,6 +623,7 @@ export type Database = {
|
|||||||
created_at: string
|
created_at: string
|
||||||
id: string
|
id: string
|
||||||
input: Json
|
input: Json
|
||||||
|
is_visible_to_customer: boolean
|
||||||
latest_data_change: string
|
latest_data_change: string
|
||||||
prompt_id: string
|
prompt_id: string
|
||||||
prompt_name: string
|
prompt_name: string
|
||||||
@@ -633,6 +634,7 @@ export type Database = {
|
|||||||
created_at?: string
|
created_at?: string
|
||||||
id?: string
|
id?: string
|
||||||
input: Json
|
input: Json
|
||||||
|
is_visible_to_customer?: boolean
|
||||||
latest_data_change: string
|
latest_data_change: string
|
||||||
prompt_id: string
|
prompt_id: string
|
||||||
prompt_name: string
|
prompt_name: string
|
||||||
@@ -643,6 +645,7 @@ export type Database = {
|
|||||||
created_at?: string
|
created_at?: string
|
||||||
id?: string
|
id?: string
|
||||||
input?: Json
|
input?: Json
|
||||||
|
is_visible_to_customer?: boolean
|
||||||
latest_data_change?: string
|
latest_data_change?: string
|
||||||
prompt_id?: string
|
prompt_id?: string
|
||||||
prompt_name?: string
|
prompt_name?: string
|
||||||
|
|||||||
@@ -84,7 +84,8 @@
|
|||||||
"preferences": "Eelistused",
|
"preferences": "Eelistused",
|
||||||
"security": "Turvalisus",
|
"security": "Turvalisus",
|
||||||
"admin": "Admin",
|
"admin": "Admin",
|
||||||
"accounts": "Kontod"
|
"accounts": "Kontod",
|
||||||
|
"lifeStyle": "Elustiil"
|
||||||
},
|
},
|
||||||
"roles": {
|
"roles": {
|
||||||
"owner": {
|
"owner": {
|
||||||
@@ -141,6 +142,7 @@
|
|||||||
"doctor": "Arst",
|
"doctor": "Arst",
|
||||||
"save": "Salvesta",
|
"save": "Salvesta",
|
||||||
"saveAsDraft": "Salvesta mustandina",
|
"saveAsDraft": "Salvesta mustandina",
|
||||||
|
"generateText": "Genereeri tekst",
|
||||||
"confirm": "Kinnita",
|
"confirm": "Kinnita",
|
||||||
"previous": "Eelmine",
|
"previous": "Eelmine",
|
||||||
"next": "Järgmine",
|
"next": "Järgmine",
|
||||||
@@ -150,5 +152,8 @@
|
|||||||
"no": "Ei",
|
"no": "Ei",
|
||||||
"preferNotToAnswer": "Eelistan mitte vastata",
|
"preferNotToAnswer": "Eelistan mitte vastata",
|
||||||
"book": "Broneeri",
|
"book": "Broneeri",
|
||||||
"change": "Muuda"
|
"change": "Muuda",
|
||||||
|
"lifeStyle": {
|
||||||
|
"title": "Elustiili soovitused"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,6 +21,13 @@
|
|||||||
"benefits": {
|
"benefits": {
|
||||||
"title": "Sinu Medreport konto seis",
|
"title": "Sinu Medreport konto seis",
|
||||||
"validUntil": "Kehtiv kuni {{date}}"
|
"validUntil": "Kehtiv kuni {{date}}"
|
||||||
|
},
|
||||||
|
"orderPackage": {
|
||||||
|
"title": "Telli analüüside pakett",
|
||||||
|
"description": "Võrdle erinevate pakettide vahel ja vali endale sobiv"
|
||||||
|
},
|
||||||
|
"lifeStyle": {
|
||||||
|
"title": "Elustiili soovitused"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"recommendations": {
|
"recommendations": {
|
||||||
|
|||||||
@@ -47,5 +47,8 @@
|
|||||||
"updateFeedbackSuccess": "Kokkuvõte uuendatud",
|
"updateFeedbackSuccess": "Kokkuvõte uuendatud",
|
||||||
"updateFeedbackLoading": "Kokkuvõtet uuendatakse...",
|
"updateFeedbackLoading": "Kokkuvõtet uuendatakse...",
|
||||||
"updateFeedbackError": "Kokkuvõtte uuendamine ebaõnnestus",
|
"updateFeedbackError": "Kokkuvõtte uuendamine ebaõnnestus",
|
||||||
"feedbackLengthError": "Kokkuvõte peab olema vähemalt 10 tähemärki pikk"
|
"feedbackLengthError": "Kokkuvõte peab olema vähemalt 10 tähemärki pikk",
|
||||||
|
"loadParameters": "Valmistame patsiendi andmed ette. Palun oodake hetk...",
|
||||||
|
"loadFeedback": "Valmistame patsiendi tagasiside teksti. Palun oodake hetk...",
|
||||||
|
"recommendedAnalyses": "Soovita neid Medreporti üksik analüüse"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,42 @@
|
|||||||
|
drop function if exists medreport.get_latest_analysis_response_elements_for_current_user(uuid);
|
||||||
|
|
||||||
|
create or replace function medreport.get_latest_analysis_response_elements_for_current_user(p_user_id uuid)
|
||||||
|
returns table (
|
||||||
|
analysis_name medreport.analysis_response_elements.analysis_name%type,
|
||||||
|
response_time medreport.analysis_response_elements.response_time%type,
|
||||||
|
norm_upper medreport.analysis_response_elements.norm_upper%type,
|
||||||
|
norm_lower medreport.analysis_response_elements.norm_lower%type,
|
||||||
|
norm_status medreport.analysis_response_elements.norm_status%type,
|
||||||
|
response_value medreport.analysis_response_elements.response_value%type,
|
||||||
|
analysis_name_lab medreport.analysis_elements.analysis_name_lab%type
|
||||||
|
)
|
||||||
|
language sql
|
||||||
|
as $$
|
||||||
|
WITH ranked AS (
|
||||||
|
SELECT
|
||||||
|
are.analysis_name,
|
||||||
|
are.response_time,
|
||||||
|
are.norm_upper,
|
||||||
|
are.norm_lower,
|
||||||
|
are.norm_status,
|
||||||
|
are.response_value,
|
||||||
|
ae.analysis_name_lab,
|
||||||
|
ROW_NUMBER() OVER (
|
||||||
|
PARTITION BY are.analysis_name
|
||||||
|
ORDER BY are.response_time DESC, are.id DESC
|
||||||
|
) AS rn
|
||||||
|
FROM medreport.analysis_responses ar
|
||||||
|
JOIN medreport.analysis_response_elements are
|
||||||
|
ON are.analysis_response_id = ar.id
|
||||||
|
JOIN medreport.analysis_elements ae
|
||||||
|
ON are.analysis_element_original_id = ae.analysis_id_original
|
||||||
|
WHERE ar.user_id = auth.uid()
|
||||||
|
AND ar.order_status IN ('COMPLETED', 'ON_HOLD')
|
||||||
|
)
|
||||||
|
SELECT analysis_name, response_time, norm_upper, norm_lower, norm_status, response_value, analysis_name_lab
|
||||||
|
FROM ranked
|
||||||
|
WHERE rn = 1
|
||||||
|
ORDER BY analysis_name;
|
||||||
|
$$;
|
||||||
|
|
||||||
|
grant execute on function medreport.get_latest_analysis_response_elements_for_current_user(uuid) to authenticated, service_role;
|
||||||
66
supabase/migrations/20251024164100_improve_ai_responses.sql
Normal file
66
supabase/migrations/20251024164100_improve_ai_responses.sql
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
ALTER TABLE medreport.ai_responses
|
||||||
|
ADD COLUMN IF NOT EXISTS is_visible_to_customer boolean NOT NULL DEFAULT false;
|
||||||
|
|
||||||
|
drop policy if exists "ai_responses_select" on medreport.ai_responses;
|
||||||
|
drop policy if exists "ai_responses_insert" on medreport.ai_responses;
|
||||||
|
|
||||||
|
create policy "ai_responses_select"
|
||||||
|
on medreport.ai_responses
|
||||||
|
for select
|
||||||
|
to authenticated
|
||||||
|
using (account_id = auth.uid() OR medreport.is_doctor());
|
||||||
|
|
||||||
|
create policy "ai_responses_insert"
|
||||||
|
on medreport.ai_responses
|
||||||
|
for insert
|
||||||
|
to authenticated
|
||||||
|
with check (account_id = auth.uid() OR medreport.is_doctor());
|
||||||
|
|
||||||
|
drop function if exists medreport.get_latest_analysis_response_elements_for_current_user(uuid);
|
||||||
|
|
||||||
|
create or replace function medreport.get_latest_analysis_response_elements_for_current_user(p_user_id uuid)
|
||||||
|
returns table (
|
||||||
|
analysis_name medreport.analysis_response_elements.analysis_name%type,
|
||||||
|
response_time medreport.analysis_response_elements.response_time%type,
|
||||||
|
norm_upper medreport.analysis_response_elements.norm_upper%type,
|
||||||
|
norm_lower medreport.analysis_response_elements.norm_lower%type,
|
||||||
|
norm_status medreport.analysis_response_elements.norm_status%type,
|
||||||
|
response_value medreport.analysis_response_elements.response_value%type,
|
||||||
|
analysis_name_lab medreport.analysis_elements.analysis_name_lab%type
|
||||||
|
)
|
||||||
|
language sql
|
||||||
|
as $$
|
||||||
|
WITH ranked AS (
|
||||||
|
SELECT
|
||||||
|
are.analysis_name,
|
||||||
|
are.response_time,
|
||||||
|
are.norm_upper,
|
||||||
|
are.norm_lower,
|
||||||
|
are.norm_status,
|
||||||
|
are.response_value,
|
||||||
|
ae.analysis_name_lab,
|
||||||
|
ROW_NUMBER() OVER (
|
||||||
|
PARTITION BY are.analysis_name
|
||||||
|
ORDER BY are.response_time DESC, are.id DESC
|
||||||
|
) AS rn
|
||||||
|
FROM medreport.analysis_responses ar
|
||||||
|
JOIN medreport.analysis_response_elements are
|
||||||
|
ON are.analysis_response_id = ar.id
|
||||||
|
JOIN medreport.analysis_elements ae
|
||||||
|
ON are.analysis_element_original_id = ae.analysis_id_original
|
||||||
|
WHERE ar.user_id = p_user_id
|
||||||
|
AND ar.order_status IN ('COMPLETED', 'ON_HOLD')
|
||||||
|
)
|
||||||
|
SELECT analysis_name, response_time, norm_upper, norm_lower, norm_status, response_value, analysis_name_lab
|
||||||
|
FROM ranked
|
||||||
|
WHERE rn = 1
|
||||||
|
ORDER BY analysis_name;
|
||||||
|
$$;
|
||||||
|
|
||||||
|
grant execute on function medreport.get_latest_analysis_response_elements_for_current_user(uuid) to authenticated, service_role;
|
||||||
|
|
||||||
|
create policy "ai_responses_update_only_doctor"
|
||||||
|
on medreport.ai_responses
|
||||||
|
for update
|
||||||
|
to authenticated
|
||||||
|
using (medreport.is_doctor());
|
||||||
Reference in New Issue
Block a user