2023-08-02 19:35:27 +01:00
|
|
|
import { supabase } from '$lib/utils/functions/supabase';
|
2024-05-22 05:49:10 +01:00
|
|
|
import { get } from 'svelte/store';
|
|
|
|
|
import { isOrgAdmin } from '$lib/utils/store/org';
|
2021-08-21 13:26:43 +03:00
|
|
|
|
2023-08-07 06:18:30 +00:00
|
|
|
export async function fetchCourses(profileId, orgId) {
|
|
|
|
|
if (!orgId || !profileId) return;
|
2021-08-21 13:26:43 +03:00
|
|
|
|
2024-05-22 05:49:10 +01:00
|
|
|
const match = {};
|
|
|
|
|
// Filter by profile_id if role isn't admin within organization
|
|
|
|
|
if (!get(isOrgAdmin)) {
|
|
|
|
|
match.profile_id = profileId;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-07 06:18:30 +00:00
|
|
|
// Gets courses for a particular organisation where the current logged in user is a groupmember
|
2021-08-21 13:26:43 +03:00
|
|
|
const { data: allCourses } = await supabase
|
2023-08-07 06:18:30 +00:00
|
|
|
.rpc('get_courses', {
|
2023-09-28 11:45:50 +01:00
|
|
|
org_id_arg: orgId,
|
|
|
|
|
profile_id_arg: profileId
|
2023-08-07 06:18:30 +00:00
|
|
|
})
|
2024-05-22 05:49:10 +01:00
|
|
|
.match(match);
|
2021-08-21 13:26:43 +03:00
|
|
|
|
2021-08-24 18:20:07 +03:00
|
|
|
console.log(`allCourses`, allCourses);
|
2021-10-18 19:12:13 +03:00
|
|
|
if (!Array.isArray(allCourses)) {
|
|
|
|
|
return {
|
2023-08-07 06:18:30 +00:00
|
|
|
allCourses: []
|
2021-10-18 19:12:13 +03:00
|
|
|
};
|
|
|
|
|
}
|
2021-08-24 18:20:07 +03:00
|
|
|
|
|
|
|
|
return { allCourses };
|
2021-08-21 13:26:43 +03:00
|
|
|
}
|