|
| 1 | +const _ = require('lodash'); |
| 2 | +const { VAR_FOLDER } = require('../config/config'); |
| 3 | +const { LOAD_SPACE_IN_CLASSROOM_CHANNEL } = require('../config/channels'); |
| 4 | +const { |
| 5 | + ERROR_GENERAL, |
| 6 | + ERROR_DUPLICATE_USERNAME_IN_CLASSROOM, |
| 7 | + ERROR_INVALID_USERNAME, |
| 8 | +} = require('../config/errors'); |
| 9 | +const logger = require('../logger'); |
| 10 | +const { isFileAvailable, clean } = require('../utilities'); |
| 11 | +const { |
| 12 | + SPACES_COLLECTION, |
| 13 | + APP_INSTANCE_RESOURCES_COLLECTION, |
| 14 | + ACTIONS_COLLECTION, |
| 15 | + CLASSROOMS_COLLECTION, |
| 16 | + USERS_COLLECTION, |
| 17 | +} = require('../db'); |
| 18 | +const { renameSpaceFolder } = require('./loadSpace'); |
| 19 | +const { deleteSpaceAndResources } = require('./deleteSpace'); |
| 20 | +const { addUserInClassroomDatabase } = require('./addUserInClassroom'); |
| 21 | + |
| 22 | +const loadSpaceInClassroom = (mainWindow, db) => async ( |
| 23 | + event, |
| 24 | + { |
| 25 | + extractPath, |
| 26 | + classroomId, |
| 27 | + username, |
| 28 | + elements: { space, actions, appInstanceResources }, |
| 29 | + selection: { |
| 30 | + space: isSpaceSelected, |
| 31 | + appInstanceResources: isResourcesSelected, |
| 32 | + actions: isActionsSelected, |
| 33 | + }, |
| 34 | + } |
| 35 | +) => { |
| 36 | + logger.debug('loading a space in a classroom'); |
| 37 | + |
| 38 | + try { |
| 39 | + const classroom = db.get(CLASSROOMS_COLLECTION).find({ id: classroomId }); |
| 40 | + |
| 41 | + // add user if doesn't exist |
| 42 | + let user = classroom |
| 43 | + .get(USERS_COLLECTION) |
| 44 | + .find({ username }) |
| 45 | + .value(); |
| 46 | + if (!user) { |
| 47 | + user = addUserInClassroomDatabase(db, { username, id: classroomId }); |
| 48 | + } |
| 49 | + |
| 50 | + // todo: check teacher can write in classroom |
| 51 | + |
| 52 | + // space should always be defined |
| 53 | + if (_.isEmpty(space)) { |
| 54 | + logger.debug('try to load undefined space'); |
| 55 | + return mainWindow.webContents.send( |
| 56 | + LOAD_SPACE_IN_CLASSROOM_CHANNEL, |
| 57 | + ERROR_GENERAL |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + // write space to database if selected |
| 62 | + if (isSpaceSelected) { |
| 63 | + const { id } = space; |
| 64 | + const finalPath = `${VAR_FOLDER}/${classroomId}/${id}`; |
| 65 | + const tmpPath = `${VAR_FOLDER}/${classroomId}/.previousSpace-${id}`; |
| 66 | + |
| 67 | + // temporary rename previous space if exists |
| 68 | + if (await isFileAvailable(finalPath)) { |
| 69 | + const wasRenamed = await renameSpaceFolder(finalPath, tmpPath); |
| 70 | + if (!wasRenamed) { |
| 71 | + logger.error('unable to rename previous space folder'); |
| 72 | + clean(extractPath); |
| 73 | + return mainWindow.webContents.send( |
| 74 | + LOAD_SPACE_IN_CLASSROOM_CHANNEL, |
| 75 | + ERROR_GENERAL |
| 76 | + ); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + // we need to wrap this operation to avoid errors in windows |
| 81 | + const wasRenamed = await renameSpaceFolder(extractPath, finalPath); |
| 82 | + if (!wasRenamed) { |
| 83 | + logger.error('unable to rename previous temporary new space folder'); |
| 84 | + clean(extractPath); |
| 85 | + return mainWindow.webContents.send( |
| 86 | + LOAD_SPACE_IN_CLASSROOM_CHANNEL, |
| 87 | + ERROR_GENERAL |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + // remove previous space |
| 92 | + deleteSpaceAndResources(db, id, tmpPath); |
| 93 | + |
| 94 | + // add new space in database |
| 95 | + classroom |
| 96 | + .get(SPACES_COLLECTION) |
| 97 | + .push(space) |
| 98 | + .write(); |
| 99 | + } else { |
| 100 | + // clean temp space folder |
| 101 | + clean(extractPath); |
| 102 | + } |
| 103 | + |
| 104 | + const { id: userId } = user; |
| 105 | + |
| 106 | + // write resources to database if selected |
| 107 | + if (isResourcesSelected) { |
| 108 | + if (_.isEmpty(appInstanceResources)) { |
| 109 | + logger.debug('try to load empty resources'); |
| 110 | + return mainWindow.webContents.send( |
| 111 | + LOAD_SPACE_IN_CLASSROOM_CHANNEL, |
| 112 | + ERROR_GENERAL |
| 113 | + ); |
| 114 | + } |
| 115 | + |
| 116 | + const savedResources = classroom.get(APP_INSTANCE_RESOURCES_COLLECTION); |
| 117 | + |
| 118 | + // todo: replace resources |
| 119 | + |
| 120 | + const newResources = appInstanceResources |
| 121 | + // keep only non-duplicate resources |
| 122 | + .filter(({ id }) => !savedResources.find({ id }).value()) |
| 123 | + // change user id with given user |
| 124 | + .map(action => ({ ...action, user: userId })); |
| 125 | + |
| 126 | + savedResources.push(...newResources).write(); |
| 127 | + } |
| 128 | + |
| 129 | + // write actions to database if selected |
| 130 | + if (isActionsSelected) { |
| 131 | + if (_.isEmpty(actions)) { |
| 132 | + logger.debug('try to load empty actions'); |
| 133 | + return mainWindow.webContents.send( |
| 134 | + LOAD_SPACE_IN_CLASSROOM_CHANNEL, |
| 135 | + ERROR_GENERAL |
| 136 | + ); |
| 137 | + } |
| 138 | + |
| 139 | + const savedActions = classroom.get(ACTIONS_COLLECTION); |
| 140 | + |
| 141 | + // todo: replace actions |
| 142 | + |
| 143 | + const newActions = actions |
| 144 | + // keep only non-duplicate actions |
| 145 | + .filter(({ id }) => !savedActions.find({ id }).value()) |
| 146 | + // change user id with given user |
| 147 | + .map(action => ({ ...action, user: userId })); |
| 148 | + |
| 149 | + savedActions.push(...newActions).write(); |
| 150 | + } |
| 151 | + |
| 152 | + return mainWindow.webContents.send(LOAD_SPACE_IN_CLASSROOM_CHANNEL, { |
| 153 | + spaceId: space.id, |
| 154 | + }); |
| 155 | + } catch (err) { |
| 156 | + switch (err) { |
| 157 | + case ERROR_DUPLICATE_USERNAME_IN_CLASSROOM: |
| 158 | + return mainWindow.webContents.send( |
| 159 | + LOAD_SPACE_IN_CLASSROOM_CHANNEL, |
| 160 | + ERROR_DUPLICATE_USERNAME_IN_CLASSROOM |
| 161 | + ); |
| 162 | + case ERROR_INVALID_USERNAME: |
| 163 | + return mainWindow.webContents.send( |
| 164 | + LOAD_SPACE_IN_CLASSROOM_CHANNEL, |
| 165 | + ERROR_INVALID_USERNAME |
| 166 | + ); |
| 167 | + default: |
| 168 | + logger.error(err); |
| 169 | + return mainWindow.webContents.send( |
| 170 | + LOAD_SPACE_IN_CLASSROOM_CHANNEL, |
| 171 | + ERROR_GENERAL |
| 172 | + ); |
| 173 | + } |
| 174 | + } |
| 175 | +}; |
| 176 | + |
| 177 | +module.exports = loadSpaceInClassroom; |
0 commit comments