# pauljackways/petitionsite architecture

> Architecture diagram of the GitHub repository pauljackways/petitionsite, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/pauljackways/petitionsite
- Repository: https://github.com/pauljackways/petitionsite

## Overview

##### Main Components

a) Client Layer:
- External clients (Postman, Web Browsers) making HTTP requests
- Note about CORS middleware handling

b) API Gateway Layer:
- Express.js server (entry point: src/server.ts)
- Route handlers (src/app/routes)
- Authentication/Session management (src/app/services/session.ts)

c) Controller Layer:
- Separate controllers for different entities:
- Petition Controller
- User Controller
- Image Controllers (User/Petition)
- Support Tier Controller
- Supporter Controller
- Backdoor Controller (for testing)

d) Service Layer:
- Password handling (passwords.ts)
- Validation service (validation.ts)
- Session management (session.ts)

e) Model Layer:
- Database models for:
- Users
- Petitions
- Images
- Support Tiers
- Supporters

f) Storage Layer:
- MySQL Database (configured in src/config/db.ts)
- File Storage System (for images in storage/images)

##### Relationship Flows to Illustrate

- File storage flow for images
- Authentication/authorization flows
- Demonstrate how the validation service interacts with controllers

##### Specific Design Elements

Use these visual elements:
- Blue rectangles for primary services
- Green diamonds for controllers
- Red hexagons for middleware/security components

##### Important Architectural Aspects to Highlight

a) Security Layer:
- Session management
- Password handling
- Authorization checks

b) Data Management:
- MySQL database connections
- Image file handling
- Database schemas (from create_database.sql)

c) API Structure:
- RESTful endpoints (defined in api_spec.yaml)
- Status code handling (200, 201, 400, 401, 403, 500, 501)

##### Implementation Details to Note

- Configuration management (env variables, database config)
- TypeScript usage throughout the application
- Testing/development tools (Postman collections)

##### Additional Notes

- Keep the main flow clear and centered
- Related components together
- Clear separation between layers
- Notes about scalability points
- Potential extension points

Remember to emphasize:
- The RESTful nature of the API
- The separation of concerns (controllers, models, services)
- The dual storage system (DB + file storage)
- The security and validation layers

This will provide a comprehensive view of the system while maintaining clarity and usefulness for development and maintenance purposes.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Client Layer
    subgraph Clients
        C1[Web Browser]
        C2[Postman]:::test
    end

    %% API Gateway Layer
    subgraph Gateway
        Server["Express Server"]:::server
        click Server "https://github.com/pauljackways/PetitionSite/blob/main/src/server.ts"
        CORS["CORS Middleware"]:::middleware
        click CORS "https://github.com/pauljackways/PetitionSite/blob/main/src/app/middleware/cors.middleware.ts"
    end

    %% Routes Layer
    subgraph Routes
        BaseRoutes["Base Routes"]:::route
        click BaseRoutes "https://github.com/pauljackways/PetitionSite/blob/main/src/app/routes/base.routes.ts"
        PetitionRoutes["Petition Routes"]:::route
        click PetitionRoutes "https://github.com/pauljackways/PetitionSite/blob/main/src/app/routes/petition.routes.ts"
        UserRoutes["User Routes"]:::route
        click UserRoutes "https://github.com/pauljackways/PetitionSite/blob/main/src/app/routes/user.routes.ts"
        BackdoorRoutes["Backdoor Routes"]:::route
        click BackdoorRoutes "https://github.com/pauljackways/PetitionSite/blob/main/src/app/routes/backdoor.routes.ts"
    end

    %% Controllers Layer
    subgraph Controllers
        PetitionCtrl["Petition Controller"]:::controller
        click PetitionCtrl "https://github.com/pauljackways/PetitionSite/blob/main/src/app/controllers/petition.controller.ts"
        UserCtrl["User Controller"]:::controller
        click UserCtrl "https://github.com/pauljackways/PetitionSite/blob/main/src/app/controllers/user.controller.ts"
        PetitionImageCtrl["Petition Image Controller"]:::controller
        click PetitionImageCtrl "https://github.com/pauljackways/PetitionSite/blob/main/src/app/controllers/petition.image.controller.ts"
        UserImageCtrl["User Image Controller"]:::controller
        click UserImageCtrl "https://github.com/pauljackways/PetitionSite/blob/main/src/app/controllers/user.image.controller.ts"
        SupportTierCtrl["Support Tier Controller"]:::controller
        click SupportTierCtrl "https://github.com/pauljackways/PetitionSite/blob/main/src/app/controllers/petition.support_tier.controller.ts"
        SupporterCtrl["Supporter Controller"]:::controller
        click SupporterCtrl "https://github.com/pauljackways/PetitionSite/blob/main/src/app/controllers/petition.supporter.controller.ts"
        BackdoorCtrl["Backdoor Controller"]:::controller
        click BackdoorCtrl "https://github.com/pauljackways/PetitionSite/blob/main/src/app/controllers/backdoor.controller.ts"
    end

    %% Services Layer
    subgraph Services
        PasswordService["Password Service"]:::service
        click PasswordService "https://github.com/pauljackways/PetitionSite/blob/main/src/app/services/passwords.ts"
        ValidationService["Validation Service"]:::service
        click ValidationService "https://github.com/pauljackways/PetitionSite/blob/main/src/app/services/validation.ts"
        SessionService["Session Service"]:::service
        click SessionService "https://github.com/pauljackways/PetitionSite/blob/main/src/app/services/session.ts"
    end

    %% Models Layer
    subgraph Models
        UserModel["User Model"]:::model
        click UserModel "https://github.com/pauljackways/PetitionSite/blob/main/src/app/models/user.model.ts"
        PetitionModel["Petition Model"]:::model
        click PetitionModel "https://github.com/pauljackways/PetitionSite/blob/main/src/app/models/petition.model.ts"
        ImageModel["Image Model"]:::model
        click ImageModel "https://github.com/pauljackways/PetitionSite/blob/main/src/app/models/image.model.ts"
        SupportTierModel["Support Tier Model"]:::model
        click SupportTierModel "https://github.com/pauljackways/PetitionSite/blob/main/src/app/models/petition.support_tier.model.ts"
        SupporterModel["Supporter Model"]:::model
        click SupporterModel "https://github.com/pauljackways/PetitionSite/blob/main/src/app/models/petition.supporter.model.ts"
    end

    %% Storage Layer
    subgraph Storage
        DB[(MySQL Database)]:::storage
        click DB "https://github.com/pauljackways/PetitionSite/blob/main/src/config/db.ts"
        FileStorage[("File Storage")]:::storage
        click FileStorage "https://github.com/pauljackways/PetitionSite/tree/main/storage/images/"
    end

    %% Relationships
    C1 & C2 --> CORS
    CORS --> Server
    Server --> BaseRoutes & PetitionRoutes & UserRoutes & BackdoorRoutes
    
    BaseRoutes --> PetitionCtrl & UserCtrl
    PetitionRoutes --> PetitionCtrl & PetitionImageCtrl & SupportTierCtrl & SupporterCtrl
    UserRoutes --> UserCtrl & UserImageCtrl
    BackdoorRoutes --> BackdoorCtrl
    
    PetitionCtrl & UserCtrl & PetitionImageCtrl & UserImageCtrl & SupportTierCtrl & SupporterCtrl --> ValidationService
    PetitionCtrl & UserCtrl & PetitionImageCtrl & UserImageCtrl & SupportTierCtrl & SupporterCtrl --> SessionService
    UserCtrl --> PasswordService
    
    PetitionCtrl --> PetitionModel
    UserCtrl --> UserModel
    PetitionImageCtrl & UserImageCtrl --> ImageModel
    SupportTierCtrl --> SupportTierModel
    SupporterCtrl --> SupporterModel
    
    PetitionModel & UserModel & ImageModel & SupportTierModel & SupporterModel --> DB
    PetitionImageCtrl & UserImageCtrl -.-> FileStorage

    %% Styles
    classDef server fill:#1E88E5,color:white
    classDef middleware fill:#FF5252,color:white
    classDef route fill:#7CB342,color:white
    classDef controller fill:#FFA000,color:white
    classDef service fill:#6D4C41,color:white
    classDef model fill:#5E35B1,color:white
    classDef storage fill:#00ACC1,color:white
    classDef test fill:#F06292,color:white

    %% Legend
    subgraph Legend
        L1[Server]:::server
        L2[Middleware]:::middleware
        L3[Route]:::route
        L4[Controller]:::controller
        L5[Service]:::service
        L6[Model]:::model
        L7[(Storage)]:::storage
        L8[Test Client]:::test
    end
```
