import * as t from 'io-ts' ; import { pipe } from 'fp-ts/lib/pipeable' ; import { fold } from 'fp-ts/lib/Either' ; import { ConcreteTaskInstance } from '../../../task_manager/server' ; import { SanitizedAlert , RuleTaskState , ruleParamsSchema , ruleStateSchema , RuleTaskParams , AlertTypeParams , } from '../../common' ; export interface AlertTaskInstance extends ConcreteTaskInstance { state : RuleTaskState ; params : RuleTaskParams ; } const enumerateErrorFields = ( e ) => `${ e . map ( ( { context } ) => context . map ( ( { key } ) => key ) . join ( '.' ) ) }` ; export function taskInstanceToAlertTaskInstance < Params extends AlertTypeParams > ( taskInstance , alert ? ) { return { ... taskInstance , params : pipe ( ruleParamsSchema . decode ( taskInstance . params ) , fold ( ( e ) => { throw new Error ( `Task "${ taskInstance . id }" ${ alert ? `(underlying Alert "${ alert . id }") ` : '' }has an invalid param at ${ enumerateErrorFields ( e ) }` ) ; } , t . identity ) ) , state : pipe ( ruleStateSchema . decode ( taskInstance . state ) , fold ( ( e ) => { throw new Error ( `Task "${ taskInstance . id }" ${ alert ? `(underlying Alert "${ alert . id }") ` : '' }has invalid state at ${ enumerateErrorFields ( e ) }` ) ; } , t . identity ) ) , } ; }