Skip to content

Commit e6f6aba

Browse files
committed
refactor: simplify longestDateRange
Signed-off-by: Umberto Sgueglia <usgueglia@contractor.linuxfoundation.org>
1 parent 3274cdd commit e6f6aba

File tree

1 file changed

+15
-13
lines changed
  • services/libs/data-access-layer/src/affiliations

1 file changed

+15
-13
lines changed

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

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ interface IWorkRow {
2727
segmentId: string | null
2828
}
2929

30-
// ─── Query: regular work experiences (bulk) ──────────────────────────────────
31-
3230
export 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-
8076
export 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+
108110
function 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

121123
function selectPrimaryWorkExperience(orgs: IWorkRow[]): IWorkRow {

0 commit comments

Comments
 (0)