@@ -8,13 +8,19 @@ const User = require('../models/UserModel')
88const mongoose = require ( 'mongoose' ) ;
99// const db = require('../databases/mongo')
1010
11- const MONGO_URI = ''
11+ const MONGO_URI = 'mongodb+srv://chronoslany:chronoslany@cluster0.tvzzzbv.mongodb.net/?retryWrites=true&w=majority' ;
1212
13- mongoose . connect ( MONGO_URI , {
14- useNewUrlParser : true ,
15- useUnifiedtopology : true ,
16- } )
13+ main ( ) . catch ( err => console . log ( err ) ) ;
14+ async function main ( ) {
15+ await mongoose . connect ( MONGO_URI ) ;
16+ console . log ( 'user info db connection established...' )
17+ }
18+ // mongoose.connect(MONGO_URI, {
19+ // useNewUrlParser: true,
20+ // useUnifiedtopology: true,
21+ // })
1722
23+ // may need to first check if db has a current user and if not, change it to guest
1824// GLOBAL VARIABLES
1925let currentUser = 'guest' ;
2026const settingsLocation = path . resolve ( __dirname , '../../settings.json' ) ;
@@ -67,6 +73,8 @@ function clearGuestSettings() {
6773 */
6874ipcMain . on ( 'addApp' , ( message : IpcMainEvent , application : any ) => {
6975 const newApp = JSON . parse ( application )
76+ console . log ( 'parsed newApp: ' , newApp ) ;
77+ console . log ( 'currentUser' , currentUser ) ;
7078 const createdOn = moment ( ) . format ( 'lll' ) ;
7179 newApp . push ( createdOn ) ;
7280
@@ -103,10 +111,33 @@ ipcMain.on('addApp', (message: IpcMainEvent, application: any) => {
103111
104112/**
105113 * @event addAwsApp
114+ * @param name, 'AWS', region, description, typeOfService, instanceID, accessKey, secretAccessKey, awsURL
106115 * @desc Adds an AWS application to the user's list in the settings.json with the provided fields
107116 * @return New list of applications
108117 */
109118ipcMain . on ( 'addAwsApp' , ( message : IpcMainEvent , application : any ) => {
119+
120+ const newAwsApp = JSON . parse ( application ) ;
121+ console . log ( 'parsed newApp: ' , newAwsApp ) ;
122+ console . log ( 'currentUser' , currentUser ) ;
123+ const createdOn = moment ( ) . format ( 'lll' ) ;
124+ newAwsApp . push ( createdOn ) ;
125+
126+ if ( currentUser !== 'guest' ) {
127+ return User . findOneAndUpdate ( { username : currentUser } , {
128+ $push : { services : newAwsApp }
129+ } , { new : true } )
130+ . then ( ( data ) => {
131+ console . log ( 'User updated' , data ) ;
132+ // returning each array element name, 'AWS', region, 'AWS/(instance)', Date
133+ message . returnValue = data . services . map ( ( arr : string [ ] ) => [ arr [ 0 ] , arr [ 1 ] , arr [ 2 ] , arr [ 4 ] , arr [ 5 ] ] )
134+ } )
135+ . catch ( ( error ) => {
136+ console . log ( `addAWSApp failed : ${ error } ` )
137+ } )
138+ } else {
139+ // if user is not guest, should not have to pull info from settings.json file
140+ console . log ( 'current user is a guest, data will be saved locally...' )
110141 // Retrieves file contents from settings.json
111142 const settings = JSON . parse ( fs . readFileSync ( settingsLocation ) . toString ( 'utf8' ) ) ;
112143 const services = settings [ currentUser ] . services ;
@@ -115,10 +146,10 @@ ipcMain.on('addAwsApp', (message: IpcMainEvent, application: any) => {
115146 // name, instance, region, description, typeOfService, accessKey, secretAccessKey
116147
117148 // Add new applicaiton to list
118- const newAwsApp = JSON . parse ( application ) ;
149+ // const newAwsApp = JSON.parse(application);
119150
120151 // Add a creation date to the application on the 5th index
121- const createdOn = moment ( ) . format ( 'lll' ) ;
152+ // const createdOn = moment().format('lll');
122153 newAwsApp . splice ( 5 , 0 , createdOn ) ;
123154
124155 // Add app to list of applications
@@ -130,6 +161,7 @@ ipcMain.on('addAwsApp', (message: IpcMainEvent, application: any) => {
130161
131162 // Sync event - return new applications list
132163 message . returnValue = services . map ( ( arr : string [ ] ) => [ arr [ 0 ] , arr [ 1 ] , arr [ 2 ] , arr [ 4 ] , arr [ 5 ] ] ) ;
164+ }
133165} ) ;
134166
135167/**
0 commit comments