import { graphqlClient } from "@/graphql/client"; import { DASHBOARD_CONFIG, DASHBOARD_COURSE_SESSION_PROGRESS, DASHBOARD_COURSE_STATISTICS, DASHBOARD_MENTOR_COMPETENCE_SUMMARY, TRAINING_RESPONSIBLE_STATISTICS, } from "@/graphql/queries"; import { itGetCached, itPost } from "@/fetchHelpers"; import type { BaseStatisticsType, CourseProgressType, CourseStatisticsType, DashboardConfigType, TrainingResponsibleStatisticsQuery, } from "@/gql/graphql"; import type { DashboardPersonsPageMode, DueDate, XlsExportRequestData, XlsExportResponseData, } from "@/types"; export type DashboardPersonRoleType = | "SUPERVISOR" | "EXPERT" | "MEMBER" | "LEARNING_MENTOR" | "BERUFSBILDNER" | "PARTICIPANT"; export type DashboardRoleKeyType = | "Supervisor" | "Trainer" | "Member" | "MentorUK" | "MentorVV" | "Berufsbildner" | "Ausbildungsverantwortlicher"; export type WidgetType = | "ProgressWidget" | "CompetenceWidget" | "MentorTasksWidget" | "MentorPersonWidget" | "MentorCompetenceWidget" | "CompetenceCertificateWidget" | "UKStatisticsWidget" | "UKBerufsbildnerStatisticsWidget" | "TrainingResponsibleStatisticsWidget"; export type DashboardPersonCourseSessionType = { id: string; session_title: string; course_id: string; course_title: string; course_slug: string; region: string; generation: string; user_role: DashboardPersonRoleType; user_role_display: string; my_role: DashboardPersonRoleType; my_role_display: string; is_uk: boolean; is_vv: boolean; competence_metrics?: { passed_count: number; failed_count: number; }; }; export type DashboardPersonType = { user_id: string; first_name: string; last_name: string; email: string; course_sessions: DashboardPersonCourseSessionType[]; avatar_url: string; avatar_url_small: string; competence_metrics?: { passed_count: number; failed_count: number; }; chosen_profile: string; }; export type DashboardCourseConfigType = { course_id: string; course_slug: string; course_title: string; role_key: DashboardRoleKeyType; is_uk: boolean; is_vv: boolean; is_mentor: boolean; widgets: WidgetType[]; has_preview: boolean; session_to_continue_id: string; }; export type DashboardDueDate = DueDate & { course_session: DashboardPersonCourseSessionType; translatedType: string; persons?: DashboardPersonType[]; }; export const fetchStatisticData = async ( courseId: string ): Promise => { try { console.log("fetching statistics for course ID: ", courseId); const res = await graphqlClient.query(DASHBOARD_COURSE_STATISTICS, { courseId }); if (res.error) { console.error("Error fetching statistics for course ID:", courseId, res.error); } // @ts-ignore return res.data?.course_statistics || null; } catch (error) { console.error(`Error fetching statistics for course ID: ${courseId}`, error); return null; } }; export const fetchTrainingResponsibleStatistics = async ( courseSessionId: string ): Promise => { try { const res = await graphqlClient.query(TRAINING_RESPONSIBLE_STATISTICS, { courseSessionId, }); if (res.error) { console.error( "Error fetching training responsible statistics for course session ID:", courseSessionId, res.error ); } return res.data || null; } catch (error) { console.error( `Error fetching training responsible statistics for course session ID: ${courseSessionId}`, error ); return null; } }; export const fetchProgressData = async ( courseId: string ): Promise => { try { const res = await graphqlClient.query(DASHBOARD_COURSE_SESSION_PROGRESS, { courseId, }); if (res.error) { console.error("Error fetching progress for course ID:", courseId, res.error); } return res.data?.course_progress || null; } catch (error) { console.error(`Error fetching progress for course ID: ${courseId}`, error); return null; } }; export const fetchDashboardConfig = async (): Promise => { try { const res = await graphqlClient.query(DASHBOARD_CONFIG, {}); if (res.error) { console.error("Error fetching dashboard config:", res.error); } return (res.data?.dashboard_config as unknown as DashboardConfigType[]) || null; } catch (error) { console.error("Error fetching dashboard config:", error); return null; } }; export const fetchMentorCompetenceSummary = async ( courseId: string, roleKey: string ): Promise => { let agentRole = ""; if ( ["MentorUK".toLowerCase(), "MentorVV".toLowerCase()].includes(roleKey.toLowerCase()) ) { agentRole = "LEARNING_MENTOR"; } else if (roleKey.toLowerCase() === "Berufsbildner".toLowerCase()) { agentRole = "BERUFSBILDNER"; } if (!agentRole) { console.error(`Invalid role key for competence summary: ${roleKey}`); return null; } try { const res = await graphqlClient.query(DASHBOARD_MENTOR_COMPETENCE_SUMMARY, { courseId, agentRole, }); if (res.error) { console.error("Error fetching data for course ID:", courseId, res.error); } return res.data?.mentor_course_statistics || null; } catch (error) { console.error(`Error fetching data for course ID: ${courseId}`, error); return null; } }; export async function fetchDashboardPersons(mode: DashboardPersonsPageMode) { let url = "/api/dashboard/persons/"; if (mode === "competenceMetrics") { url += "?with_competence_metrics=true"; } return await itGetCached(url); } export async function fetchDashboardDueDates() { return await itGetCached("/api/dashboard/duedates/"); } export async function fetchDashboardConfigv2() { return await itGetCached("/api/dashboard/config/"); } export async function fetchMenteeCount(courseId: string) { return await itGetCached<{ mentee_count: number }>( `/api/dashboard/course/${courseId}/mentees/` ); } export async function fetchOpenTasksCount(courseId: string) { return await itGetCached<{ open_task_count: number }>( `/api/dashboard/course/${courseId}/open_tasks/` ); } export async function exportFeedback( data: XlsExportRequestData, language: string ): Promise { return await itPost("/api/dashboard/export/feedback/", data, { headers: { "Accept-Language": language }, }); } export async function exportAttendance( data: XlsExportRequestData, language: string ): Promise { return await itPost("/api/dashboard/export/attendance/", data, { headers: { "Accept-Language": language }, }); } export async function exportCompetenceElements( data: XlsExportRequestData, language: string ): Promise { return await itPost("/api/dashboard/export/competence_elements/", data, { headers: { "Accept-Language": language }, }); } export async function exportPersons( data: XlsExportRequestData, language: string ): Promise { return await itPost("/api/dashboard/export/persons/", data, { headers: { "Accept-Language": language }, }); } export function courseIdForCourseSlug( dashboardConfigs: DashboardCourseConfigType[], courseSlug: string ) { const config = dashboardConfigs.find((config) => config.course_slug === courseSlug); return config?.course_id; }