From 7ef8094a4d30a7b96123674c9e62d08238671d4c Mon Sep 17 00:00:00 2001 From: martbost Date: Sun, 27 Sep 2026 06:05:19 -0500 Subject: [PATCH] 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 --- ads.js | Bin 112508 -> 116504 bytes coach.js | 10 +++++++++- public/assets/my.js | 17 ++++++++++++----- public/my.html | 2 +- server.js | 3 ++- 5 files changed, 24 insertions(+), 8 deletions(-) diff --git a/ads.js b/ads.js index 2caa946b3e067a7c0236fed9b0fba9365c7838f0..c1cecc457ad39b9b1185e30b5bdb388bc7cd015b 100644 GIT binary patch delta 3180 zcmaJ@OKjXk7?x3KX(?|hJSyNX2-YSW(^lfqEGeKCP*IaWUlF9C%-XYAgY7Z4H_NhF z774_WLwzuJ#03cnRG}U?A;ggiIB?_41(hoY4t)QN?IgS|*^SpT|NnolnLn@X{`jLk zU!1xyee_XUR2z~6c_KJ4H)h}S7sBx05KWlE|?_#`!qWr{Zvl?8dA;*9Jg zhmJfxcj)Ncktf>pVi3`a$n$KT9((*S^-?LxOQj#=**+rF5?-n@6bMj~_n7KU+9;o^ zWNwg?kw1W5QHb{i26#j@(5z`>4YNe7gndhPO zNvw+?&T_qQ`P7MK;W}DLv%F22(!a%Wh~k)&8;~0qJP{-Gt5VtsV2LiiL5fcyD~yb)H*%Jf17953Re3U&l`r_NIG>=Q4;VL@wP}4Q*!)qp8hy`T1wo z0bT51t4*taAHR)Zd7b8=xELR>8HVi;7)buUjwY9X+r0KOdqO37=GN+hnpp~?Mm3-o z3ek|%h5(sr5a%7UG8+cD*FOu`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> 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)