# eyad-abdelmohsen/social-media architecture

> Architecture diagram of the GitHub repository eyad-abdelmohsen/social-media, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-05). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/eyad-abdelmohsen/social-media
- Repository: https://github.com/eyad-abdelmohsen/social-media

## Overview

##### Project type and purpose
- This is a monolithic RESTful backend API for a social-media platform, implemented in Node.js/Express with TypeScript.
- It provides user management (signup/login), post/comment/like operations, and friend-request workflows, persisting data in MongoDB and storing uploads on the file system.

##### File structure
- Top level
- package.json, tsconfig.json, src/, dist/, uploads/
- src/ (and dist/ mirror)
- controllers/   – Express route handlers (friend.controller, post.controller, user.controller)
- routes/        – Express routers mapping HTTP verbs & paths to controllers
- services/      – Business-logic layer (user.service, post.service, friends.service, etc.)
- models/        – Mongoose schemas/models (User, Post, Comment)
- middlewares/   – Authentication, authorization, validation, pagination, error-handling
- utils/         – JWT generation, custom request typings, role definitions, Multer setup
- errors/        – ApiError class for centralized error constructs
- validators/    – Input-validation logic (e.g., user.validator)
- index.ts       – Express app bootstrap and middleware wiring

##### Key architectural patterns & design principles
- Layered (MVC + Service layer)
- Single responsibility per module (controllers orchestrate, services contain logic, models interact with DB)
- Centralized error handling & request validation via middleware
- Token-based auth (JWT)
- File-upload handling (Multer)

##### Main components
a. Client (Web/Mobile)
b. Load balancer/API Gateway (optional external Nginx or AWS ALB)
c. Express API server
- Middlewares
– Authentication (verifyToken)
– Authorization (allowedTo, isOwner, isIFrend)
– Validation & pagination
– Error & “not found” handlers
- Routing layer (UserRoute, PostRoute, FriendRoute)
- Controller layer (handles HTTP request/response)
- Service layer (business logic, coordinating models & utilities)
- Data access layer (Mongoose Models interacting with MongoDB)
- Utility modules (JWT, Multer, custom request enhancements)
d. MongoDB cluster
e. File storage (local uploads folder or S3 if extended)
f. External services (e.g., email/SMS, if added later)

##### Relationships and data flows
- Error flows: Controller/Service → throw ApiError → Error Middleware → Client

## Diagram (Mermaid)

```mermaid
flowchart TD
    Client["Client<br/>(Web/Mobile)"]:::external
    LB["Load Balancer/API Gateway"]:::external
    subgraph "API Server"
        direction TB
        ExpressApp["Express App<br/>(src/index.ts)"]:::entry

        subgraph "Middleware Layer"
            direction TB
            verifyToken["verifyToken"]:::middleware
            allowedTo["allowedTo"]:::middleware
            isOwner["isOwner"]:::middleware
            isIFrend["isIFrend"]:::middleware
            validation["validation.middleware"]:::middleware
            pagination["pagination.middleware"]:::middleware
            asyncWrap["asyncWrapper.middleware"]:::middleware
            getMe["getMe"]:::middleware
            isValid["isValid"]:::middleware
            errorHandle["error.middleware"]:::middleware
            notFound["notFound.middleware"]:::middleware
        end

        subgraph "Routing Layer"
            direction TB
            UserRoutes["UserRoutes"]:::routing
            PostRoutes["PostRoutes"]:::routing
            FriendRoutes["FriendRoutes"]:::routing
        end

        subgraph "Controller Layer"
            direction TB
            UserController["UserController"]:::controller
            PostController["PostController"]:::controller
            FriendController["FriendController"]:::controller
        end

        subgraph "Service Layer"
            direction TB
            UserService["UserService"]:::service
            PostService["PostService"]:::service
            FriendsService["FriendsService"]:::service
            CommentService["CommentService"]:::service
            ReplyService["ReplyService"]:::service
            ShareService["ShareService"]:::service
            LikeService["LikeService"]:::service
        end

        subgraph "Data Models"
            direction TB
            UserModel["UserModel"]:::model
            PostModel["PostModel"]:::model
            CommentModel["CommentModel"]:::model
        end

        subgraph "Utility Modules"
            direction TB
            generateJWT["generateJWT"]:::util
            multerSetup["multerSetup"]:::util
            customRequest["customRequest"]:::util
            userRole["userRole"]:::util
            httpStatusText["httpStatusText"]:::util
            friendsUtil["friendsUtil"]:::util
        end
    end

    subgraph "Infrastructure"
        direction TB
        MongoDB[(MongoDB)]:::db
        Uploads["uploads/ directory"]:::storage
    end

    Client -->|HTTPS| LB
    LB --> ExpressApp
    ExpressApp --> verifyToken
    verifyToken --> validation
    validation --> pagination
    pagination --> UserRoutes
    pagination --> PostRoutes
    pagination --> FriendRoutes
    UserRoutes --> UserController
    PostRoutes --> PostController
    FriendRoutes --> FriendController
    UserController --> UserService
    PostController --> PostService
    FriendController --> FriendsService
    UserService --> UserModel
    PostService --> PostModel
    FriendsService --> UserModel
    FriendsService --> PostModel
    CommentService --> CommentModel
    ReplyService --> CommentModel
    ShareService --> PostModel
    LikeService --> PostModel
    UserService --> generateJWT
    PostService --> multerSetup
    *CommentService --> customRequest
    *FriendsService --> friendsUtil
    *UserService --> userRole
    UserModel --> MongoDB
    PostModel --> MongoDB
    CommentModel --> MongoDB
    PostService --> Uploads

    %% Click Events
    click ExpressApp "https://github.com/eyad-abdelmohsen/social-media/blob/main/src/index.ts"
    click UserRoutes "https://github.com/eyad-abdelmohsen/social-media/blob/main/src/routes/user.route.ts"
    click PostRoutes "https://github.com/eyad-abdelmohsen/social-media/blob/main/src/routes/post.route.ts"
    click FriendRoutes "https://github.com/eyad-abdelmohsen/social-media/blob/main/src/routes/friend.route.ts"
    click UserController "https://github.com/eyad-abdelmohsen/social-media/blob/main/src/controllers/user.controller.ts"
    click PostController "https://github.com/eyad-abdelmohsen/social-media/blob/main/src/controllers/post.controller.ts"
    click FriendController "https://github.com/eyad-abdelmohsen/social-media/blob/main/src/controllers/friend.controller.ts"
    click UserService "https://github.com/eyad-abdelmohsen/social-media/blob/main/src/services/user.service.ts"
    click PostService "https://github.com/eyad-abdelmohsen/social-media/blob/main/src/services/post.service.ts"
    click FriendsService "https://github.com/eyad-abdelmohsen/social-media/blob/main/src/services/friends.service.ts"
    click CommentService "https://github.com/eyad-abdelmohsen/social-media/blob/main/src/services/comment.service.ts"
    click ReplyService "https://github.com/eyad-abdelmohsen/social-media/blob/main/src/services/reply.service.ts"
    click ShareService "https://github.com/eyad-abdelmohsen/social-media/blob/main/src/services/share.service.ts"
    click LikeService "https://github.com/eyad-abdelmohsen/social-media/blob/main/src/services/like.service.ts"
    click UserModel "https://github.com/eyad-abdelmohsen/social-media/blob/main/src/models/user.model.ts"
    click PostModel "https://github.com/eyad-abdelmohsen/social-media/blob/main/src/models/post.model.ts"
    click CommentModel "https://github.com/eyad-abdelmohsen/social-media/blob/main/src/models/comment.model.ts"
    click verifyToken "https://github.com/eyad-abdelmohsen/social-media/blob/main/src/middlewares/verifyToken.ts"
    click allowedTo "https://github.com/eyad-abdelmohsen/social-media/blob/main/src/middlewares/allowedTo.ts"
    click isOwner "https://github.com/eyad-abdelmohsen/social-media/blob/main/src/middlewares/isOwner.ts"
    click isIFrend "https://github.com/eyad-abdelmohsen/social-media/blob/main/src/middlewares/isIFrend.ts"
    click validation "https://github.com/eyad-abdelmohsen/social-media/blob/main/src/middlewares/validation.middleware.ts"
    click pagination "https://github.com/eyad-abdelmohsen/social-media/blob/main/src/middlewares/pagination.middleware.ts"
    click errorHandle "https://github.com/eyad-abdelmohsen/social-media/blob/main/src/middlewares/error.middleware.ts"
    click notFound "https://github.com/eyad-abdelmohsen/social-media/blob/main/src/middlewares/notFound.middleware.ts"
    click asyncWrap "https://github.com/eyad-abdelmohsen/social-media/blob/main/src/middlewares/asyncWrapper.middleware.ts"
    click getMe "https://github.com/eyad-abdelmohsen/social-media/blob/main/src/middlewares/getMe.ts"
    click isValid "https://github.com/eyad-abdelmohsen/social-media/blob/main/src/middlewares/isValid.ts"
    click generateJWT "https://github.com/eyad-abdelmohsen/social-media/blob/main/src/utils/generateJWT.ts"
    click multerSetup "https://github.com/eyad-abdelmohsen/social-media/blob/main/src/utils/multer.ts"
    click customRequest "https://github.com/eyad-abdelmohsen/social-media/blob/main/src/utils/customRequest.ts"
    click userRole "https://github.com/eyad-abdelmohsen/social-media/blob/main/src/utils/userRole.ts"
    click httpStatusText "https://github.com/eyad-abdelmohsen/social-media/blob/main/src/utils/httpStatusText.ts"
    click friendsUtil "https://github.com/eyad-abdelmohsen/social-media/blob/main/src/utils/friends.ts"

    classDef external fill:#CCCCCC,stroke:#333,stroke-width:1px
    classDef entry fill:#E0F7FA,stroke:#006064,stroke-width:2px
    classDef middleware fill:#BBDEFB,stroke:#1E88E5,stroke-width:1px
    classDef routing fill:#90CAF9,stroke:#1565C0,stroke-width:1px
    classDef controller fill:#C8E6C9,stroke:#2E7D32,stroke-width:1px
    classDef service fill:#A5D6A7,stroke:#388E3C,stroke-width:1px
    classDef model fill:#FFCC80,stroke:#EF6C00,stroke-width:1px
    classDef util fill:#FFE082,stroke:#FF8F00,stroke-width:1px
    classDef db fill:#D7CCC8,stroke:#5D4037,stroke-width:1px
    classDef storage fill:#FFE0B2,stroke:#FF6F00,stroke-width:1px
```
