Referral purchase email names the buyer: @username in subject and body

memberLabel() resolves @username, or the owner's username for a linked
position, else member #id. Subject: '@name just bought a $20 ad package'.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
martbost
2026-09-12 10:37:45 -05:00
parent ebac2fcb32
commit 9b60e6414e
2 changed files with 16 additions and 4 deletions
+13 -4
View File
@@ -472,6 +472,16 @@ async function notifyNewReferral(ref, newAcct) {
} catch (e) {}
}
// welcome email on a new account: onboarding steps + who their sponsor is
// how a member is named in emails/alerts: @username, a linked position named after its owner, else member #id
async function memberLabel(id) {
try {
const a = await accounts.byMemberId(id);
if (a && a.username) return '@' + a.username;
const pos = await accounts.positionByMember(id);
if (pos && pos.email) { const o = await accounts.byEmail(pos.email); if (o && o.username) return '@' + o.username + ' (extra position #' + id + ')'; }
} catch (e) {}
return 'member #' + id;
}
async function sendWelcome(email, ref) {
try {
if (!mailer.hasKey()) return;
@@ -514,14 +524,13 @@ async function emailOnEvent(ev) {
if (buyer && buyer.sponsorId) {
const sp = await accounts.byMemberId(buyer.sponsorId);
if (sp && sp.email) {
const ba = await accounts.byMemberId(ev.buyerId);
const bn = ba && ba.username ? '@' + ba.username : 'One of your referrals';
const bn = await memberLabel(ev.buyerId); // @username at a glance (Marty, 2026-09-12)
const usd = ('$' + (ev.priceCents / 100).toFixed(2)).replace(/\.00$/, '');
const qual = ev.priceCents >= 2000
? ' This is a $20+ purchase, so it counts toward your qualification.'
: ' (Purchases under $20 do not count toward qualification.)';
mailer.send(sp.email, 'Your referral just bought an ad package',
bn + ' just purchased a package (' + usd + ' — ' + ev.creditAmount + ' credits).' + qual + '\n\n' +
mailer.send(sp.email, bn + ' just bought a ' + usd + ' ad package',
'Your referral ' + bn + ' (member #' + ev.buyerId + ') just purchased a package (' + usd + ' — ' + ev.creditAmount + ' credits).' + qual + '\n\n' +
'See your team and the live ledger: https://instantadpay.com/my\n\nInstantAdPay').catch(() => {});
}
}