Files
classroomio/src/lib/utils/services/org/index.js
T

189 lines
4.1 KiB
JavaScript
Raw Normal View History

2023-05-16 16:30:30 +01:00
import isEmpty from 'lodash/isEmpty';
import { get } from 'svelte/store';
import { goto } from '$app/navigation';
import { supabase } from '$lib/utils/functions/supabase';
import { orgs, currentOrg, orgAudience, orgTeam } from '$lib/utils/store/org';
import { ROLE, ROLE_LABEL } from '$lib/utils/constants/roles';
2023-05-02 23:55:26 +00:00
export async function getOrgTeam(orgId) {
const { data, error } = await supabase
.from('organizationmember')
.select(
`
id,
email,
verified,
role_id,
profile(
id,
fullname,
email
)
`
)
.eq('organization_id', orgId)
.neq('role_id', ROLE.STUDENT)
2023-05-02 23:55:26 +00:00
.order('id', { ascending: false });
const team = [];
if (data?.length) {
data.forEach((teamMember) => {
team.push({
id: teamMember.id,
email: teamMember?.profile?.email || teamMember.email,
verified: teamMember.verified,
2023-05-02 23:55:26 +00:00
profileId: teamMember?.profile?.id,
fullname: teamMember?.profile?.fullname || '',
2023-05-02 23:55:26 +00:00
role: ROLE_LABEL[teamMember?.role_id] || '',
2023-08-14 06:22:03 +01:00
isAdmin: teamMember?.role_id === ROLE.ADMIN
2023-05-02 23:55:26 +00:00
});
});
orgTeam.set(team);
}
return {
team: get(orgTeam),
2023-08-14 06:22:03 +01:00
error
2023-05-02 23:55:26 +00:00
};
}
export async function getOrganizations(userId) {
const { data, error } = await supabase
.from('organizationmember')
.select(
`
2023-05-20 23:53:28 +00:00
id,
profile_id,
role_id,
2023-09-11 11:47:30 +01:00
created_at,
2023-05-20 23:53:28 +00:00
organization!organizationmember_organization_id_fkey (
id,
name,
2023-05-02 23:55:26 +00:00
siteName,
avatar_url,
landingpage,
2023-09-11 11:47:30 +01:00
theme,
created_at
)
`
)
2023-09-11 11:47:30 +01:00
.eq('profile_id', userId)
.order('id', { ascending: false });
const orgsArray = [];
2023-05-20 23:53:28 +00:00
if (Array.isArray(data) && data.length) {
data.forEach((orgMember) => {
orgsArray.push({
id: orgMember?.organization?.id,
name: orgMember?.organization?.name,
2023-08-14 06:22:03 +01:00
shortName: orgMember?.organization?.name?.substring(0, 2)?.toUpperCase() || '',
siteName: orgMember?.organization?.siteName,
2023-08-20 19:08:48 +00:00
theme: orgMember?.organization?.theme,
2023-05-02 23:55:26 +00:00
avatar_url: orgMember?.organization?.avatar_url,
memberId: orgMember?.id,
role_id: orgMember?.role_id,
landingpage: orgMember?.organization?.landingpage
});
});
orgs.set(orgsArray);
currentOrg.set(orgsArray[0]);
}
return {
orgs: orgsArray,
currentOrg: orgsArray[0],
2023-08-14 06:22:03 +01:00
error
};
}
2023-04-23 22:28:18 +00:00
export async function getOrgAudience(orgId) {
// get all students who are participants in any course belonging to an org
const { data, error } = await supabase
.from('profile')
.select(
`
id,
fullname,
email,
avatar_url,
created_at,
2023-05-20 23:53:28 +00:00
organizationmember!inner(
2023-04-23 22:28:18 +00:00
role_id,
2023-05-20 23:53:28 +00:00
organization_id
2023-04-23 22:28:18 +00:00
)
`
)
2023-05-20 23:53:28 +00:00
.eq('organizationmember.organization_id', orgId)
.eq('organizationmember.role_id', 3); // is a student, tutor is 2 and admin is 1
2023-04-23 22:28:18 +00:00
console.log('data', data);
const audience = (data || []).map((profile) => ({
id: profile.id,
name: profile.fullname,
email: profile.email,
avatar_url: profile.avatar_url,
2023-08-14 06:22:03 +01:00
date_joined: new Date(profile.created_at).toDateString()
2023-04-23 22:28:18 +00:00
}));
orgAudience.set(audience);
return {
audience: audience,
2023-08-14 06:22:03 +01:00
error
2023-04-23 22:28:18 +00:00
};
}
export async function getCourseBySiteName(siteName) {
const { data, error } = await supabase
.from('course')
.select(
`
*,
2023-08-14 06:22:03 +01:00
lessons:lesson(count),
group!inner(
organization!inner(id, name, siteName, avatar_url)
)
`
)
.eq('group.organization.siteName', siteName)
2023-09-10 07:24:06 +01:00
.eq('status', 'ACTIVE')
.eq('is_published', true);
if (error) {
return [];
}
2023-05-15 15:36:23 +00:00
return data;
}
2023-08-20 19:08:48 +00:00
export async function getCurrentOrg(siteName, justGet = false) {
const { data, error } = await supabase
.from('organization')
2023-05-16 16:30:30 +01:00
.select(
`
id,
name,
siteName,
avatar_url,
landingpage,
theme
2023-05-16 16:30:30 +01:00
`
)
.eq('siteName', siteName);
2023-08-20 19:08:48 +00:00
if (!justGet && (error || isEmpty(data))) {
console.error('Error getOrganization', error);
return goto('/404');
}
2023-08-20 19:08:48 +00:00
if (!justGet) {
currentOrg.set(data[0]);
} else if (!isEmpty(data)) {
2023-08-20 19:08:48 +00:00
return data[0];
}
}