@@ -41,19 +41,6 @@ function hashPassword(password: string) {
41
41
return bcrypt . hashSync ( password , salt ) ;
42
42
}
43
43
44
- // function checkUser(username): any {
45
- // const userExist = User.findOne({ username })
46
- // .then((data) => {
47
- // console.log('User found', data);
48
- // return true;
49
- // })
50
- // .catch((error) => {
51
- // console.log(`checkUser failed : ${error}`)
52
- // // return false;
53
- // })
54
- // // console.log('heeeeere', userExist)
55
- // }
56
-
57
44
function addUser ( username , password , email ) {
58
45
console . log ( 'inside addUser' , username )
59
46
const newUser = new User ( { username : username , password : hashPassword ( password ) , email : email } )
@@ -79,34 +66,13 @@ function clearGuestSettings() {
79
66
* @return New list of applications
80
67
*/
81
68
ipcMain . 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;
85
-
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'));
98
-
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
69
const newApp = JSON . parse ( application )
104
- console . log ( 'newApp here' , newApp )
105
- console . log ( 'current user' , currentUser )
106
70
const createdOn = moment ( ) . format ( 'lll' ) ;
107
71
newApp . push ( createdOn ) ;
108
72
73
+ //If currentUser is guest, add services to local instance (settings.json)
109
74
if ( currentUser === 'guest' ) {
75
+ // Retrieves file contents from settings.json
110
76
const settings = JSON . parse ( fs . readFileSync ( settingsLocation ) . toString ( 'utf8' ) ) ;
111
77
const services = settings [ currentUser ] . services ;
112
78
@@ -117,20 +83,20 @@ ipcMain.on('addApp', (message: IpcMainEvent, application: any) => {
117
83
fs . writeFileSync ( settingsLocation , JSON . stringify ( settings , null , '\t' ) ) ;
118
84
119
85
// Sync event - return new applications list
120
- message . returnValue = services . map ( ( arr : string [ ] ) => [ arr [ 0 ] , arr [ 1 ] , arr [ 3 ] , arr [ 4 ] , arr [ 5 ] ] ) ;
86
+ message . returnValue = services . map ( ( arr : string [ ] ) => [ ...arr ] ) ;
87
+
88
+ // Else currentUser is not guest, find user in DB and add app to list of applications
121
89
} else {
122
90
console . log ( 'not guest' )
123
91
return User . findOneAndUpdate ( { username : currentUser } , {
124
92
$push : { services : newApp }
125
- } )
93
+ } , { new : true } )
126
94
. then ( ( data ) => {
127
- console . log ( 'User found' , data ) ;
128
- console . log ( 'services here' , data . services )
129
- message . returnValue = data . services . map ( ( arr :string [ ] ) => [ ...arr ] )
95
+ console . log ( 'User updated' , data ) ;
96
+ message . returnValue = data . services . map ( ( arr ) => [ ...arr ] )
130
97
} )
131
98
. catch ( ( error ) => {
132
99
console . log ( `addApp failed : ${ error } ` )
133
- // return false;
134
100
} )
135
101
}
136
102
} ) ;
0 commit comments