Skip to content

About

A production-ready NestJS backend API for the Flexify portfolio platform, featuring authentication, content management, and comprehensive REST APIs.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Flexify Backend - NestJS API Server

A production-ready NestJS backend API for the Flexify portfolio platform, featuring authentication, content management, and comprehensive REST APIs.

📋 Table of Contents

🎯 Overview

This backend provides a complete REST API for managing portfolio content, user authentication, and administrative operations. Built with NestJS following best practices for scalability, maintainability, and security.

🛠️ Tech Stack

  • Framework: NestJS 11
  • Language: TypeScript
  • Database: Supabase (PostgreSQL)
  • Authentication: JWT (Passport.js)
  • Email: Nodemailer with Handlebars templates
  • File Upload: Multer
  • Validation: class-validator, class-transformer
  • Security: Helmet, CORS, Rate Limiting (Throttler)
  • Internationalization: Custom i18n implementation

📁 Project Structure

server/
├── src/
│   ├── main.ts                    # Application entry point
│   │                              # - Configures middleware (Helmet, CORS, cookie-parser)
│   │                              # - Sets up global pipes, filters, interceptors
│   │                              # - Starts the NestJS application
│   │
│   ├── app.module.ts              # Root module
│   │                              # - Imports all feature modules
│   │                              # - Configures global modules (Config, Throttler)
│   │
│   ├── config/                    # Configuration
│   │   ├── env.validation.ts     # Environment variable validation schema
│   │   └── mailer.config.ts      # Email service configuration
│   │
│   ├── constants/                 # Application constants
│   │   └── auth.constants.ts     # Authentication-related constants
│   │
│   ├── core/                      # Core utilities and services
│   │   ├── filters/              # Exception filters
│   │   │   └── http-exception.filter.ts
│   │   │                          # - Global exception handler
│   │   │                          # - Formats error responses
│   │   │                          # - Handles i18n error messages
│   │   │
│   │   ├── interceptors/         # Response interceptors
│   │   │   └── transform.interceptor.ts
│   │   │                          # - Transforms responses to consistent format
│   │   │                          # - Wraps data in IApiResponse structure
│   │   │
│   │   ├── lib/                  # Core libraries
│   │   │   └── supabase/         # Supabase integration
│   │   │       └── supabase.service.ts
│   │   │                          # - Database client initialization
│   │   │                          # - Storage operations
│   │   │
│   │   ├── services/             # Base services
│   │   │   └── base.service.ts   # Base service with common CRUD operations
│   │   │
│   │   └── utils/                # Utility functions
│   │       ├── request.util.ts   # Request helper utilities
│   │       ├── response.ts       # Response formatting utilities
│   │       └── translations.ts  # i18n translation utilities
│   │
│   ├── i18n/                      # Internationalization
│   │   ├── en.json               # English translations
│   │   └── ar.json               # Arabic translations
│   │
│   └── modules/                   # Feature modules (NestJS modules)
│       ├── auth/                  # Authentication module
│       │   ├── auth.controller.ts # Auth endpoints (login, register, etc.)
│       │   ├── auth.service.ts    # Authentication business logic
│       │   ├── auth.module.ts     # Module definition
│       │   ├── dto/               # Data Transfer Objects
│       │   │   ├── login.dto.ts
│       │   │   ├── register.dto.ts
│       │   │   ├── refresh-token.dto.ts
│       │   │   ├── verify-email.dto.ts
│       │   │   ├── forgot-password.dto.ts
│       │   │   ├── reset-password.dto.ts
│       │   │   ├── change-password.dto.ts
│       │   │   └── resend-verification.dto.ts
│       │   ├── guards/            # Auth guards
│       │   │   └── jwt-auth.guard.ts
│       │   │                          # - Protects routes with JWT validation
│       │   ├── strategies/        # Passport strategies
│       │   │   └── jwt.strategy.ts
│       │   │                          # - JWT token validation strategy
│       │   └── types/             # TypeScript types
│       │       └── auth.types.ts
│       │
│       ├── users/                 # User management module
│       │   ├── users.controller.ts
│       │   ├── users.service.ts
│       │   ├── users.module.ts
│       │   ├── dto/
│       │   │   ├── create-user.dto.ts
│       │   │   ├── update-user.dto.ts
│       │   │   └── query-user.dto.ts
│       │   ├── enums/
│       │   │   └── user-role.enum.ts
│       │   └── types/
│       │       └── user.types.ts
│       │
│       ├── projects/              # Projects management module
│       │   ├── projects.controller.ts
│       │   ├── projects.service.ts
│       │   ├── projects.module.ts
│       │   ├── dto/
│       │   │   ├── create-project.dto.ts
│       │   │   ├── update-project.dto.ts
│       │   │   ├── query-project.dto.ts
│       │   │   ├── upload-image.dto.ts
│       │   │   ├── create-comment.dto.ts
│       │   │   ├── query-comment.dto.ts
│       │   │   └── create-interaction.dto.ts
│       │   ├── enums/
│       │   │   ├── project-status.enum.ts
│       │   │   ├── project-type.enum.ts
│       │   │   ├── link-type.enum.ts
│       │   │   └── interaction-type.enum.ts
│       │   └── types/
│       │       └── project.types.ts
│       │
│       ├── services/              # Services management module
│       │   ├── services.controller.ts
│       │   ├── services.service.ts
│       │   ├── services.module.ts
│       │   ├── dto/
│       │   │   ├── create-service.dto.ts
│       │   │   ├── update-service.dto.ts
│       │   │   └── query-service.dto.ts
│       │   └── types/
│       │       └── service.types.ts
│       │
│       ├── categories/            # Categories management module
│       │   ├── categories.controller.ts
│       │   ├── categories.service.ts
│       │   ├── categories.module.ts
│       │   └── dto/
│       │       ├── create-category.dto.ts
│       │       └── update-category.dto.ts
│       │
│       ├── technologies/          # Technologies management module
│       │   ├── technologies.controller.ts
│       │   ├── technologies.service.ts
│       │   ├── technologies.module.ts
│       │   └── dto/
│       │       ├── create-technology.dto.ts
│       │       └── update-technology.dto.ts
│       │
│       ├── testimonials/          # Testimonials management module
│       │   ├── testimonials.controller.ts
│       │   ├── testimonials.service.ts
│       │   ├── testimonials.module.ts
│       │   ├── dto/
│       │   │   ├── create-testimonial.dto.ts
│       │   │   ├── update-testimonial.dto.ts
│       │   │   └── query-testimonial.dto.ts
│       │   └── types/
│       │       └── testimonial.types.ts
│       │
│       ├── contacts/              # Contact inquiries module
│       │   ├── contacts.controller.ts
│       │   ├── contacts.service.ts
│       │   ├── contacts.module.ts
│       │   ├── dto/
│       │   │   ├── create-contact.dto.ts
│       │   │   ├── update-contact.dto.ts
│       │   │   ├── query-contact.dto.ts
│       │   │   └── reply-contact.dto.ts
│       │   ├── enums/
│       │   │   └── contact-status.enum.ts
│       │   └── types/
│       │       └── contact.types.ts
│       │
│       ├── inquiry-types/         # Inquiry type management module
│       │   ├── inquiry-types.controller.ts
│       │   ├── inquiry-types.service.ts
│       │   ├── inquiry-types.module.ts
│       │   ├── dto/
│       │   │   ├── create-inquiry-type.dto.ts
│       │   │   ├── update-inquiry-type.dto.ts
│       │   │   └── query-inquiry-type.dto.ts
│       │   └── types/
│       │       └── inquiry-type.types.ts
│       │
│       ├── site-settings/         # Site configuration module
│       │   ├── site-settings.controller.ts
│       │   ├── site-settings.service.ts
│       │   ├── site-settings.module.ts
│       │   ├── dto/
│       │   │   └── update-site-setting.dto.ts
│       │   └── types/
│       │       └── site-settings.types.ts
│       │
│       └── mailer/                # Email service module
│           ├── mailer.module.ts
│           ├── mailer.service.ts
│           └── templates/         # Handlebars email templates
│               ├── welcome.hbs
│               ├── verify-account.hbs
│               ├── password-reset.hbs
│               ├── password-reset-confirmation.hbs
│               └── contact-reply.hbs
│
├── supabase/
│   └── migrations/                # Database migrations
│       ├── 001_create_users_table.sql
│       ├── 002_enable_rls_policies.sql
│       ├── 003_create_avatars_storage.sql
│       ├── 004_create_projects_tables.sql
│       ├── 005_create_inquiry_types_contacts_testimonials_services.sql
│       ├── 006_update_categories_table.sql
│       ├── 007_update_technologies_table.sql
│       ├── 008_remove_color_from_services_inquiry_types.sql
│       ├── 009_create_site_settings.sql
│       ├── 010_add_cv_storage.sql
│       └── 011_verify_site_settings.sql
│
├── test/                          # E2E tests
│   ├── app.e2e-spec.ts
│   └── jest-e2e.json
│
├── scripts/                       # Utility scripts
│   └── generate-hash.js          # Password hash generator
│
├── dist/                          # Compiled JavaScript (generated)
├── nest-cli.json                  # NestJS CLI configuration
├── tsconfig.json                  # TypeScript configuration
├── tsconfig.build.json            # Build TypeScript configuration
└── package.json

🏗️ Architecture

Module Structure

Each feature module follows NestJS best practices:

module-name/
├── module-name.controller.ts      # REST API endpoints
│                                  # - Defines routes and HTTP methods
│                                  # - Uses DTOs for request validation
│                                  # - Returns responses via service
│
├── module-name.service.ts         # Business logic
│                                  # - Contains all business rules
│                                  # - Interacts with database via Supabase
│                                  # - Handles data transformation
│
├── module-name.module.ts          # Module definition
│                                  # - Declares controllers and providers
│                                  # - Imports required modules
│                                  # - Exports services for other modules
│
├── dto/                           # Data Transfer Objects
│   ├── create-module-name.dto.ts  # Validation for create operations
│   ├── update-module-name.dto.ts  # Validation for update operations
│   └── query-module-name.dto.ts   # Validation for query parameters
│
├── types/                         # TypeScript type definitions
│   └── module-name.types.ts       # Shared types for the module
│
└── enums/                         # Enumerations (if needed)
    └── module-name-status.enum.ts

Request Flow

  1. Request → Controller receives HTTP request
  2. Validation → DTO validation via class-validator
  3. Guard → JWT authentication guard (if protected route)
  4. Service → Business logic execution
  5. Database → Supabase query execution
  6. Interceptor → Response transformation
  7. Response → Formatted JSON response

Response Format

All API responses follow a consistent structure:

interface IApiResponse<T> {
  success: boolean;
  message: string;
  data: T;
  errors?: string[];
}

🚀 Getting Started

Prerequisites

  • Node.js 18+ and npm
  • Supabase account and project
  • Email service credentials (SMTP)

Installation

  1. Install dependencies

    npm install
  2. Set up environment variables

    Create a .env file in the root directory:

    # Database
    SUPABASE_URL=your_supabase_url
    SUPABASE_KEY=your_supabase_key
    SUPABASE_SERVICE_KEY=your_supabase_service_key
    
    # JWT
    JWT_SECRET=your_jwt_secret
    JWT_EXPIRES_IN=15m
    JWT_REFRESH_SECRET=your_refresh_secret
    JWT_REFRESH_EXPIRES_IN=7d
    
    # Server
    PORT=3000
    NODE_ENV=development
    CORS_ORIGINS=http://localhost:3001
    
    # Email
    MAILER_HOST=smtp.example.com
    MAILER_PORT=587
    MAILER_USER=your_email@example.com
    MAILER_PASS=your_password
    MAILER_FROM=noreply@example.com
    
    # Rate Limiting
    THROTTLE_TTL=60000
    THROTTLE_LIMIT=10
  3. Run database migrations

    Apply migrations from supabase/migrations/ to your Supabase project.

  4. Start development server

    npm run start:dev

    The API will be available at http://localhost:3000/api

📚 Modules

Auth Module

Handles user authentication and authorization:

  • User registration with email verification
  • Login with JWT token generation
  • Password reset flow
  • Token refresh mechanism
  • Email verification

Endpoints:

  • POST /api/auth/register - Register new user
  • POST /api/auth/login - User login
  • POST /api/auth/refresh - Refresh access token
  • POST /api/auth/verify-email - Verify email address
  • POST /api/auth/forgot-password - Request password reset
  • POST /api/auth/reset-password - Reset password
  • POST /api/auth/change-password - Change password (authenticated)
  • POST /api/auth/resend-verification - Resend verification email

Users Module

Manages user accounts and profiles:

  • CRUD operations for users
  • Role-based access control
  • User profile management

Endpoints:

  • GET /api/users - List users (with pagination, filtering)
  • GET /api/users/:id - Get user by ID
  • POST /api/users - Create user (admin only)
  • PATCH /api/users/:id - Update user
  • DELETE /api/users/:id - Delete user

Projects Module

Manages portfolio projects:

  • Create, read, update, delete projects
  • Image gallery management
  • Project links (GitHub, Live Demo, etc.)
  • Comments and interactions
  • Project status and type management

Endpoints:

  • GET /api/projects - List projects
  • GET /api/projects/:id - Get project details
  • POST /api/projects - Create project
  • PATCH /api/projects/:id - Update project
  • DELETE /api/projects/:id - Delete project
  • POST /api/projects/:id/images - Upload project images
  • POST /api/projects/:id/comments - Add comment
  • POST /api/projects/:id/interactions - Add interaction (like/view)

Services Module

Manages portfolio services:

  • Service CRUD operations
  • Service icons and descriptions
  • Multi-language support

Endpoints:

  • GET /api/services - List services
  • GET /api/services/:id - Get service details
  • POST /api/services - Create service
  • PATCH /api/services/:id - Update service
  • DELETE /api/services/:id - Delete service

Categories Module

Manages project categories:

  • Category CRUD operations
  • Category organization

Endpoints:

  • GET /api/categories - List categories
  • GET /api/categories/:id - Get category
  • POST /api/categories - Create category
  • PATCH /api/categories/:id - Update category
  • DELETE /api/categories/:id - Delete category

Technologies Module

Manages technology stack:

  • Technology CRUD operations
  • Technology icons and information

Endpoints:

  • GET /api/technologies - List technologies
  • GET /api/technologies/:id - Get technology
  • POST /api/technologies - Create technology
  • PATCH /api/technologies/:id - Update technology
  • DELETE /api/technologies/:id - Delete technology

Testimonials Module

Manages client testimonials:

  • Testimonial CRUD operations
  • Avatar management
  • Testimonial approval workflow

Endpoints:

  • GET /api/testimonials - List testimonials
  • GET /api/testimonials/:id - Get testimonial
  • POST /api/testimonials - Create testimonial
  • PATCH /api/testimonials/:id - Update testimonial
  • DELETE /api/testimonials/:id - Delete testimonial

Contacts Module

Manages contact inquiries:

  • Contact form submissions
  • Inquiry status tracking
  • Reply functionality
  • Email notifications

Endpoints:

  • GET /api/contacts - List contacts (with filtering)
  • GET /api/contacts/:id - Get contact details
  • POST /api/contacts - Create contact inquiry
  • PATCH /api/contacts/:id - Update contact status
  • POST /api/contacts/:id/reply - Reply to contact

Inquiry Types Module

Manages inquiry type categories:

  • Inquiry type CRUD operations
  • Used for categorizing contact inquiries

Endpoints:

  • GET /api/inquiry-types - List inquiry types
  • GET /api/inquiry-types/:id - Get inquiry type
  • POST /api/inquiry-types - Create inquiry type
  • PATCH /api/inquiry-types/:id - Update inquiry type
  • DELETE /api/inquiry-types/:id - Delete inquiry type

Site Settings Module

Manages site-wide configuration:

  • Hero section settings
  • About section content
  • Statistics configuration
  • Footer settings
  • Social media links
  • CV file management

Endpoints:

  • GET /api/site-settings - Get all settings
  • GET /api/site-settings/:key - Get specific setting
  • PATCH /api/site-settings/:key - Update setting

🛠️ Development

Available Scripts

npm run start:dev      # Start development server with hot reload
npm run build           # Build for production
npm run start:prod     # Start production server
npm run test           # Run unit tests
npm run test:e2e       # Run E2E tests
npm run lint           # Lint code
npm run format         # Format code with Prettier

Code Standards

  • Validation: Use class-validator decorators in DTOs
  • Types: Define types in types/ directory
  • Error Handling: Use custom exceptions with i18n messages
  • Response Format: Always use the TransformInterceptor format
  • Naming: Follow NestJS conventions (kebab-case for files)

Adding a New Module

  1. Generate module structure:

    nest g module modules/your-module
    nest g controller modules/your-module
    nest g service modules/your-module
  2. Create DTOs in dto/ directory

  3. Define types in types/ directory

  4. Implement business logic in service

  5. Add routes in controller

  6. Import module in app.module.ts

🔒 Security Features

  • JWT Authentication: Secure token-based authentication
  • Password Hashing: bcryptjs for password security
  • Rate Limiting: Throttler protection against abuse
  • CORS: Configurable cross-origin resource sharing
  • Helmet: HTTP security headers
  • Input Validation: Comprehensive DTO validation
  • SQL Injection Protection: Parameterized queries via Supabase

📝 License

This project is private and proprietary.


Built with ❤️ using NestJS

About

A production-ready NestJS backend API for the Flexify portfolio platform, featuring authentication, content management, and comprehensive REST APIs.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages