@@ -8,13 +8,19 @@ const User = require('../models/UserModel')
8
8
const mongoose = require ( 'mongoose' ) ;
9
9
// const db = require('../databases/mongo')
10
10
11
- const MONGO_URI = ''
11
+ const MONGO_URI = 'mongodb+srv://chronoslany:[email protected] /?retryWrites=true&w=majority' ;
12
12
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
+ // })
17
22
23
+ // may need to first check if db has a current user and if not, change it to guest
18
24
// GLOBAL VARIABLES
19
25
let currentUser = 'guest' ;
20
26
const settingsLocation = path . resolve ( __dirname , '../../settings.json' ) ;
@@ -67,6 +73,8 @@ function clearGuestSettings() {
67
73
*/
68
74
ipcMain . on ( 'addApp' , ( message : IpcMainEvent , application : any ) => {
69
75
const newApp = JSON . parse ( application )
76
+ console . log ( 'parsed newApp: ' , newApp ) ;
77
+ console . log ( 'currentUser' , currentUser ) ;
70
78
const createdOn = moment ( ) . format ( 'lll' ) ;
71
79
newApp . push ( createdOn ) ;
72
80
@@ -103,10 +111,33 @@ ipcMain.on('addApp', (message: IpcMainEvent, application: any) => {
103
111
104
112
/**
105
113
* @event addAwsApp
114
+ * @param name, 'AWS', region, description, typeOfService, instanceID, accessKey, secretAccessKey, awsURL
106
115
* @desc Adds an AWS application to the user's list in the settings.json with the provided fields
107
116
* @return New list of applications
108
117
*/
109
118
ipcMain . 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...' )
110
141
// Retrieves file contents from settings.json
111
142
const settings = JSON . parse ( fs . readFileSync ( settingsLocation ) . toString ( 'utf8' ) ) ;
112
143
const services = settings [ currentUser ] . services ;
@@ -115,10 +146,10 @@ ipcMain.on('addAwsApp', (message: IpcMainEvent, application: any) => {
115
146
// name, instance, region, description, typeOfService, accessKey, secretAccessKey
116
147
117
148
// Add new applicaiton to list
118
- const newAwsApp = JSON . parse ( application ) ;
149
+ // const newAwsApp = JSON.parse(application);
119
150
120
151
// Add a creation date to the application on the 5th index
121
- const createdOn = moment ( ) . format ( 'lll' ) ;
152
+ // const createdOn = moment().format('lll');
122
153
newAwsApp . splice ( 5 , 0 , createdOn ) ;
123
154
124
155
// Add app to list of applications
@@ -130,6 +161,7 @@ ipcMain.on('addAwsApp', (message: IpcMainEvent, application: any) => {
130
161
131
162
// Sync event - return new applications list
132
163
message . returnValue = services . map ( ( arr : string [ ] ) => [ arr [ 0 ] , arr [ 1 ] , arr [ 2 ] , arr [ 4 ] , arr [ 5 ] ] ) ;
164
+ }
133
165
} ) ;
134
166
135
167
/**
0 commit comments