Created
July 10, 2024 19:28
-
-
Save MohammadHarisZia/c32602e787a868a5eeaba682ee2053de to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fetchUserExerciseProgress = async () => { | |
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) { | |
console.error('Error fetching user exercise levels:', userLevelsError); | |
return; | |
} | |
const userLevels = userLevelsData[0]; | |
// Fetch exercise limits | |
const { data: exerciseLimits, error: exerciseLimitsError } = await client | |
.from('exercise_limits') | |
.select('*'); | |
if (exerciseLimitsError) { | |
console.error('Error fetching exercise limits:', exerciseLimitsError); | |
return; | |
} | |
// Fetch training exercise scores count | |
const { count: exerciseScoresCount, error: exerciseScoresError } = | |
await client | |
.from('training_exercise_scores') | |
.select('*', { count: 'exact' }) | |
.eq('user_id', user.id); | |
if (exerciseScoresError) { | |
console.error( | |
'Error fetching exercise scores count:', | |
exerciseScoresError | |
); | |
return; | |
} | |
const exerciseTypes = [ | |
'rapid_speech', | |
'speech_in_noise', | |
'speech_reading', | |
'working_memory', | |
]; | |
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; | |
}; | |
const exerciseProgress = exerciseTypes.map((type) => { | |
const key = `${type}_level` as keyof UserLevels; | |
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) + 1 : null | |
); | |
const progress = | |
userLevel !== null && | |
currentLevelLimit && | |
exerciseScoresCount && | |
currentLevelLimit.num_exercises | |
? exerciseScoresCount / currentLevelLimit.num_exercises | |
: 0; | |
return { | |
type, | |
level: userLevel, | |
progress, | |
}; | |
}); | |
return exerciseProgress; | |
}; | |
fetchUserExerciseProgress().then((progress) => | |
console.log('User Level Progress:', progress) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment