@@ -27,8 +27,6 @@ interface IWorkRow {
2727 segmentId : string | null
2828}
2929
30- // ─── Query: regular work experiences (bulk) ──────────────────────────────────
31-
3230export async function findWorkExperiencesBulk (
3331 qx : QueryExecutor ,
3432 memberIds : string [ ] ,
@@ -75,8 +73,6 @@ export async function findWorkExperiencesBulk(
7573 )
7674}
7775
78- // ─── Query: manual affiliations (bulk) ───────────────────────────────────────
79-
8076export async function findManualAffiliationsBulk (
8177 qx : QueryExecutor ,
8278 memberIds : string [ ] ,
@@ -105,17 +101,23 @@ export async function findManualAffiliationsBulk(
105101
106102// ─── Selection priority ───────────────────────────────────────────────────────
107103
104+ function durationMs ( org : IWorkRow ) : number {
105+ const start = new Date ( org . dateStart ?? '' ) . getTime ( )
106+ const end = new Date ( org . dateEnd ?? '9999-12-31' ) . getTime ( )
107+ return end - start
108+ }
109+
108110function longestDateRange ( orgs : IWorkRow [ ] ) : IWorkRow {
109111 const withDates = orgs . filter ( ( r ) => r . dateStart )
110- if ( withDates . length === 0 ) return orgs [ 0 ]
111-
112- return withDates . reduce ( ( best , curr ) => {
113- const bestMs =
114- new Date ( best . dateEnd ?? '9999-12-31' ) . getTime ( ) - new Date ( best . dateStart ?? '' ) . getTime ( )
115- const currMs =
116- new Date ( curr . dateEnd ?? '9999-12-31' ) . getTime ( ) - new Date ( curr . dateStart ?? '' ) . getTime ( )
117- return currMs > bestMs ? curr : best
118- } )
112+ const candidates = withDates . length > 0 ? withDates : orgs
113+
114+ let best = candidates [ 0 ]
115+
116+ for ( const org of candidates ) {
117+ if ( durationMs ( org ) > durationMs ( best ) ) best = org
118+ }
119+
120+ return best
119121}
120122
121123function selectPrimaryWorkExperience ( orgs : IWorkRow [ ] ) : IWorkRow {
0 commit comments