MED-105: create analysis results page
This commit is contained in:
@@ -2,6 +2,8 @@ import { SupabaseClient } from '@supabase/supabase-js';
|
||||
|
||||
import { Database } from '@kit/supabase/database';
|
||||
|
||||
import { UserAnalysis } from '../types/accounts';
|
||||
|
||||
/**
|
||||
* Class representing an API for interacting with user accounts.
|
||||
* @constructor
|
||||
@@ -117,6 +119,7 @@ class AccountsApi {
|
||||
*/
|
||||
async getSubscription(accountId: string) {
|
||||
const response = await this.client
|
||||
.schema('medreport')
|
||||
.from('subscriptions')
|
||||
.select('*, items: subscription_items !inner (*)')
|
||||
.eq('account_id', accountId)
|
||||
@@ -168,6 +171,51 @@ class AccountsApi {
|
||||
|
||||
return response.data?.customer_id;
|
||||
}
|
||||
|
||||
async getUserAnalysis(): 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;
|
||||
}
|
||||
|
||||
const elementMap = new Map(
|
||||
analysisResponseElements.map((e) => [e.analysis_response_id, e]),
|
||||
);
|
||||
|
||||
return analysisResponses
|
||||
.filter((r) => elementMap.has(r.id))
|
||||
.map((r) => ({
|
||||
...r,
|
||||
element: elementMap.get(r.id)!,
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
export function createAccountsApi(client: SupabaseClient<Database>) {
|
||||
|
||||
6
packages/features/accounts/src/types/accounts.ts
Normal file
6
packages/features/accounts/src/types/accounts.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { Database } from '@kit/supabase/database';
|
||||
|
||||
export type UserAnalysis =
|
||||
(Database['medreport']['Tables']['analysis_responses']['Row'] & {
|
||||
element: Database['medreport']['Tables']['analysis_response_elements']['Row'];
|
||||
})[];
|
||||
Reference in New Issue
Block a user