-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add user functionalities in classroom (add, delete, edit)
- Loading branch information
Showing
33 changed files
with
1,345 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
const { USERS_COLLECTION, CLASSROOMS_COLLECTION } = require('../db'); | ||
const { ADD_USER_IN_CLASSROOM_CHANNEL } = require('../config/channels'); | ||
const { createNewUser } = require('./signIn'); | ||
const { | ||
ERROR_DUPLICATE_USERNAME_IN_CLASSROOM, | ||
ERROR_GENERAL, | ||
} = require('../config/errors'); | ||
|
||
const logger = require('../logger'); | ||
|
||
const addUserInClassroom = (mainWindow, db) => async ( | ||
event, | ||
{ username, classroomId: id } | ||
) => { | ||
logger.debug('adding a user in a classroom'); | ||
|
||
try { | ||
const users = db | ||
.get(CLASSROOMS_COLLECTION) | ||
.find({ id }) | ||
.get(USERS_COLLECTION); | ||
|
||
const now = new Date(); | ||
|
||
// check in db if username exists | ||
const found = users.find({ username }).value(); | ||
|
||
if (found) { | ||
return mainWindow.webContents.send( | ||
ADD_USER_IN_CLASSROOM_CHANNEL, | ||
ERROR_DUPLICATE_USERNAME_IN_CLASSROOM | ||
); | ||
} | ||
|
||
const user = createNewUser(username, now); | ||
users.push(user).write(); | ||
|
||
return mainWindow.webContents.send(ADD_USER_IN_CLASSROOM_CHANNEL, user); | ||
} catch (err) { | ||
logger.error(err); | ||
return mainWindow.webContents.send( | ||
ADD_USER_IN_CLASSROOM_CHANNEL, | ||
ERROR_GENERAL | ||
); | ||
} | ||
}; | ||
|
||
module.exports = addUserInClassroom; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const { USERS_COLLECTION, CLASSROOMS_COLLECTION } = require('../db'); | ||
const { DELETE_USER_IN_CLASSROOM_CHANNEL } = require('../config/channels'); | ||
const { ERROR_GENERAL } = require('../config/errors'); | ||
|
||
const logger = require('../logger'); | ||
|
||
const deleteUserInClassroom = (mainWindow, db) => async ( | ||
event, | ||
{ userId, classroomId } | ||
) => { | ||
logger.debug('deleting a user in a classroom'); | ||
|
||
try { | ||
db.get(CLASSROOMS_COLLECTION) | ||
.find({ id: classroomId }) | ||
.get(USERS_COLLECTION) | ||
.remove({ id: userId }) | ||
.write(); | ||
|
||
return mainWindow.webContents.send(DELETE_USER_IN_CLASSROOM_CHANNEL); | ||
} catch (err) { | ||
logger.error(err); | ||
return mainWindow.webContents.send( | ||
DELETE_USER_IN_CLASSROOM_CHANNEL, | ||
ERROR_GENERAL | ||
); | ||
} | ||
}; | ||
|
||
module.exports = deleteUserInClassroom; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
const { EDIT_USER_IN_CLASSROOM_CHANNEL } = require('../config/channels'); | ||
const { ERROR_GENERAL } = require('../config/errors'); | ||
const { CLASSROOMS_COLLECTION, USERS_COLLECTION } = require('../db'); | ||
const logger = require('../logger'); | ||
|
||
const editUserInClassroom = (mainWindow, db) => async ( | ||
event, | ||
{ username, userId, classroomId } | ||
) => { | ||
logger.debug('editing user in classroom'); | ||
|
||
try { | ||
const user = db | ||
.get(CLASSROOMS_COLLECTION) | ||
.find({ id: classroomId }) | ||
.get(USERS_COLLECTION) | ||
.find({ id: userId }); | ||
|
||
// check user exists | ||
const found = user.value(); | ||
if (!found) { | ||
mainWindow.webContents.send( | ||
EDIT_USER_IN_CLASSROOM_CHANNEL, | ||
ERROR_GENERAL | ||
); | ||
} | ||
|
||
// update data | ||
const now = new Date(); | ||
user.assign({ username, lastUpdatedAt: now }).write(); | ||
|
||
mainWindow.webContents.send(EDIT_USER_IN_CLASSROOM_CHANNEL); | ||
} catch (err) { | ||
logger.error(err); | ||
mainWindow.webContents.send(EDIT_USER_IN_CLASSROOM_CHANNEL, ERROR_GENERAL); | ||
} | ||
}; | ||
|
||
module.exports = editUserInClassroom; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
const { dialog } = require('electron'); | ||
const { | ||
RESPOND_DELETE_USER_IN_CLASSROOM_PROMPT_CHANNEL, | ||
} = require('../config/channels'); | ||
const logger = require('../logger'); | ||
|
||
const showDeleteUserInClassroomPrompt = mainWindow => ({ username }) => { | ||
logger.debug('showing delete user in classroom prompt'); | ||
|
||
const options = { | ||
type: 'warning', | ||
buttons: ['Cancel', 'Delete'], | ||
defaultId: 0, | ||
cancelId: 0, | ||
message: `Are you sure you want to delete ${username} from this classroom?`, | ||
}; | ||
|
||
dialog.showMessageBox(mainWindow, options).then(({ response }) => { | ||
mainWindow.webContents.send( | ||
RESPOND_DELETE_USER_IN_CLASSROOM_PROMPT_CHANNEL, | ||
response | ||
); | ||
}); | ||
}; | ||
|
||
module.exports = showDeleteUserInClassroomPrompt; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.