Skip to content

Commit 70e824d

Browse files
committed
converted addApp to DB
Co-authored-by: Jon Cruz <[email protected]> Co-authored-by: Elena Atencio <[email protected]> Co-authored-by: John Donato <[email protected]>
1 parent a9c7ae3 commit 70e824d

File tree

1 file changed

+8
-42
lines changed

1 file changed

+8
-42
lines changed

electron/routes/dashboard.ts

Lines changed: 8 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,6 @@ function hashPassword(password: string) {
4141
return bcrypt.hashSync(password, salt);
4242
}
4343

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-
5744
function addUser(username, password, email) {
5845
console.log('inside addUser', username)
5946
const newUser = new User({ username: username, password: hashPassword(password), email: email})
@@ -79,34 +66,13 @@ function clearGuestSettings() {
7966
* @return New list of applications
8067
*/
8168
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-
10369
const newApp = JSON.parse(application)
104-
console.log('newApp here', newApp)
105-
console.log('current user', currentUser)
10670
const createdOn = moment().format('lll');
10771
newApp.push(createdOn);
10872

73+
//If currentUser is guest, add services to local instance (settings.json)
10974
if (currentUser === 'guest') {
75+
// Retrieves file contents from settings.json
11076
const settings = JSON.parse(fs.readFileSync(settingsLocation).toString('utf8'));
11177
const services = settings[currentUser].services;
11278

@@ -117,20 +83,20 @@ ipcMain.on('addApp', (message: IpcMainEvent, application: any) => {
11783
fs.writeFileSync(settingsLocation, JSON.stringify(settings, null, '\t'));
11884

11985
// 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
12189
} else {
12290
console.log('not guest')
12391
return User.findOneAndUpdate({ username: currentUser }, {
12492
$push: {services: newApp}
125-
})
93+
}, {new: true})
12694
.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])
13097
})
13198
.catch((error) => {
13299
console.log(`addApp failed : ${error}`)
133-
// return false;
134100
})
135101
}
136102
});

0 commit comments

Comments
 (0)