@@ -422,6 +422,7 @@ ORDER BY
422422 verifiedProfile,
423423 profileComplete,
424424 countries,
425+ preferredRoles,
425426 sortBy = "matchIndex" ,
426427 sortOrder = "desc" ,
427428 page = 1 ,
@@ -593,6 +594,14 @@ member_address AS (
593594 ]
594595 : [ ] ;
595596
597+ const normalizedPreferredRoles = Array . isArray ( preferredRoles )
598+ ? [
599+ ...new Set (
600+ preferredRoles . map ( ( value ) => String ( value ) . trim ( ) ) . filter ( Boolean ) ,
601+ ) ,
602+ ]
603+ : [ ] ;
604+
596605 if ( normalizedCountries . length > 0 ) {
597606 const pCountries = p ( normalizedCountries ) ;
598607 where . push (
@@ -604,6 +613,30 @@ member_address AS (
604613 ) ;
605614 }
606615
616+ if ( normalizedPreferredRoles . length > 0 ) {
617+ const pPreferredRoles = p (
618+ normalizedPreferredRoles . map ( ( value ) => value . toUpperCase ( ) ) ,
619+ ) ;
620+ where . push (
621+ `EXISTS (
622+ SELECT 1
623+ FROM members."memberTraits" mtpr
624+ INNER JOIN members."memberTraitPersonalization" mtpp
625+ ON mtpp."memberTraitId" = mtpr.id
626+ WHERE mtpr."userId" = m."userId"
627+ AND mtpp.key = 'openToWork'
628+ AND mtpp.value IS NOT NULL
629+ AND mtpp.value::jsonb ? 'preferredRoles'
630+ AND jsonb_typeof(mtpp.value::jsonb -> 'preferredRoles') = 'array'
631+ AND EXISTS (
632+ SELECT 1
633+ FROM jsonb_array_elements_text(mtpp.value::jsonb -> 'preferredRoles') pr
634+ WHERE UPPER(pr) = ANY(${ pPreferredRoles } ::text[])
635+ )
636+ )` ,
637+ ) ;
638+ }
639+
607640 const whereClause = where . join ( " AND " ) ;
608641 const skillJoin =
609642 deduped . length > 0
@@ -684,10 +717,20 @@ member_address AS (
684717 // ---------------------------------------------------------------- queries
685718 const ctesBlock = ctes . join ( ",\n" ) ;
686719 const direction = sortOrder === "asc" ? "ASC" : "DESC" ;
687- const orderByClause =
688- sortBy === "handle"
689- ? `m.handle ${ direction } , "matchIndex" DESC NULLS LAST`
690- : `"matchIndex" ${ direction } NULLS LAST, m.handle ASC` ;
720+ const fallbackPreferredRoleOrder =
721+ deduped . length === 0 && normalizedPreferredRoles . length > 0 ;
722+ let orderByClause = "" ;
723+
724+ if ( sortBy === "handle" ) {
725+ orderByClause = `m.handle ${ direction } , "matchIndex" DESC NULLS LAST` ;
726+ } else if ( fallbackPreferredRoleOrder ) {
727+ orderByClause =
728+ `EXISTS (SELECT 1 FROM recently_active ra WHERE ra.user_id = m."userId") DESC, ` +
729+ `COALESCE(m."availableForGigs", false) DESC, m.handle ASC` ;
730+ } else {
731+ orderByClause = `"matchIndex" ${ direction } NULLS LAST, m.handle ASC` ;
732+ }
733+
691734 const profileCompleteJoin =
692735 profileComplete === true
693736 ? `INNER JOIN profile_complete_filtered pcf ON pcf.user_id = m."userId"`
@@ -712,7 +755,7 @@ SELECT
712755FROM members.member m
713756INNER JOIN filtered_members fm ON fm.user_id = m."userId"
714757${ profileCompleteJoin }
715- LEFT JOIN user_match_data umd ON umd.user_id = m."userId"
758+ ${ deduped . length > 0 ? ' LEFT JOIN user_match_data umd ON umd.user_id = m."userId"' : "" }
716759LEFT JOIN verified_via_trolley vt ON vt.user_id = m."userId"
717760LEFT JOIN member_address maddr ON maddr."userId" = m."userId"
718761ORDER BY ${ orderByClause }
0 commit comments