@@ -8,7 +8,7 @@ const User = require('../models/UserModel')
88const mongoose = require ( 'mongoose' ) ;
99// const db = require('../databases/mongo')
1010
11- const MONGO_URI = 'mongodb+srv://wiris316:testtesttest123@cluster0.0gybpkf.mongodb.net/?retryWrites=true&w=majority '
11+ const MONGO_URI = ''
1212
1313mongoose . connect ( MONGO_URI , {
1414 useNewUrlParser : true ,
@@ -79,26 +79,60 @@ function clearGuestSettings() {
7979 * @return New list of applications
8080 */
8181ipcMain . on ( 'addApp' , ( message : IpcMainEvent , application : any ) => {
82- // Retrieves file contents from settings.json
83- const settings = JSON . parse ( fs . readFileSync ( settingsLocation ) . toString ( 'utf8' ) ) ;
84- const services = settings [ currentUser ] . services ;
82+ // // Retrieves file contents from settings.json
83+ // const settings = JSON.parse(fs.readFileSync(settingsLocation).toString('utf8'));
84+ // const services = settings[currentUser].services;
8585
86- // Add new applicaiton to list
87- const newApp = JSON . parse ( application ) ;
86+ // // Add new applicaiton to list
87+ // const newApp = JSON.parse(application);
88+
89+ // // Add a creation date to the application
90+ // const createdOn = moment().format('lll');
91+ // newApp.push(createdOn);
92+
93+ // // Add app to list of applications
94+ // services.push(newApp);
95+
96+ // // Update settings.json with new list
97+ // fs.writeFileSync(settingsLocation, JSON.stringify(settings, null, '\t'));
8898
89- // Add a creation date to the application
99+ // // Sync event - return new applications list
100+
101+ // message.returnValue = services.map((arr: string[]) => [arr[0], arr[1], arr[3], arr[4], arr[5]]);
102+
103+ const newApp = JSON . parse ( application )
104+ console . log ( 'newApp here' , newApp )
105+ console . log ( 'current user' , currentUser )
90106 const createdOn = moment ( ) . format ( 'lll' ) ;
91107 newApp . push ( createdOn ) ;
92108
93- // Add app to list of applications
94- services . push ( newApp ) ;
109+ if ( currentUser === 'guest' ) {
110+ const settings = JSON . parse ( fs . readFileSync ( settingsLocation ) . toString ( 'utf8' ) ) ;
111+ const services = settings [ currentUser ] . services ;
95112
96- // Update settings.json with new list
97- fs . writeFileSync ( settingsLocation , JSON . stringify ( settings , null , '\t' ) ) ;
113+ // Add app to list of applications
114+ services . push ( newApp ) ;
98115
99- // Sync event - return new applications list
116+ // Update settings.json with new list
117+ fs . writeFileSync ( settingsLocation , JSON . stringify ( settings , null , '\t' ) ) ;
100118
101- message . returnValue = services . map ( ( arr : string [ ] ) => [ arr [ 0 ] , arr [ 1 ] , arr [ 3 ] , arr [ 4 ] , arr [ 5 ] ] ) ;
119+ // Sync event - return new applications list
120+ message . returnValue = services . map ( ( arr : string [ ] ) => [ arr [ 0 ] , arr [ 1 ] , arr [ 3 ] , arr [ 4 ] , arr [ 5 ] ] ) ;
121+ } else {
122+ console . log ( 'not guest' )
123+ return User . findOneAndUpdate ( { username : currentUser } , {
124+ $push : { services : newApp }
125+ } )
126+ . then ( ( data ) => {
127+ console . log ( 'User found' , data ) ;
128+ console . log ( 'services here' , data . services )
129+ message . returnValue = data . services . map ( ( arr :string [ ] ) => [ ...arr ] )
130+ } )
131+ . catch ( ( error ) => {
132+ console . log ( `addApp failed : ${ error } ` )
133+ // return false;
134+ } )
135+ }
102136} ) ;
103137
104138/**
@@ -129,7 +163,7 @@ ipcMain.on('addAwsApp', (message: IpcMainEvent, application: any) => {
129163 fs . writeFileSync ( settingsLocation , JSON . stringify ( settings , null , '\t' ) ) ;
130164
131165 // Sync event - return new applications list
132- message . returnValue = services . map ( ( arr : string [ ] ) => [ arr [ 0 ] , arr [ 1 ] , arr [ 3 ] , arr [ 4 ] , arr [ 5 ] ] ) ;
166+ message . returnValue = services . map ( ( arr : string [ ] ) => [ arr [ 0 ] , arr [ 1 ] , arr [ 2 ] , arr [ 4 ] , arr [ 5 ] ] ) ;
133167} ) ;
134168
135169/**
@@ -142,17 +176,47 @@ ipcMain.on('getApps', (message: IpcMainEvent) => {
142176 // Retrieves file contents from settings.json for current Apps
143177 const settings = JSON . parse ( fs . readFileSync ( settingsLocation ) . toString ( 'utf8' ) ) ;
144178 // const services: string[][] = settings[currentUser].services;
145- const services : string [ ] [ ] = settings [ 'guest' ] . services ; // temporarily set to guests at every login attempt
146-
147- // Return an array of arrays that is a subset of the full services array
148- const dashboardList : string [ ] [ ] = services . map ( ( arr : string [ ] ) => [
149- arr [ 0 ] ,
150- arr [ 1 ] ,
151- arr [ 3 ] ,
152- arr [ 4 ] ,
153- arr [ 5 ] ,
154- ] ) ;
155- message . returnValue = dashboardList ;
179+ let services : string [ ] [ ] = settings [ 'guest' ] . services ; // temporarily set to guests at every login attempt
180+
181+ if ( currentUser === 'guest' ) {
182+ services = settings [ 'guest' ] . services
183+ const dashboardList : string [ ] [ ] = services . map ( ( arr : string [ ] ) => [
184+ arr [ 0 ] ,
185+ arr [ 1 ] ,
186+ arr [ 3 ] ,
187+ arr [ 4 ] ,
188+ arr [ 5 ] ,
189+ ] ) ;
190+ message . returnValue = dashboardList ;
191+ } else {
192+ return User . findOne ( { username : currentUser } )
193+ . then ( ( data ) => {
194+ console . log ( 'User found' , data ) ;
195+ services = data . services ;
196+ const dashboardList : string [ ] [ ] = services . map ( ( arr : string [ ] ) => [
197+ arr [ 0 ] ,
198+ arr [ 1 ] ,
199+ arr [ 3 ] ,
200+ arr [ 4 ] ,
201+ arr [ 5 ] ,
202+ ] ) ;
203+ message . returnValue = dashboardList ;
204+ } )
205+ . catch ( ( error ) => {
206+ console . log ( `checkUser failed : ${ error } ` )
207+ // return false;
208+ } )
209+ }
210+
211+ // // Return an array of arrays that is a subset of the full services array
212+ // const dashboardList: string[][] = services.map((arr: string[]) => [
213+ // arr[0],
214+ // arr[1],
215+ // arr[3],
216+ // arr[4],
217+ // arr[5],
218+ // ]);
219+ // message.returnValue = dashboardList;
156220} ) ;
157221
158222/**
0 commit comments