import { ConnectionEventStatuses } from '@botonic/core' ; import { decode } from 'jsonwebtoken' ; import { createConnectionEvent , initChannelInformation } from '../../helpers' ; const initialBotState = { botId : '1234' , lastRoutePath : null , isFirstInteraction : true , retries : 0 , isHandoff : false , isShadowing : false , } ; export const doAuth = async ( { websocketId , data , send , dataProvider , eventHandlers = { } , } ) => { const { token } = JSON . parse ( data ) ; const { userId , idFromChannel , channel } = decode ( token ) ; let user = await dataProvider . getUser ( userId ) ; if ( ! user ) { const newUser = { id : userId , websocketId , isOnline : true , botState : initialBotState , session : { } , details : { } , ... initChannelInformation ( { idFromChannel , channel } ) , } ; user = await dataProvider . saveUser ( newUser ) ; if ( 'onNewUser' in eventHandlers ) { await eventHandlers . onNewUser ( { user , details : user } ) ; } } else { user = await dataProvider . updateUser ( { ... user , websocketId , } ) ; } await dataProvider . saveEvent ( createConnectionEvent ( { user , status : ConnectionEventStatuses . CONNECTED } ) ) ; } ;