Skip to content

Commit

Permalink
added middleware to assign the task to the user
Browse files Browse the repository at this point in the history
  • Loading branch information
vinayak-trivedi committed Sep 19, 2022
1 parent adbffeb commit 22e4c5b
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
8 changes: 7 additions & 1 deletion controllers/tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ const updateTask = async (req, res) => {
* @param req {Object} - Express request object
* @param res {Object} - Express response object
*/
const updateTaskStatus = async (req, res) => {
const updateTaskStatus = async (req, res, next) => {
try {
const taskId = req.params.id;
const { id: userId } = req.userData;
Expand All @@ -150,6 +150,12 @@ const updateTaskStatus = async (req, res) => {
return res.boom.forbidden("Status cannot be updated. Please contact admin.");

await tasks.updateTask(req.body, taskId);
// this can be uncommented when we have skillLevel of the user, currently this middleware will not be called

// if (req.body.percentCompleted) {
// return next();
// }

return res.json({ message: "Task updated successfully!" });
} catch (err) {
logger.error(`Error while updating task status : ${err}`);
Expand Down
20 changes: 20 additions & 0 deletions middlewares/assignTask.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const { fetchSkillLevelTasks } = require("../models/tasks");
const db = require("../utils/firestore");

const assignTask = async function (req, res, next) {
if (req.body.percentCompleted === 100) {
const { task } = await fetchSkillLevelTasks("frontend", 1);
if (task) {
const docId = task.id;
const userId = req.userData.id;
db.collection("tasks").doc(docId).set({ assignee: userId, status: "ASSIGNED" }, { merge: true });
return res.json({ message: "task updated and another task got assigned" });
} else {
return res.json({ message: "Task updated but another task not found" });
}
} else {
return res.json({ message: "Task updated successfully!" });
}
};

module.exports = assignTask;
45 changes: 45 additions & 0 deletions models/tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,50 @@ const fetchUserTasks = async (username, statuses = [], field, order) => {
}
};

const fetchDbForTask = async (skill, level) => {
const task = await tasksModel
.where("taskLevel.category", "==", skill)
.where("taskLevel.level", ">=", level)
.where("taskLevel.level", "<=", level + 2)
.where("status", "==", "AVAILABLE")
.limit(1)
.get();
return task;
};

/**
*
* @param skill { string } : skill category which will be used
* @param level { number } : level of the skill
* @returns {Promise<task>|object}
*/

const fetchSkillLevelTasks = async (skill, level) => {
try {
let taskData, id;
const taskLevel = Number(level);

const task = await fetchDbForTask(skill, taskLevel);
if (!task.empty) {
task.forEach((doc) => {
id = doc.id;
taskData = doc.data();
});
return {
task: {
id,
...taskData,
},
};
}

return { taskNotFound: true };
} catch (err) {
logger.error("error getting tasks", err);
throw err;
}
};

/**
*
* @param username { string } : username which will be used to fetch all self tasks
Expand Down Expand Up @@ -250,5 +294,6 @@ module.exports = {
fetchUserCompletedTasks,
fetchActiveTaskMembers,
fetchSelfTask,
fetchSkillLevelTasks,
overdueTasks,
};
2 changes: 2 additions & 0 deletions routes/tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const tasks = require("../controllers/tasks");
const { createTask, updateTask, updateSelfTask } = require("../middlewares/validators/tasks");
const authorizeRoles = require("../middlewares/authorizeRoles");
const { APPOWNER, SUPERUSER } = require("../constants/roles");
const assignTask = require("../middlewares/assignTask");

router.get("/", tasks.fetchTasks);
router.get("/self", authenticate, tasks.getSelfTasks);
Expand All @@ -13,5 +14,6 @@ router.post("/", authenticate, authorizeRoles([APPOWNER, SUPERUSER]), createTask
router.patch("/:id", authenticate, authorizeRoles([APPOWNER, SUPERUSER]), updateTask, tasks.updateTask);
router.get("/:username", tasks.getUserTasks);
router.patch("/self/:id", authenticate, updateSelfTask, tasks.updateTaskStatus);
router.patch("/self/:id", authenticate, updateSelfTask, tasks.updateTaskStatus, assignTask);

module.exports = router;

0 comments on commit 22e4c5b

Please sign in to comment.