import { wrapError } from '../client/error_wrapper' ; import { RouteInitialization } from '../types' ; import { createFilterSchema , filterIdSchema , updateFilterSchema } from './schemas/filters_schema' ; import { FilterManager , FormFilter , UpdateFilter } from '../models/filter' ; import type { MlClient } from '../lib/ml_client' ; function getAllFilters ( mlClient ) { const mgr = new FilterManager ( mlClient ) ; return mgr . getAllFilters ( ) ; } function getAllFilterStats ( mlClient ) { const mgr = new FilterManager ( mlClient ) ; return mgr . getAllFilterStats ( ) ; } function getFilter ( mlClient , filterId ) { const mgr = new FilterManager ( mlClient ) ; return mgr . getFilter ( filterId ) ; } function newFilter ( mlClient , filter ) { const mgr = new FilterManager ( mlClient ) ; return mgr . newFilter ( filter ) ; } function updateFilter ( mlClient , filterId , filter ) { const mgr = new FilterManager ( mlClient ) ; return mgr . updateFilter ( filterId , filter ) ; } function deleteFilter ( mlClient , filterId ) { const mgr = new FilterManager ( mlClient ) ; return mgr . deleteFilter ( filterId ) ; } export function filtersRoutes ( { router , routeGuard } ) { apiGroup api apiName apiDescription apiSuccess apiSuccess router . get ( { path : '/api/ml/filters' , validate : false , options : { tags : [ 'access:ml:canGetFilters' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { mlClient , response } ) => { try { const resp = await getAllFilters ( mlClient ) ; return response . ok ( { body : resp , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema apiSuccess apiSuccess router . get ( { path : '/api/ml/filters/{filterId}' , validate : { params : filterIdSchema , } , options : { tags : [ 'access:ml:canGetFilters' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { mlClient , request , response } ) => { try { const resp = await getFilter ( mlClient , request . params . filterId ) ; return response . ok ( { body : resp , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema apiSuccess apiSuccess router . put ( { path : '/api/ml/filters' , validate : { body : createFilterSchema , } , options : { tags : [ 'access:ml:canCreateFilter' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { mlClient , request , response } ) => { try { const body = request . body ; const resp = await newFilter ( mlClient , body ) ; return response . ok ( { body : resp , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema apiSchema apiSuccess apiSuccess router . put ( { path : '/api/ml/filters/{filterId}' , validate : { params : filterIdSchema , body : updateFilterSchema , } , options : { tags : [ 'access:ml:canCreateFilter' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { mlClient , request , response } ) => { try { const { filterId } = request . params ; const body = request . body ; const resp = await updateFilter ( mlClient , filterId , body ) ; return response . ok ( { body : resp , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema router . delete ( { path : '/api/ml/filters/{filterId}' , validate : { params : filterIdSchema , } , options : { tags : [ 'access:ml:canDeleteFilter' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { mlClient , request , response } ) => { try { const { filterId } = request . params ; const resp = await deleteFilter ( mlClient , filterId ) ; return response . ok ( { body : resp , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSuccess apiSuccess router . get ( { path : '/api/ml/filters/_stats' , validate : false , options : { tags : [ 'access:ml:canGetFilters' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { mlClient , response } ) => { try { const resp = await getAllFilterStats ( mlClient ) ; return response . ok ( { body : resp , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; }