Skip to content

IvanDerlich/car-reports

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿš— Car Reports API

A robust NestJS-based API for managing car reports with intelligent price estimation, user authentication, and comprehensive reporting functionality.

Example

Video Example


NestJS TypeScript SQLite License: MIT

๐Ÿ“‹ Table of Contents

โœจ Features

๐Ÿ” Authentication & Authorization

  • Session-based authentication with secure cookie management
  • Role-based access control (Admin vs Regular users)
  • User management (CRUD operations with admin privileges)

๐Ÿ“Š Car Reports Management

  • Create, read, and approve/disapprove car reports
  • Geographic filtering with longitude/latitude coordinates
  • Comprehensive validation with class-validator
  • Report approval workflow (Admin-only feature)

๐Ÿง  Intelligent Price Estimation

  • Statistical algorithm that finds the 3 most similar approved reports
  • Multi-factor matching: make, model, location, year, and mileage
  • Geographic proximity filtering (ยฑ5 degrees)
  • Year range matching (ยฑ3 years)
  • Mileage-based similarity scoring

๐Ÿ“š Developer Experience

  • Rich Swagger documentation with interactive API explorer
  • Comprehensive testing suite (Unit + E2E tests)
  • Code coverage reporting
  • Database seeding with sample data
  • VS Code REST Client support (.http files)

๐Ÿš€ Quick Start

Prerequisites

  • Node.js (v18 or higher)
  • npm or yarn

Installation

# Clone the repository
git clone https://github.com/IvanDerlich/car-reports.git
cd car-reports

# Install dependencies
npm install

# Create environment variables file
touch .env

# Add your custom cookie value to the .env file
echo "COOKIE_KEY=your-secret-test-cookie-key-here" > .env

# Add your custom database value to the .env file
echo "DB_NAME=db.your-local-database-name.sqlite" >> .env

# Run Migrations
npm run migrations:run

# Seed the database with sample data
npm run db:seed

# Start the development server
npm run dev

๐ŸŽ‰ Success! Your API is now running at http://localhost:3000

Now you should be seeing something like this:

Successful Setup

๐Ÿ“– API Documentation

Once the service is running locally visit the Swagger UI at:

http://localhost:3000/api

Swagger 1 of 2 Swagger 2 of 2

๐Ÿงช Testing Scenarios

Here are some recommended workflows to explore the API functionality:

User Management & Authentication

  • Create an admin user and test role-based permissions
  • Login/logout to verify session management
  • Use the whoAmI endpoint to check your current authentication status
  • Create regular users and test different permission levels

Report Management

  • Create car reports with various vehicle data
  • Approve/reject reports (admin-only functionality)
  • View all reports and test filtering capabilities
  • Get price estimates using the preset values in Swagger (database is pre-seeded for testing)

Price Estimation Algorithm Testing

  • Test price changes: Add reports within the algorithm's range and observe how the average price updates
  • Verify filtering: Add reports outside the geographic/year range or unapproved reports to confirm they don't affect estimates
  • Test mileage limits: Add reports with excessive mileage differences when 3 matches already exist to verify they're ignored
  • Test closer matches: Add reports with better mileage proximity to see how they change the average

Authorization Testing

Verify that non-admin users cannot:

  • View all users
  • Search for users by ID or email
  • Delete any user
  • Update user information
  • Approve or reject reports

Testing with REST Client

Use the included .http files for easy API testing with you IDE and the proper plugin:

  • User endpoints: src/users/request.http
  • Report endpoints: src/reports/requests.http

๐Ÿงฎ Price Estimation Algorithm

The price estimation feature uses a statistical approach:

Algorithm Steps

  1. ๐Ÿ” Find Similar Cars

    • Same make and model
    • Geographic proximity (ยฑ5ยฐ longitude/latitude)
    • Similar year (ยฑ3 years)
    • Closest mileage
  2. ๐Ÿ“Š Calculate Average

    • Returns average price of 3 most similar approved reports
    • Ignores unapproved reports for accuracy
  3. ๐Ÿ“ Input Requirements

    • Car make and model
    • Year (1930 to current year)
    • Geographic coordinates (longitude/latitude)
    • Mileage (0 to 1,000,000)

๐Ÿ—๏ธ Project Structure

โ”œโ”€โ”€ src/                      # Source code
โ”‚   โ”œโ”€โ”€ users/               # User management module
โ”‚   โ”‚   โ”œโ”€โ”€ dtos/            # Data Transfer Objects
โ”‚   โ”‚   โ”œโ”€โ”€ user.entity.ts   # User entity definition
โ”‚   โ”‚   โ”œโ”€โ”€ users.controller.ts # User endpoints
โ”‚   โ”‚   โ”œโ”€โ”€ users.service.ts # User business logic
โ”‚   โ”‚   โ””โ”€โ”€ users.controller.docs.ts # API documentation
โ”‚   โ”œโ”€โ”€ reports/             # Reports management module
โ”‚   โ”‚   โ”œโ”€โ”€ dtos/            # Report DTOs
โ”‚   โ”‚   โ”œโ”€โ”€ report.entity.ts # Report entity
โ”‚   โ”‚   โ”œโ”€โ”€ reports.controller.ts # Report endpoints
โ”‚   โ”‚   โ”œโ”€โ”€ reports.service.ts # Report business logic
โ”‚   โ”‚   โ””โ”€โ”€ reports.controller.docs.ts # API documentation
โ”‚   โ”œโ”€โ”€ guards/              # Authentication guards
โ”‚   โ”œโ”€โ”€ interceptors/        # Response serialization
โ”‚   โ”œโ”€โ”€ validators/          # Custom validation rules
โ”‚   โ”œโ”€โ”€ config/              # Configuration files
โ”‚   โ”œโ”€โ”€ types/               # TypeScript type definitions
โ”‚   โ””โ”€โ”€ main.ts              # Application entry point
โ”œโ”€โ”€ test/                    # End-to-end tests
โ”‚   โ”œโ”€โ”€ fixtures/            # Test data and fixtures
โ”‚   โ”œโ”€โ”€ app.e2e-spec.ts      # Main E2E test suite
โ”‚   โ”œโ”€โ”€ auth.e2e-spec.ts     # Authentication E2E tests
โ”‚   โ”œโ”€โ”€ reports.e2e-spec.ts  # Reports E2E tests
โ”‚   โ”œโ”€โ”€ setup.ts             # Test setup configuration
โ”‚   โ””โ”€โ”€ jest-e2e.json        # Jest E2E configuration
โ”œโ”€โ”€ dev/                     # Development tools and scripts
โ”‚   โ”œโ”€โ”€ db/                  # Database development files
โ”‚   โ””โ”€โ”€ scripts/             # Development scripts
โ”œโ”€โ”€ docs/                    # Documentation images
โ”œโ”€โ”€ coverage/                # Test coverage reports
โ””โ”€โ”€ dist/                    # Built application (generated)

๐Ÿ› ๏ธ Development

Available Scripts

# Development
npm run dev              # Start with hot reload

# Building
npm run build            # Build for production

# Code Quality
npm run format           # Format code with Prettier
npm run format:check     # Check code formatting

# Database
npm run db:clear         # Clear database
npm run db:seed          # Seed with sample data
npm run db:reset         # Clear + seed database
npm run migrations:run   # Run database migrations

๐Ÿงช Testing

Environment Setup

Create a .env.test file at the root with the following variables:

COOKIE_KEY=your-secret-cookie-key-here
DB_NAME=db.your-database-name.sqlite
NODE_ENV=development

Running Tests

# Unit tests
npm run test

# E2E tests
npm run test:e2e

# Test coverage for both unit and e2e tests
npm run test:cov

# Watch mode
npm run test:watch

Test Structure

  • Unit Tests: Test individual components and services
  • E2E Tests: Test complete API workflows
  • Coverage Reports: Available in coverage/ directory

๐Ÿ—„๏ธ Database Management

SQLite Database

The application uses SQLite with TypeORM for data persistence.

Database Scripts

# Clear all data
npm run db:clear

# Add sample data
npm run db:seed

# Reset database (clear + seed)
npm run db:reset

Database Inspection

# Install SQLite CLI
sudo apt update && sudo apt install sqlite3

# Access database
sqlite3 your-database-name.sqlite

# View tables
.tables

# Query data
SELECT * FROM user;
SELECT * FROM report;
SELECT * FROM migrations;

โš™๏ธ Environment Variables

These variables should be present in both the .env file for local usage and the .env.test file for testing

Variable Description Required Default
COOKIE_KEY Secret key for session encryption โœ… -
DB_NAME SQLite database file path โœ… -
NODE_ENV Environment mode โŒ development

๐Ÿ› ๏ธ Technologies

  • NestJS
  • TypeScript
  • TypeORM
  • SQLite
  • Jest
  • Swagger
  • Prettier
  • ESLint
  • Argon2
  • class-validator
  • cookie-session

๐Ÿค Contributing

Contributions are welcomed! Please follow these steps:

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

Development Guidelines

  • Write tests for new functionality
  • Ensure all tests pass (npm test)
  • Follow the existing code style
  • Update documentation as needed

๐Ÿ”ง Troubleshooting common Issues

Database Connection Issues

# Ensure database file exists
ls -la *.sqlite

# Check permissions
chmod 644 your-database.sqlite

Environment Variables Not Loading

# Check .env file exists
ls -la .env

# Verify file format (no spaces around =)
cat .env

Migration Issues

# Reset migrations
npm run db:reset
npm run migrations:run

Getting Help

๐Ÿš€ Future Improvements

  • Server-side sessions for better security
  • JWT authentication as alternative to cookies
  • Rate limiting and API throttling
  • OpenAPI export for Postman/Insomnia
  • Docker containerization
  • CI/CD pipeline setup
  • Performance monitoring and logging
  • Caching layer (Redis)
  • Email notifications for report status changes

๐Ÿ“„ License

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

๐Ÿ‘จโ€๐Ÿ’ป Author

Ivan Derlich


โญ If you found this project helpful, please give it a star!

Made for you with โค๏ธ by Ivan Derlich

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published