From 4280ac24b8d3a3a3fef78ba55a54be77c9347f0c Mon Sep 17 00:00:00 2001 From: Kazgatari <56477241+Kazgatari@users.noreply.github.com> Date: Sat, 29 Aug 2026 21:35:29 +0200 Subject: [PATCH] added new frontend service that sorts the active institutions --- .../person-card/person-card.component.ts | 29 ++--- .../app/modules/person/person.component.ts | 33 +++--- .../services/author-affiliation.service.ts | 102 ++++++++++++++++++ .../app-author-card.component.html | 4 +- .../app-author-card.component.ts | 5 + .../author-selector.component.html | 5 +- .../author-selector.component.ts | 9 ++ .../searchable-author-select.component.ts | 13 +-- 8 files changed, 146 insertions(+), 54 deletions(-) create mode 100644 webapp/src/app/shared/services/author-affiliation.service.ts diff --git a/webapp/src/app/modules/person-card/person-card.component.ts b/webapp/src/app/modules/person-card/person-card.component.ts index 14a3cfe2..b2f87016 100644 --- a/webapp/src/app/modules/person-card/person-card.component.ts +++ b/webapp/src/app/modules/person-card/person-card.component.ts @@ -1,4 +1,4 @@ -import { Component, input, computed, output } from '@angular/core'; +import { Component, input, computed, output, inject } from '@angular/core'; import { CommonModule } from '@angular/common'; import { RouterLink } from '@angular/router'; import { MatIconModule } from '@angular/material/icon'; @@ -9,6 +9,7 @@ import { TopicChipComponent } from '../../shared/ui/topic-chip/topic-chip.compon import { AppInstitutionChipComponent } from '../../shared/ui/app-institution-chip/app-institution-chip.component'; import { CareerStageChipComponent } from '../../shared/ui/career-stage-chip/career-stage-chip.component'; import { topicHierarchyColor } from '../../resolvers/utils/topic-colors'; +import { AuthorAffiliationService } from '../../shared/services/author-affiliation.service'; import { BOOKMARK_BORDER_ICON, BOOKMARK_ICON, @@ -40,6 +41,8 @@ export class PersonCardComponent { readonly PUBLICATION_ICON = PUBLICATION_ICON; readonly NETWORK_ICON = NETWORK_ICON; + private readonly authorAffiliationService = inject(AuthorAffiliationService); + person = input.required(); isBookmarked = input(false); loading = input(false); @@ -61,26 +64,10 @@ export class PersonCardComponent { }); institution = computed(() => { - const p = this.person(); - if (p.last_known_institutions?.[0]) { - return p.last_known_institutions[0]; - } - - const affiliations = p.affiliations ?? []; - let newestInstitution: DehydratedInstitution | undefined = - affiliations[0]?.institution ?? undefined; - let newestYear = -1; - - for (const affiliation of affiliations) { - for (const jahr of affiliation.years ?? []) { - if (jahr > newestYear) { - newestYear = jahr; - newestInstitution = affiliation.institution ?? undefined; - } - } - } - - return newestInstitution; + return this.authorAffiliationService.resolveInstitution( + this.person(), + this.activeInstitutionId(), + ); }); isInstitutionActive = computed(() => { diff --git a/webapp/src/app/modules/person/person.component.ts b/webapp/src/app/modules/person/person.component.ts index 290f8b62..f6c9d7e6 100644 --- a/webapp/src/app/modules/person/person.component.ts +++ b/webapp/src/app/modules/person/person.component.ts @@ -42,6 +42,7 @@ import { LoadingComponent } from '../../shared/ui/loading/loading.component'; import { PersonBioResolved } from '../../resolvers/person-bio.resolver'; import { PersonDataResolved } from '../../resolvers/person.resolver'; import { BookmarkService } from '../../shared/services/bookmark.service'; +import { AuthorAffiliationService } from '../../shared/services/author-affiliation.service'; import { collaborationGraphQueryParamKeys } from '../../resolvers/collaboration-graph.resolver'; import { workQueryParamKeys } from '../../resolvers/work-page.resolver'; import { @@ -104,6 +105,7 @@ export class PersonComponent { private readonly worksApi = inject(WorksService); private readonly destroyRef = inject(DestroyRef); readonly bookmarkService = inject(BookmarkService); + private readonly authorAffiliationService = inject(AuthorAffiliationService); canFetchGdprRelevantData = input.required(); @@ -227,32 +229,21 @@ export class PersonComponent { readonly primaryAffiliation = computed( () => this.person()?.affiliations?.[0] ?? null, ); - readonly lastAffiliation = computed(() => { - const affiliations = this.person()?.affiliations ?? []; - let newestAffiliation = affiliations[0] ?? null; - let newestYear = -1; - - for (const affiliation of affiliations) { - for (const jahr of affiliation.years ?? []) { - if (jahr > newestYear) { - newestYear = jahr; - newestAffiliation = affiliation; - } - } - } - - return newestAffiliation; - }); - readonly institution = computed( + readonly lastAffiliation = computed( () => - this.lastAffiliation()?.institution?.display_name ?? - 'Unbekannte Institution', + this.authorAffiliationService.getLatestAffiliation(this.person()) ?? null, + ); + readonly resolvedInstitution = computed(() => + this.authorAffiliationService.resolveInstitution(this.person()), + ); + readonly institution = computed( + () => this.resolvedInstitution()?.display_name ?? 'Unbekannte Institution', ); readonly countryCode = computed( - () => this.lastAffiliation()?.institution?.country_code ?? null, + () => this.resolvedInstitution()?.country_code ?? null, ); readonly institutionId = computed( - () => this.lastAffiliation()?.institution?.id?.split('/').pop() ?? null, + () => this.resolvedInstitution()?.id?.split('/').pop() ?? null, ); readonly worksCount = computed(() => this.person()?.works_count ?? null); readonly citedByCount = computed(() => this.person()?.cited_by_count ?? null); diff --git a/webapp/src/app/shared/services/author-affiliation.service.ts b/webapp/src/app/shared/services/author-affiliation.service.ts new file mode 100644 index 00000000..1dc67326 --- /dev/null +++ b/webapp/src/app/shared/services/author-affiliation.service.ts @@ -0,0 +1,102 @@ +import { Injectable } from '@angular/core'; +import { + Author, + AuthorAffiliationsInner, + CustomAuthor, + DehydratedInstitution, +} from '../../api'; + +@Injectable({ + providedIn: 'root', +}) +export class AuthorAffiliationService { + /** + * Resolves the primary institution for an author based on: + * 1. Active search filter match (if filtering by an institution) + * 2. Most recent publication affiliation (highest year in affiliations.years) + * 3. Fallback to last_known_institutions[0] if affiliations are empty + */ + resolveInstitution( + author: Author | CustomAuthor | null | undefined, + activeInstitutionId?: string | null, + ): DehydratedInstitution | undefined { + if (!author) { + return undefined; + } + + const affiliations = author.affiliations ?? []; + + // 1. If an active institution filter is present, prioritize the matching affiliation + if (activeInstitutionId) { + const cleanActiveId = this.normalizeId(activeInstitutionId); + if (cleanActiveId) { + const matchedAffiliation = affiliations.find((aff) => { + const affId = this.normalizeId(aff.institution?.id); + return affId === cleanActiveId; + }); + if (matchedAffiliation?.institution) { + return matchedAffiliation.institution; + } + + const matchedLastKnown = (author.last_known_institutions ?? []).find( + (inst) => { + const instId = this.normalizeId(inst.id); + return instId === cleanActiveId; + }, + ); + if (matchedLastKnown) { + return matchedLastKnown; + } + } + } + + // 2. Otherwise, find the newest affiliation by highest publication year + const latestAffiliation = this.getLatestAffiliation(author); + if (latestAffiliation?.institution) { + return latestAffiliation.institution; + } + + // 3. Fallback to last_known_institutions if affiliations have no valid institution + return author.last_known_institutions?.[0] ?? undefined; + } + + /** + * Returns the affiliation with the highest publication year for an author. + */ + getLatestAffiliation( + author: Author | CustomAuthor | null | undefined, + ): AuthorAffiliationsInner | undefined { + if (!author) { + return undefined; + } + + const affiliations = author.affiliations ?? []; + let newestAffiliation: AuthorAffiliationsInner | undefined = undefined; + let newestYear = -1; + + for (const affiliation of affiliations) { + if (affiliation.institution) { + const years = affiliation.years ?? []; + if (years.length > 0) { + for (const year of years) { + if (year > newestYear) { + newestYear = year; + newestAffiliation = affiliation; + } + } + } else if (!newestAffiliation) { + newestAffiliation = affiliation; + } + } + } + + return newestAffiliation; + } + + private normalizeId(id: string | null | undefined): string { + if (!id) { + return ''; + } + return id.replace('https://openalex.org/', '').trim().toUpperCase(); + } +} diff --git a/webapp/src/app/shared/ui/app-author-card/app-author-card.component.html b/webapp/src/app/shared/ui/app-author-card/app-author-card.component.html index 252b6121..c94a0f8a 100644 --- a/webapp/src/app/shared/ui/app-author-card/app-author-card.component.html +++ b/webapp/src/app/shared/ui/app-author-card/app-author-card.component.html @@ -3,9 +3,9 @@ {{ author.display_name }} - @if (author.last_known_institutions?.[0]?.display_name) { + @if (institution()?.display_name; as instName) { - {{ author.last_known_institutions?.[0]?.display_name }} + {{ instName }} } diff --git a/webapp/src/app/shared/ui/app-author-card/app-author-card.component.ts b/webapp/src/app/shared/ui/app-author-card/app-author-card.component.ts index b0cc7fe5..3370ba19 100644 --- a/webapp/src/app/shared/ui/app-author-card/app-author-card.component.ts +++ b/webapp/src/app/shared/ui/app-author-card/app-author-card.component.ts @@ -7,6 +7,7 @@ import { signal, } from '@angular/core'; import { Author, AuthService } from '../../../api'; +import { AuthorAffiliationService } from '../../services/author-affiliation.service'; import { MatCardModule } from '@angular/material/card'; import { MatIconModule } from '@angular/material/icon'; import { MatDividerModule } from '@angular/material/divider'; @@ -41,11 +42,15 @@ export class AppAuthorCardComponent { readonly PROJECT_ICON = PROJECT_ICON; private readonly authService = inject(AuthService); + private readonly authorAffiliationService = inject(AuthorAffiliationService); author = input.required(); showProjects = signal(false); rawOaAuthorId = computed(() => this.stripOpenAlexId(this.author().id)); + institution = computed(() => + this.authorAffiliationService.resolveInstitution(this.author()), + ); stripOpenAlexId(id: string | null | undefined): string | undefined { if (!id) { diff --git a/webapp/src/app/shared/ui/author-selector/author-selector.component.html b/webapp/src/app/shared/ui/author-selector/author-selector.component.html index 5efb11e9..696bce75 100644 --- a/webapp/src/app/shared/ui/author-selector/author-selector.component.html +++ b/webapp/src/app/shared/ui/author-selector/author-selector.component.html @@ -37,10 +37,7 @@
  • } diff --git a/webapp/src/app/shared/ui/author-selector/author-selector.component.ts b/webapp/src/app/shared/ui/author-selector/author-selector.component.ts index 5f4debeb..1b90993e 100644 --- a/webapp/src/app/shared/ui/author-selector/author-selector.component.ts +++ b/webapp/src/app/shared/ui/author-selector/author-selector.component.ts @@ -12,6 +12,7 @@ import { MatFormFieldModule } from '@angular/material/form-field'; import { MatInputModule } from '@angular/material/input'; import { MatButtonModule } from '@angular/material/button'; +import { AuthorAffiliationService } from '../../services/author-affiliation.service'; import { ADD_ICON, CLOSE_ICON, @@ -52,6 +53,7 @@ export class AuthorSelectorComponent { readonly ADD_ICON = ADD_ICON; private readonly elementRef = inject(ElementRef); + private readonly authorAffiliationService = inject(AuthorAffiliationService); readonly activeTab = input('publications'); readonly comparisonAuthors = input([]); readonly topSuggestions = input([]); @@ -144,4 +146,11 @@ export class AuthorSelectorComponent { (i) => this.cleanId(i.id) === cleanTarget, ); } + + getAuthorInstitutionName(author: any): string { + return ( + this.authorAffiliationService.resolveInstitution(author)?.display_name || + 'Keine Institution' + ); + } } diff --git a/webapp/src/app/shared/ui/searchable-author-select/searchable-author-select.component.ts b/webapp/src/app/shared/ui/searchable-author-select/searchable-author-select.component.ts index bb02fbaf..62dcf86d 100644 --- a/webapp/src/app/shared/ui/searchable-author-select/searchable-author-select.component.ts +++ b/webapp/src/app/shared/ui/searchable-author-select/searchable-author-select.component.ts @@ -2,6 +2,7 @@ import { Component, inject, input, output } from '@angular/core'; import { Author, AuthorPage, AuthorsV2Service } from '../../../api'; import { Observable } from 'rxjs'; import { SearchableSelectComponent } from '../searchable-select/searchable-select.component'; +import { AuthorAffiliationService } from '../../services/author-affiliation.service'; import { PERSON_ICON } from '../icons'; @Component({ @@ -14,6 +15,7 @@ export class SearchableAuthorSelectComponent { readonly PERSON_ICON = PERSON_ICON; private readonly authorsV2Service = inject(AuthorsV2Service); + private readonly authorAffiliationService = inject(AuthorAffiliationService); label = input('Authoren'); selectedAuthors = input.required(); @@ -35,11 +37,10 @@ export class SearchableAuthorSelectComponent { getAuthorDescription = (author: Author): string => { const worksCount = author.works_count ?? 0; const worksCountText = worksCount === 1 ? 'Werk' : 'Werke'; - const lastKnownInstitution = author.last_known_institutions?.[0]; - const lastKnownInstitutionDisplayName = - lastKnownInstitution?.display_name ?? 'Unbekannt'; - const lastKnownInstitutionCountryCode = - lastKnownInstitution?.country_code ?? '??'; - return `${worksCount} ${worksCountText} - ${lastKnownInstitutionDisplayName} (${lastKnownInstitutionCountryCode})`; + const institution = + this.authorAffiliationService.resolveInstitution(author); + const institutionDisplayName = institution?.display_name ?? 'Unbekannt'; + const institutionCountryCode = institution?.country_code ?? '??'; + return `${worksCount} ${worksCountText} - ${institutionDisplayName} (${institutionCountryCode})`; }; }