added new frontend service that sorts the active institutions

This commit is contained in:
Kazgatari
2026-08-29 21:35:29 +02:00
parent 23de1204f8
commit 4280ac24b8
8 changed files with 146 additions and 54 deletions
@@ -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<CustomAuthor>();
isBookmarked = input<boolean>(false);
loading = input<boolean>(false);
@@ -61,26 +64,10 @@ export class PersonCardComponent {
});
institution = computed<DehydratedInstitution | undefined>(() => {
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<boolean>(() => {
@@ -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<boolean>();
@@ -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);
@@ -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();
}
}
@@ -3,9 +3,9 @@
<mat-card appearance="outlined" class="card-content">
<mat-card-header>
<mat-card-title>{{ author.display_name }}</mat-card-title>
@if (author.last_known_institutions?.[0]?.display_name) {
@if (institution()?.display_name; as instName) {
<mat-card-subtitle class="institution">
<span>{{ author.last_known_institutions?.[0]?.display_name }}</span>
<span>{{ instName }}</span>
</mat-card-subtitle>
}
</mat-card-header>
@@ -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<Author>();
showProjects = signal<boolean>(false);
rawOaAuthorId = computed(() => this.stripOpenAlexId(this.author().id));
institution = computed(() =>
this.authorAffiliationService.resolveInstitution(this.author()),
);
stripOpenAlexId(id: string | null | undefined): string | undefined {
if (!id) {
@@ -37,10 +37,7 @@
<li>
<button type="button" (click)="onSelectResult(result)">
<span class="name">{{ result.display_name }}</span>
<span class="hint">{{
result.last_known_institutions?.[0]?.display_name ||
'Keine Institution'
}}</span>
<span class="hint">{{ getAuthorInstitutionName(result) }}</span>
</button>
</li>
}
@@ -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<string>('publications');
readonly comparisonAuthors = input<CompareItem[]>([]);
readonly topSuggestions = input<any[]>([]);
@@ -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'
);
}
}
@@ -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<string>('Authoren');
selectedAuthors = input.required<Author[]>();
@@ -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})`;
};
}