Skip to content

fernandobritto/Alpha-Tower

Repository files navigation

Alpha Tower Sales System πŸ†

A robust RESTful API built with Node.js and TypeScript for managing sales proposals, products, and client relationships

TypeScript Node.js Express TypeORM PostgreSQL

πŸ“‹ Table of Contents

πŸ” Overview

Alpha Tower Sales System is a comprehensive API solution designed to streamline sales operations through efficient management of products and user accounts. Built with modern technologies and following clean architecture principles, it provides a scalable foundation for sales-oriented applications.

Key Capabilities

  • User Management: Complete CRUD operations with authentication and authorization
  • Product Catalog: Comprehensive product management with inventory tracking
  • Authentication: JWT-based security with token management
  • File Upload: Avatar and file management with AWS S3 integration ready
  • Data Validation: Robust input validation using Joi/Celebrate
  • Database Management: TypeORM with PostgreSQL and migration support

✨ Features

πŸ” Authentication & Authorization

  • JWT token-based authentication
  • Secure password hashing with bcryptjs
  • Protected routes with middleware authentication
  • User session management

πŸ‘₯ User Management

  • User registration and profile management
  • Avatar upload and management
  • User authentication and authorization
  • Complete CRUD operations

πŸ“¦ Product Management

  • Product catalog with full CRUD operations
  • Inventory tracking with quantity management
  • Product categorization and description
  • Price management with decimal precision

πŸ›‘οΈ Security & Validation

  • Input validation with Celebrate/Joi
  • CORS support for cross-origin requests
  • Error handling with custom AppError class
  • SQL injection protection through TypeORM

πŸ“ File Management

  • File upload with Multer
  • Avatar management system
  • Static file serving
  • AWS SDK integration ready

πŸ›  Technology Stack

Core Technologies

Key Dependencies

  • Authentication: JWT, bcryptjs
  • Validation: Celebrate, Joi
  • File Upload: Multer, AWS SDK
  • Development: ts-node-dev, ESLint, Prettier
  • Testing: Jest, Supertest

πŸ— Architecture

The application follows a modular architecture with clear separation of concerns:

β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ modules/           # Business logic modules
β”‚   β”‚   β”œβ”€β”€ users/         # User management
β”‚   β”‚   └── products/      # Product management
β”‚   β”œβ”€β”€ shared/            # Shared utilities and infrastructure
β”‚   β”‚   β”œβ”€β”€ http/          # Express server configuration
β”‚   β”‚   β”œβ”€β”€ middlewares/   # Custom middleware
β”‚   β”‚   └── errors/        # Error handling
β”‚   β”œβ”€β”€ config/            # Application configuration
β”‚   β”œβ”€β”€ database/          # Database setup and migrations
β”‚   └── routes/            # Route definitions

Design Patterns

  • Repository Pattern: Data access abstraction
  • Service Layer: Business logic encapsulation
  • Dependency Injection: Loose coupling between components
  • Error Handling: Centralized error management

πŸ“‹ Prerequisites

Before running this project, ensure you have the following installed:

  • Node.js (v14.0.0 or higher)
  • npm or yarn package manager
  • PostgreSQL (v12.0 or higher)
  • Git for version control

πŸš€ Installation

1. Clone the Repository

git clone https://github.com/fernandobritto/Alpha-Tower.git
cd Alpha-Tower

2. Install Dependencies

# Using yarn (recommended)
yarn install

# Or using npm
npm install

3. Database Setup

Create a PostgreSQL database for the application:

CREATE DATABASE alpha_tower;

4. Environment Configuration

Create a .env file in the root directory with the following variables:

# Server Configuration
SERVER_PORT=8080

# Database Configuration
DB_HOST=localhost
DB_PORT=5432
DB_USERNAME=your_username
DB_PASSWORD=your_password
DB_DATABASE=alpha_tower

# JWT Configuration
JWT_SECRET=your_jwt_secret_key
JWT_EXPIRES_IN=1d

# Upload Configuration
UPLOAD_PATH=./uploads

5. Run Database Migrations

# Run all pending migrations
yarn database:run

# Or using npm
npm run database:run

6. Start the Application

# Development mode with hot reload
yarn localhost

# Or using npm
npm run localhost

The server will start on http://localhost:8080 πŸš€

βš™οΈ Configuration

Database Configuration

The application uses TypeORM for database management. Create an ormconfig.json file:

{
  "type": "postgres",
  "host": "localhost",
  "port": 5432,
  "username": "your_username",
  "password": "your_password",
  "database": "alpha_tower",
  "entities": ["src/modules/**/database/entities/*.ts"],
  "migrations": ["src/database/migrations/*.ts"],
  "cli": {
    "migrationsDir": "src/database/migrations"
  }
}

Upload Configuration

Configure file upload settings in src/config/upload.ts:

export default {
  directory: uploadFolder,
  storage: multer.diskStorage({
    destination: uploadFolder,
    filename: (request, file, callback) => {
      const fileHash = crypto.randomBytes(10).toString('hex')
      const filename = `${fileHash}-${file.originalname}`
      callback(null, filename)
    },
  }),
}

πŸ“– Usage

Starting the Server

# Development mode
yarn localhost

# Production mode
yarn start

Database Operations

# Run migrations
yarn database:run

# Revert last migration
yarn database:revert

# Sync database schema
yarn database:sync

Code Quality

# Run linting
yarn lint

# Fix linting issues
yarn lint-fix

# Run tests
yarn test

# Run tests with coverage
yarn test:coverage

πŸ“š API Documentation

The API provides the following endpoints:

Authentication

  • POST /sessions - User login and token generation

Users

  • GET /users - List all users (authenticated)
  • GET /users/:id - Get user by ID (authenticated)
  • POST /users - Create new user
  • PUT /users/:id - Update user (authenticated)
  • DELETE /users/:id - Delete user (authenticated)
  • PATCH /users/avatar - Upload user avatar (authenticated)

Products

  • GET /products - List all products (authenticated)
  • GET /products/:id - Get product by ID (authenticated)
  • POST /products - Create new product (authenticated)
  • PUT /products/:id - Update product (authenticated)
  • DELETE /products/:id - Delete product (authenticated)

For detailed API documentation, see docs/API.md.

πŸ—„οΈ Database Schema

Users Table

CREATE TABLE users (
  id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
  name VARCHAR NOT NULL,
  email VARCHAR UNIQUE NOT NULL,
  password VARCHAR NOT NULL,
  avatar VARCHAR,
  created_at TIMESTAMP DEFAULT NOW(),
  updated_at TIMESTAMP DEFAULT NOW()
);

Products Table

CREATE TABLE products (
  id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
  name VARCHAR NOT NULL,
  description VARCHAR,
  price DECIMAL(10,2) NOT NULL,
  quantity INTEGER NOT NULL,
  created_at TIMESTAMP DEFAULT NOW(),
  updated_at TIMESTAMP DEFAULT NOW()
);

For complete database documentation, see docs/DATABASE.md.

πŸ§ͺ Testing

The project is configured with Jest for testing:

# Run all tests
yarn test

# Run tests in watch mode
yarn test:silent

# Generate coverage report
yarn test:coverage

πŸ“ Project Structure

Alpha-Tower/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ @types/                 # Type definitions
β”‚   β”œβ”€β”€ config/                 # Configuration files
β”‚   β”‚   β”œβ”€β”€ auth.ts            # JWT configuration
β”‚   β”‚   └── upload.ts          # File upload configuration
β”‚   β”œβ”€β”€ database/              # Database setup
β”‚   β”‚   β”œβ”€β”€ index.ts          # Database connection
β”‚   β”‚   └── migrations/       # Database migrations
β”‚   β”œβ”€β”€ modules/               # Business modules
β”‚   β”‚   β”œβ”€β”€ products/         # Product management
β”‚   β”‚   β”‚   β”œβ”€β”€ controllers/  # Request handlers
β”‚   β”‚   β”‚   β”œβ”€β”€ database/     # Entities and repositories
β”‚   β”‚   β”‚   β”œβ”€β”€ routes/       # Route definitions
β”‚   β”‚   β”‚   └── services/     # Business logic
β”‚   β”‚   └── users/            # User management
β”‚   β”‚       β”œβ”€β”€ controllers/  # Request handlers
β”‚   β”‚       β”œβ”€β”€ database/     # Entities and repositories
β”‚   β”‚       β”œβ”€β”€ routes/       # Route definitions
β”‚   β”‚       └── services/     # Business logic
β”‚   β”œβ”€β”€ routes/                # Main route configuration
β”‚   └── shared/                # Shared utilities
β”‚       β”œβ”€β”€ errors/           # Error handling
β”‚       β”œβ”€β”€ http/             # Server configuration
β”‚       └── middlewares/      # Custom middleware
β”œβ”€β”€ docs/                      # Documentation
β”œβ”€β”€ resource/                  # Static resources
β”œβ”€β”€ package.json              # Dependencies and scripts
β”œβ”€β”€ tsconfig.json             # TypeScript configuration
β”œβ”€β”€ jest.config.js            # Testing configuration
└── README.md                 # Project documentation

🀝 Contributing

  1. Fork the project
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

Development Guidelines

  • Follow TypeScript best practices
  • Use meaningful commit messages
  • Add tests for new features
  • Update documentation as needed
  • Run linting before committing

πŸ“„ License

This project is licensed under the ISC License - see the LICENSE file for details.

πŸ‘¨β€πŸ’» Author

Fernando Britto

πŸ™ Acknowledgments

  • Node.js community for excellent tooling
  • TypeORM team for the robust ORM solution
  • Express.js for the minimal and flexible framework
  • All contributors who help improve this project

⭐ If you found this project helpful, please give it a star! ⭐

About

API Restful - Alpha Tower Sales System

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published