@@ -1035,35 +1035,20 @@ const userProfileController = function (UserProfile, Project) {
10351035 }
10361036 } ;
10371037
1038- const getUserById = function ( req , res ) {
1038+ const getUserById = ( req , res ) => {
10391039 const userid = req . params . userId ;
1040- // if (cache.getCache(`user-${userid}`)) {
1041- // const getData = JSON.parse(cache.getCache(`user-${userid}`));
1042- // res.status(200).send(getData);
1043- // return;
1044- // }
1045- const ONE_YEAR_AGO = new Date ( ) ;
1046- ONE_YEAR_AGO . setFullYear ( ONE_YEAR_AGO . getFullYear ( ) - 1 ) ;
10471040
1048- UserProfile . findById ( userid , '-password -refreshTokens -lastModifiedDate -__v' )
1041+ return UserProfile . findById ( userid , '-password -refreshTokens -lastModifiedDate -__v' )
10491042 . populate ( [
10501043 {
10511044 path : 'teams' ,
10521045 select : '_id teamName' ,
1053- options : {
1054- sort : {
1055- teamName : 1 ,
1056- } ,
1057- } ,
1046+ options : { sort : { teamName : 1 } } ,
10581047 } ,
10591048 {
10601049 path : 'projects' ,
10611050 select : '_id projectName category' ,
1062- options : {
1063- sort : {
1064- projectName : 1 ,
1065- } ,
1066- } ,
1051+ options : { sort : { projectName : 1 } } ,
10671052 } ,
10681053 {
10691054 path : 'badgeCollection' ,
@@ -1074,29 +1059,37 @@ const userProfileController = function (UserProfile, Project) {
10741059 } ,
10751060 } ,
10761061 {
1077- path : 'infringements' , // Populate infringements field
1078- match : { date : { $gte : ONE_YEAR_AGO } } ,
1079- select : ' date description' ,
1080- options : {
1081- sort : {
1082- date : - 1 , // Sort by date descending if needed
1083- } ,
1084- } ,
1062+ path : 'infringements' ,
1063+ select : '_id date description createdDate' ,
1064+ options : { sort : { date : - 1 } } ,
1065+ } ,
1066+ {
1067+ path : 'oldInfringements' ,
1068+ select : '_id date description createdDate' ,
1069+ options : { sort : { date : - 1 } } ,
10851070 } ,
10861071 ] )
10871072 . exec ( )
1088- . then ( ( results ) => {
1089- if ( ! results ) {
1090- res . status ( 400 ) . send ( { error : 'This is not a valid user' } ) ;
1091- return ;
1073+ . then ( async ( user ) => {
1074+ if ( ! user ) {
1075+ return res . status ( 400 ) . send ( { error : 'This is not a valid user' } ) ;
10921076 }
1093- userHelper . getTangibleHoursReportedThisWeekByUserId ( userid ) . then ( ( hours ) => {
1094- results . set ( 'tangibleHoursReportedThisWeek' , hours , {
1095- strict : false ,
1096- } ) ;
1097- cache . setCache ( `user-${ userid } ` , JSON . stringify ( results ) ) ;
1098- res . status ( 200 ) . send ( results ) ;
1099- } ) ;
1077+
1078+ const current = Array . isArray ( user . infringements ) ? user . infringements : [ ] ;
1079+ const old = Array . isArray ( user . oldInfringements ) ? user . oldInfringements : [ ] ;
1080+
1081+ const infringements = [ ...old , ...current ] . sort ( ( a , b ) =>
1082+ a . date < b . date ? 1 : a . date > b . date ? - 1 : 0 ,
1083+ ) ;
1084+
1085+ user . set ( 'infringements' , infringements , { strict : false } ) ;
1086+ user . set ( 'oldInfringements' , undefined , { strict : false } ) ;
1087+
1088+ const hours = await userHelper . getTangibleHoursReportedThisWeekByUserId ( userid ) ;
1089+ user . set ( 'tangibleHoursReportedThisWeek' , hours , { strict : false } ) ;
1090+
1091+ cache . setCache ( `user-${ userid } ` , JSON . stringify ( user ) ) ;
1092+ return res . status ( 200 ) . send ( user ) ;
11001093 } )
11011094 . catch ( ( error ) => res . status ( 404 ) . send ( error ) ) ;
11021095 } ;
0 commit comments