From aee3cea4c1f234f054fe75ca5ad545b024016284 Mon Sep 17 00:00:00 2001 From: Mehul Kiran Chaudhari <55375534+MehulKChaudhari@users.noreply.github.com> Date: Tue, 20 Aug 2024 17:48:52 +0530 Subject: [PATCH] clean-up: remove one time script to filter orphan tasks and old remplementation. --- controllers/tasks.js | 12 ------- middlewares/validators/tasks.js | 11 ------ models/tasks.js | 32 ------------------ routes/tasks.js | 1 - test/integration/tasks.test.js | 60 --------------------------------- 5 files changed, 116 deletions(-) diff --git a/controllers/tasks.js b/controllers/tasks.js index 9e36c639c..391435cbf 100644 --- a/controllers/tasks.js +++ b/controllers/tasks.js @@ -474,17 +474,6 @@ const updateStatus = async (req, res) => { } }; -const orphanTasks = async (req, res) => { - try { - const updatedTasksData = await tasks.updateOrphanTasksStatus(); - - return res.status(200).json({ message: "Orphan tasks filtered successfully", updatedTasksData }); - } catch (error) { - logger.error("Error in filtering orphan tasks", error); - return res.boom.badImplementation(INTERNAL_SERVER_ERROR); - } -}; - const getUsersHandler = async (req, res) => { try { const { size, cursor, q: queryString } = req.query; @@ -544,5 +533,4 @@ module.exports = { assignTask, updateStatus, getUsersHandler, - orphanTasks, }; diff --git a/middlewares/validators/tasks.js b/middlewares/validators/tasks.js index 5467137c8..5448c733b 100644 --- a/middlewares/validators/tasks.js +++ b/middlewares/validators/tasks.js @@ -7,7 +7,6 @@ const { Operators } = require("../../typeDefinitions/rqlParser"); const { daysOfWeek } = require("../../constants/constants"); const TASK_STATUS_ENUM = Object.values(TASK_STATUS); const MAPPED_TASK_STATUS_ENUM = Object.keys(MAPPED_TASK_STATUS); -const { validateMillisecondsTimestamp } = require("./utils"); const createTask = async (req, res, next) => { const schema = joi @@ -265,20 +264,10 @@ const getUsersValidator = async (req, res, next) => { } }; -const filterOrphanTasksValidator = async (req, res, next) => { - try { - await validateMillisecondsTimestamp(req.body, "lastOrphanTasksFilterationTimestamp"); - next(); - } catch (error) { - logger.error(`Error while validating request body for Orphan Tasks Filteration payload : ${error}`); - res.boom.badRequest(error); - } -}; module.exports = { createTask, updateTask, updateSelfTask, getTasksValidator, getUsersValidator, - filterOrphanTasksValidator, }; diff --git a/models/tasks.js b/models/tasks.js index 8b0754b1b..a13430fa7 100644 --- a/models/tasks.js +++ b/models/tasks.js @@ -1,6 +1,5 @@ const firestore = require("../utils/firestore"); const tasksModel = firestore.collection("tasks"); -const userModel = firestore.collection("users"); const ItemModel = firestore.collection("itemTags"); const dependencyModel = firestore.collection("taskDependencies"); const userUtils = require("../utils/users"); @@ -647,36 +646,6 @@ const updateTaskStatus = async () => { } }; -const updateOrphanTasksStatus = async () => { - try { - const users = []; - const batch = firestore.batch(); - const usersQuerySnapshot = await userModel.where("roles.in_discord", "==", false).get(); - - usersQuerySnapshot.forEach((user) => users.push({ ...user.data(), id: user.id })); - - let orphanTasksUpdatedCount = 0; - - for (const user of users) { - const tasksQuerySnapshot = await tasksModel - .where("assignee", "==", user.id) - .where("status", "not-in", [BACKLOG, COMPLETED, DONE]) - .get(); - tasksQuerySnapshot.forEach((taskDoc) => { - orphanTasksUpdatedCount++; - const taskRef = tasksModel.doc(taskDoc.id); - batch.update(taskRef, { status: BACKLOG, updated_at: Date.now() }); - }); - } - - await batch.commit(); - return { orphanTasksUpdatedCount }; - } catch (error) { - logger.error("Error marking tasks as backlog:", error); - throw error; - } -}; - const markUnDoneTasksOfArchivedUsersBacklog = async (users) => { try { let orphanTasksUpdatedCount = 0; @@ -718,6 +687,5 @@ module.exports = { getBuiltTasks, getOverdueTasks, updateTaskStatus, - updateOrphanTasksStatus, markUnDoneTasksOfArchivedUsersBacklog, }; diff --git a/routes/tasks.js b/routes/tasks.js index 5596f982c..e1bd45554 100644 --- a/routes/tasks.js +++ b/routes/tasks.js @@ -67,6 +67,5 @@ router.patch("/assign/self", authenticate, invalidateCache({ invalidationKeys: [ router.get("/users/discord", verifyCronJob, getUsersValidator, tasks.getUsersHandler); router.post("/migration", authenticate, authorizeRoles([SUPERUSER]), tasks.updateStatus); -router.post("/orphanTasks", authenticate, authorizeRoles([SUPERUSER]), tasks.orphanTasks); module.exports = router; diff --git a/test/integration/tasks.test.js b/test/integration/tasks.test.js index 6a8442875..949406f13 100644 --- a/test/integration/tasks.test.js +++ b/test/integration/tasks.test.js @@ -21,7 +21,6 @@ const { TASK_STATUS, tasksUsersStatus } = require("../../constants/tasks"); const updateTaskStatus = require("../fixtures/tasks/tasks1")(); const userStatusData = require("../fixtures/userStatus/userStatus"); const tasksModel = firestore.collection("tasks"); -const userDBModel = firestore.collection("users"); const discordService = require("../../services/discordService"); const { CRON_JOB_HANDLER } = require("../../constants/bot"); const { logType } = require("../../constants/logs"); @@ -1574,63 +1573,4 @@ describe("Tasks", function () { expect(tasksLogs.body.error).to.be.equal("Error: Error occurred"); }); }); - - describe("POST /tasks/orphanTasks", function () { - beforeEach(async function () { - const superUserId = await addUser(superUser); - superUserJwt = authService.generateAuthToken({ userId: superUserId }); - const user1 = userData[6]; - user1.roles.in_discord = false; - user1.updated_at = 1712053284000; - const user2 = userData[18]; - user2.roles.in_discord = false; - const [{ id: userId }, { id: userId2 }] = await Promise.all([userDBModel.add(user1), userDBModel.add(user2)]); - - const task1 = { - assignee: userId, - status: "ACTIVE", - }; - const task2 = { - assignee: userId2, - status: "COMPLETED", - }; - const task3 = { - assignee: userId2, - status: "IN_PROGRESS", - }; - const task4 = { - assignee: userId, - status: "DONE", - }; - await Promise.all([tasksModel.add(task1), tasksModel.add(task2), tasksModel.add(task3), tasksModel.add(task4)]); - }); - - afterEach(async function () { - await cleanDb(); - }); - - it("Should update status of orphan tasks to BACKLOG", async function () { - const res = await chai.request(app).post("/tasks/orphanTasks").set("cookie", `${cookieName}=${superUserJwt}`); - expect(res).to.have.status(200); - expect(res.body).to.deep.equal({ - message: "Orphan tasks filtered successfully", - updatedTasksData: { - orphanTasksUpdatedCount: 2, - }, - }); - }); - - it("Should return 400 if not super user", async function () { - const nonSuperUserId = await addUser(appOwner); - const nonSuperUserJwt = authService.generateAuthToken({ userId: nonSuperUserId }); - const res = await chai.request(app).post("/tasks/orphanTasks").set("Authorization", `Bearer ${nonSuperUserJwt}`); - - expect(res).to.have.status(401); - expect(res.body).to.deep.equal({ - statusCode: 401, - error: "Unauthorized", - message: "You are not authorized for this action.", - }); - }); - }); });