The following changes were made by the replace_file_content tool to: /root/aadharbot/index.js. If relevant, proactively run terminal commands to execute this code for the USER. Don't ask for permission. [diff_block_start] @@ -915,6 +915,88 @@ } } +// ── DAILY FREE SEARCH ENGINE ────────────────────────────────────── +const dailyFreeSearchesFallback = {}; + +function getTodayISTDateStr() { + const now = new Date(); + const istTime = new Date(now.getTime() + (5.5 * 60 * 60 * 1000)); + const y = istTime.getUTCFullYear(); + const m = String(istTime.getUTCMonth() + 1).padStart(2, '0'); + const d = String(istTime.getUTCDate()).padStart(2, '0'); + return `${y}-${m}-${d}`; +} + +async function getDailyFreeSearchAvailable(userId) { + if (checkIsAdmin(userId)) return true; + + const dateStr = getTodayISTDateStr(); + const redisKey = `daily_free_used:${userId}:${dateStr}`; + + // 1. Check Redis (sub-millisecond) + if (redisClient) { + try { + const val = await redisClient.get(redisKey); + if (val === '1') { + return false; + } + } catch (e) { + console.error("Redis error fetching daily free search:", e.message); + } + } + + // 2. Check Database + if (pool) { + try { + const res = await pool.query('SELECT last_free_search_date FROM users WHERE telegram_id = $1', [userId]); + if (res.rows.length > 0 && res.rows[0].last_free_search_date === dateStr) { + if (redisClient) { + redisClient.set(redisKey, '1', 'EX', 90000).catch(() => {}); + } + return false; + } + } catch (e) { + console.error("DB error fetching daily free search:", e.message); + } + } else { + const localKey = `${userId}:${dateStr}`; + if (dailyFreeSearchesFallback[localKey]) { + return false; + } + } + + return true; // Available! +} + +async function markDailyFreeSearchUsed(userId) { + const dateStr = getTodayISTDateStr(); + const redisKey = `daily_free_used:${userId}:${dateStr}`; + + if (redisClient) { + try { + await redisClient.set(redisKey, '1', 'EX', 90000); + } catch (e) { + console.error("Redis error in markDailyFreeSearchUsed:", e.message); + } + } + + if (pool) { + try { + await pool.query( + `UPDATE users + SET last_free_search_date = $1, updated_at = NOW() + WHERE telegram_id = $2`, + [dateStr, userId] + ); + } catch (e) { + console.error("DB error in markDailyFreeSearchUsed:", e.message); + } + } + + const localKey = `${userId}:${dateStr}`; + dailyFreeSearchesFallback[localKey] = 1; +} + async function checkUserAccess(userId) { // Admin bypass: Admin gets unlimited credits if (checkIsAdmin(userId)) { @@ -921,5 +921,6 @@ hasAccess: true, isUnlimited: true, + hasFreeSearch: true, credits: 999999, planExpiresAt: new Date(Date.now() + 1000 * 24 * 60 * 60 * 1000), planStartedAt: new Date(), @@ -926,11 +926,16 @@ }; } - if (!pool) return { hasAccess: false, credits: 0, isUnlimited: false, planExpiresAt: null, planStartedAt: null, createdAt: null }; - - // Real-time Database Check (No cache for plan/balance to avoid stale expired searches) - try { - const res = await pool.query('SELECT credits, plan_type, plan_expires_at, plan_started_at, created_at FROM users WHERE telegram_id = $1', [userId]); + if (!pool) { + const todayStr = getTodayISTDateStr(); + const localKey = `${userId}:${todayStr}`; + const hasFreeSearch = !dailyFreeSearchesFallback[localKey]; + return { hasAccess: hasFreeSearch, credits: 0, hasFreeSearch, isUnlimited: false, planExpiresAt: null, planStartedAt: null, createdAt: null }; + } + + // Real-time Database Check + try { + const res = await pool.query('SELECT credits, plan_type, plan_expires_at, plan_started_at, created_at, last_free_search_date FROM users WHERE telegram_id = $1', [userId]); let resultData; if (res.rows.length > 0) { const row = res.rows[0]; @@ -937,9 +937,13 @@ const planStarted = row.plan_started_at ? new Date(row.plan_started_at) : (row.created_at ? new Date(row.created_at) : null); const isUnlimited = row.plan_type !== 'credits' && planExpires && planExpires > new Date(); const credits = row.credits || 0; + const todayStr = getTodayISTDateStr(); + const hasFreeSearch = !isUnlimited && (row.last_free_search_date !== todayStr); + resultData = { - hasAccess: isUnlimited || credits > 0, + hasAccess: isUnlimited || hasFreeSearch || credits > 0, isUnlimited, + hasFreeSearch, credits, planExpiresAt: planExpires, planStartedAt: planStarted, @@ -946,6 +946,6 @@ }; } else { - resultData = { hasAccess: false, isUnlimited: false, credits: 0, planExpiresAt: null, planStartedAt: null, createdAt: null }; + resultData = { hasAccess: false, isUnlimited: false, hasFreeSearch: false, credits: 0, planExpiresAt: null, planStartedAt: null, createdAt: null }; } return applyFreeMode(resultData); @@ -952,5 +952,5 @@ console.error("Error in checkUserAccess:", err.message); - return applyFreeMode({ hasAccess: false, isUnlimited: false, credits: 0, planExpiresAt: null, planStartedAt: null, createdAt: null }); + return applyFreeMode({ hasAccess: false, isUnlimited: false, hasFreeSearch: false, credits: 0, planExpiresAt: null, planStartedAt: null, createdAt: null }); } } [diff_block_end] Please note that the above snippet only shows the MODIFIED lines from the last change. It shows up to 3 lines of unchanged lines before and after the modified lines. The actual file contents may have many more lines not shown.