Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clean-up: remove one time script to filter orphan tasks and old rempl… #2082

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions controllers/tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -544,5 +533,4 @@ module.exports = {
assignTask,
updateStatus,
getUsersHandler,
orphanTasks,
};
11 changes: 0 additions & 11 deletions middlewares/validators/tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
};
32 changes: 0 additions & 32 deletions models/tasks.js
Original file line number Diff line number Diff line change
@@ -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");
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -718,6 +687,5 @@ module.exports = {
getBuiltTasks,
getOverdueTasks,
updateTaskStatus,
updateOrphanTasksStatus,
markUnDoneTasksOfArchivedUsersBacklog,
};
1 change: 0 additions & 1 deletion routes/tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
60 changes: 0 additions & 60 deletions test/integration/tasks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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.",
});
});
});
});
Loading