@@ -77,13 +77,64 @@ const isSummaryNeeded = (tabTitles) => {
77
77
* @param {boolean } options.csv - If true, outputs in CSV format.
78
78
* @returns {Promise<boolean> } True if a summary is required, false otherwise.
79
79
*/
80
- export const isSummaryRequired = async ( { csv } ) => {
81
- if ( csv ) {
82
- return false ;
83
- }
80
+ export const isSummaryRequired = async ( options = { } ) => {
81
+ if ( options . csv ) return false ;
84
82
try {
83
+ const currentDate = new Date ( ) ;
84
+ // console.log('[DEBUG] SUT currentDate:', currentDate.toISOString());
85
+
86
+ const lastMonth = getFormattedMonth ( 'lastMonth' ) ;
87
+ // console.log('[DEBUG] SUT lastMonth:', lastMonth);
88
+
89
+ const currentMonth = getFormattedMonth ( ) ;
90
+ // console.log('[DEBUG] SUT currentMonth:', currentMonth);
91
+
92
+ let yearForSummary = getFormattedYear ( ) ;
93
+ if ( currentMonth === 'Jan' && lastMonth === 'Dec' ) {
94
+ yearForSummary = getFormattedYear ( 'lastYear' ) ;
95
+ }
96
+ // console.log('[DEBUG] SUT yearForSummary:', yearForSummary);
97
+
98
+ const targetSummaryTitle = createSummaryTitle (
99
+ lastMonth ,
100
+ yearForSummary . toString ( )
101
+ ) ;
102
+ // console.log('[DEBUG] SUT targetSummaryTitle:', targetSummaryTitle);
103
+
85
104
const existingTabTitles = await getExistingTabTitlesInRange ( ) ;
86
- return isSummaryNeeded ( existingTabTitles ) ;
105
+ // console.log('[DEBUG] SUT existingTabTitles raw:', existingTabTitles);
106
+
107
+ if ( ! existingTabTitles ) {
108
+ // console.error('Error: existingTabTitles is undefined');
109
+ return false ;
110
+ }
111
+
112
+ const summaryExists = existingTabTitles . some (
113
+ ( title ) =>
114
+ title . trim ( ) . toLowerCase ( ) === targetSummaryTitle . trim ( ) . toLowerCase ( )
115
+ ) ;
116
+ // console.log('[DEBUG] SUT summaryExists:', summaryExists);
117
+
118
+ if ( summaryExists ) {
119
+ return false ;
120
+ }
121
+
122
+ const searchPatternMonth = lastMonth ;
123
+ const searchPatternYear = yearForSummary . toString ( ) ;
124
+ // console.log('[DEBUG] SUT searchPatternMonth:', searchPatternMonth);
125
+ // console.log('[DEBUG] SUT searchPatternYear:', searchPatternYear);
126
+
127
+ const sheetsFromLastMonthExist = existingTabTitles . some ( ( title ) => {
128
+ const titleIncludesMonth = title . includes ( searchPatternMonth ) ;
129
+ const titleIncludesYear = title . includes ( searchPatternYear ) ;
130
+ return titleIncludesMonth && titleIncludesYear ;
131
+ } ) ;
132
+ // console.log(
133
+ // '[DEBUG] SUT sheetsFromLastMonthExist:',
134
+ // sheetsFromLastMonthExist
135
+ // );
136
+
137
+ return sheetsFromLastMonthExist ;
87
138
} catch ( error ) {
88
139
console . error ( 'Error in isSummaryRequired:' , error ) ;
89
140
return false ;
@@ -142,12 +193,19 @@ const isWeeklySummaryNeeded = (tabTitles) => {
142
193
return false ;
143
194
}
144
195
const expectedSummaryTitle = createWeeklySummaryTitle ( ) ;
145
- const summaryExists = tabTitles . some (
146
- ( title ) => title . startsWith ( 'Weekly' ) && title === expectedSummaryTitle
147
- ) ;
196
+ // console.log('[SUT DEBUG] isWeeklySummaryNeeded - expectedSummaryTitle:', expectedSummaryTitle);
197
+ // console.log('[SUT DEBUG] isWeeklySummaryNeeded - tabTitles:', JSON.stringify(tabTitles));
198
+
199
+ const summaryExists = tabTitles . some ( ( title ) => {
200
+ const starts = title . startsWith ( 'Weekly' ) ;
201
+ const matches = title === expectedSummaryTitle ;
202
+ // console.log(`[SUT DEBUG] Checking title: "${title}", startsWeekly: ${starts}, matchesExpected: ${matches}`);
203
+ return starts && matches ;
204
+ } ) ;
205
+ // console.log('[SUT DEBUG] isWeeklySummaryNeeded - summaryExists:', summaryExists);
148
206
return ! summaryExists ;
149
207
} catch ( error ) {
150
- console . error ( 'Error in isSummaryNeeded :' , error ) ;
208
+ console . error ( 'Error in isWeeklySummaryNeeded :' , error ) ;
151
209
return false ;
152
210
}
153
211
} ;
0 commit comments