Skip to content

Commit caeb2fe

Browse files
chore: remove lastOooUntil migration artifacts and code (#2507) (#2508)
1 parent aada0f8 commit caeb2fe

File tree

4 files changed

+3
-165
lines changed

4 files changed

+3
-165
lines changed

controllers/users.js

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const {
3131
USERS_PATCH_HANDLER_SUCCESS_MESSAGES,
3232
} = require("../constants/users");
3333
const { addLog } = require("../models/logs");
34-
const { getUserStatus, runLastOooUntilMigration } = require("../models/userStatus");
34+
const { getUserStatus } = require("../models/userStatus");
3535
const config = require("config");
3636
const { generateUniqueUsername } = require("../services/users");
3737
const userService = require("../services/users");
@@ -80,19 +80,6 @@ const getUserById = async (req, res) => {
8080
});
8181
};
8282

83-
const updateLastOooUntil = async (req, res) => {
84-
try {
85-
const summary = await runLastOooUntilMigration();
86-
return res.status(200).json({
87-
message: "lastOooUntil migration executed successfully",
88-
data: summary,
89-
});
90-
} catch (error) {
91-
logger.error(`Error while running lastOooUntil migration: ${error}`);
92-
return res.boom.badImplementation(INTERNAL_SERVER_ERROR);
93-
}
94-
};
95-
9683
/**
9784
* Fetches the data about our users
9885
*
@@ -1198,6 +1185,5 @@ module.exports = {
11981185
isDeveloper,
11991186
getIdentityStats,
12001187
updateUsernames,
1201-
updateLastOooUntil,
12021188
updateProfile,
12031189
};

models/userStatus.js

Lines changed: 1 addition & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,18 @@ const {
1616
getNextDayTimeStamp,
1717
convertTimestampsToUTC,
1818
resolveLastOooUntil,
19-
normalizeTimestamp,
2019
} = require("../utils/userStatus");
2120
const { TASK_STATUS } = require("../constants/tasks");
2221
const userStatusModel = firestore.collection("usersStatus");
2322
const tasksModel = firestore.collection("tasks");
24-
const { userState, statusState } = require("../constants/userStatus");
23+
const { userState } = require("../constants/userStatus");
2524
const discordRoleModel = firestore.collection("discord-roles");
2625
const memberRoleModel = firestore.collection("member-group-roles");
2726
const usersCollection = firestore.collection("users");
28-
const userFutureStatusCollection = firestore.collection("userFutureStatus");
2927
const config = require("config");
3028
const DISCORD_BASE_URL = config.get("services.discordBot.baseUrl");
3129
const { generateAuthTokenForCloudflare } = require("../utils/discord-actions");
3230

33-
const LAST_OOO_MIGRATION_BATCH_SIZE = 450;
34-
3531
// added this function here to avoid circular dependency
3632
/**
3733
*
@@ -130,98 +126,6 @@ const addGroupIdleRoleToDiscordUser = async (userId) => {
130126
}
131127
};
132128

133-
const getLatestAppliedOooTimestamp = async (userId) => {
134-
const snapshot = await userFutureStatusCollection
135-
.where("userId", "==", userId)
136-
.where("status", "==", userState.OOO)
137-
.where("state", "==", statusState.APPLIED)
138-
.get();
139-
140-
let latestIntervalEnd = null;
141-
snapshot.forEach((doc) => {
142-
const futureData = doc.data();
143-
const candidate = normalizeTimestamp(futureData.endsOn) ?? normalizeTimestamp(futureData.from);
144-
if (candidate && (!latestIntervalEnd || candidate > latestIntervalEnd)) {
145-
latestIntervalEnd = candidate;
146-
}
147-
});
148-
149-
return latestIntervalEnd;
150-
};
151-
152-
const determineLastOooUntilUpdate = async (docData) => {
153-
const currentState = docData.currentStatus?.state;
154-
const normalizedExisting = normalizeTimestamp(docData.lastOooUntil);
155-
156-
if (currentState === userState.OOO) {
157-
return docData.lastOooUntil === null ? undefined : null;
158-
}
159-
160-
if (normalizedExisting !== null) {
161-
return undefined;
162-
}
163-
164-
const currentUntil = normalizeTimestamp(docData.currentStatus?.until);
165-
if (currentUntil) {
166-
return currentUntil;
167-
}
168-
169-
return await getLatestAppliedOooTimestamp(docData.userId);
170-
};
171-
172-
const runLastOooUntilMigration = async () => {
173-
const summary = {
174-
processed: 0,
175-
updated: 0,
176-
skipped: 0,
177-
failed: 0,
178-
failedUserIds: [],
179-
};
180-
181-
try {
182-
const snapshot = await userStatusModel.get();
183-
let batch = firestore.batch();
184-
let batchOperations = 0;
185-
186-
for (const document of snapshot.docs) {
187-
summary.processed++;
188-
const docData = document.data();
189-
190-
try {
191-
const updateValue = await determineLastOooUntilUpdate(docData);
192-
if (updateValue !== undefined) {
193-
batch.update(document.ref, { lastOooUntil: updateValue });
194-
batchOperations++;
195-
summary.updated++;
196-
} else {
197-
summary.skipped++;
198-
}
199-
} catch (error) {
200-
summary.failed++;
201-
summary.failedUserIds.push(docData.userId);
202-
logger.error(
203-
`Error while updating lastOooUntil for user ${docData.userId}: ${error.message ?? error.toString()}`
204-
);
205-
}
206-
207-
if (batchOperations && batchOperations % LAST_OOO_MIGRATION_BATCH_SIZE === 0) {
208-
await batch.commit();
209-
batch = firestore.batch();
210-
batchOperations = 0;
211-
}
212-
}
213-
214-
if (batchOperations) {
215-
await batch.commit();
216-
}
217-
218-
return summary;
219-
} catch (error) {
220-
logger.error(`Error while running last OOO backfill migration: ${error}`);
221-
throw error;
222-
}
223-
};
224-
225129
/**
226130
* @param userId {string} : id of the user
227131
* @returns {Promise<userStatusModel|string>} : returns id of the deleted userStatus
@@ -868,5 +772,4 @@ module.exports = {
868772
cancelOooStatus,
869773
getGroupRole,
870774
addFutureStatus,
871-
runLastOooUntilMigration,
872775
};

routes/users.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ const authenticateProfile = require("../middlewares/authenticateProfile");
1515
const { devFlagMiddleware } = require("../middlewares/devFlag");
1616
const { userAuthorization } = require("../middlewares/userAuthorization");
1717
const conditionalMiddleware = require("../middlewares/conditionalMiddleware");
18-
const { commonRateLimiter } = require("../middlewares/rateLimiting");
1918

2019
router.post("/", authorizeAndAuthenticate([ROLES.SUPERUSER], [Services.CRON_JOB_HANDLER]), users.markUnverified);
2120
router.post("/update-in-discord", authenticate, authorizeRoles([SUPERUSER]), users.setInDiscordScript);
@@ -80,11 +79,5 @@ router.patch("/rejectDiff", authenticate, authorizeRoles([SUPERUSER]), users.rej
8079
router.patch("/:userId", authenticate, conditionalMiddleware(userValidator.updateUser), users.updateProfile);
8180
router.get("/suggestedUsers/:skillId", authenticate, authorizeRoles([SUPERUSER]), users.getSuggestedUsers);
8281
router.post("/batch-username-update", authenticate, authorizeRoles([SUPERUSER]), users.updateUsernames);
83-
router.post(
84-
"/migration/update-last-ooo-until",
85-
authenticate,
86-
authorizeRoles([SUPERUSER]),
87-
commonRateLimiter,
88-
users.updateLastOooUntil
89-
);
82+
9083
module.exports = router;

test/integration/users.test.js

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ const { addJoinData, addOrUpdate } = require("../../models/users");
2828
const userStatusModel = require("../../models/userStatus");
2929
const { MAX_USERNAME_LENGTH } = require("../../constants/users.ts");
3030
const { TASK_STATUS } = require("../../constants/tasks");
31-
const { userState } = require("../../constants/userStatus");
3231
const userRoleUpdate = userData[4];
3332
const userRoleUnArchived = userData[13];
3433
const userAlreadyMember = userData[0];
@@ -2424,49 +2423,6 @@ describe("Users", function () {
24242423
});
24252424
});
24262425

2427-
describe("POST /users/migration/update-last-ooo-until", function () {
2428-
it("should backfill lastOooUntil for eligible users", async function () {
2429-
const migrationUser = userData[7];
2430-
const migrationUserId = await addUser(migrationUser);
2431-
const twoDaysAgo = new Date().setUTCHours(0, 0, 0, 0) - 2 * 24 * 60 * 60 * 1000;
2432-
2433-
await firestore.collection("usersStatus").add({
2434-
userId: migrationUserId,
2435-
currentStatus: {
2436-
state: userState.ACTIVE,
2437-
message: "",
2438-
from: twoDaysAgo,
2439-
until: twoDaysAgo,
2440-
updatedAt: twoDaysAgo,
2441-
},
2442-
});
2443-
2444-
const res = await chai
2445-
.request(app)
2446-
.post("/users/migration/update-last-ooo-until")
2447-
.set("cookie", `${cookieName}=${superUserAuthToken}`)
2448-
.send();
2449-
2450-
expect(res).to.have.status(200);
2451-
expect(res.body.message).to.equal("lastOooUntil migration executed successfully");
2452-
expect(res.body.data.updated).to.be.greaterThan(0);
2453-
2454-
const snapshot = await firestore.collection("usersStatus").where("userId", "==", migrationUserId).limit(1).get();
2455-
const [statusDoc] = snapshot.docs;
2456-
expect(statusDoc.data().lastOooUntil).to.equal(twoDaysAgo);
2457-
});
2458-
2459-
it("should reject migration for non super users", async function () {
2460-
const res = await chai
2461-
.request(app)
2462-
.post("/users/migration/update-last-ooo-until")
2463-
.set("cookie", `${cookieName}=${jwt}`)
2464-
.send();
2465-
2466-
expect(res).to.have.status(401);
2467-
});
2468-
});
2469-
24702426
describe("POST /update-in-discord", function () {
24712427
it("it returns proper response", function (done) {
24722428
chai

0 commit comments

Comments
 (0)