Repository navigation
Expand file tree
/
Copy pathcomm_service.js
More file actions
328 lines (295 loc) · 10.3 KB
/
Copy pathcomm_service.js
File metadata and controls
328 lines (295 loc) · 10.3 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
// =============================================================================
// FUNCTIONAL COMMUNICATION OPERATIONS
// Pure functions for notifications and messaging
// =============================================================================
const COMM = {
// Handle communication by ID
handleCommunication: (commID, context) => {
const comm = COMM.getCommunication(commID);
if (!comm) {
Logger.log(`Communication ID ${commID} not found`);
throw new Error(`❌Communication ID ${commID} not found`);
}
Logger.log(
`Processing communication ${commID} with context:${JSON.stringify(context)}`,
);
const results = [];
if (
comm.notifyUser &&
CACHED_SETTINGS.VALUES.NOTIFICATION_SETTINGS.ENABLE_USER_NOTIFICATIONS
) {
Logger.log(
`Preparing user notification to ${context.userEmail} for ${commID}`,
);
// Fill placeholders with validation
const subjectResult = COMM.fillPlaceholders(
comm.notifyUser.subject,
context,
commID,
);
const bodyResult = COMM.fillPlaceholders(
comm.notifyUser.body,
context,
commID,
);
if (subjectResult.hasErrors || bodyResult.hasErrors) {
Logger.log(
`❌ Cannot send user notification for ${commID} - missing data: ${[...new Set([...(subjectResult.missingKeys || []), ...(bodyResult.missingKeys || [])])].join(", ")}`,
);
results.push({
success: false,
error: "Missing placeholder data",
commID,
});
} else {
const userResult = COMM.notifyUser(
context.userEmail,
subjectResult.result,
bodyResult.result,
commID,
context.isReturningForFriend ? context.friendEmail : null,
);
results.push(userResult);
}
} else {
Logger.log(
`Skipping user notification - notifyUser: ${!!comm.notifyUser}, enabled: ${CACHED_SETTINGS.VALUES.NOTIFICATION_SETTINGS.ENABLE_USER_NOTIFICATIONS}`,
);
}
if (
comm.notifyAdmin &&
CACHED_SETTINGS.VALUES.NOTIFICATION_SETTINGS.ENABLE_ADMIN_NOTIFICATIONS
) {
Logger.log(`Preparing admin notification for ${commID}`);
const subjectResult = COMM.fillPlaceholders(
comm.notifyAdmin.subject,
context,
commID,
);
const bodyResult = COMM.fillPlaceholders(
comm.notifyAdmin.body,
context,
commID,
);
const adminResult = COMM.notifyAdmin(
subjectResult.result || comm.notifyAdmin.subject,
bodyResult.result || comm.notifyAdmin.body,
);
Logger.log(`Admin notification result: ${JSON.stringify(adminResult)}`);
results.push(adminResult);
} else {
Logger.log(
`Skipping admin notification - notifyAdmin: ${!!comm.notifyAdmin}, enabled: ${CACHED_SETTINGS.VALUES.NOTIFICATION_SETTINGS.ENABLE_ADMIN_NOTIFICATIONS}`,
);
}
if (
comm.notifyDeveloper &&
CACHED_SETTINGS.VALUES.NOTIFICATION_SETTINGS.ENABLE_DEV_NOTIFICATIONS
) {
Logger.log(`Preparing developer notification for ${commID}`);
const subjectResult = COMM.fillPlaceholders(
comm.notifyDeveloper.subject,
context,
commID,
);
const bodyResult = COMM.fillPlaceholders(
comm.notifyDeveloper.body,
context,
commID,
);
const devResult = COMM.notifyDeveloper(
subjectResult.result || comm.notifyDeveloper.subject,
bodyResult.result || comm.notifyDeveloper.body,
);
Logger.log(`Developer notification result: ${JSON.stringify(devResult)}`);
results.push(devResult);
} else {
Logger.log(
`Skipping developer notification - notifyDeveloper: ${!!comm.notifyDeveloper}, enabled: ${CACHED_SETTINGS.VALUES.NOTIFICATION_SETTINGS.ENABLE_DEV_NOTIFICATIONS}`,
);
}
if (comm.markEntry && context.range) {
Logger.log(
`Marking entry per admin config - Code: ${commID}, Color: ${comm.markEntry.bgColor}, Note: ${comm.markEntry.note}`,
);
const noteResult = COMM.fillPlaceholders(
comm.markEntry.note,
context,
commID,
);
const markResult = COMM.markEntry(
context.range,
comm.markEntry.bgColor,
noteResult.result || comm.markEntry.note,
);
Logger.log(`Mark entry result: ${JSON.stringify(markResult)}`);
results.push(markResult);
} else {
Logger.log(
`Skipping entry marking - markEntry: ${comm.markEntry ? "configured as null" : "not configured"}, range: ${!!context.range}`,
);
}
return results;
},
// Send user notification with validation
notifyUser: (
userEmail,
subject,
body,
commID = "unknown",
friendEmail = null,
) => {
let subjectStr = "";
let bodyStr = "";
const ccList = [];
if (friendEmail) ccList.push(friendEmail);
if (String(commID).startsWith("ERR_"))
ccList.push(CACHED_SETTINGS.VALUES.ADMIN_EMAIL);
try {
// Validate email
if (
!userEmail ||
typeof userEmail !== "string" ||
!userEmail.includes("@")
) {
throw new Error("Invalid or missing email address");
}
// Ensure subject and body are strings
subjectStr =
typeof subject === "string" ? subject : String(subject || "");
bodyStr = typeof body === "string" ? body : String(body || "");
// Check for malformed content (missing placeholders)
if (subjectStr.includes("[MISSING:") || bodyStr.includes("[MISSING:")) {
Logger.log(
`❌ Blocking malformed email to ${userEmail} for ${commID} - contains missing placeholders`,
);
throw new Error("Email contains missing placeholder data");
}
GmailApp.sendEmail(
userEmail,
subjectStr,
`Hi,\n\n${bodyStr}\n\nThank you,\nBike Share Team`,
{ from: CACHED_SETTINGS.VALUES.ORG_EMAIL, cc: ccList.join(",") },
);
Logger.log(`✅ Email sent successfully to ${userEmail} for ${commID}`);
return { success: true, recipient: userEmail };
} catch (error) {
Logger.log(
`❌ Failed to send email to ${userEmail} from ${CACHED_SETTINGS.VALUES.ORG_EMAIL} for ${commID}: ${error.message}`,
);
// Notify admin of email failure
try {
COMM.notifyAdmin(
`Email Delivery Failure - ${commID}`,
`Failed to send notification to ${userEmail}.\n\nError: ${error.message}\n\nSubject: ${subjectStr}\n\nBody: ${bodyStr}`,
);
} catch (adminError) {
Logger.log(
`❌ Also failed to notify admin of email failure: ${adminError.message}`,
);
}
return { success: false, error: error.message };
}
},
// Send admin notification
notifyAdmin: (subject, body) => {
try {
GmailApp.sendEmail(
CACHED_SETTINGS.VALUES.ADMIN_EMAIL,
subject,
`Hi,\n\n${body}\n\nThank you,\nBike Share Team`,
{ from: CACHED_SETTINGS.VALUES.ORG_EMAIL },
);
return { success: true, recipient: "admin" };
} catch (error) {
Logger.log(`Error sending email to admin: ${error.message}`);
return { success: false, error: error.message };
}
},
// Send developer notification
notifyDeveloper: (subject, body) => {
try {
GmailApp.sendEmail(
CACHED_SETTINGS.VALUES.DEVELOPER_EMAIL,
subject,
`Hi,\n\n${body}\n\nThank you,\nBike Share Team`,
{ from: CACHED_SETTINGS.VALUES.ORG_EMAIL },
);
return { success: true, recipient: "developer" };
} catch (error) {
Logger.log(`Error sending email to developer: ${error.message}`);
return { success: false, error: error.message };
}
},
// Mark sheet entry
markEntry: (range, color = null, note = null) => {
try {
if (!range) return { success: false, error: "No range provided" };
DB.markEntry(range, color, note);
return { success: true };
} catch (error) {
Logger.log(`Error marking entry: ${error.message}`);
return { success: false, error: error.message };
}
},
// Get communication config by ID
getCommunication: (commID) =>
CACHED_SETTINGS.VALUES.COMM_CODES[commID] || null,
// Fill template placeholders with validation
fillPlaceholders: (template, context, commID = "unknown") => {
if (!template || typeof template !== "string") {
return { result: template || "", hasErrors: false, missingKeys: [] };
}
if (!context || typeof context !== "object") {
return { result: template, hasErrors: false, missingKeys: [] };
}
let hasErrors = false;
const missingKeys = [];
const result = template.replace(/{{(.*?)}}/g, (match, key) => {
const trimmedKey = key.trim();
const value = context[trimmedKey];
if (value === undefined || value === null || value === "") {
hasErrors = true;
missingKeys.push(trimmedKey);
Logger.log(
`❌ Missing placeholder data for '${trimmedKey}' in ${commID}. available: ${Object.keys(context).join(", ")}`,
);
//DEBUG
Object.entries(context).forEach(([contextKey, contextValue]) => {
Logger.log(`Context key: '${contextKey}', value: '${contextValue}'`);
});
///
return `[MISSING: ${trimmedKey}]`; // Clear indicator of missing data
}
Logger.log(
`✅ Filled placeholder '${trimmedKey}' with '${value}' in ${commID}`,
);
return String(value);
});
if (hasErrors) {
Logger.log(
`⚠️ Template filling errors in ${commID}. Missing keys: ${missingKeys.join(", ")}`,
);
}
return { result, hasErrors, missingKeys };
},
// Batch send notifications
batchNotify: (notifications) => {
return notifications.map((notification) => {
switch (notification.type) {
case "user":
return COMM.notifyUser(
notification.email,
notification.subject,
notification.body,
);
case "admin":
return COMM.notifyAdmin(notification.subject, notification.body);
case "developer":
return COMM.notifyDeveloper(notification.subject, notification.body);
default:
return { success: false, error: "Unknown notification type" };
}
});
},
};