@@ -137,15 +137,18 @@ function parseDateRangeForOpenIssues(startDate, endDate) {
137137 return { errorResponse : { status : 400 , body : { error : 'Invalid date format.' } } } ;
138138 }
139139 if ( start > end ) {
140- return { errorResponse : { status : 400 , body : { error : 'startDate must not be after endDate.' } } } ;
140+ return {
141+ errorResponse : { status : 400 , body : { error : 'startDate must not be after endDate.' } } ,
142+ } ;
141143 }
142144 const now = new Date ( ) ;
143145 if ( start > now ) {
144146 return {
145147 errorResponse : {
146148 status : 400 ,
147149 body : {
148- error : 'The selected date range is entirely in the future. No issue data exists for future dates.' ,
150+ error :
151+ 'The selected date range is entirely in the future. No issue data exists for future dates.' ,
149152 } ,
150153 } ,
151154 } ;
@@ -349,11 +352,7 @@ const bmIssueController = function (BuildingIssue, injuryIssue) {
349352 setFields . closedDate = null ;
350353 }
351354
352- const updated = await BuildingIssue . findByIdAndUpdate (
353- id ,
354- { $set : setFields } ,
355- { new : true } ,
356- ) ;
355+ const updated = await BuildingIssue . findByIdAndUpdate ( id , { $set : setFields } , { new : true } ) ;
357356
358357 if ( ! updated ) {
359358 return res . status ( 404 ) . json ( { message : 'Issue not found.' } ) ;
@@ -397,7 +396,12 @@ const bmIssueController = function (BuildingIssue, injuryIssue) {
397396
398397 if ( year !== undefined ) {
399398 const yearNum = Number ( year ) ;
400- if ( Number . isNaN ( yearNum ) || ! Number . isInteger ( yearNum ) || yearNum < MIN_YEAR || yearNum > MAX_YEAR ) {
399+ if (
400+ Number . isNaN ( yearNum ) ||
401+ ! Number . isInteger ( yearNum ) ||
402+ yearNum < MIN_YEAR ||
403+ yearNum > MAX_YEAR
404+ ) {
401405 return res . status ( 400 ) . json ( { error : 'Invalid year. Must be a 4-digit integer.' } ) ;
402406 }
403407 const startOfYear = new Date ( Date . UTC ( yearNum , 0 , 1 ) ) ;
@@ -531,37 +535,15 @@ const bmIssueController = function (BuildingIssue, injuryIssue) {
531535 query . projectId = { $in : filteredProjectIds } ;
532536 }
533537
534- let issues = await BuildingIssue . find ( query )
538+ const issues = await BuildingIssue . find ( query )
535539 . select ( 'issueTitle issueDate _id' )
536540 . populate ( 'projectId' )
537541 . lean ( ) ;
538542
539- issues = issues . map ( ( issue ) => {
540- const durationInMonths = getDurationOpenMonths ( issue . issueDate ) ;
541- return {
542- issueName : issue . issueTitle && issue . issueTitle . length > 0 ? issue . issueTitle [ 0 ] : null ,
543- durationInMonths,
544- issueId : issue . _id . toString ( ) ,
545- projectId : issue . projectId ?. _id ?. toString ( ) || issue . projectId ?. toString ( ) ,
546- projectName : issue . projectId ?. name || null ,
547- } ;
548- } ) ;
549-
550- const sortedIssues = issues
551- . sort ( ( a , b ) => b . durationInMonths - a . durationInMonths )
552- . map ( ( { issueName, durationInMonths, issueId, projectId, projectName } ) => ( {
553- issueName,
554- durationOpen : durationInMonths ,
555- issueId,
556- projectId,
557- projectName,
558- } ) ) ;
559-
560- console . log (
561- `[getLongestOpenIssues] Total issues found: ${ issues . length } , Returning: ${ sortedIssues . length } issues` ,
562- ) ;
543+ const grouped = buildGroupedIssues ( issues ) ;
544+ const response = buildLongestOpenResponse ( grouped ) ;
563545
564- res . json ( sortedIssues ) ;
546+ res . json ( response ) ;
565547 } catch ( error ) {
566548 res . status ( 500 ) . json ( { message : 'Error fetching longest open issues' } ) ;
567549 }
@@ -584,4 +566,4 @@ const bmIssueController = function (BuildingIssue, injuryIssue) {
584566 } ;
585567} ;
586568
587- module . exports = bmIssueController ;
569+ module . exports = bmIssueController ;
0 commit comments