-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsettings_module.js
More file actions
794 lines (731 loc) · 26.4 KB
/
Copy pathsettings_module.js
File metadata and controls
794 lines (731 loc) · 26.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
class Settings {
constructor() {
this.management_ss_ID = "1IMvK9c8jWMqHCIoF4_yCQIrnreLB7VyqvgQSrASOimU";
this.management_ss = SpreadsheetApp.openById(this.management_ss_ID);
this.cacheName = "configCache";
this.cacheExpirationSeconds = 21600; // 6 hours (max for ScriptCache)
this.cacheValues = null;
this.VALUES = {};
this.settingRangeMap = {
mainConfig: {
systemButtons: "A7:C14",
systemTime: "F7:H9",
coreConfig: "F14:H15",
reportGenerationSettings: "F20:H22",
miscellaneous: "A20:C20",
},
sheetsConfig: {
bikesStatus: "A10:C13",
reportSheet: "F10:H13",
checkoutLogs: "A21:C24",
userStatus: "F21:H24",
returnLogs: "A32:C35",
},
notificationsConfig: {
successMessages: "A9:H13",
errorMessages: "A21:H33",
},
};
if (!this.refreshCache(false)) {
throw new Error("Failed to initialize Settings: Cache refresh failed.");
}
}
convertType(value, type) {
if (typeof type !== "string") return value;
switch (type.toLowerCase()) {
case "number":
return Number(value);
case "button":
return value.toString().toUpperCase() === "ON";
case "datetime":
return new Date(value);
case "array":
if (typeof value !== "string") return [];
const separator = "___";
const cleaned = value
.split(separator)
.map((item) => item.trim().toLowerCase());
return cleaned;
case "string":
case "range":
default:
return value.toString();
}
}
extractEmailFromString(text) {
if (!text || text.trim() === "-") return null;
const subjectMatch = text.match(/SUBJECT: '([^']*)'/);
const bodyMatch = text.match(/BODY: '([^']*)'/);
return {
subject: subjectMatch ? subjectMatch[1] : "",
body: bodyMatch ? bodyMatch[1].replace(/""/g, '"') : "",
};
}
extractNoteFromString(cellRange, sheet, text) {
const unAllowedNotes = ["", "-", "none"];
return !unAllowedNotes.includes(text.trim().toLowerCase()) ? {
bgColor: sheet.getRange(cellRange).getBackground(),
note: text,
} : null;
}
setSettingsCache() {
const loadStartTime = Date.now();
try {
let loadedConfigs = {};
Logger.log("📊 Starting to load settings from management spreadsheet...");
Logger.log(`📋 Management Spreadsheet ID: ${this.management_ss_ID}`);
for (const sheetName in this.settingRangeMap) {
Logger.log(`📄 Loading settings from sheet: ${sheetName}`);
const sheet = this.management_ss.getSheetByName(sheetName);
if (!sheet) {
throw new Error(
`❌Sheet '${sheetName}' not found in management spreadsheet`
);
}
for (const tableName in this.settingRangeMap[sheetName]) {
Logger.log(
`📊 Loading table: ${tableName} from range: ${this.settingRangeMap[sheetName][tableName]}`
);
const tableRange = sheet.getRange(
this.settingRangeMap[sheetName][tableName]
);
const table = tableRange.getValues();
if (sheetName === "notificationsConfig") {
let rowCounter = tableRange.getRow();
loadedConfigs[tableName] = table.reduce((acc, curItem, index) => {
let [col1, , , , col5, col6, col7, col8] = curItem;
const currRow = rowCounter + index;
const cellRange = `E${currRow}`;
acc[col1] = {
markEntry: this.extractNoteFromString(cellRange, sheet, col5),
notifyUser: this.extractEmailFromString(col6),
notifyAdmin: this.extractEmailFromString(col7),
notifyDeveloper: this.extractEmailFromString(col8),
};
return acc;
}, {});
continue;
}
loadedConfigs[tableName] = table.reduce((acc, curItem) => {
let [col1, col2, col3] = curItem;
col3 = this.convertType(col3, col2);
acc[col1] = col3;
return acc;
}, {});
}
}
// Validate loaded configs
if (!loadedConfigs || Object.keys(loadedConfigs).length === 0) {
throw new Error("No configuration data loaded from spreadsheet");
}
const loadTime = Date.now() - loadStartTime;
const configSections = Object.keys(loadedConfigs);
Logger.log(
`✅ Loaded ${configSections.length} configuration sections in ${loadTime}ms:`
);
Logger.log(`📋 Sections: ${configSections.join(", ")}`);
CacheService.getScriptCache().put(
this.cacheName,
JSON.stringify(loadedConfigs),
this.cacheExpirationSeconds
);
Logger.log(
`💾 Settings cached successfully with ${
this.cacheExpirationSeconds / 3600
}-hour persistence (ScriptCache)`
);
} catch (error) {
Logger.log(`Error in setSettingsCache: ${error.message}`);
throw error;
}
}
// New unified method with enhanced cache error handling
refreshCache(forceRefresh = false) {
const startTime = Date.now();
try {
Logger.log(
`⚙️ Settings refresh started - forceRefresh: ${forceRefresh}`
);
let configs = null;
let loadSource = "unknown";
if (!forceRefresh) {
try {
configs = CacheService.getScriptCache().get(this.cacheName);
Logger.log(
`📦 ScriptCache retrieval result: ${
configs ? "found" : "not found"
}`
);
if (configs) {
// Validate cache content before using
const testParse = JSON.parse(configs);
if (
!testParse ||
typeof testParse !== "object" ||
Object.keys(testParse).length === 0
) {
Logger.log(
"⚠️ Cache found but invalid - contains empty or malformed data"
);
configs = null; // Force reload
loadSource = "cache-invalid";
} else {
Logger.log(
`✅ Cache validated - contains ${
Object.keys(testParse).length
} config sections`
);
loadSource = "cache";
}
} else {
loadSource = "cache-miss";
}
} catch (cacheError) {
Logger.log(`Cache retrieval/validation error: ${cacheError.message}`);
configs = null; // Force reload on any cache errors
}
}
if (forceRefresh || !configs) {
loadSource = forceRefresh
? "spreadsheet-forced"
: "spreadsheet-fallback";
Logger.log(
`📄 Loading settings from management spreadsheet (reason: ${loadSource})...`
);
try {
this.setSettingsCache();
configs = CacheService.getScriptCache().get(this.cacheName);
if (!configs) {
throw new Error(
"Failed to cache settings after loading from spreadsheet"
);
}
Logger.log(
"✅ Settings successfully loaded and cached from spreadsheet"
);
} catch (spreadsheetError) {
Logger.log(
`❌ Error loading from spreadsheet: ${spreadsheetError.message}`
);
throw spreadsheetError;
}
}
// Parse and validate final configs
try {
this.cacheValues = JSON.parse(configs);
const loadTime = Date.now() - startTime;
const sourceEmoji = loadSource.includes("cache") ? "📦" : "📄";
Logger.log(
`${sourceEmoji} Settings parsed successfully - Source: ${loadSource.toUpperCase()} (${loadTime}ms)`
);
} catch (parseError) {
Logger.log(`❌ JSON parse error: ${parseError.message}`);
throw new Error(
`❌Settings data corrupted - cannot parse JSON: ${parseError.message}`
);
}
if (!this.cacheValues || typeof this.cacheValues !== "object") {
throw new Error("❌Invalid cache values format - not an object");
}
// Additional validation with detailed reporting
const requiredSections = [
"bikesStatus",
"userStatus",
"checkoutLogs",
"returnLogs",
];
const missingSections = requiredSections.filter(
(section) => !this.cacheValues[section]
);
if (missingSections.length > 0) {
Logger.log(
`⚠️ Missing configuration sections: ${missingSections.join(", ")}`
);
} else {
Logger.log(
`✅ All required configuration sections present (${requiredSections.length}/${requiredSections.length})`
);
}
this.setGlobalConfigs();
const totalTime = Date.now() - startTime;
Logger.log(
`🎯 Settings refresh completed successfully in ${totalTime}ms - Source: ${loadSource.toUpperCase()}`
);
return true;
} catch (error) {
Logger.log(`CRITICAL: Settings cache refresh failed: ${error.message}`);
Logger.log("Stack trace:", error.stack);
return false;
}
}
isSystemActive() {
return this.cacheValues.systemButtons?.SYSTEM_ACTIVE ?? true;
}
getMaxCheckoutHours() {
return this.cacheValues.coreConfig?.MAX_CHECKOUT_HOURS ?? 72;
}
getAdminEmail() {
return this.cacheValues.coreConfig?.ADMIN_EMAIL ?? "bikeshare@amherst.edu";
}
isAutoResetEnabled() {
return this.cacheValues.coreConfig?.ENABLE_FORCED_RESET ?? true;
}
getReportGenerationSettings() {
return (
this.cacheValues.reportGenerationSettings ?? {
ENABLED: true,
DAYS_INTERVAL: 1,
FIRST_GENERATION_DATE: "2025-07-01",
GENERATION_HOUR: 2,
}
);
}
setGlobalConfigs() {
try {
Logger.log("⚙️ Setting global configurations...");
// Validate required cache sections
const requiredSections = ["systemButtons", "systemTime", "coreConfig"];
const missingSections = [];
for (const section of requiredSections) {
if (!this.cacheValues[section]) {
missingSections.push(section);
Logger.log(`⚠️ Missing required config section: ${section}`);
}
}
if (missingSections.length === 0) {
Logger.log(
`✅ All required config sections present (${requiredSections.join(
", "
)})`
);
}
this.VALUES = {
DEBUG_MODE: true,
ENABLE_FORCED_RESET: true,
SYSTEM_ACTIVE: this.isSystemActive(),
NEXT_SYSTEM_SHUTDOWN_DATE:
this.cacheValues.systemTime?.NEXT_SYSTEM_SHUTDOWN_DATE || null,
NEXT_SYSTEM_ACTIVATION_DATE:
this.cacheValues.systemTime?.NEXT_SYSTEM_ACTIVATION_DATE || null,
ADMIN_EMAIL: this.getAdminEmail(),
ORG_EMAIL: "bikeshare@amherst.edu",
MANAGEMENT_SS_ID: this.management_ss_ID,
MAIN_DASHBOARD_SS_ID: "1H_Tb-Ql71W1QEkucGiRByGkVDVI3B3RpTqljRonBecg",
ALLOWED_EMAIL_DOMAIN: "amherst.edu",
SHEETS: {
BIKES_STATUS: this.cacheValues.bikesStatus || {
NAME: "Bikes Status",
RESET_RANGE: "E2:L",
},
USER_STATUS: this.cacheValues.userStatus || {
NAME: "User Status",
RESET_RANGE: "A2:L",
},
CHECKOUT_LOGS: this.cacheValues.checkoutLogs || {
NAME: "Checkout Logs",
RESET_RANGE: "A2:D",
},
RETURN_LOGS: {
...(this.cacheValues.returnLogs || {
NAME: "Return Logs",
RESET_RANGE: "A2:I",
}),
DATE_COLUMN: 0,
},
REPORTS: {
...this.cacheValues.reportSheet,
OVERDUE_BIKES_COLUMN: 4,
RETURN_MISMATCHES_COLUMN: 7,
TOTAL_USAGE_HOURS_COLUMN: 9,
},
},
FORMS: {
CHECKOUT_FORM_ID: "1zSuyVnXF4zDuJNr3eHA5SjloBxARTY3F0KH_XB5-oRs",
RETURN_FORM_ID: "14Ue4nG8eTqP2OSpGsiVzOJ1TUq8oKVTqbwCaYDSQzh4",
CHECKOUT_FIELD_IDS: {
EMAIL: 1337405542,
BIKE_HASH: 697424273,
KEY_AVAILABLE: 998220660,
CONDITION_OK: 1671678893,
},
RETURN_FIELD_IDS: {
EMAIL: 1224208618,
BIKE_NAME: 1916897857,
CONFIRM_BIKE_NAME: 1814237596,
ASSURE_RODE_BIKE: 788338430,
BIKE_MISMATCH_EXPLANATION: 993479484,
RETURNING_FOR_FRIEND: 2017212460,
FRIEND_EMAIL: 552890597,
ISSUES_CONCERNS: 71285803,
},
},
REGULATIONS: {
NEED_USER_CONFIRM_KEY_ACCESS: false,
MAX_CHECKOUT_HOURS: this.getMaxCheckoutHours(),
},
FUZZY_MATCHING_THRESHOLD: 0.3,
NOTIFICATION_SETTINGS: {
ENABLE_USER_NOTIFICATIONS:
this.cacheValues.systemButtons?.ENABLE_USER_NOTIFICATIONS,
ENABLE_ADMIN_NOTIFICATIONS:
this.cacheValues.systemButtons?.ENABLE_ADMIN_NOTIFICATIONS,
ENABLE_DEV_NOTIFICATIONS:
this.cacheValues.systemButtons?.ENABLE_DEV_NOTIFICATIONS,
},
REPORT_GENERATION: {
...this.getReportGenerationSettings(),
ENABLE_REPORT_GENERATION:
this.cacheValues.systemButtons?.ENABLE_REPORT_GENERATION,
},
IGNORED_REPORT_STMTS_ON_RFORM:
this.cacheValues.miscellaneous?.IGNORED_REPORT_STMTS_ON_RFORM || [],
COMM_CODES: {
...(this.cacheValues.successMessages || {}),
...(this.cacheValues.errorMessages || {}),
},
};
// Log key configuration details
const sheetCount = Object.keys(this.VALUES.SHEETS).length;
const commCodeCount = Object.keys(this.VALUES.COMM_CODES || {}).length;
const systemActive = this.VALUES.SYSTEM_ACTIVE;
Logger.log(`📊 Global configurations set successfully:`);
Logger.log(` 🗂️ Sheets configured: ${sheetCount}`);
Logger.log(` 📧 Communication codes: ${commCodeCount}`);
Logger.log(` 🔄 System active: ${systemActive}`);
Logger.log(` 👤 Admin email: ${this.VALUES.ADMIN_EMAIL}`);
} catch (error) {
Logger.log(`❌ Error in setGlobalConfigs: ${error.message}`);
throw error;
}
}
// Enhanced debug method to check cache status and errors
debugCacheStatus() {
try {
Logger.log("=== ENHANCED CACHE DEBUG INFO ===");
Logger.log(`Cache name: ${this.cacheName}`);
// Check actual CacheService status
const cache = CacheService.getScriptCache();
const rawCacheData = cache.get(this.cacheName);
Logger.log(`Raw cache exists: ${!!rawCacheData}`);
Logger.log(
`Raw cache length: ${rawCacheData ? rawCacheData.length : 0} characters`
);
if (rawCacheData) {
try {
const parsedCache = JSON.parse(rawCacheData);
Logger.log(`Raw cache is valid JSON: true`);
Logger.log(
`Raw cache sections: ${Object.keys(parsedCache).join(", ")}`
);
} catch (parseError) {
Logger.log(`Raw cache is valid JSON: false - ${parseError.message}`);
Logger.log(`Raw cache preview: ${rawCacheData.substring(0, 200)}...`);
}
}
// Check processed cache values
Logger.log(`Processed cacheValues exists: ${!!this.cacheValues}`);
if (this.cacheValues) {
Logger.log(
`Processed cache sections: ${Object.keys(this.cacheValues).join(
", "
)}`
);
Logger.log(
`System buttons: ${JSON.stringify(this.cacheValues.systemButtons)}`
);
// Check critical sections
const criticalSections = [
"bikesStatus",
"userStatus",
"checkoutLogs",
"returnLogs",
];
const missingSections = criticalSections.filter(
(section) => !this.cacheValues[section]
);
if (missingSections.length > 0) {
Logger.log(
`❌ Missing critical sections: ${missingSections.join(", ")}`
);
} else {
Logger.log(`✅ All critical sections present`);
}
}
// Check final VALUES
Logger.log(`Final VALUES exists: ${!!this.VALUES}`);
if (this.VALUES) {
Logger.log(
`COMM_CODES count: ${
Object.keys(this.VALUES.COMM_CODES || {}).length
}`
);
Logger.log(`SHEETS config: ${!!this.VALUES.SHEETS}`);
if (this.VALUES.SHEETS) {
const sheetNames = Object.keys(this.VALUES.SHEETS).map(
(key) => `${key}: "${this.VALUES.SHEETS[key]?.NAME}"`
);
Logger.log(`Sheet configurations: ${sheetNames.join(", ")}`);
}
}
// Test spreadsheet connectivity
try {
const testSheet = this.management_ss.getSheetByName("mainConfig");
Logger.log(`✅ Management spreadsheet accessible: ${!!testSheet}`);
} catch (ssError) {
Logger.log(`❌ Management spreadsheet error: ${ssError.message}`);
}
Logger.log("=== END ENHANCED CACHE DEBUG ===");
return {
rawCacheExists: !!rawCacheData,
rawCacheValid: rawCacheData
? (() => {
try {
JSON.parse(rawCacheData);
return true;
} catch {
return false;
}
})()
: false,
processedCacheExists: !!this.cacheValues,
finalValuesExists: !!this.VALUES,
spreadsheetAccessible: (() => {
try {
return !!this.management_ss.getSheetByName("mainConfig");
} catch {
return false;
}
})(),
};
} catch (error) {
Logger.log(`❌ Error in debugCacheStatus: ${error.message}`);
return { error: error.message };
}
}
/**
* Force clear the cache and reload from spreadsheet
* Use this for troubleshooting cache issues
*/
forceClearCache() {
try {
Logger.log("🔄 Forcing cache clear...");
const cache = CacheService.getScriptCache();
cache.remove(this.cacheName);
Logger.log("✅ Cache cleared successfully");
// Reset internal state
this.cacheValues = null;
this.VALUES = {};
const result = this.refreshCache(true);
Logger.log(`🔄 Cache rebuild result: ${result}`);
return result;
} catch (error) {
Logger.log(`❌ Error in forceClearCache: ${error.message}`);
return false;
}
}
getCellByKeyName(key, tableName) {
for (const sheetName in this.settingRangeMap) {
for (const table in this.settingRangeMap[sheetName]) {
if (table === tableName) {
const sheet = this.management_ss.getSheetByName(sheetName);
const range = this.settingRangeMap[sheetName][table];
const tableRange = sheet.getRange(range);
const tableValues = tableRange.getValues();
for (let i = 0; i < tableValues.length; i++) {
if (tableValues[i][0] === key) {
// Value is always in the 3rd column (index 3 in A1 notation)
return sheet.getRange(
tableRange.getRow() + i,
tableRange.getColumn() + 2
);
}
}
return null;
}
}
}
return null;
}
}
// =============================================================================
// CONFIGURATION AND CONSTANTS
// =============================================================================
const CACHED_SETTINGS = new Settings();
function parseSettingsUpdate(e) {
try {
const editedSheet = e.source.getActiveSheet();
const curSheetName = editedSheet.getName();
const curCell = e.source.getActiveCell();
const editedRange = editedSheet.getActiveRange();
const editedCol = editedRange.getLastColumn();
const editedRow = editedRange.getLastRow();
const newValue = editedRange.getValue();
const settingsUpdateDateCol = CACHED_SETTINGS.getCellByKeyName(
"LAST_SETTINGS_UPDATED_DATE",
"systemTime"
);
const curDate = new Date();
const commContext = {};
Logger.log(
`📝 Settings update detected: Sheet=${curSheetName}, Range=${editedRange.getA1Notation()}, Value=${newValue}`
);
//Process systemButtons
if (curSheetName == "mainConfig") {
if (editedCol == 3) {
const editedParam = editedSheet.getRange(editedRow, 1).getValue();
//process the database reset
if (editedParam === "FORCE_SYSTEM_RESET" && newValue === "ON") {
cleanDatabase();
//send success confirmation to the administrator
commContext["resetDate"] = curDate;
COMM.handleCommunication("CFM_ADMIN_RESET_001", commContext);
curCell.setValue("OFF");
curCell.setNote(`Last reset on ${curDate}`);
CACHED_SETTINGS.getCellByKeyName(
"FIRST_GENERATION_DATE",
"reportGenerationSettings"
).setValue(curDate);
}
//process quick report generation
if (editedParam === "GENERATE_QUICK_REPORT" && newValue === "ON") {
Logger.log("📊 Quick report generation triggered via settings button");
try {
const reportResult = runReportPipeline();
if (reportResult.success) {
curCell.setNote(`Last instant report generated on ${curDate}`);
Logger.log(`✅ Quick report generated successfully at ${reportResult.timestamp}`);
} else {
curCell.setNote(`Quick Report generation failed on ${curDate}: ${reportResult.error}`);
Logger.log(`❌ Quick report generation failed: ${reportResult.error}`);
}
} catch (error) {
curCell.setNote(`Quick Report generation error on ${curDate}: ${error.message}`);
Logger.log(`❌ Quick report generation error: ${error.message}`);
}
curCell.setValue("OFF");
}
//process system operations pause or resume by disabling/enabling all forms
if (editedParam === "SYSTEM_ACTIVE") {
const isAcceptingResponses = newValue === "ON";
setSystemActivationState(isAcceptingResponses);
}
CACHED_SETTINGS.getCellByKeyName(
"FIRST_GENERATION_DATE",
"reportGenerationSettings"
).setValue(curDate);
}
//Process SystemTime, CoreConfig & reportGenerationSettings
if (editedCol == 8) {
const editedParam = editedSheet.getRange(editedRow, 6).getValue();
//NEXT_SYSTEM_SHUTDOWN_DATE or NEXT_SYSTEM_ACTIVATION_DATE
const handlerToDelete =
editedParam === "NEXT_SYSTEM_ACTIVATION_DATE"
? "handleScheduledSystemActivation"
: "handleScheduledSystemShutdown";
ScriptApp.getProjectTriggers()
.filter((t) => t.getHandlerFunction() === handlerToDelete)
.forEach((t) => ScriptApp.deleteTrigger(t));
installScheduleSystemShutdownAndActivationTrigger(
editedParam === "NEXT_SYSTEM_ACTIVATION_DATE" ? newValue : null,
editedParam === "NEXT_SYSTEM_SHUTDOWN_DATE" ? newValue : null,
editedParam
);
}
}
//update settings last update tracker
const delay = 1000 * 6;
if (curDate - settingsUpdateDateCol.getValue() > delay) {
settingsUpdateDateCol.setValue(curDate);
settingsUpdateDateCol.setNote(
`Affected Range ${editedRange.getA1Notation()}`
);
}
//load and save updated cache
Logger.log("🔄 Attempting to refresh settings cache after update...");
const cacheRefreshResult = CACHED_SETTINGS.refreshCache(true);
if (!cacheRefreshResult) {
Logger.log("❌ Failed to refresh settings cache after update.");
throw new Error("Failed to refresh settings cache");
} else {
Logger.log("✅ Settings cache refreshed successfully after update.");
}
} catch (error) {
Logger.log(`❌ Error in parseSettingsUpdate: ${error.message}`);
throw error;
}
}
/**
* Toggles the accepting responses state for both the checkout and return forms.
* @param {boolean} isAcceptingResponses - Indicates whether the forms should accept responses.
*/
function toogleFormAcceptResponses(isAcceptingResponses) {
if (typeof isAcceptingResponses === "undefined") {
Logger.log(
"toogleFormAcceptResponses: ‼️isAcceptingResponses is undefined, aborting."
);
return;
}
Logger.log(
`✅toogleFormAcceptResponses: Setting forms to ${
isAcceptingResponses ? "accept" : "not accept"
} responses.`
);
try {
let c_form = FormApp.openById(
CACHED_SETTINGS.VALUES.FORMS.CHECKOUT_FORM_ID
)
c_form.setAcceptingResponses(isAcceptingResponses);
Logger.log("✅Checkout form response state updated successfully.");
} catch (err) {
Logger.log(`❌Error updating Checkout form: ${err.message}`);
}
try {
let r_form = FormApp.openById(
CACHED_SETTINGS.VALUES.FORMS.RETURN_FORM_ID
)
r_form.setAcceptingResponses(isAcceptingResponses);
Logger.log("✅Return form response state updated successfully.");
} catch (err) {
Logger.log(`❌Error updating Return form: ${err.message}`);
}
}
/**
* Core logic for system activation/deactivation
* Toggles form acceptance state and updates related settings
* @param {boolean} isActive - true for activation, false for shutdown
*/
function setSystemActivationState(isActive) {
const curDate = new Date();
const statusText = isActive ? "activated" : "deactivated";
try {
Logger.log(`🔄 Setting system ${statusText}...`);
// Toggle form responses
toogleFormAcceptResponses(isActive);
// Update SYSTEM_ACTIVE in settings
const systemActiveCell = CACHED_SETTINGS.getCellByKeyName(
"SYSTEM_ACTIVE",
"systemButtons"
);
if (systemActiveCell) {
const newValue = isActive ? "ON" : "OFF";
systemActiveCell.setValue(newValue);
systemActiveCell.setNote(`System ${statusText} on ${curDate}`);
Logger.log(`✅ SYSTEM_ACTIVE updated to ${newValue}`);
}
// Update ENABLE_REPORT_GENERATION to match
const reportGenCell = CACHED_SETTINGS.getCellByKeyName(
"ENABLE_REPORT_GENERATION",
"systemButtons"
);
if (reportGenCell) {
const reportGenValue = isActive ? "ON" : "OFF";
reportGenCell.setValue(reportGenValue);
Logger.log(`✅ ENABLE_REPORT_GENERATION updated to ${reportGenValue}`);
}
Logger.log(`✅ System ${statusText} successfully`);
return true;
} catch (error) {
Logger.log(`❌ Failed to set system ${statusText}: ${error.message}`);
throw error;
}
}