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

Check API version compatiblity #710

Merged
merged 4 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 @@ -28,6 +28,7 @@ import { combineLatest, Observable, of, switchMap, takeLast } from 'rxjs'
import { filter, map, shareReplay, startWith, tap } from 'rxjs/operators'
import { LangService } from '@geonetwork-ui/util/i18n'
import { PlatformServiceInterface } from '@geonetwork-ui/common/domain/platform.service.interface'
import { coerce, satisfies, valid } from 'semver'

const IMAGE_URL = '/geonetwork/images/harvesting/'

Expand Down Expand Up @@ -144,6 +145,7 @@ export class OrganizationsFromMetadataService
}

private getAggregationSearchRequest(gnVersion: string) {
const semVersion = valid(coerce(gnVersion))
return this.esService.getSearchRequestBody({
contact: {
nested: {
Expand All @@ -152,9 +154,10 @@ export class OrganizationsFromMetadataService
aggs: {
org: {
terms: {
field: gnVersion.startsWith('4.2.2')
? 'contactForResource.organisation'
: 'contactForResource.organisationObject.default.keyword',
field:
semVersion === '4.2.2'
? 'contactForResource.organisation'
: 'contactForResource.organisationObject.default.keyword',
exclude: '',
size: 5000,
order: { _key: 'asc' },
Expand All @@ -164,12 +167,9 @@ export class OrganizationsFromMetadataService
terms: {
size: 50,
exclude: '',
field:
gnVersion.startsWith('4.2.2') ||
gnVersion.startsWith('4.2.3') ||
gnVersion.startsWith('4.2.4')
? 'contactForResource.email.keyword'
: 'contactForResource.email',
field: satisfies(semVersion, '4.2.2 - 4.2.4')
? 'contactForResource.email.keyword'
: 'contactForResource.email',
},
},
logoUrl: {
Expand All @@ -187,9 +187,10 @@ export class OrganizationsFromMetadataService
terms: {
size: 5000,
exclude: '',
field: gnVersion.startsWith('4.2.2')
? 'OrgForResource'
: 'OrgForResourceObject.default',
field:
semVersion === '4.2.2'
? 'OrgForResource'
: 'OrgForResourceObject.default',
order: {
_key: 'asc',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ export class Gn4PlatformMapper {
credentialsNonExpired,
...user
} = apiUser
return { ...apiUser, id: id + '' } as UserModel
return { ...apiUser, id: id.toString() } as UserModel
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The old way ensure a value is set and no Error is thrown. Didn't dig deeper but it may have impact.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also used to know that it was better too, @jahow wanted me to change that though.
Maybe id?.toString() 😄

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
} from '@geonetwork-ui/data-access/gn4'
import { TestBed } from '@angular/core/testing'
import { Gn4PlatformService } from './gn4-platform.service'
import { firstValueFrom, lastValueFrom, of, Subject } from 'rxjs'
import { firstValueFrom, of, Subject } from 'rxjs'
import { AvatarServiceInterface } from '../auth/avatar.service.interface'
import { Gn4PlatformMapper } from './gn4-platform.mapper'

Expand All @@ -31,6 +31,7 @@ class MeApiMock {
getMe() {
return this._me$
}

_me$ = new Subject()
}

Expand All @@ -46,6 +47,7 @@ class SiteApiServiceMock {
})
)
}

class UsersApiServiceMock {
getUsers() {
return of([
Expand Down Expand Up @@ -98,11 +100,32 @@ describe('Gn4PlatformService', () => {
expect(service).toBeTruthy()
})

it('fetches version from settings', async () => {
geonetworkVersion = '4.2.0'
const version = await firstValueFrom(service.getApiVersion())
expect(version).toEqual('4.2.0')
describe('version', () => {
describe('when version is lower than 4.2.2', () => {
beforeEach(() => {
geonetworkVersion = '4.2.0'
})
it('throws an error', async () => {
let error
await firstValueFrom(service.getApiVersion()).catch((e) => (error = e))
expect(error).toEqual(
new Error(
'Gn4 API version is not compatible.\nMinimum: 4.2.2\nYour version: 4.2.0'
)
)
})
})
describe('when version is euqal or greater than 4.2.2', () => {
beforeEach(() => {
geonetworkVersion = '4.2.2'
})
it('fetches version from settings', async () => {
const version = await firstValueFrom(service.getApiVersion())
expect(version).toEqual('4.2.2')
})
})
})

it('fetches users from api', async () => {
const users = await firstValueFrom(service.getUsers())
expect(users).toEqual([
Expand Down
24 changes: 10 additions & 14 deletions libs/api/repository/src/lib/gn4/platform/gn4-platform.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ import { PlatformServiceInterface } from '@geonetwork-ui/common/domain/platform.
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'

const minApiVersion = '4.2.0'
const minApiVersion = '4.2.2'
@Injectable()
export class Gn4PlatformService implements PlatformServiceInterface {
private readonly type = 'GeoNetwork'
private me$: Observable<UserModel>
private users$: Observable<UserModel[]>
isAnonymous$: Observable<boolean>
private isAnonymous$: Observable<boolean>

private settings$ = of(true).pipe(
switchMap(() => this.siteApiService.getSiteOrPortalDescription()),
Expand All @@ -26,18 +27,16 @@ export class Gn4PlatformService implements PlatformServiceInterface {

private readonly apiVersion$ = this.settings$.pipe(
map((info) => info['system/platform/version'] as string),
tap((version) => {
if (ltr(version, minApiVersion)) {
throw new Error(
`Gn4 API version is not compatible.\nMinimum: ${minApiVersion}\nYour version: ${version}`
)
}
}),
shareReplay(1)
)

private readonly isApiCompatible$ = this.apiVersion$.pipe(
tap(
(version) =>
version < minApiVersion &&
console.warn(`The GeoNetwork Api version is too low ${version}`)
),
map((version) => version >= minApiVersion)
)

constructor(
private siteApiService: SiteApiService,
private meApi: MeApiService,
Expand All @@ -62,9 +61,6 @@ export class Gn4PlatformService implements PlatformServiceInterface {
getApiVersion(): Observable<string> {
return this.apiVersion$
}
isApiCompatible(): Observable<boolean> {
return this.isApiCompatible$
}

getMe(): Observable<UserModel> {
return this.me$
Expand Down
1 change: 0 additions & 1 deletion libs/common/domain/src/lib/platform.service.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { Organization } from './model/record/organization.model'
export abstract class PlatformServiceInterface {
abstract getType(): string
abstract getApiVersion(): Observable<string>
abstract isApiCompatible(): Observable<boolean>

abstract getMe(): Observable<UserModel>
abstract isAnonymous(): Observable<boolean>
Expand Down
Loading
Loading