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

Fix missed updates script #1787

Merged
merged 8 commits into from
Dec 15, 2023
10 changes: 8 additions & 2 deletions controllers/tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -492,9 +492,15 @@ const getUsersHandler = async (req, res) => {
size: size && Number.parseInt(size),
excludedDates: date?.map((date) => Number.parseInt(date.value)),
excludedDays: weekday?.map((day) => daysOfWeek[day.value]),
dateGap: !!daysGap && daysGap.length === 1 && Number.parseInt(daysGap[0].value),
dateGap: !!daysGap && daysGap.length === 1 ? Number.parseInt(daysGap[0].value) : null,
});
return res.status(200).json(response);

if (response.error) {
return res.boom.badRequest(response.message);
}
return res
.status(200)
.json({ message: "Discord details of users with status missed updates fetched successfully", data: response });
} else {
return res.boom.badRequest("Unknown type and query");
}
Expand Down
12 changes: 6 additions & 6 deletions models/discordactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
const userStatusModel = firestore.collection("usersStatus");
const usersUtils = require("../utils/users");
const { getUsersBasedOnFilter, fetchUser } = require("./users");
const { convertDaysToMilliseconds } = require("../utils/time");
const { convertDaysToMilliseconds, convertMillisToSeconds } = require("../utils/time");
const { chunks } = require("../utils/array");
const tasksModel = firestore.collection("tasks");
const { FIRESTORE_IN_CLAUSE_SIZE } = require("../constants/users");
Expand Down Expand Up @@ -492,7 +492,7 @@

for (let i = 0; i < nicknameUpdateBatches.length; i++) {
const promises = [];
const usersStatusDocsBatch = nicknameUpdateBatches[i];

Check warning on line 495 in models/discordactions.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Variable Assigned to Object Injection Sink
usersStatusDocsBatch.forEach((document) => {
const doc = document.data();
const userId = doc.userId;
Expand Down Expand Up @@ -874,13 +874,12 @@
}
}

let taskQuery = buildTasksQueryForMissedUpdates(gapWindowStart, size);
let taskQuery = buildTasksQueryForMissedUpdates(size);

if (cursor) {
const data = await tasksModel.doc(cursor).get();
if (!data.data()) {
return {
statusCode: 400,
error: "Bad Request",
message: `Invalid cursor: ${cursor}`,
};
Expand All @@ -894,7 +893,9 @@

stats.tasks = tasksQuerySnapshot.size;
tasksQuerySnapshot.forEach((doc) => {
const taskAssignee = doc.data().assignee;
const { assignee: taskAssignee, startedOn: taskStartedOn } = doc.data();
if (!taskAssignee || taskStartedOn >= convertMillisToSeconds(gapWindowStart)) return;

const taskId = doc.id;

if (usersMap.has(taskAssignee)) {
Expand Down Expand Up @@ -952,8 +953,7 @@
});
});

const discordUserList = await Promise.all(discordUsersPromise);

const discordUserList = await discordUsersPromise;
const discordUserMap = new Map();
discordUserList.forEach((discordUser) => {
const discordUserData = { isBot: !!discordUser.user.bot };
Expand Down
20 changes: 13 additions & 7 deletions test/integration/tasks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1397,9 +1397,12 @@ describe("Tasks", function () {
.query({ q: `status:${tasksUsersStatus.MISSED_UPDATES}` })
.set("Authorization", `Bearer ${jwtToken}`);
expect(response.body).to.be.deep.equal({
usersToAddRole: [activeUserWithProgressUpdates.discordId],
tasks: 4,
missedUpdatesTasks: 3,
message: "Discord details of users with status missed updates fetched successfully",
data: {
usersToAddRole: [activeUserWithProgressUpdates.discordId],
tasks: 4,
missedUpdatesTasks: 3,
},
});
expect(response.status).to.be.equal(200);
});
Expand All @@ -1408,14 +1411,17 @@ describe("Tasks", function () {
.request(app)
.get("/tasks/users/discord")
.query({
size: 3,
size: 5,
q: `status:${tasksUsersStatus.MISSED_UPDATES} -weekday:sun -weekday:mon -weekday:tue -weekday:wed -weekday:thu -weekday:fri -date:231423432 -days-count:4`,
})
.set("Authorization", `Bearer ${jwtToken}`);
expect(response.body).to.be.deep.equal({
usersToAddRole: [],
tasks: 0,
missedUpdatesTasks: 0,
message: "Discord details of users with status missed updates fetched successfully",
data: {
usersToAddRole: [],
tasks: 4,
missedUpdatesTasks: 0,
},
});
expect(response.status).to.be.equal(200);
});
Expand Down
4 changes: 2 additions & 2 deletions test/unit/models/discordactions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ describe("discordactions", function () {
});
expect(result).to.be.an("object");
expect(result).to.be.deep.equal({
tasks: 0,
tasks: 4,
missedUpdatesTasks: 0,
usersToAddRole: [],
});
Expand Down Expand Up @@ -716,7 +716,7 @@ describe("discordactions", function () {
});
expect(result).to.be.an("object");
expect(result).to.be.deep.equal({
tasks: 1,
tasks: 5,
missedUpdatesTasks: 0,
usersToAddRole: [],
});
Expand Down
9 changes: 4 additions & 5 deletions utils/tasks.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
const { getUsername, getUserId, getParticipantUsernames, getParticipantUserIds } = require("./users");
const { TASK_TYPE, MAPPED_TASK_STATUS, COMPLETED_TASK_STATUS } = require("../constants/tasks");
const { TASK_TYPE, MAPPED_TASK_STATUS, COMPLETED_TASK_STATUS, TASK_STATUS } = require("../constants/tasks");
const fireStore = require("../utils/firestore");
const tasksModel = fireStore.collection("tasks");
const { convertMillisToSeconds } = require("./time");

const fromFirestoreData = async (task) => {
if (!task) {
Expand Down Expand Up @@ -113,11 +112,11 @@ const parseSearchQuery = (queryString) => {
return searchParams;
};

const buildTasksQueryForMissedUpdates = (startedOnTimestamp, size) => {
const buildTasksQueryForMissedUpdates = (size) => {
const completedTasksStatusList = Object.values(COMPLETED_TASK_STATUS);
return tasksModel
.where("status", "not-in", completedTasksStatusList)
.where("startedOn", "<", convertMillisToSeconds(startedOnTimestamp))
.where("status", "not-in", [...completedTasksStatusList, TASK_STATUS.AVAILABLE])
.orderBy("status")
.orderBy("assignee")
.limit(size);
};
Expand Down
2 changes: 1 addition & 1 deletion utils/time.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const convertDaysToMilliseconds = (days) => {
*/
const convertMillisToSeconds = (milliseconds) => {
if (typeof milliseconds !== "number") throw Error("Not a number");
return milliseconds / 1000;
return Math.round(milliseconds / 1000);
};
/**
* Returns time in seconds of timestamp after given duration
Expand Down
Loading