@@ -16,6 +16,10 @@ import { fetchData } from './dataHelpers';
16
16
import MetricsModel from '../models/MetricsModel' ;
17
17
const log = require ( 'electron-log' ) ;
18
18
19
+
20
+ const mongoose = require ( 'mongoose' ) ;
21
+ const User = require ( '../models/UserModel' )
22
+
19
23
const mongoFetch = fetchData . mongoFetch ;
20
24
const postgresFetch = fetchData . postgresFetch ;
21
25
const AWS = require ( 'aws-sdk' ) ;
@@ -37,24 +41,60 @@ const settingsLocation = path.resolve(__dirname, '../../settings.json');
37
41
* @desc Connects user to database and sets global currentDatabaseType which
38
42
* is accessed in info.commsData and info.healthData
39
43
*/
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 ) => {
41
45
try {
42
46
// Extract databaseType and URI from settings.json at particular index
43
47
// 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 ] ] ;
48
48
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
+ }
52
79
53
- // Currently set to a global variable
54
- currentDatabaseType = databaseType ;
80
+ //LOCAL INSTANCE: SETTINGS.JSON
81
+ else {
55
82
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
+ }
58
98
} catch ( { message } ) {
59
99
console . log ( 'Error in "connect" event' , message ) ;
60
100
}
0 commit comments