import { Connection } from '../connection/ConnectionInterface' ; import { createBasicMessage } from './BasicMessageMessages' ; import { InboundMessage } from '../../utils/Types' ; import { RecordType , sendOutboundMessage } from '../../utils/Helpers' ; import { WalletConfig , WalletCredentials } from '../../wallet/WalletInterface' ; import WalletStorageService from '../../wallet/WalletStorageService' ; class BasicMessageService { description param { WalletConfig } configJson param { WalletCredentials } credentialsJson param { string } message param { string } connectionId return { * } memberof async send ( configJson , credentialsJson , message , connectionId ) { try { const query = { connectionId : connectionId } ; const connection = await WalletStorageService . getWalletRecordFromQuery ( configJson , credentialsJson , RecordType . Connection , JSON . stringify ( query ) ) ; const basicMessage = createBasicMessage ( message ) ; const chatBody = { type : 'sent' , message , time : new Date ( ) . toISOString ( ) , } ; const basicMessageTags = { connectionId : connectionId , lastUpdatedAt : new Date ( ) . toISOString ( ) } ; const chatThread = await WalletStorageService . getWalletRecordFromQuery ( configJson , credentialsJson , RecordType . BasicMessage , JSON . stringify ( query ) ) ; if ( chatThread === undefined || chatThread === null || chatThread . length == 0 ) { await WalletStorageService . addWalletRecord ( configJson , credentialsJson , RecordType . BasicMessage , connectionId , JSON . stringify ( [ chatBody ] ) , JSON . stringify ( basicMessageTags ) ) ; } else { chatThread . push ( chatBody ) ; await WalletStorageService . updateWalletRecord ( configJson , credentialsJson , RecordType . BasicMessage , connectionId , JSON . stringify ( chatThread ) , JSON . stringify ( basicMessageTags ) ) ; } setTimeout ( async ( ) => { await sendOutboundMessage ( configJson , credentialsJson , connection , basicMessage ) ; } , 50 ) ; return true ; } catch ( error ) { console . log ( 'Basic message - Send message error = ' , error ) ; throw error ; } } description param { WalletConfig } configJson param { WalletCredentials } credentialsJson param { InboundMessage } inboundMessage param { string } connectionId return { * } memberof async save ( configJson , credentialsJson , inboundMessage , connectionId ) { try { const { message } = inboundMessage ; const chatBody = { type : 'receive' , message : JSON . parse ( message ) . content , time : JSON . parse ( message ) . sent_time , } ; const query = { connectionId : connectionId } ; const chatThread = await WalletStorageService . getWalletRecordFromQuery ( configJson , credentialsJson , RecordType . BasicMessage , JSON . stringify ( query ) ) ; const connection = await WalletStorageService . getWalletRecordFromQuery ( configJson , credentialsJson , RecordType . Connection , JSON . stringify ( query ) ) ; const basicMessageTags = { connectionId : connectionId , lastUpdatedAt : new Date ( ) . toISOString ( ) } ; if ( chatThread . length == 0 ) { await WalletStorageService . addWalletRecord ( configJson , credentialsJson , RecordType . BasicMessage , connectionId , JSON . stringify ( [ chatBody ] ) , JSON . stringify ( basicMessageTags ) ) ; } else { chatThread . push ( chatBody ) ; await WalletStorageService . updateWalletRecord ( configJson , credentialsJson , RecordType . BasicMessage , connectionId , JSON . stringify ( chatThread ) , JSON . stringify ( basicMessageTags ) ) ; } return connection ; } catch ( error ) { console . log ( 'Basic message - Save message error = ' , error ) ; throw ( error ) ; } } } export default new BasicMessageService ( ) ;