A robust NestJS-based API for managing car reports with intelligent price estimation, user authentication, and comprehensive reporting functionality.
- Features
- Quick Start
- API Documentation
- Price Estimation Algorithm
- Project Structure
- Development
- Testing
- Database Management
- Environment Variables
- Technologies
- Contributing
- Troubleshooting
- License
- Session-based authentication with secure cookie management
- Role-based access control (Admin vs Regular users)
- User management (CRUD operations with admin privileges)
- Create, read, and approve/disapprove car reports
- Geographic filtering with longitude/latitude coordinates
- Comprehensive validation with class-validator
- Report approval workflow (Admin-only feature)
- 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
- 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)
- Node.js (v18 or higher)
- npm or yarn
# 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:
Once the service is running locally visit the Swagger UI at:
Here are some recommended workflows to explore the API functionality:
- 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
- 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)
- 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
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
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
The price estimation feature uses a statistical approach:
-
๐ Find Similar Cars
- Same make and model
- Geographic proximity (ยฑ5ยฐ longitude/latitude)
- Similar year (ยฑ3 years)
- Closest mileage
-
๐ Calculate Average
- Returns average price of 3 most similar approved reports
- Ignores unapproved reports for accuracy
-
๐ Input Requirements
- Car make and model
- Year (1930 to current year)
- Geographic coordinates (longitude/latitude)
- Mileage (0 to 1,000,000)
โโโ 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
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 migrationsCreate 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# 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- Unit Tests: Test individual components and services
- E2E Tests: Test complete API workflows
- Coverage Reports: Available in
coverage/directory
The application uses SQLite with TypeORM for data persistence.
# Clear all data
npm run db:clear
# Add sample data
npm run db:seed
# Reset database (clear + seed)
npm run db:reset# 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;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 |
- NestJS
- TypeScript
- TypeORM
- SQLite
- Jest
- Swagger
- Prettier
- ESLint
- Argon2
- class-validator
- cookie-session
Contributions are welcomed! Please follow these steps:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
- Write tests for new functionality
- Ensure all tests pass (
npm test) - Follow the existing code style
- Update documentation as needed
# Ensure database file exists
ls -la *.sqlite
# Check permissions
chmod 644 your-database.sqlite# Check .env file exists
ls -la .env
# Verify file format (no spaces around =)
cat .env# Reset migrations
npm run db:reset
npm run migrations:run- ๐ง Email: [email protected]
- ๐ Issues: GitHub Issues
- ๐ Documentation: Swagger UI : You can access this only if the server is running locally
- 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
This project is licensed under the MIT License - see the LICENSE file for details.
Ivan Derlich
- ๐ Website: ivanderlich.com
- ๐ง Email: [email protected]
- ๐ผ LinkedIn: Ivan Derlich
โญ If you found this project helpful, please give it a star!
Made for you with โค๏ธ by Ivan Derlich



