File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -266,34 +266,23 @@ const reporthelper = function () {
266266 * @param {Object } results An array of user objects with selected fields.
267267 * @return {Object } An array of user objects with properly sorted weeklySummaries by due date.
268268 */
269+ // refactor that handles any week index without hardcoded cases
270+
269271 const formatSummaries = function ( results ) {
270272 return results . map ( ( user ) => {
271273 const { weeklySummaries : wS } = user ;
272- const wSummaries = [ ] ;
273274
274- if ( Array . isArray ( wS ) && wS . length && wS . length < 3 ) {
275- // Common cases for the first entry.
276- if ( getTheWeek ( wS [ 0 ] . dueDate ) === 0 ) wSummaries [ 0 ] = { ...wS [ 0 ] } ;
277- if ( getTheWeek ( wS [ 0 ] . dueDate ) === 1 ) {
278- wSummaries [ 0 ] = null ;
279- wSummaries [ 1 ] = { ...wS [ 0 ] } ;
280- }
281- // When single entry.
282- if ( wS . length === 1 ) {
283- // Special case when first entry belongs to week before last.
284- if ( getTheWeek ( wS [ 0 ] . dueDate ) === 2 ) {
285- wSummaries [ 0 ] = null ;
286- wSummaries [ 1 ] = null ;
287- wSummaries [ 2 ] = { ...wS [ 0 ] } ;
288- }
289- } else {
290- // When two entries.
291- if ( getTheWeek ( wS [ 1 ] . dueDate ) === 1 ) wSummaries [ 1 ] = { ...wS [ 1 ] } ;
292- if ( getTheWeek ( wS [ 1 ] . dueDate ) === 2 ) wSummaries [ 2 ] = { ...wS [ 1 ] } ;
275+ if ( ! Array . isArray ( wS ) || ! wS . length || wS . length >= 3 ) return user ;
276+
277+ const wSummaries = [ ] ;
278+ wS . forEach ( ( entry ) => {
279+ const weekIndex = getTheWeek ( entry . dueDate ) ;
280+ if ( weekIndex >= 0 ) {
281+ wSummaries [ weekIndex ] = { ...entry } ;
293282 }
294- user = { ... user , weeklySummaries : wSummaries } ;
295- }
296- return user ;
283+ } ) ;
284+
285+ return { ... user , weeklySummaries : wSummaries } ;
297286 } ) ;
298287 } ;
299288
You can’t perform that action at this time.
0 commit comments