- Frontend: Next.js with TypeScript
- Backend: Server Actions (Next.js)
- Database: MongoDB with Mongoose ODM
- Authentication: Clerk
- AI Integration: Groq API (Llama model)
- External APIs: JSearch for job listings
Fields:
clerkId: String (required) - Clerk authentication IDname: String (required) - User's display nameusername: String (unique, sparse) - Username (optional)email: String (required, unique) - User's emailpassword: String (optional) - Password fieldbio: String (optional) - User biographypicture: String (required) - Profile picture URLlocation: String (optional) - User's locationportfolioWebsite: String (optional) - Portfolio URLreputation: Number (default: 0) - User reputation pointssaved: [ObjectId] - Array of saved question IDsjoinedAt: Date (default: now) - Registration timestamp
What it stores:
- User profile information
- Authentication data
- Reputation scores
- Saved questions references
Fields:
title: String (required) - Question titlecontent: String (required) - Question body/contenttags: [ObjectId] - Array of tag referencesviews: Number (default: 0) - View countupvotes: [ObjectId] - Array of user IDs who upvoteddownvotes: [ObjectId] - Array of user IDs who downvotedauthor: ObjectId (required) - Reference to User who askedanswers: [ObjectId] - Array of answer referencescreatedAt: Date (default: now) - Creation timestamp
What it stores:
- Question content and metadata
- Voting information
- View statistics
- Relationships to users, tags, and answers
Fields:
author: ObjectId (required) - Reference to User who answeredquestion: ObjectId (required) - Reference to parent Questioncontent: String (required) - Answer contentupvotes: [ObjectId] - Array of user IDs who upvoteddownvotes: [ObjectId] - Array of user IDs who downvotedcreatedAt: Date (default: now) - Creation timestamp
What it stores:
- Answer content
- Voting data
- Relationships to users and questions
Fields:
name: String (required, unique) - Tag namedescription: String (required) - Tag descriptionquestions: [ObjectId] - Array of question referencesfollowers: [ObjectId] - Array of user references who follow tagcreatedOn: Date (default: now) - Creation timestamp
What it stores:
- Tag metadata
- Question associations
- Follower relationships
Fields:
user: ObjectId (required) - Reference to Useraction: String (required) - Action type (view, ask_question, answer, etc.)question: ObjectId (optional) - Reference to Questionanswer: ObjectId (optional) - Reference to Answertags: [ObjectId] - Array of tag referencescreatedAt: Date (default: now) - Interaction timestamp
What it stores:
- User activity tracking
- Interaction history for recommendations
- Analytics data
Use Case: Fetch user by Clerk ID
Parameters: { userId: string }
Returns: User object
Database Operations: Read from User collection
Use Case: Create new user account (webhook triggered) Parameters: User registration data Database Operations:
- Create new User document
- Add 50 reputation points for new user Reputation System: +50 for account creation
Use Case: Update user profile information
Parameters: { clerkId, path, updateData }
Database Operations: Update User document
Cache: Revalidates path
Use Case: Delete user account and all associated data
Parameters: { clerkId: string }
Database Operations:
- Delete User document
- Delete all user's Questions
- Cascade delete answers, comments, etc.
Use Case: Get paginated list of users with search/filter
Parameters: { searchQuery?, filter?, page?, pageSize? }
Features:
- Search by name/username
- Filter by: new_users, old_users, top_contributors
- Pagination support
Returns:
{ users, isNext }
Use Case: Save/unsave questions to user profile
Parameters: { questionId, userId, path }
Reputation System:
- +5/-5 to question author
- +5/-5 to user (if not own question) Database Operations: Update User.saved array
Use Case: Get user's saved questions with pagination
Parameters: { clerkId, searchQuery?, filter?, page?, pageSize? }
Features:
- Search within saved questions
- Filter by: most_recent, oldest, most_voted, most_viewed, most_answered
- Pagination support
Use Case: Get comprehensive user statistics for profile Returns:
- Total questions count
- Total answers count
- Question upvotes aggregate
- Answer upvotes aggregate
- Question views aggregate
- Badge calculations
- User reputation
Use Case: Get user's questions with pagination
Parameters: { userId, page?, pageSize? }
Features: Sorted by creation date, views, upvotes
Use Case: Get user's answers with pagination
Parameters: { userId, page?, pageSize? }
Features: Sorted by creation date, upvotes
Use Case: Get tags user interacts with most (currently mock data) Status: Placeholder implementation Future: Will use Interaction model for real data
Use Case: Get paginated list of all tags
Parameters: { searchQuery?, filter?, page?, pageSize? }
Features:
- Search by tag name
- Filter by: popular, recent, old, name
- Pagination support
Use Case: Get specific tag by ID
Parameters: { tagId: string }
Use Case: Get questions associated with specific tag
Parameters: { tagId, searchQuery?, page?, pageSize? }
Features:
- Search within tag's questions
- Pagination support
- Returns tag name and questions
Use Case: Get top 5 most popular tags for sidebar Database Operations: Aggregation pipeline to count questions per tag Returns: Tags sorted by question count
Use Case: Create new question
Parameters: { title, content, tags, author, path }
Database Operations:
- Create Question document
- Create/update Tag documents
- Create Interaction record Reputation System: +10 to author Features:
- Automatic tag creation/association
- Interaction tracking
Use Case: Get paginated list of questions for home page
Parameters: { searchQuery?, filter?, page?, pageSize? }
Features:
- Search by title/content
- Filter by: newest, frequent, unanswered
- Pagination support
- Populates author and tags
Use Case: Get specific question for detail page
Parameters: { questionId: string }
Returns: Populated question with author and tags
Use Case: Upvote/remove upvote from question
Parameters: { userId, questionId, hasupVoted, hasdownVoted, path }
Reputation System:
- Voter: +1/-1 reputation
- Author: +10/-10 reputation (if not self-vote) Features: Handles vote switching
Use Case: Downvote/remove downvote from question
Parameters: { userId, questionId, hasdownVoted, hasupVoted, path }
Reputation System:
- Voter: +2/-2 reputation
- Author: +10/-10 reputation (minimum 0) Features: Prevents negative reputation
Use Case: Delete question and cascade delete related data
Parameters: { questionId, path }
Database Operations:
- Delete Question document
- Delete associated Answers
- Delete Interactions
- Update Tag question arrays Reputation System: -10 to author
Use Case: Edit existing question
Parameters: { content, path, title, questionId }
Features: Updates title and content only
Use Case: Get trending questions for sidebar Sorting: By views and upvotes (descending) Limit: 5 questions
Use Case: Get personalized question recommendations
Parameters: { userId, page?, pageSize?, searchQuery? }
Algorithm:
- Based on user's interaction tags
- Excludes user's own questions
- Search capability
- Pagination support
Use Case: Create new answer to question
Parameters: { content, question, author, path }
Database Operations:
- Create Answer document
- Update Question.answers array
- Create Interaction record Reputation System: +10 to author (if not self-answer)
Use Case: Get paginated answers for question
Parameters: { questionId, sortBy?, page?, pageSize? }
Features:
- Sort by: highestUpvotes, lowestUpvotes, recent, old
- Pagination support
- Populates author information
Use Case: Upvote/remove upvote from answer
Parameters: { answerId, hasdownVoted, hasupVoted, path, userId }
Reputation System:
- Voter: +2/-2 reputation
- Author: +10/-10 reputation (if not self-vote)
Use Case: Downvote/remove downvote from answer
Parameters: { answerId, hasdownVoted, hasupVoted, path, userId }
Reputation System:
- Voter: +2/-2 reputation
- Author: +10/-10 reputation (minimum 0)
Use Case: Delete answer and related data
Parameters: { answerId, path }
Database Operations:
- Delete Answer document
- Update Question.answers array
- Delete Interactions
Use Case: Global search across all content types
Parameters: { query, type? }
Features:
- Search across: questions, users, answers, tags
- Type-specific search or global search
- Returns formatted results with titles and IDs
Use Case: Track question views and user interactions
Parameters: { questionId, userId? }
Database Operations:
- Increment Question.views
- Create Interaction record (if user logged in) Features: Prevents duplicate view tracking per user
Use Case: Get job listings from static JSON data
Parameters: { page?, pageSize?, filter?, location?, remote?, wage?, skills?, searchQuery? }
Data Source: /content/jsearch.json
Features:
- Search by job title
- Filter by location, remote work, salary, skills
- Employment type filtering: fulltime, parttime, contractor, intern
- Pagination support
Use Case: Get country options for job filtering
Data Source: /content/countries.json
Returns: Country names and codes
Use Case: Handle Clerk authentication webhooks Events Handled:
user.created: Create user in MongoDB with fallback usernameuser.updated: Update user information in MongoDBuser.deleted: Delete user and cascade delete related data Features:- Automatic username generation from email
- Error handling and validation
- Reputation initialization (+50 for new users)
Use Case: Get AI-generated answers using Groq API
Parameters: { question: string }
AI Model: llama-3.3-70b-versatile
Returns: AI-generated response
Features: Error handling for API failures
- Account Creation: +50 points
- Question Creation: +10 points
- Answer Creation: +10 points (if not self-answer)
- Question Upvote: +10 to author, +1 to voter
- Question Downvote: -10 to author, +2 to voter
- Answer Upvote: +10 to author, +2 to voter
- Answer Downvote: -10 to author, +2 to voter
- Save Question: +5 to author, +5 to saver
- Unsave Question: -5 to author, -5 to saver
- Question Deletion: -10 to author
- Self-voting doesn't affect reputation
- Reputation cannot go below 0
- Different point values for different actions
QUESTION_COUNT: Based on number of questions askedANSWER_COUNT: Based on number of answers providedQUESTION_UPVOTES: Based on upvotes received on questionsANSWER_UPVOTES: Based on upvotes received on answersTOTAL_VIEWS: Based on total views on user's content per User Account
- Question Detail Page:
/question/[id]- Displays question with answers, voting, and answer form - User Profile Pages: Individual user statistics and content
- Tag Pages: Questions filtered by specific tags
- Search Results: Global search across all content
- Job Listings: External job integration
- Voting System: Upvote/downvote with reputation tracking
- Answer Form: Rich text editor for creating answers
- Metrics Display: Views, answers, creation time
- Tag Rendering: Clickable tags with styling
- HTML Parsing: Safe rendering of user-generated content
- User submits question form
createQuestionaction processes data- Question document created
- Tags created/updated with question reference
- Interaction record created
- User reputation increased (+10)
- Page revalidated
- User clicks vote button
- Vote action processes current state
- Database updated with new vote state
- Reputation adjustments applied
- Page revalidated with new data
- Clerk webhook triggered on user creation
- Webhook processes user data
- MongoDB user created with fallback username
- Initial reputation assigned (+50)
- User ready for platform interaction
- Tag interactions are mocked (needs real implementation)
- Job data is static (could integrate live APIs)
- Comment system not implemented
- Email notifications not implemented
- Advanced search filters limited
- Pagination implemented throughout
- Database indexing on frequently queried fields
- Aggregation pipelines for complex queries
- Caching with Next.js revalidation
- Server-side validation
- Clerk authentication integration
- Webhook signature verification
- XSS protection with HTML parsing
- Rate limiting (could be added)
- Question views
- User actions (ask, answer, vote, view)
- Tag associations
- Recommendation system data
- User reputation scores
- Question/answer counts
- Vote counts and ratios
- View statistics
- Badge progression
- User activity patterns
This comprehensive system provides a fully-featured Stack Overflow clone with user management, content creation, voting, search, AI integration, and job listings functionality.