import { Db } from 'mongodb' ; import { useCache } from '../caching' ; import { computeTermAggregationByYear } from '../compute' ; import { getOtherKey } from '../helpers' ; import { RequestContext , SurveyConfig } from '../types' ; import { Filters } from '../filters' ; import { Entity } from '../types' ; import { getEntities } from '../entities' ; import { YearAggregations } from '../compute/generic' ; interface OtherFeaturesConfig { survey : SurveyConfig ; id : string ; filters ? : Filters ; } const computeOtherFeatures = async ( db , survey , id , filters ? ) => { const features = await getEntities ( { tag : 'feature' } ) ; const otherFeaturesByYear = await useCache ( computeTermAggregationByYear , db , [ survey , `features_others.${ getOtherKey ( id ) }` , { filters } ] ) ; return otherFeaturesByYear . map ( ( yearOtherFeatures ) => { return { ... yearOtherFeatures , buckets : yearOtherFeatures . buckets . map ( bucket => { const feature = features . find ( ( f ) => f . id === bucket . id ) ; return { ... bucket , name : feature ? feature . name : bucket . id } ; } ) } ; } ) ; } ; export default { OtherFeatures : { all_years : async ( { survey , id , filters } , args , { db } ) => computeOtherFeatures ( db , survey , id , filters ) , year : async ( { survey , id , filters } , { year } , { db } ) => { const allYears = await computeOtherFeatures ( db , survey , id , filters ) ; return allYears . find ( ( yearItem ) => yearItem . year === year ) ; } } } ;