Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update gn4 API client to 4.2.7 #718

Merged
merged 16 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
class="py-[37px] pl-[47px] rounded-lg border bg-white mb-5 card-shadow cursor-pointer"
[figure]="recordsCount$ | async"
[icon]="'folder_open'"
[title]="'catalog.figures.datasets'"
title="catalog.figures.datasets"
[color]="'secondary'"
></gn-ui-figure>
</a>
Expand All @@ -13,7 +13,7 @@
class="py-[37px] pl-[47px] rounded-lg bg-white border card-shadow cursor-pointer"
[figure]="orgsCount$ | async"
[icon]="'corporate_fare'"
[title]="'catalog.figures.organisations'"
title="catalog.figures.organisations"
[color]="'secondary'"
></gn-ui-figure>
</a>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import { RecordsService } from '@geonetwork-ui/feature/catalog'
import { ROUTER_ROUTE_SEARCH } from '@geonetwork-ui/feature/router'
import { ROUTER_ROUTE_ORGANISATIONS } from '../../../router/constants'
import { OrganizationsServiceInterface } from '@geonetwork-ui/common/domain/organizations.service.interface'
import { marker } from '@biesbjerg/ngx-translate-extract-marker'

marker('catalog.figures.datasets')
marker('catalog.figures.organisations')

@Component({
selector: 'datahub-key-figures',
Expand Down
2 changes: 1 addition & 1 deletion libs/api/repository/src/lib/gn4/gn4-repository.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class ElasticsearchServiceMock {
}

class SearchApiServiceMock {
search = jest.fn((bucket, payload) => {
search = jest.fn((bucket, relatedType, payload) => {
const body = JSON.parse(payload)
const count = body.size || 1234
const result: EsSearchResponse = {
Expand Down
6 changes: 6 additions & 0 deletions libs/api/repository/src/lib/gn4/gn4-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export class Gn4Repository implements RecordsRepositoryInterface {
return this.gn4SearchApi
.search(
'bucket',
null,
JSON.stringify(
this.gn4SearchHelper.getSearchRequestBody(
{},
Expand Down Expand Up @@ -67,6 +68,7 @@ export class Gn4Repository implements RecordsRepositoryInterface {
return this.gn4SearchApi
.search(
'records-count',
null,
JSON.stringify({
...this.gn4SearchHelper.getSearchRequestBody(
{},
Expand All @@ -88,6 +90,7 @@ export class Gn4Repository implements RecordsRepositoryInterface {
return this.gn4SearchApi
.search(
'bucket',
null,
JSON.stringify(
this.gn4SearchHelper.getMetadataByIdPayload(uniqueIdentifier)
)
Expand All @@ -104,6 +107,7 @@ export class Gn4Repository implements RecordsRepositoryInterface {
return this.gn4SearchApi
.search(
'bucket',
null,
JSON.stringify(
this.gn4SearchHelper.getRelatedRecordPayload(
similarTo.title,
Expand All @@ -126,6 +130,7 @@ export class Gn4Repository implements RecordsRepositoryInterface {
return this.gn4SearchApi
.search(
'bucket',
null,
JSON.stringify(this.gn4SearchHelper.getSearchRequestBody(aggregations))
)
.pipe(
Expand All @@ -148,6 +153,7 @@ export class Gn4Repository implements RecordsRepositoryInterface {
return this.gn4SearchApi
.search(
'bucket',
null,
JSON.stringify(this.gn4SearchHelper.buildAutocompletePayload(query))
)
.pipe(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export class OrganizationsFromGroupsService
private groupsAggregation$ = this.searchApiService
.search(
'bucket',
null,
JSON.stringify(
this.esService.getSearchRequestBody({
groups: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ describe.each(['4.2.2-00', '4.2.3-xx', '4.2.5-xx'])(
it('call search service', () => {
expect(searchService.search).toHaveBeenCalledWith(
'bucket',
null,
JSON.stringify({
aggregations: {
contact: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export class OrganizationsFromMetadataService
switchMap((version) =>
this.searchApiService.search(
'bucket',
null,
JSON.stringify(this.getAggregationSearchRequest(version))
)
),
Expand Down
11 changes: 11 additions & 0 deletions libs/api/repository/src/lib/gn4/platform/gn4-platform.mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Injectable } from '@angular/core'
import { AvatarServiceInterface } from '../auth/avatar.service.interface'
import { map } from 'rxjs/operators'
import { Observable, of } from 'rxjs'
import { ThesaurusModel } from '@geonetwork-ui/common/domain/model/thesaurus/thesaurus.model'

@Injectable()
export class Gn4PlatformMapper {
Expand Down Expand Up @@ -43,4 +44,14 @@ export class Gn4PlatformMapper {
} = apiUser
return { ...apiUser, id: id.toString() } as UserModel
}

thesaurusFromApi(thesaurus: any[]): ThesaurusModel {
return thesaurus.map((keyword) => {
const { uri, value } = keyword
return {
key: uri,
label: value,
}
})
}
}
102 changes: 101 additions & 1 deletion libs/api/repository/src/lib/gn4/platform/gn4-platform.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import {
MeApiService,
RegistriesApiService,
SiteApiService,
ToolsApiService,
UsersApiService,
} from '@geonetwork-ui/data-access/gn4'
import { TestBed } from '@angular/core/testing'
import { Gn4PlatformService } from './gn4-platform.service'
import { firstValueFrom, of, Subject } from 'rxjs'
import { firstValueFrom, lastValueFrom, of, Subject } from 'rxjs'
import { AvatarServiceInterface } from '../auth/avatar.service.interface'
import { Gn4PlatformMapper } from './gn4-platform.mapper'

Expand Down Expand Up @@ -65,9 +67,62 @@ class UsersApiServiceMock {
}
}

class ToolsApiServiceMock {
getTranslationsPackage1 = jest.fn(() =>
of({
'First value': 'Translated first value',
'Second value': 'Hello',
'Third value': 'Bla',
})
)
}

class RegistriesApiServiceMock {
searchKeywords = jest.fn(() =>
of([
{
values: {
fre: 'Adresses',
},
definitions: {
fre: 'Localisation des propriétés fondée sur les identifiants des adresses, habituellement le nom de la rue, le numéro de la maison et le code postal.',
},
coordEast: '',
coordWest: '',
coordSouth: '',
coordNorth: '',
thesaurusKey: 'external.theme.httpinspireeceuropaeutheme-theme',
uri: 'http://inspire.ec.europa.eu/theme/ad',
definition:
'Localisation des propriétés fondée sur les identifiants des adresses, habituellement le nom de la rue, le numéro de la maison et le code postal.',
value: 'Adresses',
},
{
values: {
fre: 'Altitude',
},
definitions: {
fre: "Modèles numériques pour l'altitude des surfaces terrestres, glaciaires et océaniques. Comprend l'altitude terrestre, la bathymétrie et la ligne de rivage.",
},
coordEast: '',
coordWest: '',
coordSouth: '',
coordNorth: '',
thesaurusKey: 'external.theme.httpinspireeceuropaeutheme-theme',
uri: 'http://inspire.ec.europa.eu/theme/el',
definition:
"Modèles numériques pour l'altitude des surfaces terrestres, glaciaires et océaniques. Comprend l'altitude terrestre, la bathymétrie et la ligne de rivage.",
value: 'Altitude',
},
])
)
}

describe('Gn4PlatformService', () => {
let service: Gn4PlatformService
let meApiService: MeApiService
let toolsApiService: ToolsApiService
let registriesApiService: RegistriesApiService

beforeEach(() => {
TestBed.configureTestingModule({
Expand All @@ -90,10 +145,20 @@ describe('Gn4PlatformService', () => {
provide: AvatarServiceInterface,
useClass: AvatarServiceInterfaceMock,
},
{
provide: ToolsApiService,
useClass: ToolsApiServiceMock,
},
{
provide: RegistriesApiService,
useClass: RegistriesApiServiceMock,
},
],
})
service = TestBed.inject(Gn4PlatformService)
meApiService = TestBed.inject(MeApiService)
toolsApiService = TestBed.inject(ToolsApiService)
registriesApiService = TestBed.inject(RegistriesApiService)
})

it('creates', () => {
Expand Down Expand Up @@ -184,4 +249,39 @@ describe('Gn4PlatformService', () => {
})
})
})
describe('#translateKey', () => {
it('returns translation ', async () => {
const translation = await lastValueFrom(
service.translateKey('First value')
)
expect(translation).toEqual('Translated first value')
})
it('fetch api translations once ', async () => {
await lastValueFrom(service.translateKey('First value'))
await lastValueFrom(service.translateKey('Second value'))
expect(toolsApiService.getTranslationsPackage1).toHaveBeenCalledTimes(1)
})
})
describe('#getThesaurusByLang', () => {
it('calls api service ', async () => {
service.getThesaurusByLang('inspire', 'fre')
expect(registriesApiService.searchKeywords).toHaveBeenCalledWith(
null,
'fre',
1000,
0,
null,
['inspire']
)
})
it('returns mapped thesaurus ', async () => {
const thesaurusDomain = await lastValueFrom(
service.getThesaurusByLang('inspire', 'fre')
)
expect(thesaurusDomain).toEqual([
{ key: 'http://inspire.ec.europa.eu/theme/ad', label: 'Adresses' },
{ key: 'http://inspire.ec.europa.eu/theme/el', label: 'Altitude' },
])
})
})
})
34 changes: 32 additions & 2 deletions libs/api/repository/src/lib/gn4/platform/gn4-platform.service.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { Injectable } from '@angular/core'
import { Observable, of, switchMap } from 'rxjs'
import { map, shareReplay, tap } from 'rxjs/operators'
import { catchError, map, shareReplay, tap } from 'rxjs/operators'
import {
MeApiService,
RegistriesApiService,
SiteApiService,
ToolsApiService,
UsersApiService,
} from '@geonetwork-ui/data-access/gn4'
import { PlatformServiceInterface } from '@geonetwork-ui/common/domain/platform.service.interface'
import { UserModel } from '@geonetwork-ui/common/domain/model/user/user.model'
import { Organization } from '@geonetwork-ui/common/domain/model/record'
import { Gn4PlatformMapper } from './gn4-platform.mapper'
import { ltr } from 'semver'
import { ThesaurusModel } from '@geonetwork-ui/common/domain/model/thesaurus/thesaurus.model'

const minApiVersion = '4.2.2'
@Injectable()
Expand All @@ -20,6 +23,16 @@ export class Gn4PlatformService implements PlatformServiceInterface {
private users$: Observable<UserModel[]>
private isAnonymous$: Observable<boolean>

private keyTranslations$ = this.toolsApiService
.getTranslationsPackage1('gnui')
.pipe(
catchError(() => {
console.warn('Error while loading gnui language package')
return of({})
}),
shareReplay(1)
)

private settings$ = of(true).pipe(
switchMap(() => this.siteApiService.getSiteOrPortalDescription()),
shareReplay(1)
Expand All @@ -41,7 +54,9 @@ export class Gn4PlatformService implements PlatformServiceInterface {
private siteApiService: SiteApiService,
private meApi: MeApiService,
private usersApi: UsersApiService,
private mapper: Gn4PlatformMapper
private mapper: Gn4PlatformMapper,
private toolsApiService: ToolsApiService,
private registriesApiService: RegistriesApiService
) {
this.me$ = this.meApi.getMe().pipe(
switchMap((apiUser) => this.mapper.userFromMeApi(apiUser)),
Expand Down Expand Up @@ -81,4 +96,19 @@ export class Gn4PlatformService implements PlatformServiceInterface {
getUsers(): Observable<UserModel[]> {
return this.users$
}

translateKey(key: string): Observable<string> {
return this.keyTranslations$.pipe(map((translations) => translations[key]))
}

getThesaurusByLang(
thesaurusName: string,
lang: string
): Observable<ThesaurusModel> {
return this.registriesApiService
.searchKeywords(null, lang, 1000, 0, null, [thesaurusName])
.pipe(
map((thesaurus) => this.mapper.thesaurusFromApi(thesaurus as any[]))
)
}
}
1 change: 1 addition & 0 deletions libs/common/domain/src/lib/model/thesaurus/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './thesaurus.model'
6 changes: 6 additions & 0 deletions libs/common/domain/src/lib/model/thesaurus/thesaurus.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface ThesaurusItemModel {
key: string
label: string
}

export type ThesaurusModel = ThesaurusItemModel[]
12 changes: 9 additions & 3 deletions libs/common/domain/src/lib/platform.service.interface.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Observable } from 'rxjs'
import { UserModel } from './model/user/user.model'
import { Organization } from './model/record/organization.model'
import type { Observable } from 'rxjs'
import type { UserModel } from './model/user/user.model'
import type { Organization } from './model/record/organization.model'
import type { ThesaurusModel } from './model/thesaurus/'

export abstract class PlatformServiceInterface {
abstract getType(): string
Expand All @@ -13,4 +14,9 @@ export abstract class PlatformServiceInterface {
organisation: Organization
): Observable<UserModel[]>
abstract getOrganizations(): Observable<Organization[]>
abstract translateKey(key: string): Observable<string>
abstract getThesaurusByLang(
thesaurusName: string,
lang: string
): Observable<ThesaurusModel>
}
Loading
Loading