feat(MED-161): move analyses logic to separate package from kit/accounts

This commit is contained in:
2025-09-17 11:16:59 +03:00
parent e7650e3c32
commit 4ac261fd0d
11 changed files with 594 additions and 201 deletions

View File

@@ -1,11 +1,8 @@
import { SupabaseClient } from '@supabase/supabase-js';
import { Database } from '@kit/supabase/database';
import type { UuringElement, UuringuVastus } from '@kit/shared/types/medipost-analysis';
import PersonalCode from '~/lib/utils';
import type { AnalysisResultDetails, AnalysisResultDetailsMapped, UserAnalysis } from '../types/analysis-results';
import type { AnalysisOrder } from '../types/analysis-orders';
import { AccountWithParams } from '../types/accounts';
/**
@@ -207,89 +204,6 @@ class AccountsApi {
return response.data?.customer_id;
}
async getUserAnalysis(
analysisOrderId: number,
): Promise<AnalysisResultDetails | null> {
const authUser = await this.client.auth.getUser();
const { data, error: userError } = authUser;
if (userError) {
console.error('Failed to get user', userError);
throw userError;
}
const { user } = data;
const { data: analysisResponse } = await this.client
.schema('medreport')
.from('analysis_responses')
.select(
`*,
elements:analysis_response_elements(analysis_name,norm_status,response_value,unit,norm_lower_included,norm_upper_included,norm_lower,norm_upper,response_time),
order:analysis_order_id(medusa_order_id, status, created_at),
summary:analysis_order_id(doctor_analysis_feedback(*))`,
)
.eq('user_id', user.id)
.eq('analysis_order_id', analysisOrderId)
.throwOnError();
const responseWithElements = analysisResponse?.[0];
if (!responseWithElements) {
return null;
}
const feedback = responseWithElements.summary.doctor_analysis_feedback?.[0];
return {
...responseWithElements,
summary:
feedback?.status === 'COMPLETED'
? responseWithElements.summary.doctor_analysis_feedback?.[0]
: null,
};
}
async getUserAnalyses(): Promise<UserAnalysis | null> {
const authUser = await this.client.auth.getUser();
const { data, error: userError } = authUser;
if (userError) {
console.error('Failed to get user', userError);
throw userError;
}
const { user } = data;
const { data: analysisResponses } = await this.client
.schema('medreport')
.from('analysis_responses')
.select('*')
.eq('user_id', user.id);
if (!analysisResponses) {
return null;
}
const analysisResponseIds = analysisResponses.map((r) => r.id);
const { data: analysisResponseElements } = await this.client
.schema('medreport')
.from('analysis_response_elements')
.select('*')
.in('analysis_response_id', analysisResponseIds);
if (!analysisResponseElements) {
return null;
}
return analysisResponses.map((r) => ({
...r,
elements: analysisResponseElements.filter(
(e) => e.analysis_response_id === r.id,
),
}));
}
async hasAccountTeamMembership(accountId?: string) {
if (!accountId) {
return false;
@@ -307,23 +221,6 @@ class AccountsApi {
return (count ?? 0) > 0;
}
async fetchBmiThresholds() {
// Fetch BMI
const { data, error } = await this.client
.schema('medreport')
.from('bmi_thresholds')
.select(
'age_min,age_max,underweight_max,normal_min,normal_max,overweight_min,strong_min,obesity_min',
)
.order('age_min', { ascending: true });
if (error) {
console.error('Error fetching BMI thresholds:', error);
throw error;
}
return data;
}
}
export function createAccountsApi(client: SupabaseClient<Database>) {