diff --git a/ads.js b/ads.js index 2caa946..c1cecc4 100644 Binary files a/ads.js and b/ads.js differ diff --git a/coach.js b/coach.js index 7f8b094..0e47c6f 100644 --- a/coach.js +++ b/coach.js @@ -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 diff --git a/public/assets/my.js b/public/assets/my.js index 4947ca5..bf9728f 100644 --- a/public/assets/my.js +++ b/public/assets/my.js @@ -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) {} } diff --git a/public/my.html b/public/my.html index 2907be7..c6eb1a3 100644 --- a/public/my.html +++ b/public/my.html @@ -1081,7 +1081,7 @@ - + diff --git a/server.js b/server.js index 66182f4..7d58eb6 100644 --- a/server.js +++ b/server.js @@ -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)