Skip to content

Commit

Permalink
Merge pull request #538 from geonetwork/fix-org-thumbnail
Browse files Browse the repository at this point in the history
fix: org thumbnail loading time and contacts overrides
  • Loading branch information
fgravin committed Jul 24, 2023
2 parents f24bd23 + 7ce3304 commit 116e47c
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
ElasticsearchService,
getAsArray,
getAsUrl,
hydrateContactsWithRecordLogo,
mapContact,
MetadataContact,
MetadataRecord,
Expand Down Expand Up @@ -132,17 +133,20 @@ export class OrganisationsFromGroupsService
const groupId = parseInt(selectField(source, 'groupOwner'))
const resourceContacts = getAsArray(
selectField(source, 'contactForResource')
).map((contact) => mapContact(contact, source))
).map((contact) => mapContact(contact))
return this.groups$.pipe(
map((groups) => {
const group = groups.find((g) => g.id === groupId)
if (!group) return record
if (!group) return hydrateContactsWithRecordLogo(record, source)
const contact = this.mapContactFromGroup(group, lang3)
return {
...record,
contact,
resourceContacts: [contact, ...resourceContacts],
}
return hydrateContactsWithRecordLogo(
{
...record,
contact,
resourceContacts: [contact, ...resourceContacts],
},
source
)
})
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,22 +317,18 @@ describe('OrganisationsFromMetadataService', () => {
title: 'Surval - Données par paramètre',
uuid: 'cf5048f6-5bbf-4e44-ba74-e6f429af51ea',
contact: {
name: 'Ifremer',
email: 'ifremer.ifremer@ifremer.admin.ch',
logoUrl: 'http://localhost/geonetwork/images/harvesting/ifremer.png',
name: "Cellule d'administration Quadrige",
organisation: 'Ifremer',
email: 'q2suppor@ifremer.fr',
website: 'https://www.ifremer.fr/',
logoUrl:
'http://localhost/geonetwork/images/logos/81e8a591-7815-4d2f-a7da-5673192e74c9.png',
},
resourceContacts: [
{
email: 'ifremer.ifremer@ifremer.admin.ch',
logoUrl:
'http://localhost/geonetwork/images/harvesting/ifremer.png',
name: 'Ifremer',
organisation: 'Ifremer',
},
{
email: 'q2_support@ifremer.fr',
logoUrl:
'http://localhost/geonetwork/images/logos/81e8a591-7815-4d2f-a7da-5673192e74c9.png',
'http://localhost/geonetwork/images/harvesting/ifremer.png',
name: "Cellule d'Administration Quadrige",
organisation: 'Ifremer',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
getAsArray,
getAsUrl,
getFirstValue,
hydrateContactsWithRecordLogo,
mapContact,
MetadataContact,
MetadataRecord,
Expand All @@ -17,8 +18,8 @@ import {
selectField,
SourceWithUnknownProps,
} from '@geonetwork-ui/util/shared'
import { combineLatest, Observable, of } from 'rxjs'
import { filter, map, shareReplay, startWith, takeLast } from 'rxjs/operators'
import { combineLatest, Observable, of, takeLast } from 'rxjs'
import { filter, map, shareReplay, startWith } from 'rxjs/operators'
import { OrganisationsServiceInterface } from './organisations.service.interface'

const IMAGE_URL = '/geonetwork/images/harvesting/'
Expand Down Expand Up @@ -70,7 +71,8 @@ export class OrganisationsFromMetadataService
]).pipe(
map(([organisations, groups]) => {
return !groups ? organisations : this.mapWithGroups(organisations, groups)
})
}),
shareReplay()
)

constructor(
Expand Down Expand Up @@ -163,19 +165,28 @@ export class OrganisationsFromMetadataService
})
}

private mapContactFromOrganisation(
organisation: Organisation,
contact: MetadataContact
private hydrateFirstResourceContactWithOrganisation(
firstResourceContact: MetadataContact,
contactOrganisation: Organisation
): MetadataContact {
const logoUrl = organisation.logoUrl
? getAsUrl(`${organisation.logoUrl}`)
: contact.logoUrl
const logoUrl =
firstResourceContact.logoUrl ||
(contactOrganisation.logoUrl
? getAsUrl(`${contactOrganisation.logoUrl}`)
: null)

const organisation = contactOrganisation.name
const name = firstResourceContact.name || contactOrganisation.name
const email = firstResourceContact.email || contactOrganisation.email
const { website } = firstResourceContact

return {
name: organisation.name,
organisation: organisation.name,
email: organisation.email,
logoUrl: logoUrl,
} as MetadataContact
name,
organisation,
email,
logoUrl,
website,
}
}

getFiltersForOrgs(organisations: Organisation[]): Observable<SearchFilters> {
Expand All @@ -201,38 +212,39 @@ export class OrganisationsFromMetadataService
source: SourceWithUnknownProps,
record: MetadataRecord
): Observable<MetadataRecord> {
const metadataRecord = {
const resourceContacts = getAsArray(
selectField(source, 'contactForResource')
).map((contact) => mapContact(contact))
const contact = mapContact(getFirstValue(selectField(source, 'contact')))
const metadataRecord: MetadataRecord = {
...record,
resourceContacts: [
...getAsArray(selectField(source, 'contactForResource')).map(
(contact) => mapContact(contact, source)
),
],
contact: {
...mapContact(getFirstValue(selectField(source, 'contact')), source),
},
resourceContacts,
contact,
}
const [firstResourceContact, ...otherResourceContacts] = resourceContacts

return this.organisations$.pipe(
takeLast(1),
map((organisations) => {
const org = organisations.filter(
(o) => o.name === metadataRecord.resourceContacts[0]?.organisation
const recordOrganisation = organisations.filter(
(org) => org.name === firstResourceContact?.organisation
)[0]

if (org) {
const contactFromOrg = this.mapContactFromOrganisation(
org,
metadataRecord.contact
)
metadataRecord.contact = contactFromOrg
metadataRecord.resourceContacts = [
contactFromOrg, // FIXME: this should go into an organization field
...metadataRecord.resourceContacts,
]
}

return metadataRecord
return hydrateContactsWithRecordLogo(
{
...metadataRecord,
...(recordOrganisation && {
resourceContacts: [
this.hydrateFirstResourceContactWithOrganisation(
firstResourceContact,
recordOrganisation
),
...otherResourceContacts,
],
}),
},
source
)
})
)
}
Expand Down
21 changes: 17 additions & 4 deletions libs/util/shared/src/lib/utils/atomic-operations.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MetadataContact } from '../models'
import { MetadataContact, MetadataRecord } from '../models'

export type SourceWithUnknownProps = { [key: string]: unknown }

Expand Down Expand Up @@ -54,12 +54,25 @@ export const mapLogo = (source: SourceWithUnknownProps) => {
return logo ? getAsUrl(`/geonetwork${logo}`) : null
}

export const hydrateContactsWithRecordLogo = (
record: MetadataRecord,
source: SourceWithUnknownProps
): MetadataRecord => {
const recordLogo = mapLogo(source)
if (recordLogo) {
if (record.contact) record.contact.logoUrl ??= recordLogo
record.resourceContacts?.map((r) => {
r.logoUrl ??= recordLogo
})
}
return record
}

export const mapContact = (
sourceContact: SourceWithUnknownProps,
sourceRecord: SourceWithUnknownProps
sourceContact: SourceWithUnknownProps
): MetadataContact => {
const website = getAsUrl(selectField<string>(sourceContact, 'website'))
const logoUrl = mapLogo(sourceContact) || mapLogo(sourceRecord)
const logoUrl = mapLogo(sourceContact)
const address = selectField<string>(sourceContact, 'address')
const phone = selectField<string>(sourceContact, 'phone')
return {
Expand Down

0 comments on commit 116e47c

Please sign in to comment.