Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 51 additions & 5 deletions src/services/api/SupabaseApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3824,11 +3824,57 @@ export class SupabaseApi implements ServiceApi {
return (allSchools ?? []).map((school) => ({ school, role }));
}

// --- PROGRAM MANAGER / FIELD COORDINATOR ---
if (
role === RoleType.PROGRAM_MANAGER ||
role === RoleType.FIELD_COORDINATOR
) {
// --- FIELD COORDINATOR ---
if (role === RoleType.FIELD_COORDINATOR) {
const { data: schoolUsers, error: schoolUserErr } = await this.supabase
.from(TABLES.SchoolUser)
.select('role, school:school_id(*)')
.eq('user_id', userId)
.eq('role', RoleType.FIELD_COORDINATOR)
.eq('is_deleted', false);

if (schoolUserErr) {
logger.error(
'Error fetching field coordinator school_user rows:',
schoolUserErr,
);
return [];
}

const unique = new Map<
string,
{ school: TableTypes<'school'>; role: RoleType }
>();
for (const row of schoolUsers ?? []) {
const school = firstOrSelf(row.school);
if (
!school?.id ||
school.is_deleted ||
(search &&
!String(school.name ?? '')
.toLowerCase()
.includes(search.toLowerCase()))
) {
continue;
}
Comment on lines +3848 to +3859

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To improve efficiency, consider converting the search string to lowercase once outside the loop instead of calling search.toLowerCase() on every iteration.

        const searchLower = search?.toLowerCase();
        for (const row of schoolUsers ?? []) {
          const school = firstOrSelf(row.school);
          if (
            !school?.id ||
            school.is_deleted ||
            (searchLower &&
              !String(school.name ?? '')
                .toLowerCase()
                .includes(searchLower))
          ) {
            continue;
          }


unique.set(String(school.id), {
school: school as TableTypes<'school'>,
role,
});
}

return Array.from(unique.values())
.sort((a, b) =>
String(a.school.name ?? '').localeCompare(
String(b.school.name ?? ''),
),
)
.slice(from, to + 1);
}

// --- PROGRAM MANAGER ---
if (role === RoleType.PROGRAM_MANAGER) {
const { data: progUsers, error: puErr } = await this.supabase
.from(TABLES.ProgramUser)
.select('program_id')
Expand Down
Loading