Skip to content

fedirko-pro/archery-app-backend

Repository files navigation

Archery App Backend

A NestJS-based backend application for managing archery tournaments, users, and authentication. Built with TypeScript, PostgreSQL, and MikroORM.

πŸš€ Features

  • Authentication: JWT-based authentication with Google OAuth support
  • User Management: User registration, login, password reset functionality, password change
  • Tournament Management: Create and manage archery tournaments with multiple applications support
  • Application System: Tournament applications with deadline management and category-based restrictions
  • Email Integration: Password reset and notification emails
  • Database: PostgreSQL with MikroORM for type-safe database operations
  • API: RESTful API with comprehensive endpoints
  • Code Quality: Pre-commit hooks with ESLint and Prettier

πŸ“‹ Prerequisites

Before running this application, make sure you have the following installed:

  • Node.js (v18 or higher)
  • pnpm (recommended) or npm
  • Docker and Docker Compose (for database)
  • PostgreSQL (if not using Docker)

πŸ› οΈ Installation

  1. Clone the repository

    git clone <repository-url>
    cd archery-app-backend
  2. Install dependencies

    pnpm install
  3. Set up environment variables Create a .env file in the root directory with the following variables:

    # Database Configuration
    DATABASE_HOST=localhost
    DATABASE_PORT=5432
    DATABASE_USER=archery_user
    DATABASE_PASSWORD=archery_password
    DATABASE_NAME=archery_db
    
    # JWT Configuration
    JWT_SECRET=your-super-secret-jwt-key
    
    # Google OAuth Configuration
    GOOGLE_CLIENT_ID=your-google-client-id
    GOOGLE_CLIENT_SECRET=your-google-client-secret
    GOOGLE_CALLBACK_URL=http://localhost:3000/auth/google/callback
    
    # Application URLs
    FRONTEND_URL=http://localhost:5173
    BACKEND_URL=http://localhost:3000
    
    # Server Configuration
    PORT=3000
    NODE_ENV=development

πŸ—„οΈ Database Setup

Option 1: Using Docker (Recommended)

  1. Start the PostgreSQL database

    docker-compose up -d
  2. Verify the database is running

    docker-compose ps

Option 2: Local PostgreSQL

If you prefer to use a local PostgreSQL installation:

  1. Create a database named archery_db
  2. Create a user archery_user with password archery_password
  3. Grant necessary permissions to the user

πŸƒβ€β™‚οΈ Running the Application

Development Mode

  1. Start the development server

    pnpm run start:dev

    The application will be available at http://localhost:3000

  2. With debugging enabled

    pnpm run start:debug

Production Mode

  1. Build the application

    pnpm run build
  2. Start the production server

    pnpm run start:prod

πŸ—ƒοΈ Database Migrations

Running Migrations

  1. Generate a new migration (when you modify entities)

    pnpm run mikro-orm migration:create
  2. Run pending migrations

    pnpm run mikro-orm migration:up
  3. Check migration status

    pnpm run mikro-orm migration:list
  4. Revert the last migration

    pnpm run mikro-orm migration:down

Migration Commands Reference

# Create a new migration
pnpm run mikro-orm migration:create

# Run all pending migrations
pnpm run mikro-orm migration:up

# Run migrations up to a specific version
pnpm run mikro-orm migration:up --to=Migration20241119213454

# Revert the last migration
pnpm run mikro-orm migration:down

# List all migrations and their status
pnpm run mikro-orm migration:list

# Show pending migrations
pnpm run mikro-orm migration:pending

πŸ§ͺ Testing

Unit Tests

pnpm run test

E2E Tests

pnpm run test:e2e

Test Coverage

pnpm run test:cov

Watch Mode

pnpm run test:watch

πŸ”§ Development Tools

Code Formatting

pnpm run format

Linting

pnpm run lint

Type Checking

pnpm run build

πŸ“ Project Structure

src/
β”œβ”€β”€ auth/                 # Authentication module
β”‚   β”œβ”€β”€ controllers/      # Auth controllers
β”‚   β”œβ”€β”€ services/         # Auth services
β”‚   β”œβ”€β”€ guards/           # JWT and role guards
β”‚   β”œβ”€β”€ strategies/       # Passport strategies
β”‚   └── dto/             # Auth DTOs
β”œβ”€β”€ user/                 # User management module
β”‚   β”œβ”€β”€ entity/          # User entity
β”‚   β”œβ”€β”€ controllers/     # User controllers
β”‚   β”œβ”€β”€ services/        # User services
β”‚   └── dto/            # User DTOs
β”œβ”€β”€ tournament/           # Tournament management module
β”‚   β”œβ”€β”€ entity/          # Tournament entities
β”‚   β”œβ”€β”€ controllers/     # Tournament controllers
β”‚   β”œβ”€β”€ services/        # Tournament services
β”‚   └── dto/            # Tournament DTOs
β”œβ”€β”€ email/               # Email service module
β”œβ”€β”€ config/              # Configuration files
β”œβ”€β”€ migrations/          # Database migrations
└── main.ts             # Application entry point

πŸ” Authentication Setup

Google OAuth Setup

  1. Go to the Google Cloud Console
  2. Create a new project or select an existing one
  3. Enable the Google+ API
  4. Create OAuth 2.0 credentials
  5. Add your redirect URI: http://localhost:3000/auth/google/callback
  6. Update your .env file with the client ID and secret

For detailed setup instructions, see docs/GOOGLE_OAUTH_SETUP.md

Email Setup

Configure your email service for password reset functionality. See docs/EMAIL_SETUP.md for detailed instructions.

πŸš€ Deployment

Environment Variables for Production

Make sure to update your environment variables for production:

NODE_ENV=production
DATABASE_HOST=your-production-db-host
DATABASE_PORT=5432
DATABASE_USER=your-production-db-user
DATABASE_PASSWORD=your-production-db-password
DATABASE_NAME=your-production-db-name
JWT_SECRET=your-production-jwt-secret
FRONTEND_URL=https://your-frontend-domain.com
BACKEND_URL=https://your-backend-domain.com
GOOGLE_CALLBACK_URL=https://your-backend-domain.com/auth/google/callback

Build and Deploy

  1. Build the application

    pnpm run build
  2. Start the production server

    pnpm run start:prod

πŸ“š API Documentation

The API endpoints are organized as follows:

  • Authentication: /auth/* - Login, register, password reset
  • Users: /users/* - User management
  • Tournaments: /tournaments/* - Tournament management
  • Email: /email/* - Email-related endpoints

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests for new functionality
  5. Run the test suite
  6. Submit a pull request

πŸ“„ License

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

πŸ†˜ Troubleshooting

Common Issues

  1. Database Connection Error

    • Ensure PostgreSQL is running
    • Check your database credentials in .env
    • Verify the database exists
  2. Migration Errors

    • Make sure all previous migrations are applied
    • Check for syntax errors in your entity files
    • Verify the database schema matches your entities
  3. Port Already in Use

    • Change the PORT in your .env file
    • Or kill the process using the current port
  4. Google OAuth Issues

    • Verify your Google OAuth credentials
    • Check that the redirect URI matches exactly
    • Ensure the Google+ API is enabled

Getting Help

If you encounter any issues:

  1. Check the existing documentation in the docs/ folder
  2. Review the error logs
  3. Ensure all prerequisites are installed
  4. Verify your environment variables are correctly set

πŸ”§ Code Quality

Pre-commit Hooks

The project uses Husky and lint-staged to ensure code quality:

  • ESLint - Code linting and error detection
  • Prettier - Code formatting
  • TypeScript - Type checking
  • Automatic formatting - Code is automatically formatted on commit

Available Scripts

  • npm run lint - Run ESLint with auto-fix
  • npm run format - Format code with Prettier
  • npm run build - Type checking and build

πŸ”— Related Documentation

About

Server for the archery application

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •