|
|
||
|---|---|---|
| .. | ||
| db | ||
| README.md | ||
| api.ts | ||
| auth.ts | ||
| badges.ts | ||
| points.ts | ||
| types.ts | ||
README.md
Base Infrastructure
This directory contains the core backend utilities for the task-based earning platform.
Structure
app/lib/
├── types.ts # TypeScript type definitions
├── auth.ts # Authentication utilities
├── points.ts # Points management
├── badges.ts # Badge logic
└── db/
└── store.ts # In-memory data store
Types (types.ts)
All TypeScript interfaces and enums:
- User: User account data
- Transaction: Points transactions (earned/spent)
- Task: Task definitions (checkin, ad, survey, quiz, etc.)
- UserTask: Completed tasks by users
- Badge: Achievement definitions
- UserBadge: Badges earned by users
- Quiz: Quiz questions and answers
- Referral: User referral tracking
- API Response Types: Structured responses for all endpoints
Database Store (db/store.ts)
In-memory storage with CRUD operations:
- User management (create, find, update)
- Transaction tracking
- Task management
- Badge operations
- Quiz handling
- Referral tracking
Seed Data
Pre-populated with:
- 6 Tasks: Daily checkin, watch ad, survey, quiz, video, reading
- 6 Badges: First steps, point collector, dedicated, week warrior, referral master, point millionaire
- 1 Quiz: Sample quiz for quiz task
Authentication (auth.ts)
User authentication and security:
hashPassword(): Hash passwords (base64 for MVP)verifyPassword(): Verify password against hashgenerateToken(): Create JWT-like tokensverifyToken(): Validate and decode tokensgetCurrentUser(): Extract user from requestrequireAuth(): Middleware for protected routesisValidEmail(): Email validationisValidPassword(): Password strength validationsanitizeUser(): Remove sensitive data from user object
Points Management (points.ts)
Points and transactions:
addPoints(): Award points to userdeductPoints(): Spend points (with balance check)getBalance(): Get current balancegetTransactions(): Get transaction historygetTransactionStats(): Earnings/spending statisticsgetPointsBreakdown(): Points by sourceawardTaskPoints(): Task completion rewardsawardReferralBonus(): Referral bonusesawardStreakBonus(): Daily streak bonuses
Badge System (badges.ts)
Achievement tracking:
checkAndAwardBadges(): Auto-award eligible badgesgetUserBadgesWithDetails(): Get earned badgesgetAvailableBadges(): Get unearned badgesgetBadgeProgress(): Progress towards specific badgegetAllBadgeProgress(): Progress for all badgesgetBadgeStats(): Badge statistics
Badge Requirements
- POINTS_TOTAL: Total points earned
- TASKS_COMPLETED: Number of tasks completed
- STREAK_DAYS: Consecutive daily activity
- REFERRALS: Number of successful referrals
Usage Example
import { createUser, findUserByEmail } from './db/store';
import { hashPassword, generateToken } from './auth';
import { addPoints, getBalance } from './points';
import { checkAndAwardBadges } from './badges';
// Register user
const passwordHash = await hashPassword('SecurePass123');
const user = createUser('user@example.com', passwordHash, 'John Doe');
// Generate auth token
const token = generateToken(user.id);
// Award points for task completion
addPoints(user.id, 10, 'task:daily_checkin');
// Check balance
const balance = getBalance(user.id);
// Check and award badges
const newBadges = checkAndAwardBadges(user.id);
Next Steps
This base infrastructure is ready for API endpoint integration:
- Create
/api/auth/*routes (register, login, logout) - Create
/api/tasks/*routes (list, complete) - Create
/api/points/*routes (balance, transactions) - Create
/api/badges/*routes (earned, available, progress) - Create
/api/leaderboardroute