diff --git a/coach.js b/coach.js
index 0e47c6f..f08f784 100644
--- a/coach.js
+++ b/coach.js
@@ -205,6 +205,103 @@ async function linkStats(email) {
return { angles: Object.values(rows), sources, totalViews: views.length, totalJoins: joined.length };
}
+// ---- the weekly member email (Marty, 2026-09-14; reformatted 2026-10-01 because one long text block was hard to read):
+// one section per topic, each with a heading, the number that matters up front and ONE button. Sent as HTML with a
+// plain-text twin built from the same sections.
+const SITE = 'https://instantadpay.com';
+const escH = v => String(v == null ? '' : v).replace(/&/g, '&').replace(//g, '>').replace(/"/g, '"');
+const n0 = v => Number(v || 0).toLocaleString('en-US');
+async function weeklyDigest(acct, view, lbWeek, tankN, now) {
+ const name = acct.username ? '@' + acct.username : 'there';
+ const S = []; // { title, big, bigLabel, lines[], rows[[label, value]], quotes[{to, text}], button{label, url} }
+ // credits: they sit on positions and each campaign pays from one, so the pooled total alone misleads
+ // members with linked positions (Jim Watts, 2026-09-27: "561 unspent" but a top-up allowed 19)
+ try {
+ const ids = [acct.memberId, ...(await accounts.positions(acct.email)).map(p => p.memberId)].filter(Boolean);
+ const bal = await X.ads.balances(ids, acct.email);
+ if (bal.available > 0) {
+ const onPos = (bal.per || []).filter(p => p.avail > 0).sort((a, b) => b.avail - a.avail);
+ const loose = (bal.credited || 0) + (bal.earned || 0);
+ const sec = { title: 'Ad credits ready to use', big: n0(bal.available), bigLabel: 'credits not running yet', lines: [], button: { label: 'Start a campaign', url: SITE + '/my#campaigns' } };
+ if (onPos.length > 1) {
+ sec.rows = onPos.map(p => ['Position #' + p.memberId, n0(p.avail)]);
+ if (loose) sec.rows.push(['Any position can use', n0(loose)]);
+ sec.lines.push('Each campaign spends from one position. Start from #' + onPos[0].memberId + ' to use the most credits at once.');
+ }
+ sec.lines.push('Point the campaign at your invite link so the views bring people to you.');
+ S.push(sec);
+ } else if (bal.inCampaigns > 0) S.push({ title: 'Ad credits', big: n0(bal.inCampaigns), bigLabel: 'credits working in live campaigns', lines: ['Everything you have is running. Nice.'] });
+ } catch (e) {}
+ // streak
+ try {
+ const st = await X.ads.viewStatus(acct.email);
+ if (st.streakDay > 1) S.push({ title: 'Daily claim streak', big: 'Day ' + st.streakDay, bigLabel: 'of your streak. Today’s claim pays ' + n0(st.claimCredits) + ' credits.', lines: ['Miss a day and the streak starts over.'], button: { label: 'Claim today', url: SITE + '/my#earn' } });
+ else S.push({ title: 'Free daily credits', lines: ['View five ads and claim once a day. The claim grows 5, 7, 10 credits, then 25 on every seventh day in a row.'], button: { label: 'Start today', url: SITE + '/my#earn' } });
+ } catch (e) {}
+ // contest
+ let first = false;
+ if (lbWeek && lbWeek.top) {
+ const me = lbWeek.top.find(r => r.name === '@' + acct.username) || null;
+ let mine = me; if (!mine) { try { mine = (await X.lb().view('week', acct.email)).me; } catch (e) {} }
+ first = !!(lbWeek.top.length && lbWeek.top[0].name === '@' + acct.username);
+ const sec = { title: 'This week’s referral contest', lines: [], button: { label: 'See the leaderboard', url: SITE + '/leaderboard' } };
+ if (lbWeek.prize) sec.intro = 'Prizes: ' + lbWeek.prize + '.';
+ if (lbWeek.top.length) sec.rows = lbWeek.top.slice(0, 3).map(r => [r.rank + '. ' + r.name, r.sales + ' sold']);
+ else sec.lines.push('No sales yet. The first one takes the top spot.');
+ if (mine) { sec.big = '#' + mine.rank; sec.bigLabel = 'is your place, with ' + mine.sales + ' sold'; }
+ else sec.lines.push('You are not on the board yet. One $20 package bought by someone you sponsor puts you there.');
+ S.push(sec);
+ }
+ // tank
+ if (tankN > 0 && acct.memberId) S.push({ title: 'Holding tank', big: n0(tankN), bigLabel: 'member' + (tankN === 1 ? '' : 's') + ' waiting for a sponsor', lines: ['Adopt one from My line and they join your team.'], button: { label: 'Adopt a member', url: SITE + '/my#line' } });
+ // line (sponsors only)
+ if (view.directs.length) {
+ const week = view.directs.filter(d => now - d.joined < 7 * DAY).length;
+ const sec = { title: 'Your team', rows: [['Direct members', n0(view.directs.length)], ['Joined this week', n0(week)], ['Gone quiet', n0(view.stalled)]], lines: [], button: { label: 'Open My line', url: SITE + '/my#line' } };
+ const q = view.directs.filter(d => d.stalled).slice(0, 3).map(d => ({ to: d.name, text: d.say.replace('{{name}}', d.name.replace(/^@/, '')) }));
+ if (q.length) { sec.lines.push('A quick message gets most people moving again. Copy these:'); sec.quotes = q; }
+ S.push(sec);
+ }
+ const subject = first ? 'You are #1 this week on InstantAdPay' : 'Your week on InstantAdPay: credits, streak, contest';
+ const tip = { text: 'Short on time? The twenty-minute day: claim your set, message one person on your team, check the tank, start one new conversation.', link: SITE + '/blog/the-twenty-minute-day' };
+ const legal = 'No income is guaranteed. Credits are advertising, not money.';
+
+ // plain text twin
+ const T = ['Hi ' + name + ', here is your week on InstantAdPay.', ''];
+ for (const s of S) {
+ T.push(s.title.toUpperCase());
+ if (s.big) T.push(s.big + ' ' + s.bigLabel);
+ if (s.intro) T.push(s.intro);
+ for (const r of s.rows || []) T.push(' ' + r[0] + ': ' + r[1]);
+ for (const l of s.lines) T.push(l);
+ for (const q of s.quotes || []) T.push(' To ' + q.to + ': "' + q.text + '"');
+ if (s.button) T.push(s.button.label + ': ' + s.button.url);
+ T.push('');
+ }
+ T.push(tip.text + ' ' + tip.link, '', 'InstantAdPay · ' + SITE + '/my', legal);
+
+ // html
+ const btn = b => '' + escH(b.label) + '';
+ const card = s => '
'
+ + '
' + escH(s.title) + '
'
+ + (s.big ? '
' + escH(s.big) + '' + escH(s.bigLabel) + '
' : '')
+ + (s.intro ? '
' + escH(s.intro) + '
' : '')
+ + (s.rows ? '
' + s.rows.map(r => '
' + escH(r[0]) + '
' + escH(r[1]) + '
').join('') + '
' : '')
+ + s.lines.map(l => '
' + escH(l) + '
').join('')
+ + (s.quotes || []).map(q => '
To ' + escH(q.to) + ' ' + escH(q.text) + '
').join('')
+ + (s.button ? '
' + btn(s.button) + '
' : '')
+ + '
';
+ const html = ''
+ + '
'
+ + '
'
+ + '
Your week on InstantAdPay
Hi ' + escH(name) + ', here is where things stand.