feat: Add task request flow#931
Conversation
Earlier in the task flow the user would automatically be assigned
the task, in this change the flow is changed to create a task
request first which will be approved by superuser
|
Waiting on: Task Request API Contract |
Update create task request model function
Add approve task controller and model fucntion
Add fetch tasks for user controller and model function
toFirestoreData for task request has been added
This function validates the payload and returns a valid firestore
object.
models/taskRequests.js
Outdated
| const users = await Promise.all( | ||
| taskRequest.requestors.map((requestor) => { | ||
| if (visitedUsers.includes(requestor)) { | ||
| return visitedUsersId.find((visitedUserId) => (visitedUserId.id = requestor)); |
There was a problem hiding this comment.
The bug is that the visitedUsersId variable is not being initialized. This means that the find() method will always return undefined. To fix this bug, you need to initialize the visitedUsersId variable to an empty array. You can do this by adding the following line to the top of the code:
There was a problem hiding this comment.
visitedUsersId is already defined as a Set above, thanks for pointing this out though as I need to use .has method instead of .includes.
controllers/tasksRequests.js
Outdated
| try { | ||
| const data = await taskRequestsModel.fetchTaskRequests(); | ||
|
|
||
| return res.status(200).json({ |
There was a problem hiding this comment.
In case there are no tasks found we should be doing a 404 Not found status right??
controllers/tasksRequests.js
Outdated
|
|
||
| const { userStatusExists, data: userStatus } = await userStatusModel.getUserStatus(userId); | ||
| if (!userStatusExists) { | ||
| return res.boom.conflict("User status does not exist"); |
There was a problem hiding this comment.
instead of doing 3 such rigorous if checks, how about we create a separate validator function?
controllers/tasksRequests.js
Outdated
| } | ||
|
|
||
| const { userStatusExists, data: userStatus } = await userStatusModel.getUserStatus(userId); | ||
| if (!userStatusExists) { |
There was a problem hiding this comment.
Same validator function suggestion as above
controllers/tasksRequests.js
Outdated
| if (!userStatusExists) { | ||
| return res.boom.conflict("User status does not exists"); | ||
| } | ||
| if (userStatus.currentStatus.state === userState.OOO) { |
There was a problem hiding this comment.
capital OOO seems like a bad choice of the property name. We can go with OUT_OF_OFFICE. In this case maybe is a constant but OOO seems smelly).
There was a problem hiding this comment.
This constant has been there for a while, I believe its best to deal with this issue separately.
There was a problem hiding this comment.
I guess this is the time to fix this @RitikJaiswal75
models/taskRequests.js
Outdated
| continue; | ||
| } | ||
|
|
||
| const { user } = await userModel.fetchUser({ userId: requestor }); |
There was a problem hiding this comment.
instead of doing two await calls inside a for-of loop, why not we keep them as Promises and do a Promise.all outside the loop. That will drastically improve the performance of this code. We can sit and discuss in a call if you have any doubts.
DashDeipayan
left a comment
There was a problem hiding this comment.
Good job with the PR. Lets get it done
| const { validateUser } = require("../middlewares/taskRequests"); | ||
|
|
||
| router.get("/", authenticate, authorizeRoles([SUPERUSER]), cache(), taskRequests.fetchTaskRequests); | ||
| router.post("/addOrUpdate", authenticate, validateUser, taskRequests.addOrUpdate); |
Check failure
Code scanning / CodeQL
Missing rate limiting
|
|
||
| router.get("/", authenticate, authorizeRoles([SUPERUSER]), cache(), taskRequests.fetchTaskRequests); | ||
| router.post("/addOrUpdate", authenticate, validateUser, taskRequests.addOrUpdate); | ||
| router.patch("/approve", authenticate, authorizeRoles([SUPERUSER]), validateUser, taskRequests.approveTaskRequest); |
Check failure
Code scanning / CodeQL
Missing rate limiting
DashDeipayan
left a comment
There was a problem hiding this comment.
There are few comments that still needs to be addressed(not every comment is a change request, some are just queries 🙂)
models/taskRequests.js
Outdated
| const taskRequestData = taskRequestsSnapshot.data(); | ||
| const { requestors } = taskRequestData; | ||
|
|
||
| const { taskData } = await tasksModel.fetchTask(taskRequestData.taskId); |
There was a problem hiding this comment.
You can move this line out of the for-loop by creating an array of promises(not writing await statements inside a for loop) and outside the for-loop this will be resolved with a promise.all()
models/taskRequests.js
Outdated
| const { requestors } = taskRequestData; | ||
|
|
||
| const { taskData } = await tasksModel.fetchTask(taskRequestData.taskId); | ||
| const users = await Promise.all(requestors.map((requestor) => userModel.fetchUser({ userId: requestor }))); |
There was a problem hiding this comment.
Same suggestion as above for the users
iamitprakash
left a comment
There was a problem hiding this comment.
please add test coverage stats
Covering all test cases for controllers, improving it from 86.95 to 89.18
|
Coverage stats are as follows
|
|
@iamitprakash Have commented the test coverage please check |
* add in_discord false role for every new user (#1167) * feat: Add task request flow (#931) * feat: Add get route for tasks requests for Super User * feat: Add createTaskRequest model function * feat: Add controller for adding task request * feat: Edit assign task flow to create task reques instead Earlier in the task flow the user would automatically be assigned the task, in this change the flow is changed to create a task request first which will be approved by superuser * feat: Add controllers and model functions for task tasksRequests Update create task request model function Add approve task controller and model fucntion Add fetch tasks for user controller and model function * fix: Call createTaskRequest model function in task controller * feat: Add function to validate task request payload toFirestoreData for task request has been added This function validates the payload and returns a valid firestore object. * feat: Make create task request a working implementation from stub * feat: Add endpoint for task requests * feat: Make fetch requests working from stub * feat: Make approve task route working from stub * test: Add test for put request in task request * fix: Task request when a already existed task is requested by another user * test: Add test for already existing tasks requests * fix: task requests cached response is fetched to unauthorized users * tests: add tests for fetch request * test: Add test for approve task request * feat: Add more test for approve tasks * fix: Return response instead of throwing error in controllers * feat: update task request controller added * refactor: Add different controller for updating task request requestor Earlier createTaskRequest handled updation and creation of task request, this controller had been deconstructed into two controllers i.e createTaskRequest and addRequestor * fix: Make one function for updating and creating task request * fix: change create controller name to addOrUpdate add sinon stubs in test * fix: remove mock verify function that wraps sinon stub * Delete empty file * fix: Change createTaskRequest to addOrUpdate * fix: Task request error message for task does not exist * refactor(tasksRequests): Review comments Use shorthand firestore methode `docs` for creating array of tasksRequests Fix server errors * feat: send user to task request fetch api * fix: Check user status before creating task request Check if the user status exists before creating task request * fix: Review changes - Use array.some for requestors - Store taskId in the taskrequest obj - Fix test description for approve route on 400 request status * fix: Review comments - Fixed variable name from requestorExists to already requesting - Restrict OOO and active users to request for tasks * fix: Review comments - Remove redundant `.limit()` - Remove redundant if statement * fix: match task request data with the data-model * refactor: code restructure for taskrequests controller and models * feat: Fix visited users bug mentioned in review comments * fix: fetchTaskRequest task request requestors data * fix: Review comments * fix: Use Promise.all to fetch task requests * fix: Generic Object Sink * test: Improve test coverage Covering all test cases for controllers, improving it from 86.95 to 89.18 * [Live-Site] Feat/event room apis (#994) * docs: made the prerequisites setup more clear * docs: fixed an unwanted change * feat: initial setup for 100ms * fix: private variable issue * feat: rooms apis * feat: added jsdoc comments to the controller and model * fix: added object destructuring to payloads * test: added unit tests for models * test: added integration tests for room APIs * fix: removed logs * fix: added constant and refresh token function. * fix: replaced uuid with crypto package to generate the unique ids. * fix: unit test for event update API * fix: removed crypto module * fix: rooms to events * fix: fallback for env variables in prod * fix: changed the endActiveRoom from delete to patch * fix: env variables in prod * feat: added removeUnwantedProperties utility function * fix: added env in test config * fix: getAllRooms controller * chore: added env variables to the config * fix: sinon variable naming * fix: added www-authenticate header check and removed payload check in post method * fix: added config default value and removed extra expiration check in isTokenExpired method * fix: changed naming from rooms to events everywhere * fix: integration and unit tests according to the changes * add: added validators for response * fix: added a constant and updated the imports * fix: joinEvent API * fix: integration test for event apis * fix: utility function * fix: integration tests for events * [Fix #1173]: Add proper room_id and user_id (#1174) Co-authored-by: sanketumesh.dhabarde <sanketumesh.dhabarde@clarivate.com> * add tests for arts middleware (#1182) * add tests for join section models (#1177) * Script to Add Unverified Role to Users Without Discord Link (#1181) * add unverified role to all users who have not linked their discord id * add unit tests * add integration tests * move generate auth token for cloudflare to a util * beautify addRoleToUser function in services * add role id to test config * add unverified role id to configs * change the url from discord actions to post users --------- Co-authored-by: Tanishq Singla <tanishqsingla00@gmail.com> Co-authored-by: Prerana Nawar <61601706+prerana1821@users.noreply.github.com> Co-authored-by: Sanket Dhabarde <40385118+SanketDhabarde@users.noreply.github.com> Co-authored-by: sanketumesh.dhabarde <sanketumesh.dhabarde@clarivate.com>
related to closes #904
Task Request Flow
This merge introduces api for task request feature.
The features include