- Database schema:
* Extended user_credits table with daily_limit, API costs, last_reset_at
* Created credit_transactions table for full audit trail
* Added RLS policies for secure access control
- Core services:
* credit-service.ts - balance checking, deduction, addition, transaction history
* admin-credit-service.ts - admin controls for managing users and credits
- API integration:
* Integrated credit checking into discover-whiskybase.ts
* Credits deducted after successful API calls
* Insufficient credits error handling
- Admin interface:
* /admin/users page with user management
* Statistics dashboard (total users, credits in circulation, usage)
* Interactive user table with search
* Edit modal for credit adjustment and settings
* Per-user daily limits and API cost configuration
- Features:
* Automatic credit initialization (100 credits for new users)
* Credit transaction logging with balance_after tracking
* Admin can add/remove credits with reason
* Admin can set custom daily limits per user
* Admin can set custom API costs per user
* Low credit warnings (< 10 credits)
* Full transaction history
- User experience:
* Credits checked before API calls
* Clear error messages for insufficient credits
* Graceful handling of credit deduction failures
System is ready for future enhancements like credit packages,
auto-recharge, and payment integration.
The admin_users policy was checking if user is admin by querying
the same table, causing infinite recursion. Changed to allow users
to view their own admin record directly using auth.uid() = user_id.
This fixes the error: 'infinite recursion detected in policy for
relation admin_users'
PostgreSQL requires functions in indexes to be marked IMMUTABLE.
Removed idx_api_usage_user_date composite index that used DATE().
Remaining indexes (user_id, api_type, created_at) still provide
good query performance for daily limit checks.
- Added database schema for API tracking system:
* api_usage table - tracks all Google Search and Gemini AI calls
* user_credits table - prepared for future credits system
* admin_users table - controls admin dashboard access
- Created comprehensive tracking service (track-api-usage.ts):
* trackApiUsage() - records API calls with success/failure
* checkDailyLimit() - enforces 80 Google Search calls/day limit
* getUserApiStats() - per-user statistics
* getGlobalApiStats() - app-wide statistics (admin only)
* checkIsAdmin() - server-side authorization
- Integrated tracking into discover-whiskybase.ts:
* Pre-call limit checking with friendly error messages
* Post-call usage tracking for success and failures
* User authentication verification
- Built admin dashboard at /admin:
* Global statistics cards (total, today, by API type)
* Top 10 users by API usage
* Recent activity log with 50 latest calls
* Color-coded status indicators
* Secure access with RLS policies
- Features:
* Daily limit resets at midnight Europe/Berlin timezone
* Graceful error handling (allows on tracking failure)
* Comprehensive indexes for fast queries
* Ready for future credits/monetization system
- Created type-safe i18n system with TranslationKeys interface
- Added German (de) and English (en) translations with 160+ keys
- Implemented I18nContext provider and useI18n hook
- Added LanguageSwitcher component for language selection
- Refactored all major components to use translations:
* Home page, StatsDashboard, DramOfTheDay
* BottleGrid, EditBottleForm, CameraCapture
* BuddyList, SessionList, TastingNoteForm
* StatusSwitcher and bottle management features
- Implemented locale-aware currency formatting (EUR)
- Implemented locale-aware date formatting
- Added localStorage persistence for language preference
- Added automatic browser language detection
- Organized translations into 8 main categories
- System is extensible for additional languages