11const firestore = require ( "../utils/firestore" ) ;
22const tasksModel = firestore . collection ( "tasks" ) ;
3+ const userModel = firestore . collection ( "users" ) ;
34const ItemModel = firestore . collection ( "itemTags" ) ;
45const dependencyModel = firestore . collection ( "taskDependencies" ) ;
56const userUtils = require ( "../utils/users" ) ;
@@ -8,7 +9,8 @@ const { chunks } = require("../utils/array");
89const { DOCUMENT_WRITE_SIZE } = require ( "../constants/constants" ) ;
910const { fromFirestoreData, toFirestoreData, buildTasks } = require ( "../utils/tasks" ) ;
1011const { TASK_TYPE , TASK_STATUS , TASK_STATUS_OLD , TASK_SIZE } = require ( "../constants/tasks" ) ;
11- const { IN_PROGRESS , NEEDS_REVIEW , IN_REVIEW , ASSIGNED , BLOCKED , SMOKE_TESTING , COMPLETED , SANITY_CHECK } = TASK_STATUS ;
12+ const { IN_PROGRESS , NEEDS_REVIEW , IN_REVIEW , ASSIGNED , BLOCKED , SMOKE_TESTING , COMPLETED , SANITY_CHECK , BACKLOG } =
13+ TASK_STATUS ;
1214const { OLD_ACTIVE , OLD_BLOCKED , OLD_PENDING , OLD_COMPLETED } = TASK_STATUS_OLD ;
1315const { INTERNAL_SERVER_ERROR } = require ( "../constants/errorMessages" ) ;
1416
@@ -631,6 +633,40 @@ const updateTaskStatus = async () => {
631633 }
632634} ;
633635
636+ const updateOrphanTasksStatus = async ( lastOrphanTasksFilterationTimestamp ) => {
637+ const lastTimestamp = Number ( lastOrphanTasksFilterationTimestamp ) ;
638+ try {
639+ const users = [ ] ;
640+ const currentTimeStamp = Date . now ( ) ;
641+
642+ const usersQuerySnapshot = await userModel
643+ . where ( "roles.in_discord" , "==" , false )
644+ . where ( "updated_at" , ">=" , lastTimestamp )
645+ . where ( "updated_at" , "<=" , currentTimeStamp )
646+ . get ( ) ;
647+
648+ usersQuerySnapshot . forEach ( ( user ) => users . push ( { ...user . data ( ) , id : user . id } ) ) ;
649+
650+ let orphanTasksUpdatedCount = 0 ;
651+
652+ for ( const user of users ) {
653+ const tasksQuerySnapshot = await tasksModel
654+ . where ( "assigneeId" , "==" , user . id )
655+ . where ( "status" , "not-in" , [ COMPLETED , BACKLOG ] )
656+ . get ( ) ;
657+ tasksQuerySnapshot . forEach ( async ( taskDoc ) => {
658+ orphanTasksUpdatedCount ++ ;
659+ await tasksModel . doc ( taskDoc . id ) . update ( { status : BACKLOG } ) ;
660+ } ) ;
661+ }
662+
663+ return { orphanTasksUpdatedCount } ;
664+ } catch ( error ) {
665+ logger . error ( "Error marking tasks as backlog:" , error ) ;
666+ throw error ;
667+ }
668+ } ;
669+
634670module . exports = {
635671 updateTask,
636672 fetchTasks,
@@ -648,4 +684,5 @@ module.exports = {
648684 getBuiltTasks,
649685 getOverdueTasks,
650686 updateTaskStatus,
687+ updateOrphanTasksStatus,
651688} ;
0 commit comments