@@ -16,22 +16,18 @@ const {
1616 getNextDayTimeStamp,
1717 convertTimestampsToUTC,
1818 resolveLastOooUntil,
19- normalizeTimestamp,
2019} = require ( "../utils/userStatus" ) ;
2120const { TASK_STATUS } = require ( "../constants/tasks" ) ;
2221const userStatusModel = firestore . collection ( "usersStatus" ) ;
2322const tasksModel = firestore . collection ( "tasks" ) ;
24- const { userState, statusState } = require ( "../constants/userStatus" ) ;
23+ const { userState } = require ( "../constants/userStatus" ) ;
2524const discordRoleModel = firestore . collection ( "discord-roles" ) ;
2625const memberRoleModel = firestore . collection ( "member-group-roles" ) ;
2726const usersCollection = firestore . collection ( "users" ) ;
28- const userFutureStatusCollection = firestore . collection ( "userFutureStatus" ) ;
2927const config = require ( "config" ) ;
3028const DISCORD_BASE_URL = config . get ( "services.discordBot.baseUrl" ) ;
3129const { 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} ;
0 commit comments