Top-ups: check the ad's own position, say where the free credits are, offer to pay from another

Bug: topUpCampaign read the position from the owner's campaign list, which
does not carry memberId, so every top-up was checked against the member's
MAIN position (Jim Watts: every ad said 19 free). It now reads the ad's row.

When the ad's position is short, the error names it and lists the member's
other positions with their free credits; the dashboard offers to pay from
the best one. Moving an ad settles any charges its old position already ran
up (a burn on the old member id) before switching, so the new position only
pays for views after the move.

Weekly email: unspent credits are broken down by position with the advice to
start a campaign from the largest.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
martbost
2026-09-27 06:05:19 -05:00
parent 5059d46bcb
commit 7ef8094a4d
5 changed files with 24 additions and 8 deletions
BIN
View File
Binary file not shown.
+9 -1
View File
@@ -248,7 +248,15 @@ async function nudgeTick() {
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) parts.push('- You have ' + bal.available.toLocaleString() + ' ad credits sitting unspent. That is ' + bal.available.toLocaleString() + ' cents of delivery doing nothing. Campaigns > New campaign, point it at your invite link: https://instantadpay.com/my#campaigns');
if (bal.available > 0) {
// credits sit on positions and each campaign pays from one, so a pooled total alone misleads
// members with linked positions (Jim Watts, 2026-09-27: "561 unspent" but a top-up allowed 19)
const onPos = (bal.per || []).filter(p => p.avail > 0).sort((a, b) => b.avail - a.avail);
const loose = (bal.credited || 0) + (bal.earned || 0);
let where = '';
if (onPos.length > 1) where = ' They are spread across your positions: ' + onPos.map(p => '#' + p.memberId + ' has ' + p.avail.toLocaleString()).join(', ') + (loose ? ', plus ' + loose.toLocaleString() + ' that any position can use' : '') + '. Each campaign pays from one position, so start a new campaign from #' + onPos[0].memberId + ' to use the most in one go.';
parts.push('- You have ' + bal.available.toLocaleString() + ' ad credits sitting unspent. That is ' + bal.available.toLocaleString() + ' cents of delivery doing nothing.' + where + ' Campaigns > New campaign, point it at your invite link: https://instantadpay.com/my#campaigns');
}
else if (bal.inCampaigns > 0) parts.push('- All ' + bal.inCampaigns.toLocaleString() + ' of your credits are working in live campaigns. Good.');
} catch (e) {}
// streak
+12 -5
View File
@@ -22,7 +22,7 @@
async function api(path, body) {
const r = await (await fetch(path, { method: 'POST',
headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body || {}) })).json();
if (r.error) throw new Error(r.error);
if (r.error) { const e = new Error(r.error); e.data = r; throw e; } // e.data: the full answer (e.g. top-up switch options)
return r;
}
@@ -1023,10 +1023,17 @@
el.querySelectorAll('button[data-topup]').forEach(b => b.addEventListener('click', async () => {
const n = await IAP.ask({ title: 'Add credits', text: 'How many credits to add to this campaign? More credits buy more views. They are set aside now and spend as views are delivered; anything unspent comes back when the campaign ends.', type: 'number', placeholder: 'e.g. 100', ok: 'Add credits' });
if (!n) return;
try { const r = await api('/api/my/campaigns/' + b.dataset.topup + '/topup', { credits: Number(n) });
IAP.status('Added ' + r.added + ' credits' + (r.reactivated ? ' — campaign is live again.' : '.'), 'ok');
await loadCampaigns(); loadDashboard();
} catch (e) { IAP.status(e.message, 'bad'); }
const done = r => { IAP.status('Added ' + r.added + ' credits' + (r.paidFrom ? ', paid from position #' + r.paidFrom : '') + (r.reactivated ? '. The campaign is live again.' : '.'), 'ok'); };
try { done(await api('/api/my/campaigns/' + b.dataset.topup + '/topup', { credits: Number(n) })); await loadCampaigns(); loadDashboard(); }
catch (e) {
// the ad's own position is short but another of yours has the credits: offer to pay from that one
const opt = e.data && e.data.switchOptions && e.data.switchOptions[0];
if (!opt) { IAP.status(e.message, 'bad'); return; }
const yes = await IAP.confirmBox('This ad pays from your position #' + e.data.fundId + ', which has ' + e.data.free + ' credits free. Your position #' + opt.memberId + ' has ' + opt.free + ' free. Pay for this top-up from #' + opt.memberId + '? From now on this ad spends from #' + opt.memberId + '.', { title: 'Pay from another position?', ok: 'Pay from #' + opt.memberId, cancel: 'Cancel' });
if (!yes) return;
try { done(await api('/api/my/campaigns/' + b.dataset.topup + '/topup', { credits: Number(n), switchTo: opt.memberId })); await loadCampaigns(); loadDashboard(); }
catch (e2) { IAP.status(e2.message, 'bad'); }
}
}));
} catch (e) {}
}
+1 -1
View File
@@ -1081,7 +1081,7 @@
<script src="/assets/common.js?v=20260923b"></script><script src="/assets/welcome.js?v=20260923b"></script>
<script src="/assets/wallet.js?v=20260911a"></script>
<script src="/assets/promo.js?v=20260925a"></script>
<script src="/assets/my.js?v=20260926b"></script>
<script src="/assets/my.js?v=20260927a"></script>
<script src="/assets/chat.js?v=20260907l"></script>
</body>
</html>
+2 -1
View File
@@ -3006,7 +3006,8 @@ const server = http.createServer(async (req, res) => {
if (!s || !s.email) return json(res, 401, { error: 'Sign in first.' });
const memberId = await auth.refreshMemberId(s);
const b = await readBody(req);
const r = await ads.topUpCampaign(s.email, memberId, m[1], b.credits);
const positions = [memberId, ...(await accounts.positions(s.email).catch(() => [])).map(x => x.memberId)].filter(Boolean);
const r = await ads.topUpCampaign(s.email, memberId, m[1], b.credits, { positions, switchTo: Number(b.switchTo) || 0 });
return json(res, r.error ? 400 : 200, r);
}
m = /^\/api\/my\/campaigns\/(\d+)\/extend$/.exec(p); // featured links: book more days (flat, charged now)