import uuid from 'uuid' ; import { SavedObject , SavedObjectsFindResponse , SavedObjectsUpdateResponse } from 'kibana/server' ; import { CommentsArray , CreateComment , CreateCommentsArray , CreateExceptionListItemSchema , ExceptionListItemSchema , ExceptionListSchema , FoundExceptionListItemSchema , FoundExceptionListSchema , UpdateCommentsArrayOrUndefined , UpdateExceptionListItemSchema , exceptionListItemType , exceptionListType , } from '@kbn/securitysolution-io-ts-list-types' ; import { getExceptionListType } from '@kbn/securitysolution-list-utils' ; import { ExceptionListSoSchema } from '../../../schemas/saved_objects' ; import { CreateExceptionListItemOptions , UpdateExceptionListItemOptions , } from '../exception_list_client_types' ; export { validateData } from './validate_data' ; export const transformSavedObjectToExceptionList = ( { savedObject , } ) : ExceptionListSchema => { const dateNow = new Date ( ) . toISOString ( ) ; const { version : _version , attributes : { created_at , created_by , description , immutable , list_id , meta , name , os_types , tags , tie_breaker_id , type , updated_by , version , } , id , updated_at : updatedAt , } = savedObject ; return { _version , created_at , created_by , description , id , immutable : immutable ?? false , list_id , meta , name , namespace_type : getExceptionListType ( { savedObjectType : savedObject . type } ) , os_types , tags , tie_breaker_id , type : exceptionListType . is ( type ) ? type : 'detection' , updated_at : updatedAt ?? dateNow , updated_by , version : version ?? 1 , } ; } ; export const transformSavedObjectUpdateToExceptionList = ( { exceptionList , savedObject , } ) : ExceptionListSchema => { const dateNow = new Date ( ) . toISOString ( ) ; const { version : _version , attributes : { description , immutable , meta , name , os_types : osTypes , tags , type , updated_by : updatedBy , version , } , id , updated_at : updatedAt , } = savedObject ; return { _version , created_at : exceptionList . created_at , created_by : exceptionList . created_by , description : description ?? exceptionList . description , id , immutable : immutable ?? exceptionList . immutable , list_id : exceptionList . list_id , meta : meta ?? exceptionList . meta , name : name ?? exceptionList . name , namespace_type : getExceptionListType ( { savedObjectType : savedObject . type } ) , os_types : osTypes ?? exceptionList . os_types , tags : tags ?? exceptionList . tags , tie_breaker_id : exceptionList . tie_breaker_id , type : exceptionListType . is ( type ) ? type : exceptionList . type , updated_at : updatedAt ?? dateNow , updated_by : updatedBy ?? exceptionList . updated_by , version : version ?? exceptionList . version , } ; } ; export const transformSavedObjectToExceptionListItem = ( { savedObject , } ) : ExceptionListItemSchema => { const dateNow = new Date ( ) . toISOString ( ) ; const { version : _version , attributes : { comments , created_at , created_by , description , entries , item_id : itemId , list_id , meta , name , os_types , tags , tie_breaker_id , type , updated_by , } , id , updated_at : updatedAt , } = savedObject ; return { _version , comments : comments ?? [ ] , created_at , created_by , description , entries : entries ?? [ ] , id , item_id : itemId ?? '(unknown)' , list_id , meta , name , namespace_type : getExceptionListType ( { savedObjectType : savedObject . type } ) , os_types , tags , tie_breaker_id , type : exceptionListItemType . is ( type ) ? type : 'simple' , updated_at : updatedAt ?? dateNow , updated_by , } ; } ; export const transformSavedObjectUpdateToExceptionListItem = ( { exceptionListItem , savedObject , } ) : ExceptionListItemSchema => { const dateNow = new Date ( ) . toISOString ( ) ; const { version : _version , attributes : { comments , description , entries , meta , name , os_types : osTypes , tags , type , updated_by : updatedBy , } , id , updated_at : updatedAt , } = savedObject ; return { _version , comments : comments ?? exceptionListItem . comments , created_at : exceptionListItem . created_at , created_by : exceptionListItem . created_by , description : description ?? exceptionListItem . description , entries : entries ?? exceptionListItem . entries , id , item_id : exceptionListItem . item_id , list_id : exceptionListItem . list_id , meta : meta ?? exceptionListItem . meta , name : name ?? exceptionListItem . name , namespace_type : getExceptionListType ( { savedObjectType : savedObject . type } ) , os_types : osTypes ?? exceptionListItem . os_types , tags : tags ?? exceptionListItem . tags , tie_breaker_id : exceptionListItem . tie_breaker_id , type : exceptionListItemType . is ( type ) ? type : exceptionListItem . type , updated_at : updatedAt ?? dateNow , updated_by : updatedBy ?? exceptionListItem . updated_by , } ; } ; export const transformSavedObjectsToFoundExceptionListItem = ( { savedObjectsFindResponse , } ) : FoundExceptionListItemSchema => { return { data : savedObjectsFindResponse . saved_objects . map ( ( savedObject ) => transformSavedObjectToExceptionListItem ( { savedObject } ) ) , page : savedObjectsFindResponse . page , per_page : savedObjectsFindResponse . per_page , total : savedObjectsFindResponse . total , } ; } ; export const transformSavedObjectsToFoundExceptionList = ( { savedObjectsFindResponse , } ) : FoundExceptionListSchema => { return { data : savedObjectsFindResponse . saved_objects . map ( ( savedObject ) => transformSavedObjectToExceptionList ( { savedObject } ) ) , page : savedObjectsFindResponse . page , per_page : savedObjectsFindResponse . per_page , total : savedObjectsFindResponse . total , } ; } ; export const transformUpdateCommentsToComments = ( { comments , existingComments , user , } ) : CommentsArray => { const incomingComments = comments ?? [ ] ; const newComments = incomingComments . filter ( ( comment ) => comment . id == null ) ; const newCommentsFormatted = transformCreateCommentsToComments ( { incomingComments : newComments , user , } ) ; return [ ... existingComments , ... newCommentsFormatted ] ; } ; export const transformCreateCommentsToComments = ( { incomingComments , user , } ) : CommentsArray => { const dateNow = new Date ( ) . toISOString ( ) ; return incomingComments . map ( ( comment ) => ( { comment : comment . comment , created_at : dateNow , created_by : user , id : uuid . v4 ( ) , } ) ) ; } ; export const transformCreateExceptionListItemOptionsToCreateExceptionListItemSchema = ( { listId , itemId , namespaceType , osTypes , ... rest } ) : CreateExceptionListItemSchema => { return { ... rest , item_id : itemId , list_id : listId , namespace_type : namespaceType , os_types : osTypes , } ; } ; export const transformUpdateExceptionListItemOptionsToUpdateExceptionListItemSchema = ( { itemId , namespaceType , osTypes , description = '' , name = '' , type = 'simple' , ... rest } ) : UpdateExceptionListItemSchema => { return { ... rest , description , item_id : itemId , name , namespace_type : namespaceType , os_types : osTypes , type , } ; } ;