@@ -17,16 +17,25 @@ export default defineTask({
1717 const thisMonday = startOfWeek ( utcNow , { weekStartsOn : 1 } )
1818 const thisSunday = endOfWeek ( utcNow , { weekStartsOn : 1 } )
1919
20+ // Previous week
21+ const utcWeekAgo = new Date ( utcNow . getTime ( ) - 7 * 24 * 60 * 60 * 1000 )
22+ const prevMonday = startOfWeek ( utcWeekAgo , { weekStartsOn : 1 } )
23+ const prevSunday = endOfWeek ( utcWeekAgo , { weekStartsOn : 1 } )
24+
2025 for ( const kitchen of kitchens ) {
21- const revenues = await repository . kitchen . listRevenuesByKitchenForPeriod ( kitchen . id , thisMonday , thisSunday )
26+ const revenuesThisWeek = await repository . kitchen . listRevenuesByKitchenForPeriod ( kitchen . id , thisMonday , thisSunday )
27+ const revenuesPrevWeek = await repository . kitchen . listRevenuesByKitchenForPeriod ( kitchen . id , prevMonday , prevSunday )
28+
29+ const revenueForThisWeek = Math . round ( revenuesThisWeek . reduce ( ( acc , curr ) => acc + curr . total , 0 ) )
30+ const revenueForPreviousWeek = Math . round ( revenuesPrevWeek . reduce ( ( acc , curr ) => acc + curr . total , 0 ) )
2231
23- const revenueForThisWeek = Math . round ( revenues . reduce ( ( acc , curr ) => acc + curr . total , 0 ) )
24- if ( revenueForThisWeek === kitchen . revenueForThisWeek ) {
32+ if ( revenueForThisWeek === kitchen . revenueForThisWeek && revenueForPreviousWeek === kitchen . revenueForPreviousWeek ) {
2533 continue
2634 }
2735
2836 await repository . kitchen . update ( kitchen . id , {
2937 revenueForThisWeek,
38+ revenueForPreviousWeek,
3039 } )
3140
3241 // logger.log(`Kitchen ${kitchen.id}: Revenue updated from ${kitchen.revenueForThisWeek} to ${revenueForThisWeek}`)
0 commit comments