@@ -16,6 +16,10 @@ import { fetchData } from './dataHelpers';
1616import MetricsModel from '../models/MetricsModel' ;
1717const log = require ( 'electron-log' ) ;
1818
19+
20+ const mongoose = require ( 'mongoose' ) ;
21+ const User = require ( '../models/UserModel' )
22+
1923const mongoFetch = fetchData . mongoFetch ;
2024const postgresFetch = fetchData . postgresFetch ;
2125const AWS = require ( 'aws-sdk' ) ;
@@ -37,24 +41,60 @@ const settingsLocation = path.resolve(__dirname, '../../settings.json');
3741 * @desc Connects user to database and sets global currentDatabaseType which
3842 * is accessed in info.commsData and info.healthData
3943 */
40- ipcMain . on ( 'connect' , async ( message : Electron . IpcMainEvent , username : string , index : number ) => {
44+ ipcMain . on ( 'connect' , async ( message : Electron . IpcMainEvent , username : string , index : number , URI : string ) => {
4145 try {
4246 // Extract databaseType and URI from settings.json at particular index
4347 // get index from application context
44- const fileContents = JSON . parse ( fs . readFileSync ( settingsLocation , 'utf8' ) ) ;
45- const userDatabase = fileContents [ username ] . services [ index ] ;
46- // We get index from sidebar container: which is the mapplication (DEMO)
47- const [ databaseType , URI ] = [ userDatabase [ 1 ] , userDatabase [ 2 ] ] ;
4848
49- // Connect to the proper database
50- if ( databaseType === 'MongoDB' ) await connectMongo ( index , URI ) ;
51- if ( databaseType === 'SQL' ) pool = await connectPostgres ( index , URI ) ;
49+ // Connect to User database instantiated in 'dashboard.ts'
50+ if ( username !== 'guest' ) {
51+
52+ const MONGO_URI = URI
53+ mongoose . connect ( MONGO_URI , {
54+ useNewUrlParser : true ,
55+ useUnifiedtopology : true ,
56+ } )
57+
58+ // Check for existing user in DB, if found, connect to load application based on database type
59+ return User . findOne ( { username : username } )
60+ . then ( async ( data ) => {
61+ const databaseType = data . services [ index ] [ 1 ]
62+ const appURI = data . services [ index ] [ 2 ]
63+ console . log ( 'database type' , databaseType )
64+ if ( databaseType === 'MongoDB' ) {
65+ await connectMongo ( index , appURI )
66+ currentDatabaseType = databaseType ;
67+ message . sender . send ( 'databaseConnected' , 'connected!' ) ;
68+ } else if ( databaseType === 'SQL' ) {
69+ pool = await connectPostgres ( index , appURI ) ;
70+ currentDatabaseType = databaseType ;
71+ message . sender . send ( 'databaseConnected' , 'connected!' ) ;
72+ }
73+ } )
74+ . catch ( ( error ) => {
75+ console . log ( ` Error in connect, failed to load application : ${ error } ` )
76+ // return false;
77+ } )
78+ }
5279
53- // Currently set to a global variable
54- currentDatabaseType = databaseType ;
80+ //LOCAL INSTANCE: SETTINGS.JSON
81+ else {
5582
56- message . sender . send ( 'databaseConnected' , 'connected!' ) ;
57- // eslint-disable-next-line no-shadow
83+ const fileContents = JSON . parse ( fs . readFileSync ( settingsLocation , 'utf8' ) ) ;
84+ const userDatabase = fileContents [ username ] . services [ index ] ;
85+ // We get index from sidebar container: which is the mapplication (DEMO)
86+ const [ databaseType , URI ] = [ userDatabase [ 1 ] , userDatabase [ 2 ] ] ;
87+
88+ // Connect to the proper database
89+ if ( databaseType === 'MongoDB' ) await connectMongo ( index , URI ) ;
90+ if ( databaseType === 'SQL' ) pool = await connectPostgres ( index , URI ) ;
91+
92+ // Currently set to a global variable
93+ currentDatabaseType = 'MongoDB' ;
94+
95+ message . sender . send ( 'databaseConnected' , 'connected!' ) ;
96+ // eslint-disable-next-line no-shadow
97+ }
5898 } catch ( { message } ) {
5999 console . log ( 'Error in "connect" event' , message ) ;
60100 }
0 commit comments