Skip to content

Commit b1b8d7f

Browse files
committed
refactor: simplify affiliations
Signed-off-by: Umberto Sgueglia <usgueglia@contractor.linuxfoundation.org>
1 parent e67aa51 commit b1b8d7f

1 file changed

Lines changed: 4 additions & 16 deletions

File tree

  • services/libs/data-access-layer/src/affiliations

services/libs/data-access-layer/src/affiliations/index.ts

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,7 @@ function durationMs(org: IWorkRow): number {
108108
function longestDateRange(orgs: IWorkRow[]): IWorkRow {
109109
const withDates = orgs.filter((r) => r.dateStart)
110110
const candidates = withDates.length > 0 ? withDates : orgs
111-
112-
let best = candidates[0]
113-
114-
for (const org of candidates) {
115-
if (durationMs(org) > durationMs(best)) best = org
116-
}
117-
118-
return best
111+
return candidates.reduce((best, org) => (durationMs(org) > durationMs(best) ? org : best))
119112
}
120113

121114
function selectPrimaryWorkExperience(orgs: IWorkRow[]): IWorkRow {
@@ -130,11 +123,7 @@ function selectPrimaryWorkExperience(orgs: IWorkRow[]): IWorkRow {
130123

131124
// 2. isPrimaryWorkExperience = true — prefer those with a dateStart
132125
const primary = orgs.filter((r) => r.isPrimaryWorkExperience)
133-
if (primary.length > 0) {
134-
const withDates = primary.filter((r) => r.dateStart)
135-
if (withDates.length > 0) return withDates[0]
136-
return primary[0]
137-
}
126+
if (primary.length > 0) return primary.find((r) => r.dateStart) ?? primary[0]
138127

139128
// 3. Only one org has a dateStart — pick it
140129
const withDates = orgs.filter((r) => r.dateStart)
@@ -223,8 +212,7 @@ function buildTimeline(
223212
let currentWindowStart: Date = null
224213
let uncoveredPeriodStart: Date = null
225214

226-
for (let i = 0; i < boundaries.length; i++) {
227-
const boundaryDate = boundaries[i]
215+
for (const boundaryDate of boundaries) {
228216
const activeOrgsAtBoundary = orgsActiveAt(allRows, boundaryDate)
229217

230218
// No orgs active at this boundary — close the current window and start tracking a gap
@@ -272,7 +260,7 @@ function buildTimeline(
272260
if (currentOrg.organizationId !== winningAffiliation.organizationId) {
273261
affiliations.push({
274262
organization: currentOrg.organizationName,
275-
startDate: (currentWindowStart ?? boundaryDate).toISOString(),
263+
startDate: currentWindowStart.toISOString(),
276264
endDate: dayBefore(boundaryDate).toISOString(),
277265
})
278266
currentOrg = winningAffiliation

0 commit comments

Comments
 (0)