* MED-88: add doctor email notifications * add logging, send open jobs notification on partial analysis response * update permissions * fix import, permissions * casing, let email be null * unused import
84 lines
2.4 KiB
TypeScript
84 lines
2.4 KiB
TypeScript
import type { Tables } from '@/packages/supabase/src/database.types';
|
|
|
|
import { getSupabaseServerAdminClient } from '@kit/supabase/server-admin-client';
|
|
import { getSupabaseServerClient } from '@kit/supabase/server-client';
|
|
|
|
type Account = Tables<{ schema: 'medreport' }, 'accounts'>;
|
|
type Membership = Tables<{ schema: 'medreport' }, 'accounts_memberships'>;
|
|
|
|
export type AccountWithMemberships = Account & { memberships: Membership[] };
|
|
|
|
export async function getAccount(id: string): Promise<AccountWithMemberships> {
|
|
const { data } = await getSupabaseServerClient()
|
|
.schema('medreport')
|
|
.from('accounts')
|
|
.select('*, memberships: accounts_memberships (*)')
|
|
.eq('id', id)
|
|
.single()
|
|
.throwOnError();
|
|
|
|
return data as unknown as AccountWithMemberships;
|
|
}
|
|
|
|
export async function getAccountAdmin({
|
|
primaryOwnerUserId,
|
|
}: {
|
|
primaryOwnerUserId: string;
|
|
}): Promise<AccountWithMemberships> {
|
|
const query = getSupabaseServerAdminClient()
|
|
.schema('medreport')
|
|
.from('accounts')
|
|
.select('*, memberships: accounts_memberships (*)')
|
|
.eq('is_personal_account', true);
|
|
|
|
if (primaryOwnerUserId) {
|
|
query.eq('primary_owner_user_id', primaryOwnerUserId);
|
|
} else {
|
|
throw new Error('primaryOwnerUserId is required');
|
|
}
|
|
|
|
const { data } = await query.single().throwOnError();
|
|
|
|
return data as unknown as AccountWithMemberships;
|
|
}
|
|
|
|
export async function getDoctorAccounts() {
|
|
const { data } = await getSupabaseServerAdminClient()
|
|
.schema('medreport')
|
|
.from('accounts')
|
|
.select('id, email, name, last_name, preferred_locale')
|
|
.eq('is_personal_account', true)
|
|
.eq('application_role', 'doctor')
|
|
.throwOnError();
|
|
|
|
return data?.map(({ id, email, name, last_name, preferred_locale }) => ({
|
|
id,
|
|
email,
|
|
name,
|
|
lastName: last_name,
|
|
preferredLocale: preferred_locale,
|
|
}));
|
|
}
|
|
|
|
export async function getAssignedDoctorAccount(analysisOrderId: number) {
|
|
const { data: doctorUser } = await getSupabaseServerAdminClient()
|
|
.schema('medreport')
|
|
.from('doctor_analysis_feedback')
|
|
.select('doctor_user_id')
|
|
.eq('analysis_order_id', analysisOrderId)
|
|
.throwOnError();
|
|
|
|
const doctorData = doctorUser[0];
|
|
if (!doctorData || !doctorData.doctor_user_id) {
|
|
return null;
|
|
}
|
|
|
|
const { data } = await getSupabaseServerAdminClient()
|
|
.schema('medreport')
|
|
.from('accounts')
|
|
.select('email')
|
|
.eq('primary_owner_user_id', doctorData.doctor_user_id);
|
|
|
|
return { email: data?.[0]?.email };
|
|
}
|