Merge branch 'develop' of https://github.com/MR-medreport/MRB2B into MED-103
This commit is contained in:
@@ -1,23 +1,9 @@
|
||||
import { SupabaseClient } from '@supabase/supabase-js';
|
||||
|
||||
import { Database } from '@kit/supabase/database';
|
||||
|
||||
import { AnalysisResultDetails, UserAnalysis } from '../types/accounts';
|
||||
import PersonalCode from '~/lib/utils';
|
||||
|
||||
export type AccountWithParams =
|
||||
Database['medreport']['Tables']['accounts']['Row'] & {
|
||||
accountParams:
|
||||
| (Pick<
|
||||
Database['medreport']['Tables']['account_params']['Row'],
|
||||
'weight' | 'height'
|
||||
> & {
|
||||
isSmoker:
|
||||
| Database['medreport']['Tables']['account_params']['Row']['is_smoker']
|
||||
| null;
|
||||
})
|
||||
| null;
|
||||
};
|
||||
import { AccountWithParams } from '../types/accounts';
|
||||
|
||||
/**
|
||||
* Class representing an API for interacting with user accounts.
|
||||
@@ -25,7 +11,7 @@ export type AccountWithParams =
|
||||
* @param {SupabaseClient<Database>} client - The Supabase client instance.
|
||||
*/
|
||||
class AccountsApi {
|
||||
constructor(private readonly client: SupabaseClient<Database>) {}
|
||||
constructor(private readonly client: SupabaseClient<Database>) { }
|
||||
|
||||
/**
|
||||
* @name getAccount
|
||||
@@ -218,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;
|
||||
@@ -318,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>) {
|
||||
|
||||
@@ -1,15 +1,5 @@
|
||||
import * as z from 'zod';
|
||||
|
||||
import { Database } from '@kit/supabase/database';
|
||||
|
||||
export type UserAnalysisElement =
|
||||
Database['medreport']['Tables']['analysis_response_elements']['Row'];
|
||||
export type UserAnalysisResponse =
|
||||
Database['medreport']['Tables']['analysis_responses']['Row'] & {
|
||||
elements: UserAnalysisElement[];
|
||||
};
|
||||
export type UserAnalysis = UserAnalysisResponse[];
|
||||
|
||||
export type ApplicationRole =
|
||||
Database['medreport']['Tables']['accounts']['Row']['application_role'];
|
||||
export enum ApplicationRoleEnum {
|
||||
@@ -18,50 +8,16 @@ export enum ApplicationRoleEnum {
|
||||
SuperAdmin = 'super_admin',
|
||||
}
|
||||
|
||||
export const ElementSchema = z.object({
|
||||
unit: z.string(),
|
||||
norm_lower: z.number(),
|
||||
norm_upper: z.number(),
|
||||
norm_status: z.number(),
|
||||
analysis_name: z.string(),
|
||||
response_time: z.string(),
|
||||
response_value: z.number(),
|
||||
norm_lower_included: z.boolean(),
|
||||
norm_upper_included: z.boolean(),
|
||||
});
|
||||
export type Element = z.infer<typeof ElementSchema>;
|
||||
|
||||
export const OrderSchema = z.object({
|
||||
status: z.string(),
|
||||
medusa_order_id: z.string(),
|
||||
created_at: z.coerce.date(),
|
||||
});
|
||||
export type Order = z.infer<typeof OrderSchema>;
|
||||
|
||||
export const SummarySchema = z.object({
|
||||
id: z.number(),
|
||||
value: z.string(),
|
||||
status: z.string(),
|
||||
user_id: z.string(),
|
||||
created_at: z.coerce.date(),
|
||||
created_by: z.string(),
|
||||
updated_at: z.coerce.date().nullable(),
|
||||
updated_by: z.string(),
|
||||
doctor_user_id: z.string().nullable(),
|
||||
analysis_order_id: z.number(),
|
||||
});
|
||||
export type Summary = z.infer<typeof SummarySchema>;
|
||||
|
||||
export const AnalysisResultDetailsSchema = z.object({
|
||||
id: z.number(),
|
||||
analysis_order_id: z.number(),
|
||||
order_number: z.string(),
|
||||
order_status: z.string(),
|
||||
user_id: z.string(),
|
||||
created_at: z.coerce.date(),
|
||||
updated_at: z.coerce.date().nullable(),
|
||||
elements: z.array(ElementSchema),
|
||||
order: OrderSchema,
|
||||
summary: SummarySchema.nullable(),
|
||||
});
|
||||
export type AnalysisResultDetails = z.infer<typeof AnalysisResultDetailsSchema>;
|
||||
export type AccountWithParams =
|
||||
Database['medreport']['Tables']['accounts']['Row'] & {
|
||||
accountParams:
|
||||
| (Pick<
|
||||
Database['medreport']['Tables']['account_params']['Row'],
|
||||
'weight' | 'height'
|
||||
> & {
|
||||
isSmoker:
|
||||
| Database['medreport']['Tables']['account_params']['Row']['is_smoker']
|
||||
| null;
|
||||
})
|
||||
| null;
|
||||
};
|
||||
|
||||
3
packages/features/accounts/src/types/analysis-orders.ts
Normal file
3
packages/features/accounts/src/types/analysis-orders.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import { Tables } from '@kit/supabase/database';
|
||||
|
||||
export type AnalysisOrder = Tables<{ schema: 'medreport' }, 'analysis_orders'>;
|
||||
139
packages/features/accounts/src/types/analysis-results.ts
Normal file
139
packages/features/accounts/src/types/analysis-results.ts
Normal file
@@ -0,0 +1,139 @@
|
||||
import * as z from 'zod';
|
||||
|
||||
import { Database } from '@kit/supabase/database';
|
||||
|
||||
export type UserAnalysisElement =
|
||||
Database['medreport']['Tables']['analysis_response_elements']['Row'];
|
||||
export type UserAnalysisResponse =
|
||||
Database['medreport']['Tables']['analysis_responses']['Row'] & {
|
||||
elements: UserAnalysisElement[];
|
||||
};
|
||||
export type UserAnalysis = UserAnalysisResponse[];
|
||||
|
||||
const ElementSchema = z.object({
|
||||
unit: z.string(),
|
||||
norm_lower: z.number(),
|
||||
norm_upper: z.number(),
|
||||
norm_status: z.number(),
|
||||
analysis_name: z.string(),
|
||||
response_time: z.string(),
|
||||
response_value: z.number(),
|
||||
response_value_is_negative: z.boolean(),
|
||||
norm_lower_included: z.boolean(),
|
||||
norm_upper_included: z.boolean(),
|
||||
status: z.string(),
|
||||
analysis_element_original_id: z.string(),
|
||||
original_response_element: z.object({
|
||||
|
||||
}),
|
||||
});
|
||||
|
||||
const OrderSchema = z.object({
|
||||
status: z.string(),
|
||||
medusa_order_id: z.string(),
|
||||
created_at: z.coerce.date(),
|
||||
});
|
||||
|
||||
const DoctorAnalysisFeedbackSchema = z.object({
|
||||
id: z.number(),
|
||||
status: z.string(),
|
||||
user_id: z.string(),
|
||||
created_at: z.coerce.date(),
|
||||
created_by: z.string(),
|
||||
});
|
||||
|
||||
const SummarySchema = z.object({
|
||||
id: z.number(),
|
||||
value: z.string(),
|
||||
status: z.string(),
|
||||
user_id: z.string(),
|
||||
created_at: z.coerce.date(),
|
||||
created_by: z.string(),
|
||||
updated_at: z.coerce.date().nullable(),
|
||||
updated_by: z.string(),
|
||||
doctor_user_id: z.string().nullable(),
|
||||
analysis_order_id: z.number(),
|
||||
doctor_analysis_feedback: z.array(DoctorAnalysisFeedbackSchema),
|
||||
});
|
||||
|
||||
export const AnalysisResultDetailsSchema = z.object({
|
||||
id: z.number(),
|
||||
analysis_order_id: z.number(),
|
||||
order_number: z.string(),
|
||||
order_status: z.string(),
|
||||
user_id: z.string(),
|
||||
created_at: z.coerce.date(),
|
||||
updated_at: z.coerce.date().nullable(),
|
||||
elements: z.array(ElementSchema),
|
||||
order: OrderSchema,
|
||||
summary: SummarySchema.nullable(),
|
||||
});
|
||||
export type AnalysisResultDetails = z.infer<typeof AnalysisResultDetailsSchema>;
|
||||
|
||||
export type AnalysisResultDetailsElementResults = {
|
||||
unit: string | null;
|
||||
normLower: number | null;
|
||||
normUpper: number | null;
|
||||
normStatus: number | null;
|
||||
responseTime: string | null;
|
||||
responseValue: number | null;
|
||||
responseValueIsNegative: boolean | null;
|
||||
responseValueIsWithinNorm: boolean | null;
|
||||
normLowerIncluded: boolean;
|
||||
normUpperIncluded: boolean;
|
||||
status: string;
|
||||
analysisElementOriginalId: string;
|
||||
nestedElements: {
|
||||
analysisElementOriginalId: string;
|
||||
normLower?: number | null;
|
||||
normLowerIncluded: boolean;
|
||||
normStatus: number;
|
||||
normUpper?: number | null;
|
||||
normUpperIncluded: boolean;
|
||||
responseTime: string;
|
||||
responseValue: number;
|
||||
status: number;
|
||||
unit: string;
|
||||
}[];
|
||||
labComment?: string | null;
|
||||
};
|
||||
|
||||
export type AnalysisResultDetailsElement = {
|
||||
analysisIdOriginal: string;
|
||||
isWaitingForResults: boolean;
|
||||
analysisName: string;
|
||||
results: AnalysisResultDetailsElementResults;
|
||||
};
|
||||
|
||||
export type AnalysisResultDetailsMapped = {
|
||||
id: number;
|
||||
order: {
|
||||
status: string;
|
||||
medusaOrderId: string;
|
||||
createdAt: Date | string;
|
||||
};
|
||||
elements: {
|
||||
id: string;
|
||||
unit: string;
|
||||
norm_lower: number;
|
||||
norm_upper: number;
|
||||
norm_status: number;
|
||||
analysis_name: string;
|
||||
response_time: string;
|
||||
response_value: number;
|
||||
norm_lower_included: boolean;
|
||||
norm_upper_included: boolean;
|
||||
status: string;
|
||||
analysis_element_original_id: string;
|
||||
}[];
|
||||
orderedAnalysisElementIds: number[];
|
||||
orderedAnalysisElements: AnalysisResultDetailsElement[];
|
||||
summary: {
|
||||
id: number;
|
||||
status: string;
|
||||
user_id: string;
|
||||
created_at: Date;
|
||||
created_by: string;
|
||||
value?: string;
|
||||
} | null;
|
||||
};
|
||||
3
packages/features/user-analyses/eslint.config.mjs
Normal file
3
packages/features/user-analyses/eslint.config.mjs
Normal file
@@ -0,0 +1,3 @@
|
||||
import eslintConfigBase from '@kit/eslint-config/base.js';
|
||||
|
||||
export default eslintConfigBase;
|
||||
33
packages/features/user-analyses/package.json
Normal file
33
packages/features/user-analyses/package.json
Normal file
@@ -0,0 +1,33 @@
|
||||
{
|
||||
"name": "@kit/user-analyses",
|
||||
"private": true,
|
||||
"version": "0.1.0",
|
||||
"scripts": {
|
||||
"clean": "git clean -xdf .turbo node_modules",
|
||||
"format": "prettier --check \"**/*.{ts,tsx}\"",
|
||||
"lint": "eslint .",
|
||||
"typecheck": "tsc --noEmit"
|
||||
},
|
||||
"exports": {
|
||||
"./api": "./src/server/api.ts",
|
||||
"./types/*": "./src/types/*.ts"
|
||||
},
|
||||
"dependencies": {
|
||||
"nanoid": "^5.1.5"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@kit/eslint-config": "workspace:*",
|
||||
"@kit/prettier-config": "workspace:*",
|
||||
"@kit/shared": "workspace:*",
|
||||
"@kit/supabase": "workspace:*",
|
||||
"@kit/tsconfig": "workspace:*"
|
||||
},
|
||||
"prettier": "@kit/prettier-config",
|
||||
"typesVersions": {
|
||||
"*": {
|
||||
"*": [
|
||||
"src/*"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
312
packages/features/user-analyses/src/server/api.ts
Normal file
312
packages/features/user-analyses/src/server/api.ts
Normal file
@@ -0,0 +1,312 @@
|
||||
import { SupabaseClient } from '@supabase/supabase-js';
|
||||
|
||||
import { Database } from '@kit/supabase/database';
|
||||
import type { UuringElement, UuringuVastus } from '@kit/shared/types/medipost-analysis';
|
||||
|
||||
import type { AnalysisResultDetails, AnalysisResultDetailsMapped, UserAnalysis } from '../types/analysis-results';
|
||||
import type { AnalysisOrder } from '../types/analysis-orders';
|
||||
|
||||
/**
|
||||
* Class representing an API for interacting with user accounts.
|
||||
* @constructor
|
||||
* @param {SupabaseClient<Database>} client - The Supabase client instance.
|
||||
*/
|
||||
class UserAnalysesApi {
|
||||
constructor(private readonly client: SupabaseClient<Database>) { }
|
||||
|
||||
async getAnalysisOrder({
|
||||
medusaOrderId,
|
||||
analysisOrderId,
|
||||
}: {
|
||||
medusaOrderId?: string;
|
||||
analysisOrderId?: number;
|
||||
}) {
|
||||
const query = this.client
|
||||
.schema('medreport')
|
||||
.from('analysis_orders')
|
||||
.select('*')
|
||||
if (medusaOrderId) {
|
||||
query.eq('medusa_order_id', medusaOrderId);
|
||||
} else if (analysisOrderId) {
|
||||
query.eq('id', analysisOrderId);
|
||||
} else {
|
||||
throw new Error('Either medusaOrderId or orderId must be provided');
|
||||
}
|
||||
|
||||
const { data: order, error } = await query.single();
|
||||
if (error) {
|
||||
throw new Error(`Failed to get order by medusaOrderId=${medusaOrderId} or analysisOrderId=${analysisOrderId}, message=${error.message}, data=${JSON.stringify(order)}`);
|
||||
}
|
||||
return order as AnalysisOrder;
|
||||
}
|
||||
|
||||
async getUserAnalysis(
|
||||
analysisOrderId: number,
|
||||
): Promise<AnalysisResultDetailsMapped | 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 analysisOrder = await this.getAnalysisOrder({ analysisOrderId });
|
||||
const orderedAnalysisElementIds = analysisOrder.analysis_element_ids ?? [];
|
||||
if (orderedAnalysisElementIds.length === 0) {
|
||||
console.error('No ordered analysis element ids found for analysis order id=', analysisOrderId);
|
||||
return null;
|
||||
}
|
||||
const { data: orderedAnalysisElements, error: orderedAnalysisElementsError } = await this.client
|
||||
.schema('medreport')
|
||||
.from('analysis_elements')
|
||||
.select('analysis_id_original,analysis_name_lab')
|
||||
.in('id', orderedAnalysisElementIds);
|
||||
if (orderedAnalysisElementsError) {
|
||||
console.error('Failed to get ordered analysis elements for analysis order id=', analysisOrderId, orderedAnalysisElementsError);
|
||||
throw orderedAnalysisElementsError;
|
||||
}
|
||||
|
||||
const orderedAnalysisElementOriginalIds = orderedAnalysisElements.map(({ analysis_id_original }) => analysis_id_original);
|
||||
if (orderedAnalysisElementOriginalIds.length === 0) {
|
||||
console.error('No ordered analysis element original ids found for analysis order id=', analysisOrderId);
|
||||
return null;
|
||||
}
|
||||
|
||||
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,status,analysis_element_original_id,original_response_element,response_value_is_negative,response_value_is_within_norm),
|
||||
summary:analysis_order_id(doctor_analysis_feedback(*))`,
|
||||
)
|
||||
.eq('user_id', user.id)
|
||||
.eq('analysis_order_id', analysisOrderId)
|
||||
.throwOnError();
|
||||
|
||||
const responseWithElements = analysisResponse?.[0] as AnalysisResultDetails | null;
|
||||
if (!responseWithElements) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const analysisResponseElements = responseWithElements.elements;
|
||||
|
||||
const feedback = responseWithElements.summary?.doctor_analysis_feedback?.[0];
|
||||
|
||||
const mappedOrderedAnalysisElements = orderedAnalysisElements.map(({ analysis_id_original, analysis_name_lab }) => {
|
||||
return this.getOrderedAnalysisElements({
|
||||
analysisIdOriginal: analysis_id_original,
|
||||
analysisNameLab: analysis_name_lab,
|
||||
analysisResponseElements,
|
||||
});
|
||||
}).sort((a, b) => a.analysisName.localeCompare(b.analysisName));
|
||||
const nestedAnalysisElementIds = mappedOrderedAnalysisElements.map(({ results }) => results?.nestedElements.map(({ analysisElementOriginalId }) => analysisElementOriginalId)).flat().filter(Boolean);
|
||||
if (nestedAnalysisElementIds.length > 0) {
|
||||
const { data: nestedAnalysisElements, error: nestedAnalysisElementsError } = await this.client
|
||||
.schema('medreport')
|
||||
.from('analysis_elements')
|
||||
.select('*')
|
||||
.in('id', nestedAnalysisElementIds);
|
||||
if (!nestedAnalysisElementsError && nestedAnalysisElements) {
|
||||
for (const mappedOrderedAnalysisElement of mappedOrderedAnalysisElements) {
|
||||
const { results } = mappedOrderedAnalysisElement;
|
||||
if (!results) {
|
||||
continue;
|
||||
}
|
||||
for (const nestedElement of results.nestedElements) {
|
||||
const { analysisElementOriginalId } = nestedElement;
|
||||
const nestedAnalysisElement = nestedAnalysisElements.find(({ id }) => id === analysisElementOriginalId);
|
||||
if (!nestedAnalysisElement) {
|
||||
continue;
|
||||
}
|
||||
results.nestedElements.push({
|
||||
...nestedAnalysisElement,
|
||||
analysisElementOriginalId,
|
||||
analysisName: nestedAnalysisElement.analysis_name_lab,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
mappedOrderedAnalysisElements.forEach(({ results }) => {
|
||||
results?.nestedElements.forEach(({ analysisElementOriginalId }) => {
|
||||
const nestedAnalysisElement = nestedAnalysisElements.find(({ id }) => id === analysisElementOriginalId);
|
||||
if (nestedAnalysisElement) {
|
||||
results?.nestedElements.push({
|
||||
...nestedAnalysisElement,
|
||||
analysisElementOriginalId,
|
||||
analysisName: nestedAnalysisElement.analysis_name_lab,
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
id: analysisOrderId,
|
||||
order: {
|
||||
status: analysisOrder.status,
|
||||
medusaOrderId: analysisOrder.medusa_order_id,
|
||||
createdAt: new Date(analysisOrder.created_at),
|
||||
},
|
||||
orderedAnalysisElementIds,
|
||||
orderedAnalysisElements: mappedOrderedAnalysisElements,
|
||||
summary:
|
||||
feedback?.status === 'COMPLETED'
|
||||
? (responseWithElements.summary?.doctor_analysis_feedback?.[0] ?? null)
|
||||
: null,
|
||||
};
|
||||
}
|
||||
|
||||
getOrderedAnalysisElements({
|
||||
analysisIdOriginal,
|
||||
analysisNameLab,
|
||||
analysisResponseElements,
|
||||
}: {
|
||||
analysisIdOriginal: string;
|
||||
analysisNameLab: string;
|
||||
analysisResponseElements: AnalysisResultDetails['elements'];
|
||||
}) {
|
||||
const elementResponse = analysisResponseElements.find((element) => element.analysis_element_original_id === analysisIdOriginal);
|
||||
if (!elementResponse) {
|
||||
return {
|
||||
analysisIdOriginal,
|
||||
isWaitingForResults: true,
|
||||
analysisName: analysisNameLab,
|
||||
};
|
||||
}
|
||||
const labComment = elementResponse.original_response_element?.UuringuKommentaar;
|
||||
return {
|
||||
analysisIdOriginal,
|
||||
isWaitingForResults: false,
|
||||
analysisName: analysisNameLab,
|
||||
results: {
|
||||
nestedElements: (() => {
|
||||
const nestedElements = elementResponse.original_response_element?.UuringuElement as UuringElement[] | undefined;
|
||||
if (!nestedElements) {
|
||||
return [];
|
||||
}
|
||||
return nestedElements.map((element) => {
|
||||
const elementVastus = element.UuringuVastus as UuringuVastus | undefined;
|
||||
const responseValue = elementVastus?.VastuseVaartus;
|
||||
const responseValueIsNumeric = !isNaN(Number(responseValue));
|
||||
const responseValueIsNegative = responseValue === 'Negatiivne';
|
||||
const responseValueIsWithinNorm = responseValue === 'Normi piires';
|
||||
return {
|
||||
status: element.UuringOlek,
|
||||
unit: element.Mootyhik,
|
||||
normLower: elementVastus?.NormAlum?.['#text'],
|
||||
normUpper: elementVastus?.NormYlem?.['#text'],
|
||||
normStatus: elementVastus?.NormiStaatus,
|
||||
responseTime: elementVastus?.VastuseAeg,
|
||||
response_value: responseValueIsNegative || !responseValueIsNumeric ? null : (responseValue ?? null),
|
||||
response_value_is_negative: responseValueIsNumeric ? null : responseValueIsNegative,
|
||||
response_value_is_within_norm: responseValueIsNumeric ? null : responseValueIsWithinNorm,
|
||||
normLowerIncluded: elementVastus?.NormAlum?.['@_kaasaarvatud'] === 'JAH',
|
||||
normUpperIncluded: elementVastus?.NormYlem?.['@_kaasaarvatud'] === 'JAH',
|
||||
analysisElementOriginalId: element.UuringId,
|
||||
};
|
||||
});
|
||||
})(),
|
||||
labComment,
|
||||
//originalResponseElement: elementResponse.original_response_element ?? null,
|
||||
unit: elementResponse.unit,
|
||||
normLower: elementResponse.norm_lower,
|
||||
normUpper: elementResponse.norm_upper,
|
||||
normStatus: elementResponse.norm_status,
|
||||
responseTime: elementResponse.response_time,
|
||||
responseValue: elementResponse.response_value,
|
||||
responseValueIsNegative: elementResponse.response_value_is_negative === true,
|
||||
responseValueIsWithinNorm: elementResponse.response_value_is_within_norm === true,
|
||||
normLowerIncluded: elementResponse.norm_lower_included,
|
||||
normUpperIncluded: elementResponse.norm_upper_included,
|
||||
status: elementResponse.status,
|
||||
analysisElementOriginalId: elementResponse.analysis_element_original_id,
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// @TODO unused currently
|
||||
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;
|
||||
}
|
||||
|
||||
const { count, error } = await this.client
|
||||
.schema('medreport')
|
||||
.from('accounts_memberships')
|
||||
.select('account_id', { count: 'exact', head: true })
|
||||
.eq('account_id', accountId);
|
||||
|
||||
if (error) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
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 createUserAnalysesApi(client: SupabaseClient<Database>) {
|
||||
return new UserAnalysesApi(client);
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
import { Tables } from '@kit/supabase/database';
|
||||
|
||||
export type AnalysisOrder = Tables<{ schema: 'medreport' }, 'analysis_orders'>;
|
||||
140
packages/features/user-analyses/src/types/analysis-results.ts
Normal file
140
packages/features/user-analyses/src/types/analysis-results.ts
Normal file
@@ -0,0 +1,140 @@
|
||||
import * as z from 'zod';
|
||||
|
||||
import { Database } from '@kit/supabase/database';
|
||||
|
||||
export type UserAnalysisElement =
|
||||
Database['medreport']['Tables']['analysis_response_elements']['Row'];
|
||||
export type UserAnalysisResponse =
|
||||
Database['medreport']['Tables']['analysis_responses']['Row'] & {
|
||||
elements: UserAnalysisElement[];
|
||||
};
|
||||
export type UserAnalysis = UserAnalysisResponse[];
|
||||
|
||||
const ElementSchema = z.object({
|
||||
unit: z.string(),
|
||||
norm_lower: z.number(),
|
||||
norm_upper: z.number(),
|
||||
norm_status: z.number(),
|
||||
analysis_name: z.string(),
|
||||
response_time: z.string(),
|
||||
response_value: z.number(),
|
||||
response_value_is_negative: z.boolean(),
|
||||
response_value_is_within_norm: z.boolean(),
|
||||
norm_lower_included: z.boolean(),
|
||||
norm_upper_included: z.boolean(),
|
||||
status: z.string(),
|
||||
analysis_element_original_id: z.string(),
|
||||
original_response_element: z.object({
|
||||
|
||||
}),
|
||||
});
|
||||
|
||||
const OrderSchema = z.object({
|
||||
status: z.string(),
|
||||
medusa_order_id: z.string(),
|
||||
created_at: z.coerce.date(),
|
||||
});
|
||||
|
||||
const DoctorAnalysisFeedbackSchema = z.object({
|
||||
id: z.number(),
|
||||
status: z.string(),
|
||||
user_id: z.string(),
|
||||
created_at: z.coerce.date(),
|
||||
created_by: z.string(),
|
||||
});
|
||||
|
||||
const SummarySchema = z.object({
|
||||
id: z.number(),
|
||||
value: z.string(),
|
||||
status: z.string(),
|
||||
user_id: z.string(),
|
||||
created_at: z.coerce.date(),
|
||||
created_by: z.string(),
|
||||
updated_at: z.coerce.date().nullable(),
|
||||
updated_by: z.string(),
|
||||
doctor_user_id: z.string().nullable(),
|
||||
analysis_order_id: z.number(),
|
||||
doctor_analysis_feedback: z.array(DoctorAnalysisFeedbackSchema),
|
||||
});
|
||||
|
||||
export const AnalysisResultDetailsSchema = z.object({
|
||||
id: z.number(),
|
||||
analysis_order_id: z.number(),
|
||||
order_number: z.string(),
|
||||
order_status: z.string(),
|
||||
user_id: z.string(),
|
||||
created_at: z.coerce.date(),
|
||||
updated_at: z.coerce.date().nullable(),
|
||||
elements: z.array(ElementSchema),
|
||||
order: OrderSchema,
|
||||
summary: SummarySchema.nullable(),
|
||||
});
|
||||
export type AnalysisResultDetails = z.infer<typeof AnalysisResultDetailsSchema>;
|
||||
|
||||
export type AnalysisResultDetailsElementResults = {
|
||||
unit: string | null;
|
||||
normLower: number | null;
|
||||
normUpper: number | null;
|
||||
normStatus: number | null;
|
||||
responseTime: string | null;
|
||||
responseValue: number | null;
|
||||
responseValueIsNegative: boolean | null;
|
||||
responseValueIsWithinNorm: boolean | null;
|
||||
normLowerIncluded: boolean;
|
||||
normUpperIncluded: boolean;
|
||||
status: string;
|
||||
analysisElementOriginalId: string;
|
||||
nestedElements: {
|
||||
analysisElementOriginalId: string;
|
||||
normLower?: number | null;
|
||||
normLowerIncluded: boolean;
|
||||
normStatus: number;
|
||||
normUpper?: number | null;
|
||||
normUpperIncluded: boolean;
|
||||
responseTime: string;
|
||||
responseValue: number;
|
||||
status: number;
|
||||
unit: string;
|
||||
}[];
|
||||
labComment?: string | null;
|
||||
};
|
||||
|
||||
export type AnalysisResultDetailsElement = {
|
||||
analysisIdOriginal: string;
|
||||
isWaitingForResults: boolean;
|
||||
analysisName: string;
|
||||
results: AnalysisResultDetailsElementResults;
|
||||
};
|
||||
|
||||
export type AnalysisResultDetailsMapped = {
|
||||
id: number;
|
||||
order: {
|
||||
status: string;
|
||||
medusaOrderId: string;
|
||||
createdAt: Date | string;
|
||||
};
|
||||
elements: {
|
||||
id: string;
|
||||
unit: string;
|
||||
norm_lower: number;
|
||||
norm_upper: number;
|
||||
norm_status: number;
|
||||
analysis_name: string;
|
||||
response_time: string;
|
||||
response_value: number;
|
||||
norm_lower_included: boolean;
|
||||
norm_upper_included: boolean;
|
||||
status: string;
|
||||
analysis_element_original_id: string;
|
||||
}[];
|
||||
orderedAnalysisElementIds: number[];
|
||||
orderedAnalysisElements: AnalysisResultDetailsElement[];
|
||||
summary: {
|
||||
id: number;
|
||||
status: string;
|
||||
user_id: string;
|
||||
created_at: Date;
|
||||
created_by: string;
|
||||
value?: string;
|
||||
} | null;
|
||||
};
|
||||
11
packages/features/user-analyses/tsconfig.json
Normal file
11
packages/features/user-analyses/tsconfig.json
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"extends": "@kit/tsconfig/base.json",
|
||||
"compilerOptions": {
|
||||
"tsBuildInfoFile": "node_modules/.cache/tsbuildinfo.json",
|
||||
"paths": {
|
||||
"~/lib/utils": ["../../../lib/utils.ts"]
|
||||
}
|
||||
},
|
||||
"include": ["*.ts", "*.tsx", "src"],
|
||||
"exclude": ["node_modules"]
|
||||
}
|
||||
Reference in New Issue
Block a user