Skip to content

Commit

Permalink
Merge pull request #274 from graasp/267/importDataInClassroom
Browse files Browse the repository at this point in the history
267/import data in classroom
  • Loading branch information
juancarlosfarah authored Jun 3, 2020
2 parents 3b81e96 + ee9cffd commit c79e909
Show file tree
Hide file tree
Showing 41 changed files with 1,540 additions and 494 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
"react-redux-toastr": "7.6.4",
"react-router": "5.1.2",
"react-router-dom": "5.1.2",
"react-select": "3.1.0",
"react-split-pane": "0.1.89",
"recharts": "1.8.5",
"redux": "4.0.5",
Expand Down
3 changes: 3 additions & 0 deletions public/app/config/channels.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,7 @@ module.exports = {
RESPOND_DELETE_USERS_IN_CLASSROOM_PROMPT_CHANNEL:
'prompt:classroom:user:delete:respond',
EDIT_USER_IN_CLASSROOM_CHANNEL: 'classroom:user:edit',
GET_SPACE_IN_CLASSROOM_CHANNEL: 'classroom:space:get',
LOAD_SPACE_IN_CLASSROOM_CHANNEL: 'classroom:space:load',
GET_SPACE_TO_LOAD_IN_CLASSROOM_CHANNEL: 'classroom:space:load:get-space',
};
2 changes: 2 additions & 0 deletions public/app/config/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const ERROR_ACCESS_DENIED_CLASSROOM = 'ERROR_ACCESS_DENIED_CLASSROOM';
const ERROR_DUPLICATE_USERNAME_IN_CLASSROOM =
'ERROR_DUPLICATE_USERNAME_IN_CLASSROOM';
const ERROR_NO_USER_TO_DELETE = 'ERROR_NO_USER_TO_DELETE';
const ERROR_INVALID_USERNAME = 'ERROR_INVALID_USERNAME';

module.exports = {
ERROR_ZIP_CORRUPTED,
Expand All @@ -22,4 +23,5 @@ module.exports = {
ERROR_ACCESS_DENIED_CLASSROOM,
ERROR_DUPLICATE_USERNAME_IN_CLASSROOM,
ERROR_NO_USER_TO_DELETE,
ERROR_INVALID_USERNAME,
};
5 changes: 4 additions & 1 deletion public/app/config/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ const SUCCESS_DELETING_USERS_IN_CLASSROOM_MESSAGE =
'The user was successfully deleted';
const ERROR_INVALID_USERNAME_MESSAGE = 'This username is invalid';
const ERROR_NO_USER_TO_DELETE_MESSAGE = 'There is no user to delete';
const ERROR_GETTING_SPACE_IN_CLASSROOM_MESSAGE =
'There was an error getting the space in this classroom';

module.exports = {
ERROR_GETTING_DEVELOPER_MODE,
Expand Down Expand Up @@ -162,6 +164,7 @@ module.exports = {
ERROR_EDITING_USER_IN_CLASSROOM_MESSAGE,
SUCCESS_DELETING_USERS_IN_CLASSROOM_MESSAGE,
SUCCESS_EDITING_USER_IN_CLASSROOM_MESSAGE,
ERROR_INVALID_USERNAME_MESSAGE,
ERROR_NO_USER_TO_DELETE_MESSAGE,
ERROR_GETTING_SPACE_IN_CLASSROOM_MESSAGE,
ERROR_INVALID_USERNAME_MESSAGE,
};
5 changes: 5 additions & 0 deletions public/app/listeners/addClassroom.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const {
USERS_COLLECTION,
} = require('../db');
const logger = require('../logger');
const { createClassroomDirectory } = require('../utilities');

const DEFAULT_CLASSROOM = {
[SPACES_COLLECTION]: [],
Expand All @@ -39,6 +40,10 @@ const addClassroom = (mainWindow, db) => async (event, { name, userId }) => {

// create new classroom
const id = ObjectId().str;

// create directory where resources will be stored
createClassroomDirectory({ id });

const now = new Date();
const newClassroom = {
..._.cloneDeep(DEFAULT_CLASSROOM),
Expand Down
71 changes: 46 additions & 25 deletions public/app/listeners/addUserInClassroom.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,67 @@ const { ADD_USER_IN_CLASSROOM_CHANNEL } = require('../config/channels');
const { createNewUser } = require('./signIn');
const {
ERROR_DUPLICATE_USERNAME_IN_CLASSROOM,
ERROR_INVALID_USERNAME,
ERROR_GENERAL,
} = require('../config/errors');

const logger = require('../logger');

const addUserInClassroomDatabase = (db, { username, id }) => {
// check username validity
if (!username || !username.length) {
throw new Error(ERROR_INVALID_USERNAME);
}

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) {
throw new Error(ERROR_DUPLICATE_USERNAME_IN_CLASSROOM);
}

const user = createNewUser(username, now);
users.push(user).write();
return user;
};

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();
const user = addUserInClassroomDatabase(db, { username, id });

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
);
switch (err) {
case ERROR_DUPLICATE_USERNAME_IN_CLASSROOM:
return mainWindow.webContents.send(
ADD_USER_IN_CLASSROOM_CHANNEL,
ERROR_DUPLICATE_USERNAME_IN_CLASSROOM
);
case ERROR_INVALID_USERNAME:
return mainWindow.webContents.send(
ADD_USER_IN_CLASSROOM_CHANNEL,
ERROR_INVALID_USERNAME
);
default:
logger.error(err);
return mainWindow.webContents.send(
ADD_USER_IN_CLASSROOM_CHANNEL,
ERROR_GENERAL
);
}
}
};

module.exports = addUserInClassroom;
module.exports = { addUserInClassroom, addUserInClassroomDatabase };
2 changes: 2 additions & 0 deletions public/app/listeners/deleteClassroom.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const deleteClassroomAndResources = (db, id) => {
.write();

// todo: remove containing spaces

// todo: remove folder
};

const deleteClassroom = (mainWindow, db) => async (event, { id }) => {
Expand Down
24 changes: 24 additions & 0 deletions public/app/listeners/getSpaceInClassroom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const { GET_SPACE_IN_CLASSROOM_CHANNEL } = require('../config/channels');
const logger = require('../logger');
const { SPACES_COLLECTION, CLASSROOMS_COLLECTION } = require('../db');

const getSpaceInClassroom = (mainWindow, db) => async (
event,
{ id, classroomId }
) => {
try {
// get space in classroom from local db
const space = db
.get(CLASSROOMS_COLLECTION)
.find({ id: classroomId })
.get(SPACES_COLLECTION)
.find({ id })
.value();
mainWindow.webContents.send(GET_SPACE_IN_CLASSROOM_CHANNEL, space);
} catch (err) {
logger.error(err);
mainWindow.webContents.send(GET_SPACE_IN_CLASSROOM_CHANNEL, null);
}
};

module.exports = getSpaceInClassroom;
6 changes: 5 additions & 1 deletion public/app/listeners/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ const editClassroom = require('./editClassroom');
const showDeleteClassroomPrompt = require('./showDeleteClassroomPrompt');
const showDeleteUsersInClassroomPrompt = require('./showDeleteUsersInClassroomPrompt');
const getClassroom = require('./getClassroom');
const addUserInClassroom = require('./addUserInClassroom');
const { addUserInClassroom } = require('./addUserInClassroom');
const deleteUsersInClassroom = require('./deleteUsersInClassroom');
const editUserInClassroom = require('./editUserInClassroom');
const getSpaceInClassroom = require('./getSpaceInClassroom');
const loadSpaceInClassroom = require('./loadSpaceInClassroom');

module.exports = {
loadSpace,
Expand Down Expand Up @@ -93,4 +95,6 @@ module.exports = {
deleteUsersInClassroom,
showDeleteUsersInClassroomPrompt,
editUserInClassroom,
getSpaceInClassroom,
loadSpaceInClassroom,
};
18 changes: 11 additions & 7 deletions public/app/listeners/loadSpace.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const renameSpaceFolder = async (prevPath, newPath) => {
return wasRenamed;
};

const extractFileToLoadSpace = (mainWindow, db) => async (
const extractFileToLoadSpace = mainWindow => async (
event,
{ fileLocation }
) => {
Expand All @@ -81,19 +81,15 @@ const extractFileToLoadSpace = (mainWindow, db) => async (
const { id } = manifest;
const spacePath = `${extractPath}/${id}.json`;

// get handle to spaces collection
const spaces = db.get(SPACES_COLLECTION);
const savedSpace = spaces.find({ id }).value();

const spaceString = await fsPromises.readFile(spacePath);
const { space = {}, appInstanceResources = [], actions = [] } = JSON.parse(
spaceString
);
const elements = { space, appInstanceResources, actions };

return mainWindow.webContents.send(EXTRACT_FILE_TO_LOAD_SPACE_CHANNEL, {
spaceId: id,
extractPath,
savedSpace,
elements,
});
} catch (err) {
Expand Down Expand Up @@ -165,6 +161,9 @@ const loadSpace = (mainWindow, db) => async (
db.get(SPACES_COLLECTION)
.push(space)
.write();
} else {
// clean temp space folder
clean(extractPath);
}

const userId = db.get('user.id').value();
Expand Down Expand Up @@ -218,4 +217,9 @@ const loadSpace = (mainWindow, db) => async (
}
};

module.exports = { clearLoadSpace, extractFileToLoadSpace, loadSpace };
module.exports = {
clearLoadSpace,
extractFileToLoadSpace,
loadSpace,
renameSpaceFolder,
};
Loading

0 comments on commit c79e909

Please sign in to comment.