My line: show payouts from an email-only session and across all owned positions

refreshMemberId falls back to the account's on-chain id when the session has no
wallet address (phone sign-in by email code), without caching it into the
session. /api/my/line now sums TierPaid events to every position the account
owns (main id + linked positions), so each direct's 'paid you so far' is right
from any device.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
martbost
2026-09-12 04:46:22 -05:00
parent 1fd61eca56
commit b844a227d3
2 changed files with 14 additions and 4 deletions
+9 -1
View File
@@ -146,7 +146,15 @@ async function fromRequest(req) {
return impl().get(decodeURIComponent(m[1])); return impl().get(decodeURIComponent(m[1]));
} }
async function refreshMemberId(sess) { async function refreshMemberId(sess) {
if (!sess || !sess.address) return (sess && sess.memberId) || 0; if (!sess) return 0;
if (!sess.address) {
if (sess.memberId) return sess.memberId;
// email-only session (phone sign-in with no wallet connected): read the account's
// on-chain id instead, so read-only views (My line, earnings, P&L) work from any
// device. Not cached into the session: a session without an address stays wallet-less.
if (sess.email) { try { const a = await require('./accounts').byEmail(sess.email); if (a && a.memberId) return a.memberId; } catch (e) {} }
return 0;
}
try { try {
const id = await chain.memberIdByAccount(sess.address); const id = await chain.memberIdByAccount(sess.address);
if (id && id !== sess.memberId) await updateSession(sess.token, { memberId: id }); if (id && id !== sess.memberId) await updateSession(sess.token, { memberId: id });
+5 -3
View File
@@ -1177,11 +1177,14 @@ const server = http.createServer(async (req, res) => {
const levels = await accounts.downline(s.email, 3); const levels = await accounts.downline(s.email, 3);
// what each person has paid THIS member so far: sum of TierPaid events where // what each person has paid THIS member so far: sum of TierPaid events where
// this member is the recipient and that person is the buyer (all indexed events) // this member is the recipient and that person is the buyer (all indexed events)
// ...counting payouts to every position this account owns (main id + linked positions)
const myId = await auth.refreshMemberId(s); const myId = await auth.refreshMemberId(s);
const own = (await accounts.positions(s.email)).filter(p => p.memberId);
const myIds = new Set([myId, ...own.map(p => p.memberId)].filter(Boolean));
const earnedBy = {}; const earnedBy = {};
if (myId) { if (myIds.size) {
for (const ev of chain.recentEvents(1e9)) { for (const ev of chain.recentEvents(1e9)) {
if (ev.type === 'TierPaid' && ev.recipientId === myId && ev.buyerId) if (ev.type === 'TierPaid' && myIds.has(ev.recipientId) && ev.buyerId)
earnedBy[ev.buyerId] = (BigInt(earnedBy[ev.buyerId] || '0') + BigInt(ev.amountWei)).toString(); earnedBy[ev.buyerId] = (BigInt(earnedBy[ev.buyerId] || '0') + BigInt(ev.amountWei)).toString();
} }
} }
@@ -1192,7 +1195,6 @@ const server = http.createServer(async (req, res) => {
joined: m.created, joined: m.created,
earnedWei: (m.memberId && earnedBy[m.memberId]) || '0' })) })); earnedWei: (m.memberId && earnedBy[m.memberId]) || '0' })) }));
// the member's own linked positions sit on level 1 too, labelled as theirs // the member's own linked positions sit on level 1 too, labelled as theirs
const own = (await accounts.positions(s.email)).filter(p => p.memberId);
if (own.length) { if (own.length) {
if (!out.find(L => L.level === 1)) out.unshift({ level: 1, members: [] }); if (!out.find(L => L.level === 1)) out.unshift({ level: 1, members: [] });
const L1 = out.find(L => L.level === 1); const L1 = out.find(L => L.level === 1);