Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MohammadHarisZia/a3ddb60aea8fe8d990d90fcd0b95bcbd to your computer and use it in GitHub Desktop.
Save MohammadHarisZia/a3ddb60aea8fe8d990d90fcd0b95bcbd to your computer and use it in GitHub Desktop.
Exercise Progress and Total Progress
/* eslint-disable max-lines-per-function */
import { getUser } from '@/core/auth/utils';
import { getSupabaseMobileClient } from '../common';
type ExerciseLimit = {
lesson_type: string;
level: number;
num_exercises: number;
};
type UserLevels = {
rapid_speech_level: number | null;
speech_in_noise_level: number | null;
speech_reading_level: number | null;
user_id: string;
working_memory_level: number | null;
};
type ExerciseProgress = {
type: string;
level: number | null;
progress: number;
};
export const fetchUserExerciseProgress = async () => {
try {
const client = getSupabaseMobileClient();
const user = await getUser();
// Fetch user exercise levels
const { data: userLevelsData, error: userLevelsError } = await client
.from('user_exercise_levels')
.select('*')
.eq('user_id', user.id);
if (userLevelsError || !userLevelsData || userLevelsData.length === 0) {
throw new Error('Error fetching user exercise levels');
}
const userLevels = userLevelsData[0] as UserLevels;
// Fetch exercise limits
const { data: exerciseLimitsData, error: exerciseLimitsError } =
await client.from('exercise_limits').select('*');
if (exerciseLimitsError || !exerciseLimitsData) {
throw new Error('Error fetching exercise limits');
}
const exerciseLimits = exerciseLimitsData as ExerciseLimit[];
const exerciseTypes = [
'rapid_speech',
'speech_in_noise',
'speech_reading',
'working_memory',
];
const exerciseProgressPromises = exerciseTypes.map(async (type) => {
const key = `${type}_level` as keyof UserLevels;
// Fetch training exercise scores count
const { count: exerciseScoresCount, error: exerciseScoresError } =
await client
.from('training_exercise_scores')
.select('*', { count: 'exact' })
.eq('user_id', user.id)
.eq('lesson_type', type);
if (exerciseScoresError) {
console.error(
'Error fetching exercise scores count:',
exerciseScoresError
);
return null;
}
const userLevel = userLevels[key];
const levelLimits = exerciseLimits.filter(
(limit) => limit.lesson_type.toLowerCase().replaceAll(' ', '_') === type
);
const currentLevelLimit = levelLimits.find((limit) =>
userLevel !== null ? limit.level === Number(userLevel) : null
);
const progressCal =
userLevel !== null &&
currentLevelLimit &&
exerciseScoresCount &&
currentLevelLimit.num_exercises
? exerciseScoresCount / currentLevelLimit.num_exercises
: 0;
return {
type,
level: userLevel,
progress: progressCal,
};
});
const exerciseProgress = (
await Promise.all(exerciseProgressPromises)
).filter((item): item is ExerciseProgress => item !== null);
// Fetch total exercise scores count
const { count: totalExerciseScoresCount, error: totalExerciseScoresError } =
await client
.from('training_exercise_scores')
.select('*', { count: 'exact' })
.eq('user_id', user.id);
if (totalExerciseScoresError) {
throw new Error('Error fetching total exercise scores count');
}
// Fetch total exercise limit
const { data: totalExerciseLimitData, error: totalExerciseLimitError } =
await client.from('exercise_limits_total').select('*');
if (
totalExerciseLimitError ||
!totalExerciseLimitData ||
totalExerciseLimitData.length === 0
) {
throw new Error('Error fetching total exercise limit');
}
const totalExercise = totalExerciseLimitData[0].num_exercises;
return {
exerciseProgress,
totalProgress: totalExerciseScoresCount
? totalExerciseScoresCount / totalExercise
: 0,
};
} catch (error) {
console.error('Error in fetchUserExerciseProgress:', error);
return null;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment