import { IRouter } from 'kibana/server' ; import { schema } from '@kbn/config-schema' ; import { ILicenseState } from '../lib' ; import { RewriteResponseCase , verifyAccessAndContext } from './lib' ; import { AlertingRequestHandlerContext , INTERNAL_BASE_ALERTING_API_PATH , RuleTaskState , } from '../types' ; const paramSchema = schema . object ( { id : schema . string ( ) , } ) ; const rewriteBodyRes = ( { alertTypeState , alertInstances , previousStartedAt , ... rest } ) => ( { ... rest , rule_type_state : alertTypeState , alerts : alertInstances , previous_started_at : previousStartedAt , } ) ; export const getRuleStateRoute = ( router , licenseState ) => { router . get ( { path : `${ INTERNAL_BASE_ALERTING_API_PATH }/rule/{id}/state` , validate : { params : paramSchema , } , } , router . handleLegacyErrors ( verifyAccessAndContext ( licenseState , async function ( context , req , res ) { const rulesClient = context . alerting . getRulesClient ( ) ; const { id } = req . params ; const state = await rulesClient . getAlertState ( { id } ) ; return state ? res . ok ( { body : rewriteBodyRes ( state ) } ) : res . noContent ( ) ; } ) ) ) ; } ;