Files
medreport_mrb2b/lib/services/audit/doctorPageView.service.ts
Helena 8c6ce29c23 MED-147: add doctor actions logging (#59)
* MED-147: add doctor actions logging

* enum casing
2025-08-27 08:11:13 +03:00

47 lines
1.1 KiB
TypeScript

import { getSupabaseServerClient } from '@kit/supabase/server-client';
export enum DoctorPageViewAction {
VIEW_ANALYSIS_RESULTS = 'VIEW_ANALYSIS_RESULTS',
VIEW_DASHBOARD = 'VIEW_DASHBOARD',
VIEW_OPEN_JOBS = 'VIEW_OPEN_JOBS',
VIEW_OWN_JOBS = 'VIEW_OWN_JOBS',
VIEW_DONE_JOBS = 'VIEW_DONE_JOBS',
}
export const createDoctorPageViewLog = async ({
action,
recordKey,
dataOwnerUserId,
}: {
action: DoctorPageViewAction;
recordKey?: string;
dataOwnerUserId?: string;
}) => {
try {
const supabase = getSupabaseServerClient();
const {
data: { user },
error: userError,
} = await supabase.auth.getUser();
if (userError || !user) {
console.error('No authenticated user found; skipping audit insert');
return;
}
await supabase
.schema('audit')
.from('doctor_page_views')
.insert({
viewer_user_id: user.id,
data_owner_user_id: dataOwnerUserId,
viewed_record_key: recordKey,
action,
})
.throwOnError();
} catch (error) {
console.error('Failed to insert doctor page view log', error);
}
};