2023-05-16 16:30:30 +01:00
|
|
|
import isEmpty from 'lodash/isEmpty';
|
2023-09-07 10:47:25 +00:00
|
|
|
import { get } from 'svelte/store';
|
|
|
|
|
|
2023-08-02 19:35:27 +01:00
|
|
|
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-10-30 04:52:00 +00:00
|
|
|
import type { CurrentOrg, OrgTeamMember, OrgAudience } from '$lib/utils/types/org';
|
2023-05-02 23:55:26 +00:00
|
|
|
|
2023-10-30 04:52:00 +00:00
|
|
|
export async function getOrgTeam(orgId: string) {
|
2023-05-02 23:55:26 +00:00
|
|
|
const { data, error } = await supabase
|
|
|
|
|
.from('organizationmember')
|
|
|
|
|
.select(
|
|
|
|
|
`
|
|
|
|
|
id,
|
|
|
|
|
email,
|
|
|
|
|
verified,
|
|
|
|
|
role_id,
|
|
|
|
|
profile(
|
|
|
|
|
id,
|
|
|
|
|
fullname,
|
|
|
|
|
email
|
|
|
|
|
)
|
|
|
|
|
`
|
|
|
|
|
)
|
|
|
|
|
.eq('organization_id', orgId)
|
2023-05-16 04:31:45 +00:00
|
|
|
.neq('role_id', ROLE.STUDENT)
|
2023-10-30 04:52:00 +00:00
|
|
|
.order('id', { ascending: false })
|
|
|
|
|
.returns<
|
|
|
|
|
{
|
|
|
|
|
id: number;
|
|
|
|
|
email: string;
|
|
|
|
|
verified: boolean;
|
|
|
|
|
role_id: number;
|
|
|
|
|
profile: {
|
|
|
|
|
id: string;
|
|
|
|
|
fullname: string;
|
|
|
|
|
email: string;
|
|
|
|
|
};
|
|
|
|
|
}[]
|
|
|
|
|
>();
|
2023-05-02 23:55:26 +00:00
|
|
|
|
2023-10-30 04:52:00 +00:00
|
|
|
const team: OrgTeamMember[] = [];
|
2023-05-02 23:55:26 +00:00
|
|
|
if (data?.length) {
|
|
|
|
|
data.forEach((teamMember) => {
|
|
|
|
|
team.push({
|
|
|
|
|
id: teamMember.id,
|
2023-09-07 10:47:25 +00:00
|
|
|
email: teamMember?.profile?.email || teamMember.email,
|
|
|
|
|
verified: teamMember.verified,
|
2023-05-02 23:55:26 +00:00
|
|
|
profileId: teamMember?.profile?.id,
|
2023-09-07 10:47:25 +00:00
|
|
|
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 {
|
2023-09-07 10:47:25 +00:00
|
|
|
team: get(orgTeam),
|
2023-08-14 06:22:03 +01:00
|
|
|
error
|
2023-05-02 23:55:26 +00:00
|
|
|
};
|
|
|
|
|
}
|
2023-01-03 09:26:08 +01:00
|
|
|
|
2023-10-30 04:52:00 +00:00
|
|
|
export async function getOrganizations(userId: string) {
|
2023-01-03 09:26:08 +01:00
|
|
|
const { data, error } = await supabase
|
|
|
|
|
.from('organizationmember')
|
|
|
|
|
.select(
|
|
|
|
|
`
|
2023-05-20 23:53:28 +00:00
|
|
|
id,
|
2023-01-03 09:26:08 +01:00
|
|
|
profile_id,
|
2023-09-01 15:28:47 +01:00
|
|
|
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 (
|
2023-01-03 09:26:08 +01:00
|
|
|
id,
|
|
|
|
|
name,
|
2023-05-02 23:55:26 +00:00
|
|
|
siteName,
|
2023-08-21 15:44:15 +00:00
|
|
|
avatar_url,
|
|
|
|
|
landingpage,
|
2023-09-11 11:47:30 +01:00
|
|
|
theme,
|
|
|
|
|
created_at
|
2023-01-03 09:26:08 +01:00
|
|
|
)
|
|
|
|
|
`
|
|
|
|
|
)
|
2023-09-11 11:47:30 +01:00
|
|
|
.eq('profile_id', userId)
|
2023-10-30 04:52:00 +00:00
|
|
|
.order('id', { ascending: false })
|
|
|
|
|
.returns<
|
|
|
|
|
{
|
|
|
|
|
id: string;
|
|
|
|
|
profile_id: string;
|
|
|
|
|
role_id: string;
|
|
|
|
|
created_at: string;
|
|
|
|
|
organization: CurrentOrg;
|
|
|
|
|
}[]
|
|
|
|
|
>();
|
2023-01-03 09:26:08 +01:00
|
|
|
|
2023-10-30 04:52:00 +00:00
|
|
|
const orgsArray: CurrentOrg[] = [];
|
2023-09-01 15:28:47 +01:00
|
|
|
|
2023-05-20 23:53:28 +00:00
|
|
|
if (Array.isArray(data) && data.length) {
|
2023-01-03 09:26:08 +01:00
|
|
|
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() || '',
|
2023-01-03 09:26:08 +01:00
|
|
|
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,
|
2023-08-21 15:44:15 +00:00
|
|
|
memberId: orgMember?.id,
|
2023-09-01 15:28:47 +01:00
|
|
|
role_id: orgMember?.role_id,
|
2023-08-21 15:44:15 +00:00
|
|
|
landingpage: orgMember?.organization?.landingpage
|
2023-01-03 09:26:08 +01:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
orgs.set(orgsArray);
|
2023-09-13 06:38:12 +01:00
|
|
|
|
2023-09-13 20:22:15 +01:00
|
|
|
if (localStorage) {
|
|
|
|
|
const lastOrgSiteName = localStorage.getItem('classroomio_org_sitename');
|
|
|
|
|
|
|
|
|
|
const lastOrg = orgsArray.find((org) => org.siteName === lastOrgSiteName);
|
|
|
|
|
|
|
|
|
|
if (lastOrg) {
|
|
|
|
|
currentOrg.set(lastOrg);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const _currentOrg = get(currentOrg);
|
|
|
|
|
if (!_currentOrg.siteName) {
|
2023-09-13 06:38:12 +01:00
|
|
|
currentOrg.set(orgsArray[0]);
|
|
|
|
|
}
|
2023-01-03 09:26:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
orgs: orgsArray,
|
2023-09-13 20:22:15 +01:00
|
|
|
currentOrg: get(currentOrg),
|
2023-08-14 06:22:03 +01:00
|
|
|
error
|
2023-01-03 09:26:08 +01:00
|
|
|
};
|
|
|
|
|
}
|
2023-04-23 22:28:18 +00:00
|
|
|
|
2023-10-30 04:52:00 +00:00
|
|
|
export async function getOrgAudience(orgId: string) {
|
2023-04-23 22:28:18 +00:00
|
|
|
// 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-09-15 08:50:04 +01:00
|
|
|
groupmember!inner(
|
2023-04-23 22:28:18 +00:00
|
|
|
role_id,
|
2023-09-15 08:50:04 +01:00
|
|
|
group_id:group!inner(
|
|
|
|
|
organization_id
|
|
|
|
|
)
|
2023-04-23 22:28:18 +00:00
|
|
|
)
|
|
|
|
|
`
|
|
|
|
|
)
|
2023-09-15 08:50:04 +01:00
|
|
|
.eq('groupmember.group.organization_id', orgId)
|
|
|
|
|
.eq('groupmember.role_id', ROLE.STUDENT);
|
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
|
|
|
};
|
|
|
|
|
}
|
2023-05-15 15:13:42 +00:00
|
|
|
|
2023-10-30 04:52:00 +00:00
|
|
|
export async function getCourseBySiteName(siteName: string) {
|
2023-05-15 15:13:42 +00:00
|
|
|
const { data, error } = await supabase
|
|
|
|
|
.from('course')
|
|
|
|
|
.select(
|
|
|
|
|
`
|
|
|
|
|
*,
|
2023-08-14 06:22:03 +01:00
|
|
|
lessons:lesson(count),
|
2023-05-15 15:13:42 +00:00
|
|
|
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')
|
2023-05-15 15:13:42 +00:00
|
|
|
.eq('is_published', true);
|
|
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
2023-05-15 15:36:23 +00:00
|
|
|
|
2023-05-15 15:13:42 +00:00
|
|
|
return data;
|
|
|
|
|
}
|
2023-05-16 04:31:45 +00:00
|
|
|
|
2023-10-30 04:52:00 +00:00
|
|
|
export async function getCurrentOrg(siteName: string, justGet = false) {
|
2023-05-16 04:31:45 +00:00
|
|
|
const { data, error } = await supabase
|
|
|
|
|
.from('organization')
|
2023-05-16 16:30:30 +01:00
|
|
|
.select(
|
|
|
|
|
`
|
|
|
|
|
id,
|
|
|
|
|
name,
|
|
|
|
|
siteName,
|
2023-08-21 15:44:15 +00:00
|
|
|
avatar_url,
|
|
|
|
|
landingpage,
|
|
|
|
|
theme
|
2023-05-16 16:30:30 +01:00
|
|
|
`
|
|
|
|
|
)
|
2023-10-30 04:52:00 +00:00
|
|
|
.eq('siteName', siteName)
|
|
|
|
|
.returns<CurrentOrg[]>();
|
2023-05-16 04:31:45 +00:00
|
|
|
|
2023-10-30 04:52:00 +00:00
|
|
|
const isDataEmpty = !data?.[0];
|
|
|
|
|
|
|
|
|
|
if (!justGet && (error || isDataEmpty)) {
|
2023-05-16 04:31:45 +00:00
|
|
|
console.error('Error getOrganization', error);
|
|
|
|
|
return goto('/404');
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-20 19:08:48 +00:00
|
|
|
if (!justGet) {
|
2023-10-30 04:52:00 +00:00
|
|
|
if (isDataEmpty) return;
|
|
|
|
|
|
2023-08-20 19:08:48 +00:00
|
|
|
currentOrg.set(data[0]);
|
2023-10-30 04:52:00 +00:00
|
|
|
} else if (!isDataEmpty) {
|
2023-08-20 19:08:48 +00:00
|
|
|
return data[0];
|
|
|
|
|
}
|
2023-05-16 04:31:45 +00:00
|
|
|
}
|