-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
72 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { IResolver } from '../cachedResolvers' | ||
import { ImpressoApplication } from '../../types' | ||
import { MediaSource } from '../../models/generated/schemas' | ||
|
||
interface Partner { | ||
id: string | ||
title: string | ||
url?: string | ||
} | ||
|
||
const getPropValue = (source: MediaSource, prop: string) => source.properties?.find(p => p.id === prop)?.value | ||
|
||
// In-memory cache | ||
let partnersCache: Record<string, Partner> | null = null | ||
|
||
export const getPartnerResolver = (app: ImpressoApplication): IResolver<Partner> => { | ||
const mediaSources = app.service('media-sources') | ||
|
||
const loadPartnersData = async (): Promise<Record<string, Partner>> => { | ||
const sources = await mediaSources.getLookup() | ||
|
||
const partners = Object.values(sources).reduce( | ||
(acc, source) => { | ||
const partnerUid = getPropValue(source, 'partnerUid') | ||
|
||
if (partnerUid == null) return acc | ||
|
||
if (acc[partnerUid] == null) { | ||
const partnerNames = getPropValue(source, 'institutionNames')?.split(', ') | ||
const partnerLinks = getPropValue(source, 'institutionLinks')?.split(', ') | ||
|
||
acc[partnerUid] = { | ||
id: partnerUid, | ||
title: partnerNames?.[0] ?? partnerUid, | ||
url: partnerLinks?.[0], | ||
} | ||
} | ||
return acc | ||
}, | ||
{} as Record<string, Partner> | ||
) | ||
|
||
// Store in memory cache | ||
partnersCache = partners | ||
return partners | ||
} | ||
|
||
const getPartnersData = async (): Promise<Record<string, Partner>> => { | ||
if (partnersCache) { | ||
return partnersCache | ||
} | ||
return loadPartnersData() | ||
} | ||
|
||
return async (id: string) => { | ||
const partners = await getPartnersData() | ||
return partners[id] | ||
} | ||
} |