-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmailService.js
More file actions
222 lines (196 loc) · 8.67 KB
/
Copy pathEmailService.js
File metadata and controls
222 lines (196 loc) · 8.67 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
/**
* EmailService
* Phase 5: Drafts and sends pre-session engagement emails.
* Uses Gemini to draft, then sends via GmailApp.
*/
const EmailService = {
/**
* Drafts a pre-session email using Gemini and sends it to the recipient list.
* @param {string} sessionId
* @param {string[]} recipients - Array of email addresses
* @returns {{ subject, recipientCount }}
*/
draftAndSend: function(sessionId, recipients) {
const session = SessionService.getSession(sessionId);
if (!session) throw new Error('Session not found: ' + sessionId);
if (!session.brief) throw new Error('No session brief. Complete the Wizard first.');
// Get library URL for this session
const libraryUrl = session.libraryUrl || this._buildLibraryUrl(sessionId);
// Get top pre-reading resources — exclude Internal resources (Planning Doc, Context, etc.)
const allResources = SynthesisService.getResources(sessionId);
const publicResources = allResources.filter(r => r.isPublic !== false);
const preReadingResources = publicResources
.filter(r => r.preReading)
.slice(0, 3)
.map(r => ({ url: r.url, title: r.title, relevanceStatement: r.relevanceStatement }));
// If no pre-reading flagged yet, take top 3 public resources by score
const resources = preReadingResources.length > 0
? preReadingResources
: publicResources.slice(0, 3).map(r => ({ url: r.url, title: r.title, relevanceStatement: r.relevanceStatement }));
// Draft with Gemini
const draft = GeminiService.draftPreSessionEmail(
session.brief,
resources,
session.date,
libraryUrl
);
const subject = draft.subject || 'Getting Ready for Our Next Session';
const htmlBody = this._wrapInEmailTemplate(draft.body || '', session, libraryUrl);
// Send to all recipients
recipients.forEach(email => {
GmailApp.sendEmail(email, subject, '', { htmlBody: htmlBody });
});
// Mark email as sent in session row
SessionService.updateSession(sessionId, {
EMAIL_SENT: 'Yes — ' + new Date().toLocaleDateString()
});
return { subject, recipientCount: recipients.length };
},
/**
* Returns just the draft (subject + body) without sending.
* Used by the web app for the human-review preview modal.
*/
getDraft: function(sessionId) {
const session = SessionService.getSession(sessionId);
if (!session || !session.brief) return null;
const libraryUrl = session.libraryUrl || this._buildLibraryUrl(sessionId);
const allResources = SynthesisService.getResources(sessionId);
const publicResources = allResources.filter(r => r.isPublic !== false);
const resources = publicResources
.filter(r => r.preReading)
.slice(0, 3)
.map(r => ({ url: r.url, title: r.title, relevanceStatement: r.relevanceStatement }));
return GeminiService.draftPreSessionEmail(
session.brief,
resources.length > 0 ? resources : publicResources.slice(0, 3).map(r => ({
url: r.url, title: r.title, relevanceStatement: r.relevanceStatement
})),
session.date,
libraryUrl
);
},
/**
* Returns structured newsletter draft data for the email wizard.
* Includes AI-generated sections + session metadata + resources for client-side rendering.
* @param {string} sessionId
* @returns {{ subject, heroLine, intro, highlights, preReadingNote, sessionName, sessionDate, sessionFormat, sessionAudience, libraryUrl, hasGems, resources }}
*/
getNewsletterDraft: function(sessionId) {
const session = SessionService.getSession(sessionId);
if (!session || !session.brief) return null;
const libraryUrl = session.libraryUrl || this._buildLibraryUrl(sessionId);
const allResources = SynthesisService.getResources(sessionId);
const publicResources = allResources.filter(r => r.isPublic !== false);
const preReading = publicResources.filter(r => r.preReading).slice(0, 3);
const topResources = preReading.length > 0 ? preReading : publicResources.slice(0, 3);
const resourcesForGemini = topResources.map(r => ({
url: r.url || '',
title: r.title || r.name || 'Resource',
relevanceStatement: r.relevanceStatement || ''
}));
// Find the main meeting link (isMain first, then first meeting-type resource)
const meetings = allResources.filter(r => {
if ((r.type || '').toLowerCase() === 'meeting link') return true;
const u = (r.url || '').toLowerCase();
return u.includes('meet.google.com') || u.includes('zoom.us') || u.includes('teams.microsoft.com');
});
meetings.sort((a, b) => (b.isMain ? 1 : 0) - (a.isMain ? 1 : 0));
const mainMeeting = meetings[0] || null;
const sections = GeminiService.draftNewsletterSections(
session.brief,
resourcesForGemini,
session.date,
libraryUrl
);
return Object.assign({}, sections, {
sessionName: session.name,
sessionDate: session.date,
sessionFormat: session.format,
sessionAudience: session.audience,
libraryUrl: libraryUrl,
hasGems: !!(session.gems && session.gems.length),
resources: resourcesForGemini,
mainMeetingUrl: mainMeeting ? (mainMeeting.url || '') : '',
mainMeetingName: mainMeeting ? (mainMeeting.title || 'Join the Session') : ''
});
},
/**
* Sends a pre-assembled full HTML email directly — no template wrapping.
* Used by the newsletter wizard which builds its own template client-side.
* @param {string} sessionId
* @param {string[]} recipients
* @param {string} subject
* @param {string} fullHtml - Complete DOCTYPE HTML string
*/
sendRawEmail: function(sessionId, recipients, subject, fullHtml) {
recipients.forEach(email => {
GmailApp.sendEmail(email, subject, '', { htmlBody: fullHtml });
});
SessionService.updateSession(sessionId, {
EMAIL_SENT: 'Yes — ' + new Date().toLocaleDateString()
});
return { success: true, recipientCount: recipients.length };
},
/**
* Sends a pre-approved draft directly (called from web app after review).
* Applies the branded email template around the body content before sending.
*/
sendApprovedDraft: function(sessionId, recipients, subject, bodyContent) {
const session = SessionService.getSession(sessionId);
const libraryUrl = session ? (session.libraryUrl || this._buildLibraryUrl(sessionId)) : '';
const htmlBody = this._wrapInEmailTemplate(bodyContent, session || {}, libraryUrl);
recipients.forEach(email => {
GmailApp.sendEmail(email, subject, '', { htmlBody: htmlBody });
});
SessionService.updateSession(sessionId, {
EMAIL_SENT: 'Yes — ' + new Date().toLocaleDateString()
});
return { success: true, recipientCount: recipients.length };
},
// ─── Private ────────────────────────────────────────────────────────────────
_buildLibraryUrl: function(sessionId) {
try {
const base = ScriptApp.getService().getUrl();
return base ? base + '?page=library&session=' + sessionId : '';
} catch (e) {
return '';
}
},
_wrapInEmailTemplate: function(body, session, libraryUrl) {
const brandColor = '#0B2B46';
const accentColor = '#5DCDF5';
const libraryLink = libraryUrl
? `<p style="text-align:center;margin:24px 0;">
<a href="${libraryUrl}" style="background:${accentColor};color:${brandColor};padding:12px 28px;border-radius:6px;text-decoration:none;font-weight:bold;display:inline-block;">
Open Learning Library →
</a>
</p>`
: '';
return `
<!DOCTYPE html>
<html>
<head><meta charset="UTF-8"></head>
<body style="margin:0;padding:0;background:#f5f7fa;font-family:'Open Sans',Arial,sans-serif;">
<div style="max-width:600px;margin:0 auto;background:#ffffff;border-radius:8px;overflow:hidden;margin-top:24px;">
<!-- Header -->
<div style="background:${brandColor};padding:28px 32px;">
<p style="color:${accentColor};font-size:12px;letter-spacing:2px;text-transform:uppercase;margin:0 0 6px;">Learning Library Learning Community</p>
<h1 style="color:#ffffff;font-size:22px;margin:0;">${session.name || 'Upcoming Session'}</h1>
</div>
<!-- Body -->
<div style="padding:32px;color:#2d2d2d;font-size:15px;line-height:1.7;">
${body}
${libraryLink}
</div>
<!-- Footer -->
<div style="background:#f5f7fa;padding:20px 32px;border-top:1px solid #e0e6ed;">
<p style="color:#8a9bae;font-size:12px;margin:0;">
Learning Library Learning Community · ${session.date || ''}
</p>
</div>
</div>
</body>
</html>
`.trim();
}
};