import { logManager } from '../log-manager' ; import { Setting } from '../models/Setting' ; import { TrackItem } from '../models/TrackItem' ; export class SettingsService { logger = logManager . getLogger ( 'SettingsService' ) ; cache : any = { } ; async findCreateFind ( name ) { return Setting . query ( ) . where ( 'name' , name ) . then ( function ( rows ) { if ( rows . length === 0 ) { return Setting . query ( ) . insert ( { name } ) ; } else { return rows [ 0 ] ; } } ) ; } async findByName ( name ) { if ( this . cache [ name ] ) { return this . cache [ name ] ; } const item = await this . findCreateFind ( name ) ; this . cache [ name ] = item ; return item ; } async updateByName ( name , jsonDataStr ) { this . logger . debug ( 'Updating Setting:' , name , jsonDataStr ) ; try { const jsonData = JSON . parse ( jsonDataStr ) ; let item = await this . findByName ( name ) ; if ( item ) { const savedItem = await item . $query ( ) . patchAndFetch ( { jsonData } ) ; delete this . cache [ name ] ; return savedItem ; } else { this . logger . error ( `No item with ${ name } found to update.` ) ; } } catch ( e ) { this . logger . error ( 'Parsing jsonData failed:' , e , jsonDataStr ) ; } } async fetchWorkSettings ( ) { let item = await this . findByName ( 'WORK_SETTINGS' ) ; if ( ! item || ! item . jsonData ) { return { } ; } return item . jsonData ; } async fetchWorkSettingsJsonString ( ) { const data = await this . fetchWorkSettings ( ) ; return JSON . stringify ( data ) ; } isObject ( val ) { return val instanceof Object ; } async fetchAnalyserSettings ( ) { let item = await this . findByName ( 'ANALYSER_SETTINGS' ) ; if ( ! item || ! Array . isArray ( item . jsonData ) ) { return [ ] ; } return item . jsonData ; } async fetchAnalyserSettingsJsonString ( ) { const data = await this . fetchAnalyserSettings ( ) ; return JSON . stringify ( data ) ; } async getRunningLogItemAsJson ( ) { let settingsItem = await this . findByName ( 'RUNNING_LOG_ITEM' ) ; if ( settingsItem && settingsItem . jsonData && settingsItem . jsonData . id ) { let logItem = await TrackItem . query ( ) . findById ( settingsItem . jsonData . id ) ; if ( ! logItem ) { this . logger . error ( `No Track item found by pk: ${ settingsItem . jsonData . id }` ) ; return null ; } return logItem . toJSON ( ) ; } return null ; } async saveRunningLogItemReference ( logItemId ) { const item = await this . updateByName ( 'RUNNING_LOG_ITEM' , JSON . stringify ( { id : logItemId } ) ) ; this . logger . debug ( 'Updated RUNNING_LOG_ITEM!' , logItemId ) ; return logItemId ; } } export const settingsService = new SettingsService ( ) ;