From f794db7d887be4aec367e0edcf30995e7e61830c Mon Sep 17 00:00:00 2001 From: Mehul Kiran Chaudhari <55375534+MehulKChaudhari@users.noreply.github.com> Date: Tue, 26 Mar 2024 01:28:40 +0530 Subject: [PATCH] update updated_at in user documents whenever user data is updated (#1980) --- controllers/auth.js | 2 +- controllers/staging.js | 3 +++ models/members.js | 2 ++ models/users.js | 8 +++++--- services/users.js | 1 + 5 files changed, 12 insertions(+), 4 deletions(-) diff --git a/controllers/auth.js b/controllers/auth.js index 335830799..bf0835a29 100644 --- a/controllers/auth.js +++ b/controllers/auth.js @@ -80,7 +80,7 @@ const githubAuthCallback = (req, res, next) => { github_created_at: Number(new Date(user._json.created_at).getTime()), github_user_id: user.id, created_at: Date.now(), - updated_at: Date.now(), + updated_at: null, }; const { userId, incompleteUserDetails, role } = await users.addOrUpdate(userData); diff --git a/controllers/staging.js b/controllers/staging.js index 2afe18c45..f87b5f896 100644 --- a/controllers/staging.js +++ b/controllers/staging.js @@ -15,6 +15,7 @@ const updateRoles = async (req, res) => { ...userData.roles, ...req.body, }, + updated_at: Date.now(), }, userId ); @@ -48,6 +49,7 @@ const removePrivileges = async (req, res) => { ...member.roles, member: false, }, + updated_at: Date.now(), }, member.id ) @@ -61,6 +63,7 @@ const removePrivileges = async (req, res) => { ...superUser.roles, super_user: false, }, + updated_at: Date.now(), }, superUser.id ) diff --git a/models/members.js b/models/members.js index 4f469f692..a666b210e 100644 --- a/models/members.js +++ b/models/members.js @@ -54,6 +54,7 @@ const moveToMembers = async (userId) => { const roles = user.roles ? { ...user.roles, member: true } : { member: true }; await userModel.doc(userId).update({ roles, + updated_at: Date.now(), }); return { isAlreadyMember: false, movedToMember: true }; } catch (err) { @@ -101,6 +102,7 @@ const addArchiveRoleToMembers = async (userId) => { const roles = { ...user.roles, [ROLES.ARCHIVED]: true }; await userModel.doc(userId).update({ roles, + updated_at: Date.now(), }); return { isArchived: false }; } catch (err) { diff --git a/models/users.js b/models/users.js index 2c1803daf..a20084fc2 100644 --- a/models/users.js +++ b/models/users.js @@ -61,7 +61,7 @@ const addOrUpdate = async (userData, userId = null) => { user = await userModel.where("github_id", "==", userData.github_id).limit(1).get(); } if (user && !user.empty && user.docs !== null) { - await userModel.doc(user.docs[0].id).set(userData, { merge: true }); + await userModel.doc(user.docs[0].id).set({ ...userData, updated_at: Date.now() }, { merge: true }); const data = user.docs[0].data(); return { isNewUser: false, @@ -308,6 +308,7 @@ const setIncompleteUserDetails = async (userId) => { if (doc.exists) { return userRef.update({ incompleteUserDetails: false, + updated_at: Date.now(), }); } return {}; @@ -423,6 +424,7 @@ const updateUserPicture = async (image, userId) => { const userDoc = userModel.doc(userId); await userDoc.update({ picture: image, + updated_at: Date.now(), }); } catch (err) { logger.error("Error updating user picture data", err); @@ -788,7 +790,7 @@ const updateUsersInBatch = async (usersData) => { usersData.forEach((user) => { const id = user.id; delete user.id; - bulkWriter.update(userModel.doc(id), user); + bulkWriter.update(userModel.doc(id), { ...user, updated_at: Date.now() }); }); await bulkWriter.close(); @@ -913,7 +915,7 @@ const addGithubUserId = async (page, size) => { }) .then((data) => { const githubUserId = data.id; - batchWrite.update(userDoc.ref, { github_user_id: `${githubUserId}` }); + batchWrite.update(userDoc.ref, { github_user_id: `${githubUserId}`, updated_at: Date.now() }); countUserFound++; }) .catch((error) => { diff --git a/services/users.js b/services/users.js index 4ef57af3b..3b193dfc4 100644 --- a/services/users.js +++ b/services/users.js @@ -19,6 +19,7 @@ const archiveUsers = async (usersData) => { ...user.roles, archived: true, }, + updated_at: Date.now(), }; batch.update(userModel.doc(id), updatedUserData); usersBatch.push({ id, firstName, lastName });