diff --git a/backend/src/services/marketingService.ts b/backend/src/services/marketingService.ts index e406d093..1aa6ad4e 100644 --- a/backend/src/services/marketingService.ts +++ b/backend/src/services/marketingService.ts @@ -186,6 +186,46 @@ export function mergeFeedItems( return out; } +/** + * Withdrawals feed: treat real + synthetic the same — one de-duped pool, + * time-rotated so neither sticks permanently at the top. + */ +export function blendAndRotateFeedItems( + real: T[], + synthetic: T[], + target: number, + now = Date.now() +): T[] { + const pool: T[] = []; + const seen = new Set(); + const push = (item: T) => { + const key = item.publicId ?? item.id; + if (seen.has(key)) return; + seen.add(key); + pool.push(item); + }; + for (const r of real) push(r); + for (const s of synthetic) push(s); + if (pool.length === 0 || target <= 0) return []; + const slot = slotIndex(now); + const start = slot % pool.length; + const n = Math.min(target, pool.length); + const out: T[] = []; + for (let i = 0; i < n; i++) { + out.push(pool[(start + i) % pool.length]); + } + return out; +} + +/** Marketing display amount: whole USDT-like units (same look as synthetic labels). */ +export function formatWithdrawalMarketingAmount(raw: string | number): string { + const n = typeof raw === 'number' ? raw : Number(raw); + if (!Number.isFinite(n)) return '0'; + const human = n / 1_000_000; + if (!Number.isFinite(human) || human <= 0) return '0'; + return String(Math.max(1, Math.round(human))); +} + function mapRealPlaying(row: marketingRepository.RealActivityRow): MarketingActivityItem { return { id: row.id, @@ -202,7 +242,7 @@ function mapRealWithdrawal(row: marketingRepository.RealWithdrawalRow): Marketin return { id: row.id, publicId: maskPublicId(row.user_id), - amountLabel: formatAmountRaw(row.amount), + amountLabel: formatWithdrawalMarketingAmount(row.amount), statusLabel: 'Completed', createdAt: row.created_at.toISOString(), synthetic: false, @@ -267,21 +307,30 @@ export async function getWithdrawalFeed(): Promise<{ items: MarketingWithdrawalI const ttlMs = settings.cacheTtlSeconds * 1000; return cached('withdrawal-feed', ttlMs, async () => { const pad = settings.maxSyntheticWithdrawals; + const realFetch = Math.max(8, pad); + const displayTarget = Math.max(pad > 0 ? pad : 8, 8); const [rows, realActive, personas] = await Promise.all([ - marketingRepository.listRecentRealWithdrawals(8, settings.withdrawalRealWindowHours), + marketingRepository.listRecentRealWithdrawals( + realFetch, + settings.withdrawalRealWindowHours + ), marketingRepository.countRecentDistinctPlayers(settings.activeWindowMinutes), loadActivePersonasCached(ttlMs, Math.max(pad, settings.maxSyntheticEntries)), ]); const real = rows.map(mapRealWithdrawal); const useSynthetic = shouldUseSynthetic(realActive, settings); - if (!useSynthetic || pad <= 0) return { items: real }; - const synthetic = buildSyntheticWithdrawalsFromPersonas( - personas, - pad, - settings.syntheticWithdrawalAmounts - ); - const feedTarget = real.length + pad; - return { items: mergeFeedItems(real, synthetic, feedTarget) }; + const synthetic = + useSynthetic && pad > 0 + ? buildSyntheticWithdrawalsFromPersonas( + personas, + pad, + settings.syntheticWithdrawalAmounts + ) + : []; + // Same presentation for real + synthetic: shared rotating pool (no sticky real-first). + return { + items: blendAndRotateFeedItems(real, synthetic, displayTarget), + }; }); } diff --git a/backend/src/tests/marketingService.test.ts b/backend/src/tests/marketingService.test.ts index 71dfe6b5..4204b1ab 100644 --- a/backend/src/tests/marketingService.test.ts +++ b/backend/src/tests/marketingService.test.ts @@ -5,11 +5,13 @@ import { utcDayBounds, } from '../repositories/marketingRepository'; import { + blendAndRotateFeedItems, buildSyntheticActivityFromPersonas, buildSyntheticWithdrawalsFromPersonas, clearMarketingCache, computeActivePlayersCount, formatAmountRaw, + formatWithdrawalMarketingAmount, maskPublicId, mergeFeedItems, shouldUseSynthetic, @@ -60,6 +62,36 @@ describe('marketingService helpers', () => { assert.equal(formatAmountRaw('50000000'), '50.00'); }); + test('formatWithdrawalMarketingAmount rounds to whole units like synthetics', () => { + assert.equal(formatWithdrawalMarketingAmount('3200000'), '3'); + assert.equal(formatWithdrawalMarketingAmount('120000000'), '120'); + }); + + test('blendAndRotateFeedItems mixes real and synthetic without sticky real-first', () => { + const real = [ + { id: 'r1', publicId: 'aa***11' }, + { id: 'r2', publicId: 'bb***22' }, + ]; + const synthetic = [ + { id: 's1', publicId: 'cc***33' }, + { id: 's2', publicId: 'dd***44' }, + { id: 's3', publicId: 'ee***55' }, + ]; + // slotIndex = floor(now/10_000); pick times that shift start by 1 in a pool of 5. + const a = blendAndRotateFeedItems(real, synthetic, 4, 0); + const b = blendAndRotateFeedItems(real, synthetic, 4, 10_000); + assert.equal(a.length, 4); + assert.equal(b.length, 4); + assert.deepEqual( + a.map((x) => x.id), + ['r1', 'r2', 's1', 's2'] + ); + assert.deepEqual( + b.map((x) => x.id), + ['r2', 's1', 's2', 's3'] + ); + }); + test('mergeFeedItems keeps real first then pads synthetic', () => { const real = [ { id: 'r1', publicId: 'aa***11' },