@@ -186,20 +186,48 @@ ipcMain.on('getApps', (message: IpcMainEvent) => {
186186 * @return Returns the new list of applications
187187 */
188188ipcMain . on ( 'deleteApp' , ( message : IpcMainEvent , index ) => {
189- // Retrives file contents from settings.json
190- const settings = JSON . parse ( fs . readFileSync ( settingsLocation ) . toString ( 'utf8' ) ) ;
191- const userServices = settings [ currentUser ] . services ;
192189
193- // Remove application from settings.json
194- userServices . splice ( index , 1 ) ;
190+ if ( currentUser === 'guest' ) {
195191
196- // Update settings.json with new list
197- fs . writeFileSync ( settingsLocation , JSON . stringify ( settings , null , '\t' ) , {
198- encoding : 'utf8' ,
199- } ) ;
192+ // Retrives file contents from settings.json
193+ const settings = JSON . parse ( fs . readFileSync ( settingsLocation ) . toString ( 'utf8' ) ) ;
194+ const userServices = settings [ currentUser ] . services ;
195+
196+ // Remove application from settings.json
197+ userServices . splice ( index , 1 ) ;
198+
199+ // Update settings.json with new list
200+ fs . writeFileSync ( settingsLocation , JSON . stringify ( settings , null , '\t' ) , {
201+ encoding : 'utf8' ,
202+ } ) ;
203+
204+ // Sync event - return new applications list
205+ message . returnValue = userServices . map ( ( arr : string [ ] ) => [ arr [ 0 ] , arr [ 1 ] , arr [ 3 ] , arr [ 4 ] ] ) ;
206+ }
200207
201- // Sync event - return new applications list
202- message . returnValue = userServices . map ( ( arr : string [ ] ) => [ arr [ 0 ] , arr [ 1 ] , arr [ 3 ] , arr [ 4 ] ] ) ;
208+ else {
209+ console . log ( 'not guest' )
210+ return User . findOne ( { username : currentUser } )
211+ . then ( ( data ) => {
212+ console . log ( 'User found' , data ) ;
213+ const service = data . services [ index ] ;
214+ return User . findOneAndUpdate ( { username : currentUser } , {
215+ $pull : { services : service }
216+ } , { new : true } )
217+ . then ( ( data ) => {
218+ console . log ( 'Service deleted' , data ) ;
219+ message . returnValue = data . services . map ( ( arr ) => [ ...arr ] )
220+ } )
221+ . catch ( ( error ) => {
222+ console . log ( `addApp failed : ${ error } ` )
223+ } )
224+ } )
225+ . catch ( ( error ) => {
226+ console . log ( `checkUser failed : ${ error } ` )
227+ // return false;
228+ } )
229+
230+ }
203231} ) ;
204232
205233/**
0 commit comments