import { Injectable } from '@nestjs/common' ; import { Connection } from 'typeorm' ; import { Filter , INSIGHTS_MAP } from './insight-objects' ; import { PossibleOutputTypes , InsightObject , ListInsightsResponse , } from '@koh/common' ; import { UserModel } from 'profile/user.entity' ; type ComputeOutputParams = { insight : InsightObject ; filters : Filter [ ] ; } ; type GenerateAllInsightParams = { insights : InsightObject [ ] ; filters : Filter [ ] ; } ; @ Injectable ( ) export class InsightsService { constructor ( private connection ) { } async computeOutput ( { insight , filters , } ) { const output = await insight . compute ( filters ) ; return output ; } async generateAllInsights ( { insights , filters , } ) { return await Promise . all ( insights . map ( async ( insight ) => await this . computeOutput ( { insight , filters } ) ) ) ; } convertToInsightsListResponse ( insightNames ) { return insightNames . reduce ( ( obj , insightName ) => { const { displayName , description , component , size } = INSIGHTS_MAP [ insightName ] ; return { ... obj , [ insightName ] : { displayName , description , component , size , } , } ; } , { } ) ; } async toggleInsightOn ( user , insightName ) { user . hideInsights = user . hideInsights ?. filter ( ( insight ) => insight !== insightName ) ; await user . save ( ) ; return ; } async toggleInsightOff ( user , insightName ) { if ( user . hideInsights === null ) { user . hideInsights = [ ] ; } user . hideInsights = [ insightName , ... user . hideInsights ] ; await user . save ( ) ; return ; } }