Skip to content

Commit 28bce3d

Browse files
add: one-time script to filter unfinished tasks from archived users. (#2040)
* add: one-time script to filter unfinished tasks from archived users. * fix: remove verified status as done --------- Co-authored-by: Amit Prakash <34869115+iamitprakash@users.noreply.github.com>
1 parent 23c956e commit 28bce3d

File tree

4 files changed

+32
-45
lines changed

4 files changed

+32
-45
lines changed

controllers/tasks.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -491,9 +491,7 @@ const updateStatus = async (req, res) => {
491491

492492
const orphanTasks = async (req, res) => {
493493
try {
494-
const { lastOrphanTasksFilterationTimestamp = 0 } = req.body;
495-
496-
const updatedTasksData = await tasks.updateOrphanTasksStatus(lastOrphanTasksFilterationTimestamp);
494+
const updatedTasksData = await tasks.updateOrphanTasksStatus();
497495

498496
return res.status(200).json({ message: "Orphan tasks filtered successfully", updatedTasksData });
499497
} catch (error) {

models/tasks.js

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -648,33 +648,29 @@ const updateTaskStatus = async () => {
648648
}
649649
};
650650

651-
const updateOrphanTasksStatus = async (lastOrphanTasksFilterationTimestamp) => {
652-
const lastTimestamp = Number(lastOrphanTasksFilterationTimestamp);
651+
const updateOrphanTasksStatus = async () => {
653652
try {
654653
const users = [];
655-
const currentTimeStamp = Date.now();
656-
657-
const usersQuerySnapshot = await userModel
658-
.where("roles.in_discord", "==", false)
659-
.where("updated_at", ">=", lastTimestamp)
660-
.where("updated_at", "<=", currentTimeStamp)
661-
.get();
654+
const batch = firestore.batch();
655+
const usersQuerySnapshot = await userModel.where("roles.in_discord", "==", false).get();
662656

663657
usersQuerySnapshot.forEach((user) => users.push({ ...user.data(), id: user.id }));
664658

665659
let orphanTasksUpdatedCount = 0;
666660

667661
for (const user of users) {
668662
const tasksQuerySnapshot = await tasksModel
669-
.where("assigneeId", "==", user.id)
670-
.where("status", "not-in", [COMPLETED, BACKLOG])
663+
.where("assignee", "==", user.id)
664+
.where("status", "not-in", [BACKLOG, COMPLETED, DONE])
671665
.get();
672-
tasksQuerySnapshot.forEach(async (taskDoc) => {
666+
tasksQuerySnapshot.forEach((taskDoc) => {
673667
orphanTasksUpdatedCount++;
674-
await tasksModel.doc(taskDoc.id).update({ status: BACKLOG });
668+
const taskRef = tasksModel.doc(taskDoc.id);
669+
batch.update(taskRef, { status: BACKLOG, updated_at: Date.now() });
675670
});
676671
}
677672

673+
await batch.commit();
678674
return { orphanTasksUpdatedCount };
679675
} catch (error) {
680676
logger.error("Error marking tasks as backlog:", error);
@@ -691,7 +687,7 @@ const markUnDoneTasksOfArchivedUsersBacklog = async (users) => {
691687
.where("assignee", "==", user.id)
692688
.where("status", "not-in", [COMPLETED, DONE, BACKLOG])
693689
.get();
694-
tasksQuerySnapshot.forEach(async (taskDoc) => {
690+
tasksQuerySnapshot.forEach((taskDoc) => {
695691
orphanTasksUpdatedCount++;
696692
const taskRef = tasksModel.doc(taskDoc.id);
697693
batch.update(taskRef, { status: BACKLOG, updated_at: Date.now() });

routes/tasks.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ const {
88
updateSelfTask,
99
getTasksValidator,
1010
getUsersValidator,
11-
filterOrphanTasksValidator,
1211
} = require("../middlewares/validators/tasks");
1312
const authorizeRoles = require("../middlewares/authorizeRoles");
1413
const { authorizeAndAuthenticate } = require("../middlewares/authorizeUsersAndService");
@@ -68,6 +67,6 @@ router.patch("/assign/self", authenticate, invalidateCache({ invalidationKeys: [
6867
router.get("/users/discord", verifyCronJob, getUsersValidator, tasks.getUsersHandler);
6968

7069
router.post("/migration", authenticate, authorizeRoles([SUPERUSER]), tasks.updateStatus);
71-
router.post("/orphanTasks", verifyCronJob, filterOrphanTasksValidator, tasks.orphanTasks);
70+
router.post("/orphanTasks", authenticate, authorizeRoles([SUPERUSER]), tasks.orphanTasks);
7271

7372
module.exports = router;

test/integration/tasks.test.js

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1573,66 +1573,60 @@ describe("Tasks", function () {
15731573
});
15741574

15751575
describe("POST /tasks/orphanTasks", function () {
1576-
let jwtToken;
1577-
15781576
beforeEach(async function () {
1577+
const superUserId = await addUser(superUser);
1578+
superUserJwt = authService.generateAuthToken({ userId: superUserId });
15791579
const user1 = userData[6];
15801580
user1.roles.in_discord = false;
15811581
user1.updated_at = 1712053284000;
15821582
const user2 = userData[18];
1583-
user2.updated_at = 1712064084000;
1583+
user2.roles.in_discord = false;
15841584
const [{ id: userId }, { id: userId2 }] = await Promise.all([userDBModel.add(user1), userDBModel.add(user2)]);
15851585

15861586
const task1 = {
1587-
assigneeId: userId,
1587+
assignee: userId,
15881588
status: "ACTIVE",
15891589
};
15901590
const task2 = {
1591-
assigneeId: userId2,
1591+
assignee: userId2,
15921592
status: "COMPLETED",
15931593
};
15941594
const task3 = {
1595-
assigneeId: userId2,
1595+
assignee: userId2,
15961596
status: "IN_PROGRESS",
15971597
};
1598-
await Promise.all([tasksModel.add(task1), tasksModel.add(task2), tasksModel.add(task3)]);
1599-
1600-
jwtToken = generateCronJobToken({ name: CRON_JOB_HANDLER });
1598+
const task4 = {
1599+
assignee: userId,
1600+
status: "DONE",
1601+
};
1602+
await Promise.all([tasksModel.add(task1), tasksModel.add(task2), tasksModel.add(task3), tasksModel.add(task4)]);
16011603
});
16021604

16031605
afterEach(async function () {
16041606
await cleanDb();
16051607
});
1606-
it("Should update status of orphan tasks to BACKLOG", async function () {
1607-
const res = await chai.request(app).post("/tasks/orphanTasks").set("Authorization", `Bearer ${jwtToken}`).send({
1608-
lastOrphanTasksFilterationTimestamp: 1712040715000,
1609-
});
16101608

1609+
it("Should update status of orphan tasks to BACKLOG", async function () {
1610+
const res = await chai.request(app).post("/tasks/orphanTasks").set("cookie", `${cookieName}=${superUserJwt}`);
16111611
expect(res).to.have.status(200);
16121612
expect(res.body).to.deep.equal({
16131613
message: "Orphan tasks filtered successfully",
16141614
updatedTasksData: {
16151615
orphanTasksUpdatedCount: 2,
16161616
},
16171617
});
1618-
}).timeout(10000);
1618+
});
16191619

1620-
it("Should return 400 if not cron worker", async function () {
1620+
it("Should return 400 if not super user", async function () {
16211621
const nonSuperUserId = await addUser(appOwner);
16221622
const nonSuperUserJwt = authService.generateAuthToken({ userId: nonSuperUserId });
1623-
const res = await chai
1624-
.request(app)
1625-
.post("/tasks/orphanTasks")
1626-
.set("Authorization", `Bearer ${nonSuperUserJwt}`)
1627-
.send({
1628-
lastOrphanTasksFilterationTimestamp: 1712040715000,
1629-
});
1623+
const res = await chai.request(app).post("/tasks/orphanTasks").set("Authorization", `Bearer ${nonSuperUserJwt}`);
16301624

1631-
expect(res).to.have.status(400);
1625+
expect(res).to.have.status(401);
16321626
expect(res.body).to.deep.equal({
1633-
statusCode: 400,
1634-
error: "Bad Request",
1635-
message: "Unauthorized Cron Worker",
1627+
statusCode: 401,
1628+
error: "Unauthorized",
1629+
message: "You are not authorized for this action.",
16361630
});
16371631
});
16381632
});

0 commit comments

Comments
 (0)