import { schema } from '@kbn/config-schema' ; import { IRouter } from 'kibana/server' ; import { ILicenseState , AlertTypeDisabledError , validateDurationSchema } from '../lib' ; import { AlertNotifyWhenType } from '../../common' ; import { UpdateOptions } from '../rules_client' ; import { verifyAccessAndContext , RewriteResponseCase , RewriteRequestCase , handleDisabledApiKeysError , } from './lib' ; import { AlertTypeParams , AlertingRequestHandlerContext , BASE_ALERTING_API_PATH , validateNotifyWhenType , PartialAlert , } from '../types' ; const paramSchema = schema . object ( { id : schema . string ( ) , } ) ; const bodySchema = schema . object ( { name : schema . string ( ) , tags : schema . arrayOf ( schema . string ( ) , { defaultValue : [ ] } ) , schedule : schema . object ( { interval : schema . string ( { validate : validateDurationSchema } ) , } ) , throttle : schema . nullable ( schema . string ( { validate : validateDurationSchema } ) ) , params : schema . recordOf ( schema . string ( ) , schema . any ( ) , { defaultValue : { } } ) , actions : schema . arrayOf ( schema . object ( { group : schema . string ( ) , id : schema . string ( ) , params : schema . recordOf ( schema . string ( ) , schema . any ( ) , { defaultValue : { } } ) , } ) , { defaultValue : [ ] } ) , notify_when : schema . string ( { validate : validateNotifyWhenType } ) , } ) ; const rewriteBodyReq = ( result ) => { const { notify_when : notifyWhen , ... rest } = result . data ; return { ... result , data : { ... rest , notifyWhen , } , } ; } ; const rewriteBodyRes = ( { actions , alertTypeId , scheduledTaskId , createdBy , updatedBy , createdAt , updatedAt , apiKeyOwner , notifyWhen , muteAll , mutedInstanceIds , executionStatus , ... rest } ) => ( { ... rest , api_key_owner : apiKeyOwner , created_by : createdBy , updated_by : updatedBy , ... ( alertTypeId ? { rule_type_id : alertTypeId } : { } ) , ... ( scheduledTaskId ? { scheduled_task_id : scheduledTaskId } : { } ) , ... ( createdAt ? { created_at : createdAt } : { } ) , ... ( updatedAt ? { updated_at : updatedAt } : { } ) , ... ( notifyWhen ? { notify_when : notifyWhen } : { } ) , ... ( muteAll !== undefined ? { mute_all : muteAll } : { } ) , ... ( mutedInstanceIds ? { muted_alert_ids : mutedInstanceIds } : { } ) , ... ( executionStatus ? { execution_status : { status : executionStatus . status , last_execution_date : executionStatus . lastExecutionDate , last_duration : executionStatus . lastDuration , } , } : { } ) , ... ( actions ? { actions : actions . map ( ( { group , id , actionTypeId , params } ) => ( { group , id , params , connector_type_id : actionTypeId , } ) ) , } : { } ) , } ) ; export const updateRuleRoute = ( router , licenseState ) => { router . put ( { path : `${ BASE_ALERTING_API_PATH }/rule/{id}` , validate : { body : bodySchema , params : paramSchema , } , } , handleDisabledApiKeysError ( router . handleLegacyErrors ( verifyAccessAndContext ( licenseState , async function ( context , req , res ) { const rulesClient = context . alerting . getRulesClient ( ) ; const { id } = req . params ; const rule = req . body ; try { const alertRes = await rulesClient . update ( rewriteBodyReq ( { id , data : { ... rule , notify_when : rule . notify_when as AlertNotifyWhenType , } , } ) ) ; return res . ok ( { body : rewriteBodyRes ( alertRes ) , } ) ; } catch ( e ) { if ( e instanceof AlertTypeDisabledError ) { return e . sendResponse ( res ) ; } throw e ; } } ) ) ) ) ; } ;