Skip to content

Commit

Permalink
added partner resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
theorm committed Feb 28, 2025
1 parent 3a98559 commit 35b766e
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/internalServices/cachedResolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import { ImpressoApplication } from '../types'
import { Newspaper as NewspaperInternal } from '../models/generated/schemas'
import { Topic as ITopic } from '../models/generated/schemas'
import { WellKnownKeys } from '../cache'
import { getPartnerResolver } from './facetResolvers/partnerResolver'

export type CachedFacetType = 'newspaper' | 'topic' | 'person' | 'location' | 'collection' | 'year'
export type CachedFacetType = 'newspaper' | 'topic' | 'person' | 'location' | 'collection' | 'year' | 'partner'

export type IResolver<T> = (id: string) => Promise<T | undefined>

Expand Down Expand Up @@ -48,11 +49,14 @@ const getNewspaperResolver = (app: ImpressoApplication): IResolver<NewspaperInte
}
}

export const buildResolvers = (app: ImpressoApplication): ICachedResolvers => ({
collection: collectionResolver,
location: (id: string) => entityResolver(id, 'location'),
person: (id: string) => entityResolver(id, 'person'),
topic: getTopicResolver(app),
year: yearResolver,
newspaper: getNewspaperResolver(app),
})
export const buildResolvers = (app: ImpressoApplication): ICachedResolvers => {
return {
collection: collectionResolver,
location: (id: string) => entityResolver(id, 'location'),
person: (id: string) => entityResolver(id, 'person'),
topic: getTopicResolver(app),
year: yearResolver,
newspaper: getNewspaperResolver(app),
partner: getPartnerResolver(app),
}
}
59 changes: 59 additions & 0 deletions src/internalServices/facetResolvers/partnerResolver.ts
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]
}
}

0 comments on commit 35b766e

Please sign in to comment.