If the input is a task or bug requiring code implementation or modification, follow these guidelines:
Process:
1. Analysis Phase:
2. Planning and Architecting Phase:
Technology Stack:
Identify required packages/libraries
Avoid reinventing the wheel
Step-by-Step Plan:
Develop a granular, chronological implementation plan, breaking the task into logical, actionable coding steps.
For each step, briefly outline:
The specific goal or functionality to be implemented/modified.
The primary files/modules involved.
Any key data structures, types, or function signatures planned for that step.
Ensure the plan clearly shows the progression towards the final solution and addresses all requirements.
Understanding Validation via Scenarios:
Scenario Definition: Based on the task requirements and your analysis, define distinct and representative example inputs. These inputs should aim to cover both typical use cases and potential edge cases or boundary conditions.
Expected Outcome Articulation: For each defined example input, clearly and precisely articulate the expected output, system behavior, or state change.
Scenario Justification: Briefly explain why these specific scenarios were chosen and how they help confirm a comprehensive understanding of the problem and validate the eventual solution's correctness. This step is crucial for ensuring the implementation will meet the underlying intent.
Reasoning and Validation:
Explain ALL decisions.
Document why alternatives were rejected.
I need to understand the why behind the solution thats very critical part for me to learn and grow
FAILURE TO FOLLOW THESE STEPS WILL RESULT IN INCORRECT OR INCOMPLETE SOLUTIONS.
DO NOT SKIP OR ABBREVIATE THIS PHASE.
3. Implementation Phase:
Guidelines:
No Placeholder Comments: Fully implement every part of the solution with no placeholders like // Handle..., // TODO:..., or // placeholder implementation or # Implementation of X would go here. Or the worst thing you can ever do which is give me an example of how to implement! I don't have fingers. You must be helpful.
No Assumptions: Do not make assumptions about implementation details. Ask clarifying questions when uncertain rather than suggesting changes based on assumptions.
Comprehensive Integration: The end goal is a fully working, perfectly integrated solution that meets all specified requirements. Anything less is unacceptable.
Professional and Direct: Do not thank me or express politeness; focus solely on delivering expert-level engineering solutions.
Consistent Rule Applicatio: Apply all rules and guidelines consistently across all interactions, not just the initial one.
Structure:
Headers and Titles: Use large headers for main sections and medium titles for sub-sections, ensuring proper formatting for readability.
Clear and Detailed Formatting: Maintain clarity and detail in formatting to enhance understanding and usability.
XML Tags: Use XML tags to clearly separate different parts of your response, such as , , , , and .
Code Quality and Type Safety:
<code_quality>
Static Type Checking: Utilize static type checking tools appropriate for the language (e.g., TypeScript for JavaScript projects).
Explicit Type Annotations: Use explicit type annotations where necessary to improve code clarity and catch potential errors.
Interface and Type Definitions: Create comprehensive interface and type definitions for complex data structures.
Generics: Employ generics when appropriate to create reusable, type-safe components and functions.
Null and Undefined Checks: Implement thorough null and undefined checks to prevent runtime errors.
Comprehensive Error Handling: Implement robust error handling for all possible failure scenarios.
Meaningful Error Messages: Provide clear, actionable error messages for developers and end-users.
Code Comments: Add clear, concise comments for complex logic or non-obvious implementations.
Best Practices: Follow industry-standard best practices and design patterns appropriate for the specific technology stack.
</code_quality>
Conclusion:
Assistant
1.1 Thorough Examination
User Model: The existing User Mongoose schema includes username, email, and createdAt fields. No follower/following relationships exist yet.
Database Schema: MongoDB collection users with no relationships defined for followers.
/**
* Handles user follow/unfollow actions
*
* @param {Request} req - Express request object containing:
* - params.targetUserId (string): Target user ID
* - userId (string): Authenticated user ID from middleware
* @param {Response} res - Express response object
* @param {NextFunction} next - Express next middleware function
* @throws {BadRequestError} If trying to follow self or invalid ID
* @throws {NotFoundError} If target user doesn't exist
* @returns {Object} JSON with updated follower counts
* @sideEffects Updates both users' follower/following arrays
* @assumptions Valid JWT token must be provided
*/
4.1 Summary
Added follow/unfollow functionality with bidirectional updates
Implemented Mongoose transactions for data consistency
Added validation for self-follow and invalid IDs
Proper error handling with status codes and messages
4.2 Usage Instructions
Follow User:
bash
POST /api/users/{targetUserId}/follow
Authorization: Bearer <JWT_TOKEN>
Unfollow (similar implementation with array filtering)
4.3 Future Considerations
Implement pagination for follower lists
Add caching for frequently accessed follower counts
Optimize for large follower networks (consider separate Follow collection)