Skip to content

Commit

Permalink
fix(orgs): hydrate correctly organisation contact
Browse files Browse the repository at this point in the history
  • Loading branch information
fgravin committed Jul 24, 2023
1 parent 2f22969 commit 7ce3304
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
ElasticsearchService,
getAsArray,
getAsUrl,
hydrateWithRecordLogo,
hydrateContactsWithRecordLogo,
mapContact,
MetadataContact,
MetadataRecord,
Expand Down Expand Up @@ -137,9 +137,9 @@ export class OrganisationsFromGroupsService
return this.groups$.pipe(
map((groups) => {
const group = groups.find((g) => g.id === groupId)
if (!group) return hydrateWithRecordLogo(record, source)
if (!group) return hydrateContactsWithRecordLogo(record, source)
const contact = this.mapContactFromGroup(group, lang3)
return hydrateWithRecordLogo(
return hydrateContactsWithRecordLogo(
{
...record,
contact,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,14 +321,15 @@ describe('OrganisationsFromMetadataService', () => {
organisation: 'Ifremer',
email: 'q2suppor@ifremer.fr',
website: 'https://www.ifremer.fr/',
logoUrl: 'http://localhost/geonetwork/images/harvesting/ifremer.png',
logoUrl:
'http://localhost/geonetwork/images/logos/81e8a591-7815-4d2f-a7da-5673192e74c9.png',
},
resourceContacts: [
{
email: 'q2suppor@ifremer.fr',
email: 'q2_support@ifremer.fr',
logoUrl:
'http://localhost/geonetwork/images/harvesting/ifremer.png',
name: "Cellule d'administration Quadrige",
name: "Cellule d'Administration Quadrige",
organisation: 'Ifremer',
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
getAsArray,
getAsUrl,
getFirstValue,
hydrateWithRecordLogo,
hydrateContactsWithRecordLogo,
mapContact,
MetadataContact,
MetadataRecord,
Expand Down Expand Up @@ -165,35 +165,28 @@ export class OrganisationsFromMetadataService
})
}

private hydrateContactsWithRecordLogo(
metadataRecord: MetadataRecord,
organisation: Organisation
): MetadataRecord {
const firstResourceContact = metadataRecord.resourceContacts[0]
private hydrateFirstResourceContactWithOrganisation(
firstResourceContact: MetadataContact,
contactOrganisation: Organisation
): MetadataContact {
const logoUrl =
metadataRecord.contact.logoUrl ||
firstResourceContact.logoUrl ||
(organisation.logoUrl ? getAsUrl(`${organisation.logoUrl}`) : null)
const mappedOrg = {
name:
metadataRecord.contact?.name ||
firstResourceContact.name ||
organisation.name,
organisation: organisation.name,
email:
metadataRecord.contact?.email ||
firstResourceContact.email ||
organisation.email,
logoUrl: logoUrl,
website:
metadataRecord.contact?.website ||
metadataRecord.resourceContacts[0].website,
} as MetadataContact
(contactOrganisation.logoUrl
? getAsUrl(`${contactOrganisation.logoUrl}`)
: null)

metadataRecord.contact = mappedOrg
metadataRecord.resourceContacts[0] = mappedOrg
const organisation = contactOrganisation.name
const name = firstResourceContact.name || contactOrganisation.name
const email = firstResourceContact.email || contactOrganisation.email
const { website } = firstResourceContact

return metadataRecord
return {
name,
organisation,
email,
logoUrl,
website,
}
}

getFiltersForOrgs(organisations: Organisation[]): Observable<SearchFilters> {
Expand All @@ -219,29 +212,37 @@ 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)
),
],
contact: {
...mapContact(getFirstValue(selectField(source, 'contact'))),
},
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]

return hydrateWithRecordLogo(
org
? this.hydrateContactsWithRecordLogo(metadataRecord, org)
: metadataRecord,
return hydrateContactsWithRecordLogo(
{
...metadataRecord,
...(recordOrganisation && {
resourceContacts: [
this.hydrateFirstResourceContactWithOrganisation(
firstResourceContact,
recordOrganisation
),
...otherResourceContacts,
],
}),
},
source
)
})
Expand Down
2 changes: 1 addition & 1 deletion libs/util/shared/src/lib/utils/atomic-operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export const mapLogo = (source: SourceWithUnknownProps) => {
return logo ? getAsUrl(`/geonetwork${logo}`) : null
}

export const hydrateWithRecordLogo = (
export const hydrateContactsWithRecordLogo = (
record: MetadataRecord,
source: SourceWithUnknownProps
): MetadataRecord => {
Expand Down

0 comments on commit 7ce3304

Please sign in to comment.