@@ -2497,39 +2497,44 @@ const createControllerMethods = function (UserProfile, Project, cache) {
24972497 }
24982498
24992499 const originalinfringements = record ?. infringements ?? [ ] ;
2500- // record.infringements = originalinfringements.concat(req.body.blueSquare);
2501- record . infringements = originalinfringements . concat ( newInfringement ) ;
2502- record . infringementCount += 1 ;
25032500
2504- console . log ( 'Original infringements:' , originalinfringements ) ;
2505- console . log ( 'Record infringements:' , record . infringements ) ;
2501+ try {
2502+ // ← KEY FIX: use $push instead of record.save() to avoid overwriting
2503+ // cron-assigned infringements added between findById and save
2504+ const status = await UserProfile . findByIdAndUpdate (
2505+ userid ,
2506+ {
2507+ $push : { infringements : newInfringement } ,
2508+ $inc : { infringementCount : 1 } ,
2509+ } ,
2510+ { new : true } ,
2511+ ) ;
25062512
2507- record
2508- . save ( )
2509- . then ( async ( results ) => {
2510- await userHelper . notifyInfringements (
2511- originalinfringements ,
2512- results . infringements ,
2513- results . firstName ,
2514- results . lastName ,
2515- results . email ,
2516- results . role ,
2517- results . startDate ,
2518- results . jobTitle [ 0 ] ,
2519- results . weeklycommittedHours ,
2520- ) ;
2521- res . status ( 200 ) . json ( {
2522- _id : record . _id ,
2523- infringements : record . infringements ,
2524- } ) ;
2513+ await userHelper . notifyInfringements (
2514+ originalinfringements ,
2515+ status . infringements ,
2516+ status . firstName ,
2517+ status . lastName ,
2518+ status . email ,
2519+ status . role ,
2520+ status . startDate ,
2521+ status . jobTitle [ 0 ] ,
2522+ status . weeklycommittedHours ,
2523+ ) ;
25252524
2526- // update alluser cache if we have cache
2527- if ( isUserInCache ) {
2528- allUserData . splice ( userIdx , 1 , userData ) ;
2529- cache . setCache ( 'allusers' , JSON . stringify ( allUserData ) ) ;
2530- }
2531- } )
2532- . catch ( ( error ) => res . status ( 400 ) . send ( error ) ) ;
2525+ res . status ( 200 ) . json ( {
2526+ _id : status . _id ,
2527+ infringements : status . infringements ,
2528+ } ) ;
2529+
2530+ // update alluser cache if we have cache
2531+ if ( isUserInCache ) {
2532+ allUserData . splice ( userIdx , 1 , userData ) ;
2533+ cache . setCache ( 'allusers' , JSON . stringify ( allUserData ) ) ;
2534+ }
2535+ } catch ( error ) {
2536+ res . status ( 400 ) . send ( error ) ;
2537+ }
25332538 } ) ;
25342539 } ;
25352540
0 commit comments