@@ -54,6 +54,7 @@ export class MemberSearchService {
5454 verifiedProfile,
5555 profileComplete,
5656 countries,
57+ preferredRoles,
5758 sortBy = "matchIndex" ,
5859 sortOrder = "desc" ,
5960 page = 1 ,
@@ -225,6 +226,14 @@ member_address AS (
225226 ]
226227 : [ ] ;
227228
229+ const normalizedPreferredRoles = Array . isArray ( preferredRoles )
230+ ? [
231+ ...new Set (
232+ preferredRoles . map ( ( value ) => String ( value ) . trim ( ) ) . filter ( Boolean ) ,
233+ ) ,
234+ ]
235+ : [ ] ;
236+
228237 if ( normalizedCountries . length > 0 ) {
229238 const pCountries = p ( normalizedCountries ) ;
230239 where . push (
@@ -236,6 +245,30 @@ member_address AS (
236245 ) ;
237246 }
238247
248+ if ( normalizedPreferredRoles . length > 0 ) {
249+ const pPreferredRoles = p (
250+ normalizedPreferredRoles . map ( ( value ) => value . toUpperCase ( ) ) ,
251+ ) ;
252+ where . push (
253+ `EXISTS (
254+ SELECT 1
255+ FROM members."memberTraits" mtpr
256+ INNER JOIN members."memberTraitPersonalization" mtpp
257+ ON mtpp."memberTraitId" = mtpr.id
258+ WHERE mtpr."userId" = m."userId"
259+ AND mtpp.key = 'openToWork'
260+ AND mtpp.value IS NOT NULL
261+ AND mtpp.value::jsonb ? 'preferredRoles'
262+ AND jsonb_typeof(mtpp.value::jsonb -> 'preferredRoles') = 'array'
263+ AND EXISTS (
264+ SELECT 1
265+ FROM jsonb_array_elements_text(mtpp.value::jsonb -> 'preferredRoles') pr
266+ WHERE UPPER(pr) = ANY(${ pPreferredRoles } ::text[])
267+ )
268+ )` ,
269+ ) ;
270+ }
271+
239272 const whereClause = where . join ( " AND " ) ;
240273 const skillJoin =
241274 deduped . length > 0
@@ -316,10 +349,20 @@ member_address AS (
316349 // ---------------------------------------------------------------- queries
317350 const ctesBlock = ctes . join ( ",\n" ) ;
318351 const direction = sortOrder === "asc" ? "ASC" : "DESC" ;
319- const orderByClause =
320- sortBy === "handle"
321- ? `m.handle ${ direction } , "matchIndex" DESC NULLS LAST`
322- : `"matchIndex" ${ direction } NULLS LAST, m.handle ASC` ;
352+ const fallbackPreferredRoleOrder =
353+ deduped . length === 0 && normalizedPreferredRoles . length > 0 ;
354+ let orderByClause = "" ;
355+
356+ if ( sortBy === "handle" ) {
357+ orderByClause = `m.handle ${ direction } , "matchIndex" DESC NULLS LAST` ;
358+ } else if ( fallbackPreferredRoleOrder ) {
359+ orderByClause =
360+ `EXISTS (SELECT 1 FROM recently_active ra WHERE ra.user_id = m."userId") DESC, ` +
361+ `COALESCE(m."availableForGigs", false) DESC, m.handle ASC` ;
362+ } else {
363+ orderByClause = `"matchIndex" ${ direction } NULLS LAST, m.handle ASC` ;
364+ }
365+
323366 const profileCompleteJoin =
324367 profileComplete === true
325368 ? `INNER JOIN profile_complete_filtered pcf ON pcf.user_id = m."userId"`
@@ -344,7 +387,7 @@ SELECT
344387FROM members.member m
345388INNER JOIN filtered_members fm ON fm.user_id = m."userId"
346389${ profileCompleteJoin }
347- LEFT JOIN user_match_data umd ON umd.user_id = m."userId"
390+ ${ deduped . length > 0 ? ' LEFT JOIN user_match_data umd ON umd.user_id = m."userId"' : "" }
348391LEFT JOIN verified_via_trolley vt ON vt.user_id = m."userId"
349392LEFT JOIN member_address maddr ON maddr."userId" = m."userId"
350393ORDER BY ${ orderByClause }
0 commit comments