diff --git a/auth.js b/auth.js index cd4e2a5..11f8f58 100644 --- a/auth.js +++ b/auth.js @@ -146,7 +146,15 @@ async function fromRequest(req) { return impl().get(decodeURIComponent(m[1])); } 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 { const id = await chain.memberIdByAccount(sess.address); if (id && id !== sess.memberId) await updateSession(sess.token, { memberId: id }); diff --git a/server.js b/server.js index 81105cc..f8f603a 100644 --- a/server.js +++ b/server.js @@ -1177,11 +1177,14 @@ const server = http.createServer(async (req, res) => { const levels = await accounts.downline(s.email, 3); // 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) + // ...counting payouts to every position this account owns (main id + linked positions) 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 = {}; - if (myId) { + if (myIds.size) { 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(); } } @@ -1192,7 +1195,6 @@ const server = http.createServer(async (req, res) => { joined: m.created, earnedWei: (m.memberId && earnedBy[m.memberId]) || '0' })) })); // 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 (!out.find(L => L.level === 1)) out.unshift({ level: 1, members: [] }); const L1 = out.find(L => L.level === 1);