Skip to content

Commit

Permalink
Add ace.js code sections - Related changes still in progress.
Browse files Browse the repository at this point in the history
JS code separations made in angularjs related modules / controllers / filters / directives & jquery extentions.
  • Loading branch information
Korel Kashri committed Apr 18, 2020
1 parent a6669e9 commit 8749b77
Show file tree
Hide file tree
Showing 945 changed files with 389,432 additions and 1,152 deletions.
2 changes: 2 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ let app = express()

.use('/uploads_dir', express.static(__dirname + '/helpers/db_controllers/uploads'))

.use('/viewer', express.static(__dirname + '/node_modules/node-viewerjs/release'))

.use(session({
cookieName: 'session',
secret: 'as#*&*(asf4334@21#$@#1\t2ikl;4j\'32fsda8a4asd\5fet1d42hjk3@#$#\P^\"^%wefrwe19a3f2sED75*^&*5qj$!@#$!#12\+34#\'$!@joUILvxcxcuXds1ik\\laD5$cbv6ef\\\\\fjDASrA87\"345\'yttr%@asdf##$@34234',
Expand Down
12 changes: 12 additions & 0 deletions controllers/uploads.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,16 @@ exports.delete_files = async (req, res, next) => {
} catch (e) {
return responses_gen.generate_response(res, 400, null, e.message);
}
};


// View

exports.view_files = async (req, res, next) => {
try {
let filename = requests_handler.require_param(req, "route", "filename");
res.render("public/resources/ViewerJS/index.html#../uploads_dir/" + filename);
} catch (e) {
return responses_gen.generate_response(res, 400, null, e.message);
}
};
27 changes: 18 additions & 9 deletions controllers/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,16 @@ exports.is_plan_registrable_by_user = is_plan_registrable_by_user;
exports.get_plan_progress = async (req, res, next) => {
try {
let progress = await users_model.get_plan_progress(req, res, next);
return responses_gen.generate_response(res, 200, progress, "Progress successfully restored");
return responses_gen.generate_response(res, 200, progress, "Progress successfully restored.");
} catch (e) {
return responses_gen.generate_response(res, 400, null, e.message);
}
};

exports.get_current_plan_task = async (req, res, next) => {
try {
let current_task = await users_model.get_current_plan_task(req, res, next);
return responses_gen.generate_response(res, 200, current_task, "Current task successfully restored.");
} catch (e) {
return responses_gen.generate_response(res, 400, null, e.message);
}
Expand All @@ -69,7 +78,7 @@ exports.get_available_plans = async (req, res, next) => {
for (let i = 0; i < plans.length; i++) {
plans[i]._doc.is_registered = await is_user_registered_to_plan(username, plans[i].name);
}
return responses_gen.generate_response(res, 200, plans, "Plans successfully restored");
return responses_gen.generate_response(res, 200, plans, "Plans successfully restored.");
} catch (e) {
return responses_gen.generate_response(res, 400, null, e.message);
}
Expand All @@ -92,7 +101,7 @@ exports.get_registered_plans = async (req, res, next) => {
plans[i]._doc.is_registered = true;
}

return responses_gen.generate_response(res, 200, plans, "Plans successfully restored");
return responses_gen.generate_response(res, 200, plans, "Plans successfully restored.");
} catch (e) {
return responses_gen.generate_response(res, 400, null, e.message);
}
Expand All @@ -101,7 +110,7 @@ exports.get_registered_plans = async (req, res, next) => {
exports.get_users = async (req, res, next) => {
try {
let users = await users_model.get(req, res, next); // all
return responses_gen.generate_response(res, 200, users, "Users successfully restored");
return responses_gen.generate_response(res, 200, users, "Users successfully restored.");
} catch (e) {
return responses_gen.generate_response(res, 400, null, e.message);
}
Expand All @@ -110,7 +119,7 @@ exports.get_users = async (req, res, next) => {
exports.get_user = async (req, res, next) => {
try {
let user = await users_model.get(req, res, next); // by id
return responses_gen.generate_response(res, 200, user, "User successfully restored");
return responses_gen.generate_response(res, 200, user, "User successfully restored.");
} catch (e) {
return responses_gen.generate_response(res, 400, null, e.message);
}
Expand All @@ -119,7 +128,7 @@ exports.get_user = async (req, res, next) => {
exports.add_user = async (req, res, next) => {
try {
let user = await users_model.add(req, res, next);
return responses_gen.generate_response(res, 200, user, "User successfully created");
return responses_gen.generate_response(res, 200, user, "User successfully created.");
} catch (e) {
return responses_gen.generate_response(res, 400, null, e.message);
}
Expand All @@ -128,7 +137,7 @@ exports.add_user = async (req, res, next) => {
exports.modify_user = async (req, res, next) => {
try {
let user = await users_model.modify(req, res, next);
return responses_gen.generate_response(res, 200, user, "User successfully modified");
return responses_gen.generate_response(res, 200, user, "User successfully modified.");
} catch (e) {
return responses_gen.generate_response(res, 400, null, e.message);
}
Expand All @@ -137,7 +146,7 @@ exports.modify_user = async (req, res, next) => {
exports.register_to_plan = async (req, res, next) => {
try {
let user = await users_model.add_plan(req, res, next);
return responses_gen.generate_response(res, 200, user, "User successfully registered to plan");
return responses_gen.generate_response(res, 200, user, "User successfully registered to plan.");
} catch (e) {
return responses_gen.generate_response(res, 400, null, e.message);
}
Expand All @@ -146,7 +155,7 @@ exports.register_to_plan = async (req, res, next) => {
exports.remove_user = async (req, res, next) => {
try {
let user = await users_model.remove(req, res, next);
return responses_gen.generate_response(res, 200, user, "User successfully deleted");
return responses_gen.generate_response(res, 200, user, "User successfully deleted.");
} catch (e) {
return responses_gen.generate_response(res, 400, null, e.message);
}
Expand Down
21 changes: 18 additions & 3 deletions helpers/db_controllers/services/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,13 @@ let init_tasks_schema = async _ => {
},
file_names: [String],
answer_options: [String], // For boolean
code_sections: [String],
code_sections: [
{
content: String,
theme: String,
language: String
}
],
judgement_criteria: [String],
hints: [String],
plan_exceptions: [
Expand All @@ -98,7 +104,7 @@ let init_tasks_schema = async _ => {
type: String, // Plan ID
required: true
},
task_progress_value: Number, // if not mentioned -- 1, else the number will define how much progress solving this task means to this specific plan (Zero/Positive only numbers)
task_progress_value: Number, // if not mentioned -- 1, the number will define how much progress solving this task means to this specific plan (Zero/Positive only numbers)
details: String,
search_keywords: [String],
file_names: [String],
Expand Down Expand Up @@ -160,9 +166,14 @@ let init_users_schema = _ => {
reviewer: { // Reviewer user id || "System" => For auto test
type: String,
required: true
},
reviewer_msg: {
type: String,
required: true
}
}
],
skipped_tasks: [String],
tasks_for_review: [
{
id: {
Expand All @@ -179,7 +190,11 @@ let init_users_schema = _ => {
required: true
},
answer: [String], // TODO make it required
last_reviewer: { // Reviewer user id
last_reviewer: { // Reviewer user id || "System" => For auto test
type: String,
required: true
},
reviewer_msg: {
type: String,
required: true
}
Expand Down
96 changes: 72 additions & 24 deletions models/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,51 +199,98 @@ let is_user_registered_to_plan = async (req, res, next) => {

exports.is_user_registered_to_plan = is_user_registered_to_plan;

/**
* @param req - pure request variable, checks for req.user.role
* @param target_username - Username to access it's data.
*
* @throws Error if the current user have no permissions to access the desired data.
*/
let validate_user_access_to_user_data = (req, target_username) => {
if (req.user && req.user.role < access_limitations.min_access_required.view_users_details && req.user.username !== target_username) {
throw new Error("You have no permission to watch this user.");
}
};

/**
* @param req
* req["query"]["username"] - Target user
* req["query"]["plan_name"] - Check for plan
*
* @returns The plan data related to the user (in pseudo DB: users[target_user].plans[target_plan])
*/
let get_user_plan_data = async (req, res, next) => {
// Get DB
let users_db_model = database.users_model();

// Extract main param
let username = requests_handler.require_param(req, "route", "username");
let target_plan_id = (await plans_model.get_plan_id(req, res, next)).toString();

// Validations
validate_user_access_to_user_data(req, username);

assert.ok(await is_user_registered_to_plan(req, res, next), "User does not registered to this plan.");

let query = {};
query.username = username;
let user_data = await users_db_model.find(query, '-password').exec();
user_data = user_data[0];
return user_data.plans.filter((plan) => plan.id === target_plan_id)[0];
};

/**
* @param req
* req["query"]["username"] - Target user
* req["query"]["plan_name"] - Check for plan
*
* @returns The plan current task id related to the user (in pseudo DB: users[target_user].plans[target_plan].current_task.id)
*/
let get_user_plan_current_task_id = async (req, res, next) => {
return (await get_user_plan_data(req, res, next)).current_task.id;
};

// API

exports.get_current_plan_task = async (req, res, next) => {
// Prepare params for future access
req.query.username = req.params.username;
req.query.plan_name = req.params.plan_name;

// Arrange data
req.params.task_id = await get_user_plan_current_task_id(req, res, next);

return await tasks_model.get(req, res, next);
};

exports.get_plan_progress = async (req, res, next) => {
// Get DB
let users_db_model = database.users_model();

// Extract param
let username = requests_handler.require_param(req, "route", "username");
let target_plan = requests_handler.optional_param(req, "route", "plan_name");

// Prepare params for future access
req.query.username = req.params.username;
req.query.plan_name = req.params.plan_name;

// Validation
if (req.user && req.user.role < access_limitations.min_access_required.view_users_details && req.user.username !== username) {
throw new Error("You have no permission to watch this user.");
}

let is_registered = await is_user_registered_to_plan(req, res, next);
assert.ok(is_registered, "User does not registered to this plan.");

// Arrange data
let query = {};
query.username = username;
let plan_details = await plans_model.get(req, res, next);
plan_details = plan_details[0];
let user_data = await users_db_model.find(query, '-password').exec();
let target_plan_id = plan_details._id;
req.query.plan_id = target_plan_id; // Used inside the loop in the call for tasks_model.get_task_plan_exceptions

let plan_details = (await plans_model.get(req, res, next))[0];
let plan_route = plan_details.route;
let user_plan = user_data.plans.filter((plan) => plan.id === target_plan_id)[0];

let user_plan = await get_user_plan_data(req, res, next);
let current_user_task_id = user_plan.current_task.id;
let is_user_completed_current_task = true; // Inside the loop, indicates if the current checked task is already completed by this user.

// Get Progress Algorithm
req.query.plan_id = plan_details._id.toString(); // Used inside the loop in the call for tasks_model.get_task_plan_exceptions
let is_user_completed_current_task = true; // Inside the loop, indicates if the current checked task is already completed by this user.
let progress;
let current_user_plan_progress_value; // TODO consider to save in the DB to save time
let total_plan_progress_value; // TODO consider to save in the DB to save time

current_user_plan_progress_value = total_plan_progress_value = 0;
for (let i = 0; i < plan_route.length; i++) {
let task_id = plan_route[i];
req.query.task_id = task_id;
let task_value;
let plan_exceptions = await tasks_model.get_task_plan_exceptions(req, res, next);
if (!plan_exceptions) {
Expand All @@ -260,7 +307,11 @@ exports.get_plan_progress = async (req, res, next) => {
total_plan_progress_value += task_value;
}
progress = current_user_plan_progress_value / total_plan_progress_value;
return progress;
return {
value: progress,
is_tasks_declined: user_plan.declined_tasks.length,
is_tasks_waiting_for_review: user_plan.tasks_for_review.length
};
};

exports.get = async (req, res, next) => {
Expand All @@ -271,9 +322,7 @@ exports.get = async (req, res, next) => {
let username = requests_handler.optional_param(req, "route", "username");

// Validation
if (req.user && req.user.role < access_limitations.min_access_required.view_users_details && req.user.username !== username) {
throw new Error("You have no permission to watch this user.");
}
validate_user_access_to_user_data(req, username);

/* Assumptions here:
* 1. The user might be logged in OR logged out.
Expand Down Expand Up @@ -413,7 +462,6 @@ exports.add_plan = async (req, res, next) => {
// Extract required details
let plan_id = await plans_model.get_plan_id(req);
let first_task_id = await plans_model.get_plan_next_mission_id(req);
first_task_id = "a";
// Prepare query
let filter = {username: username};
let update = {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"password-hasher": "^1.0.1",
"multer": "^1.4.2",
"fs": "^0.0.1-security",
"path": "^0.12.7"
"path": "^0.12.7",
"node-viewerjs": "^0.7.1"
},
"devDependencies": {
"mark.js": "^8.11.1"
Expand Down
Loading

0 comments on commit 8749b77

Please sign in to comment.