Skip to content

Commit df64116

Browse files
committed
feat: add user functionalities in classroom (add, delete, edit)
1 parent eefd1f5 commit df64116

33 files changed

+1345
-28
lines changed

public/app/config/channels.js

+7
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,11 @@ module.exports = {
5959
RESPOND_DELETE_CLASSROOM_PROMPT_CHANNEL: 'prompt:classroom:delete:respond',
6060
EDIT_CLASSROOM_CHANNEL: 'classroom:edit',
6161
GET_CLASSROOM_CHANNEL: 'classroom:get',
62+
ADD_USER_IN_CLASSROOM_CHANNEL: 'classroom:user:add',
63+
DELETE_USER_IN_CLASSROOM_CHANNEL: 'classroom:user:delete',
64+
SHOW_DELETE_USER_IN_CLASSROOM_PROMPT_CHANNEL:
65+
'prompt:classroom:user:delete:show',
66+
RESPOND_DELETE_USER_IN_CLASSROOM_PROMPT_CHANNEL:
67+
'prompt:classroom:user:delete:respond',
68+
EDIT_USER_IN_CLASSROOM_CHANNEL: 'classroom:user:edit',
6269
};

public/app/config/errors.js

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ const ERROR_DOWNLOADING_FILE = 'ERROR_DOWNLOADING_FILE';
88
const ERROR_GENERAL = 'ERROR_GENERAL';
99
const ERROR_DUPLICATE_CLASSROOM_NAME = 'ERROR_DUPLICATE_CLASSROOM_NAME';
1010
const ERROR_ACCESS_DENIED_CLASSROOM = 'ERROR_ACCESS_DENIED_CLASSROOM';
11+
const ERROR_DUPLICATE_USERNAME_IN_CLASSROOM =
12+
'ERROR_DUPLICATE_USERNAME_IN_CLASSROOM';
1113

1214
module.exports = {
1315
ERROR_ZIP_CORRUPTED,
@@ -17,4 +19,5 @@ module.exports = {
1719
ERROR_GENERAL,
1820
ERROR_DUPLICATE_CLASSROOM_NAME,
1921
ERROR_ACCESS_DENIED_CLASSROOM,
22+
ERROR_DUPLICATE_USERNAME_IN_CLASSROOM,
2023
};

public/app/config/messages.js

+20
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,19 @@ const SUCCESS_EDITING_CLASSROOM_MESSAGE =
8383
const ERROR_GETTING_CLASSROOM_MESSAGE =
8484
'There was an error getting the classroom';
8585
const ERROR_ACCESS_DENIED_CLASSROOM_MESSAGE = `This user does not have access to this classroom`;
86+
const ERROR_DUPLICATE_USERNAME_IN_CLASSROOM_MESSAGE =
87+
'This username already exists in this classroom';
88+
const ERROR_ADDING_USER_IN_CLASSROOM_MESSAGE =
89+
'There was an error adding a new user in this classroom';
90+
const ERROR_DELETING_USER_IN_CLASSROOM_MESSAGE =
91+
'There was an error deleting a user in this classroom';
92+
const ERROR_EDITING_USER_IN_CLASSROOM_MESSAGE =
93+
'There was an error editing a user in this classroom';
94+
const SUCCESS_EDITING_USER_IN_CLASSROOM_MESSAGE =
95+
'The user was successfully edited';
96+
const SUCCESS_DELETING_USER_IN_CLASSROOM_MESSAGE =
97+
'The user was successfully deleted';
98+
const ERROR_INVALID_USERNAME_MESSAGE = 'This username is invalid';
8699

87100
module.exports = {
88101
ERROR_GETTING_DEVELOPER_MODE,
@@ -142,4 +155,11 @@ module.exports = {
142155
SUCCESS_EDITING_CLASSROOM_MESSAGE,
143156
ERROR_GETTING_CLASSROOM_MESSAGE,
144157
ERROR_ACCESS_DENIED_CLASSROOM_MESSAGE,
158+
ERROR_DUPLICATE_USERNAME_IN_CLASSROOM_MESSAGE,
159+
ERROR_ADDING_USER_IN_CLASSROOM_MESSAGE,
160+
ERROR_DELETING_USER_IN_CLASSROOM_MESSAGE,
161+
ERROR_EDITING_USER_IN_CLASSROOM_MESSAGE,
162+
SUCCESS_DELETING_USER_IN_CLASSROOM_MESSAGE,
163+
SUCCESS_EDITING_USER_IN_CLASSROOM_MESSAGE,
164+
ERROR_INVALID_USERNAME_MESSAGE,
145165
};

public/app/listeners/addClassroom.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ const addClassroom = (mainWindow, db) => async (event, { name, userId }) => {
2424
logger.debug('adding a classroom');
2525

2626
try {
27-
// check name does not already exists
27+
// check name does not already exists for given user
2828
const found = db
2929
.get(CLASSROOMS_COLLECTION)
30-
.find({ name })
30+
.find({ name, teacherId: userId })
3131
.value();
3232
if (found) {
3333
mainWindow.webContents.send(
+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
const { USERS_COLLECTION, CLASSROOMS_COLLECTION } = require('../db');
2+
const { ADD_USER_IN_CLASSROOM_CHANNEL } = require('../config/channels');
3+
const { createNewUser } = require('./signIn');
4+
const {
5+
ERROR_DUPLICATE_USERNAME_IN_CLASSROOM,
6+
ERROR_GENERAL,
7+
} = require('../config/errors');
8+
9+
const logger = require('../logger');
10+
11+
const addUserInClassroom = (mainWindow, db) => async (
12+
event,
13+
{ username, classroomId: id }
14+
) => {
15+
logger.debug('adding a user in a classroom');
16+
17+
try {
18+
const users = db
19+
.get(CLASSROOMS_COLLECTION)
20+
.find({ id })
21+
.get(USERS_COLLECTION);
22+
23+
const now = new Date();
24+
25+
// check in db if username exists
26+
const found = users.find({ username }).value();
27+
28+
if (found) {
29+
return mainWindow.webContents.send(
30+
ADD_USER_IN_CLASSROOM_CHANNEL,
31+
ERROR_DUPLICATE_USERNAME_IN_CLASSROOM
32+
);
33+
}
34+
35+
const user = createNewUser(username, now);
36+
users.push(user).write();
37+
38+
return mainWindow.webContents.send(ADD_USER_IN_CLASSROOM_CHANNEL, user);
39+
} catch (err) {
40+
logger.error(err);
41+
return mainWindow.webContents.send(
42+
ADD_USER_IN_CLASSROOM_CHANNEL,
43+
ERROR_GENERAL
44+
);
45+
}
46+
};
47+
48+
module.exports = addUserInClassroom;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const { USERS_COLLECTION, CLASSROOMS_COLLECTION } = require('../db');
2+
const { DELETE_USER_IN_CLASSROOM_CHANNEL } = require('../config/channels');
3+
const { ERROR_GENERAL } = require('../config/errors');
4+
5+
const logger = require('../logger');
6+
7+
const deleteUserInClassroom = (mainWindow, db) => async (
8+
event,
9+
{ userId, classroomId }
10+
) => {
11+
logger.debug('deleting a user in a classroom');
12+
13+
try {
14+
db.get(CLASSROOMS_COLLECTION)
15+
.find({ id: classroomId })
16+
.get(USERS_COLLECTION)
17+
.remove({ id: userId })
18+
.write();
19+
20+
return mainWindow.webContents.send(DELETE_USER_IN_CLASSROOM_CHANNEL);
21+
} catch (err) {
22+
logger.error(err);
23+
return mainWindow.webContents.send(
24+
DELETE_USER_IN_CLASSROOM_CHANNEL,
25+
ERROR_GENERAL
26+
);
27+
}
28+
};
29+
30+
module.exports = deleteUserInClassroom;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const { EDIT_USER_IN_CLASSROOM_CHANNEL } = require('../config/channels');
2+
const { ERROR_GENERAL } = require('../config/errors');
3+
const { CLASSROOMS_COLLECTION, USERS_COLLECTION } = require('../db');
4+
const logger = require('../logger');
5+
6+
const editUserInClassroom = (mainWindow, db) => async (
7+
event,
8+
{ username, userId, classroomId }
9+
) => {
10+
logger.debug('editing user in classroom');
11+
12+
try {
13+
const user = db
14+
.get(CLASSROOMS_COLLECTION)
15+
.find({ id: classroomId })
16+
.get(USERS_COLLECTION)
17+
.find({ id: userId });
18+
19+
// check user exists
20+
const found = user.value();
21+
if (!found) {
22+
mainWindow.webContents.send(
23+
EDIT_USER_IN_CLASSROOM_CHANNEL,
24+
ERROR_GENERAL
25+
);
26+
}
27+
28+
// update data
29+
const now = new Date();
30+
user.assign({ username, lastUpdatedAt: now }).write();
31+
32+
mainWindow.webContents.send(EDIT_USER_IN_CLASSROOM_CHANNEL);
33+
} catch (err) {
34+
logger.error(err);
35+
mainWindow.webContents.send(EDIT_USER_IN_CLASSROOM_CHANNEL, ERROR_GENERAL);
36+
}
37+
};
38+
39+
module.exports = editUserInClassroom;

public/app/listeners/index.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const setUserMode = require('./setUserMode');
2626
const clearUserInput = require('./clearUserInput');
2727
const showClearUserInputPrompt = require('./showClearUserInputPrompt');
2828
const postAction = require('./postAction');
29-
const signIn = require('./signIn');
29+
const { signIn } = require('./signIn');
3030
const signOut = require('./signOut');
3131
const isAuthenticated = require('./isAuthenticated');
3232
const getAppInstanceResources = require('./getAppInstanceResources');
@@ -40,7 +40,11 @@ const getClassrooms = require('./getClassrooms');
4040
const deleteClassroom = require('./deleteClassroom');
4141
const editClassroom = require('./editClassroom');
4242
const showDeleteClassroomPrompt = require('./showDeleteClassroomPrompt');
43+
const showDeleteUserInClassroomPrompt = require('./showDeleteUserInClassroomPrompt');
4344
const getClassroom = require('./getClassroom');
45+
const addUserInClassroom = require('./addUserInClassroom');
46+
const deleteUserInClassroom = require('./deleteUserInClassroom');
47+
const editUserInClassroom = require('./editUserInClassroom');
4448

4549
module.exports = {
4650
loadSpace,
@@ -85,4 +89,8 @@ module.exports = {
8589
getClassrooms,
8690
getClassroom,
8791
showDeleteClassroomPrompt,
92+
addUserInClassroom,
93+
deleteUserInClassroom,
94+
showDeleteUserInClassroomPrompt,
95+
editUserInClassroom,
8896
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// eslint-disable-next-line import/no-extraneous-dependencies
2+
const { dialog } = require('electron');
3+
const {
4+
RESPOND_DELETE_USER_IN_CLASSROOM_PROMPT_CHANNEL,
5+
} = require('../config/channels');
6+
const logger = require('../logger');
7+
8+
const showDeleteUserInClassroomPrompt = mainWindow => ({ username }) => {
9+
logger.debug('showing delete user in classroom prompt');
10+
11+
const options = {
12+
type: 'warning',
13+
buttons: ['Cancel', 'Delete'],
14+
defaultId: 0,
15+
cancelId: 0,
16+
message: `Are you sure you want to delete ${username} from this classroom?`,
17+
};
18+
19+
dialog.showMessageBox(mainWindow, options).then(({ response }) => {
20+
mainWindow.webContents.send(
21+
RESPOND_DELETE_USER_IN_CLASSROOM_PROMPT_CHANNEL,
22+
response
23+
);
24+
});
25+
};
26+
27+
module.exports = showDeleteUserInClassroomPrompt;

public/app/listeners/signIn.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,4 @@ const signIn = (mainWindow, db) => async (
5252
mainWindow.webContents.send(SIGN_IN_CHANNEL, updatedUser);
5353
};
5454

55-
module.exports = signIn;
55+
module.exports = { signIn, createNewUser };

public/electron.js

+29
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ const {
6666
SHOW_DELETE_CLASSROOM_PROMPT_CHANNEL,
6767
EDIT_CLASSROOM_CHANNEL,
6868
GET_CLASSROOM_CHANNEL,
69+
ADD_USER_IN_CLASSROOM_CHANNEL,
70+
DELETE_USER_IN_CLASSROOM_CHANNEL,
71+
SHOW_DELETE_USER_IN_CLASSROOM_PROMPT_CHANNEL,
72+
EDIT_USER_IN_CLASSROOM_CHANNEL,
6973
} = require('./app/config/channels');
7074
const env = require('./env.json');
7175
const {
@@ -110,6 +114,10 @@ const {
110114
showDeleteClassroomPrompt,
111115
editClassroom,
112116
getClassroom,
117+
addUserInClassroom,
118+
showDeleteUserInClassroomPrompt,
119+
deleteUserInClassroom,
120+
editUserInClassroom,
113121
} = require('./app/listeners');
114122
const isMac = require('./app/utils/isMac');
115123

@@ -501,6 +509,21 @@ app.on('ready', async () => {
501509
// called when editing a classroom
502510
ipcMain.on(EDIT_CLASSROOM_CHANNEL, editClassroom(mainWindow, db));
503511

512+
// called when adding a user in a classroom
513+
ipcMain.on(ADD_USER_IN_CLASSROOM_CHANNEL, addUserInClassroom(mainWindow, db));
514+
515+
// prompt when deleting a user in a classroom
516+
ipcMain.on(
517+
SHOW_DELETE_USER_IN_CLASSROOM_PROMPT_CHANNEL,
518+
showDeleteUserInClassroomPrompt(mainWindow, db)
519+
);
520+
521+
// called when deleting a user in a classroom
522+
ipcMain.on(
523+
DELETE_USER_IN_CLASSROOM_CHANNEL,
524+
deleteUserInClassroom(mainWindow, db)
525+
);
526+
504527
// prompt when deleting a classroom
505528
ipcMain.on(
506529
SHOW_DELETE_CLASSROOM_PROMPT_CHANNEL,
@@ -510,6 +533,12 @@ app.on('ready', async () => {
510533
// called when deleting a classroom
511534
ipcMain.on(DELETE_CLASSROOM_CHANNEL, deleteClassroom(mainWindow, db));
512535

536+
// called when editing a user in a classroom
537+
ipcMain.on(
538+
EDIT_USER_IN_CLASSROOM_CHANNEL,
539+
editUserInClassroom(mainWindow, db)
540+
);
541+
513542
// called when getting the database
514543
ipcMain.on(GET_DATABASE_CHANNEL, async () => {
515544
try {

0 commit comments

Comments
 (0)