Skip to content

Shu1237/Be_Movie_Theater

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

682 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Nest Logo

🎬 Movie Theater Booking System

A comprehensive online movie ticket booking system built with NestJS

NPM Version Package License TypeScript NestJS MySQL

πŸ“‹ Overview

The Movie Theater Booking System is a backend web application developed with NestJS and TypeScript, providing RESTful APIs for cinema management, online ticket booking, and diverse payment processing. The system supports advanced features such as real-time seat booking, automated scheduling, and multiple payment gateway integrations.

✨ Key Features

🎭 Cinema & Movie Management

  • Movie Management: CRUD operations for movies with detailed information (actors, genres, versions)
  • Cinema Management: Create and manage cinema rooms with seat layouts
  • Smart Scheduling: Automatically generate schedules based on revenue and popularity
  • Movie Versions: Support multiple versions (2D, 3D, IMAX, Dolby Atmos)

🎫 Ticket Booking System

  • Real-time Booking: WebSocket for real-time seat status updates
  • Seat Management: Hold seats for 10 minutes during booking process
  • Diverse Ticket Types: Regular, VIP, Couple seats with different pricing
  • QR Code: Automatically generate QR codes for paid tickets

πŸ’³ Multiple Payment Methods

  • MoMo: Popular e-wallet in Vietnam
  • VNPay: Vietnamese banking payment gateway
  • ZaloPay: Zalo e-wallet
  • PayPal: International payment
  • Stripe (Visa/Mastercard): Credit card payment
  • Cash: Counter payment

πŸ‘₯ User Management

  • Registration/Login: Authentication system with JWT
  • Google OAuth: Quick login with Google account
  • Role Management: Admin, Employee, Customer with different permissions
  • Points System: Accumulate and use reward points

πŸ“Š Management & Reports

  • Overview Dashboard: Revenue statistics, ticket sales
  • Order Management: View order details, refunds
  • Email Notifications: Send confirmation and refund emails
  • Promotion System: Create and manage discount codes

πŸ”§ Technical Features

  • Caching: Redis for performance optimization
  • File Upload: AWS S3 for image storage
  • Scheduled Tasks: Cron jobs for cleanup and automation
  • WebSocket: Real-time communication
  • API Documentation: Swagger/OpenAPI

πŸ—οΈ System Architecture

Technology Stack

  • Backend Framework: NestJS 11.x
  • Language: TypeScript 5.x
  • Database: MySQL 2.x with TypeORM
  • Cache: Redis (ioredis)
  • Authentication: JWT + Passport
  • File Storage: AWS S3
  • Email: NodeMailer with Handlebars templates
  • WebSocket: Socket.IO
  • Payment: Multiple gateways integration
  • Documentation: Swagger/OpenAPI
  • Testing: Jest

Directory Structure

src/
β”œβ”€β”€ common/                 # Shared utilities & configurations
β”‚   β”œβ”€β”€ config/            # Application configuration
β”‚   β”œβ”€β”€ cron/              # Scheduled tasks & automation
β”‚   β”œβ”€β”€ decorator/         # Custom decorators
β”‚   β”œβ”€β”€ enums/             # Application enums
β”‚   β”œβ”€β”€ exceptions/        # Custom exceptions
β”‚   β”œβ”€β”€ gateways/          # WebSocket gateways
β”‚   β”œβ”€β”€ guards/            # Authentication & authorization guards
β”‚   β”œβ”€β”€ interceptors/      # Request/response interceptors
β”‚   β”œβ”€β”€ pagination/        # Pagination utilities
β”‚   β”œβ”€β”€ qrcode/            # QR code generation
β”‚   β”œβ”€β”€ redis/             # Redis configuration
β”‚   β”œβ”€β”€ s3/                # AWS S3 integration
β”‚   └── utils/             # Helper functions
β”œβ”€β”€ database/              # Database entities & models
β”‚   └── entities/
β”‚       β”œβ”€β”€ cinema/        # Cinema, Movie, Schedule entities
β”‚       β”œβ”€β”€ order/         # Order, Ticket, Transaction entities
β”‚       β”œβ”€β”€ user/          # User & authentication entities
β”‚       └── item/          # Product & promotion entities
β”œβ”€β”€ modules/               # Feature modules
β”‚   β”œβ”€β”€ auth/              # Authentication & authorization
β”‚   β”œβ”€β”€ actor/             # Actor management
β”‚   β”œβ”€β”€ cinema-room/       # Cinema room management
β”‚   β”œβ”€β”€ gerne/             # Movie genres
β”‚   β”œβ”€β”€ movie/             # Movie management
β”‚   β”œβ”€β”€ order/             # Order processing & payment
β”‚   β”‚   └── payment-method/ # Payment gateway integrations
β”‚   β”œβ”€β”€ schedule/          # Movie scheduling
β”‚   β”œβ”€β”€ seat/              # Seat management
β”‚   β”œβ”€β”€ ticket/            # Ticket management
β”‚   β”œβ”€β”€ member/            # User management
β”‚   β”œβ”€β”€ promotion/         # Promotion & discount system
β”‚   └── overview/          # Dashboard & statistics
└── template/              # Email templates

πŸš€ API Endpoints

Authentication

POST   /auth/register              # Register account
POST   /auth/login                 # Login
POST   /auth/google                # Google OAuth login
POST   /auth/refresh-token         # Refresh JWT token
POST   /auth/logout                # Logout

Movies & Cinema

GET    /movies                     # List movies
GET    /movies/:id                 # Movie details
POST   /movies                     # Create new movie (Admin)
PUT    /movies/:id                 # Update movie (Admin)
DELETE /movies/:id                 # Delete movie (Admin)

GET    /cinema-rooms               # List cinema rooms
POST   /cinema-rooms               # Create cinema room (Admin)
PUT    /cinema-rooms/:id           # Update cinema room (Admin)

Scheduling

GET    /schedules                  # List schedules
GET    /schedules/:id              # Schedule details
POST   /schedules                  # Create schedule (Admin)
PUT    /schedules/:id              # Update schedule (Admin)

Booking & Orders

GET    /seats/room/:roomId         # List seats by room
GET    /schedule-seats/:scheduleId # Seat status by schedule

POST   /order                      # Create order
GET    /order                      # User's order list
GET    /order/:id                  # Order details
PATCH  /order/:id/cancel           # Cancel order

Payment Processing

POST   /order/momo                 # MoMo payment
POST   /order/vnpay                # VNPay payment
POST   /order/zalopay              # ZaloPay payment
POST   /order/paypal               # PayPal payment
POST   /order/visa                 # Stripe (Visa) payment

# Payment callbacks
GET    /order/momo/return          # MoMo callback
GET    /order/vnpay/return         # VNPay callback
GET    /order/zalopay/return       # ZaloPay callback
GET    /order/paypal/return        # PayPal callback
GET    /order/visa/return          # Stripe callback

User Management

GET    /users/profile              # Profile information
PUT    /users/profile              # Update profile
GET    /users/booking-history      # Booking history
GET    /users/points               # Accumulated points

Admin Dashboard

GET    /overview/dashboard         # Overview dashboard
GET    /overview/revenue           # Revenue report
GET    /overview/popular-movies    # Popular movies
GET    /overview/user-stats        # User statistics

πŸ”§ Installation and Setup

System Requirements

  • Node.js >= 18.x
  • MySQL >= 8.0
  • Redis >= 6.0
  • npm or yarn

1. Clone repository

git clone <repository-url>
cd Be_Nestjs

2. Install dependencies

npm install

3. Configure environment variables

Create .env file with required environment variables:

# Database
DB_HOST=localhost
DB_PORT=3306
DB_USERNAME=your_username
DB_PASSWORD=your_password
DB_DATABASE=movie_theater

# JWT
JWT_SECRET=your_jwt_secret
JWT_REFRESH_SECRET=your_refresh_secret

# Redis
REDIS_PUBLIC_URL=redis://localhost:6379

# AWS S3
S3_ACCESS_KEY=your_s3_access_key
S3_SECRET_KEY=your_s3_secret_key
S3_REGION=your_s3_region
S3_BUCKET_NAME=your_bucket_name

# Email
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USER=your_email@gmail.com
MAIL_PASSWORD=your_app_password

# Payment Gateways
# MoMo
MOMO_ACCESS_KEY=your_momo_access_key
MOMO_SECRET_KEY=your_momo_secret_key
MOMO_PARTNER_CODE=your_partner_code

# VNPay
VNP_TMN_CODE=your_vnpay_tmn_code
VNP_HASH_SECRET=your_vnpay_hash_secret
VNP_URL=https://sandbox.vnpayment.vn/paymentv2/vpcpay.html

# ZaloPay
ZALO_APP_ID=your_zalopay_app_id
ZALO_KEY=your_zalopay_key

# PayPal
PAYPAL_CLIENT_ID=your_paypal_client_id
PAYPAL_CLIENT_SECRET=your_paypal_client_secret

# Stripe
VISA_PUBLIC_KEY=your_stripe_public_key
VISA_SECRET_KEY=your_stripe_secret_key

# Google OAuth
GOOGLE_CLIENT_ID=your_google_client_id
GOOGLE_CLIENT_SECRET=your_google_client_secret

4. Run database migrations

# Create database
npm run db:create

# Run migrations
npm run migration:run

5. Run the application

Development mode

npm run dev

Production mode

npm run build
npm run start:prod

6. Access the application

πŸ§ͺ Testing

Run unit tests

npm run test

Run tests with coverage

npm run test:cov

Run end-to-end tests

npm run test:e2e

Watch mode

npm run test:watch

πŸ”„ Advanced Features

WebSocket Real-time Features

  • Seat Booking: Real-time seat status updates
  • Live Notifications: Order and payment notifications
  • Admin Dashboard: Real-time statistics updates

Automated Scheduling System

The system automatically creates schedules based on:

  • Revenue from the last 3 days for each movie
  • Golden hours (6 PM - 9 PM) prioritize popular movies
  • Optimize cinema room usage
  • Runs daily at 2 AM

Smart Payment Processing

  • Multi-gateway support: 6 different payment gateways
  • Currency conversion: Automatic VND ↔ USD conversion
  • Payment verification: Signature and callback verification
  • Refund system: Automatic refunds via API

Security Features

  • JWT Authentication: Access token + Refresh token
  • Role-based Access Control: Admin, Employee, Customer
  • API Rate Limiting: Request limit per IP
  • Input Validation: Sanitize and validate all inputs
  • CORS Protection: Secure CORS configuration

Performance Optimization

  • Redis Caching: Cache hot data and sessions
  • Database Indexing: Optimize query performance
  • Pagination: Pagination for large lists
  • Lazy Loading: Load relations when needed
  • Connection Pooling: Optimize database connections

πŸ“Š Database Schema

Core Entities

User Management

users              # User information
β”œβ”€β”€ id (PK)
β”œβ”€β”€ email (unique)
β”œβ”€β”€ password_hash
β”œβ”€β”€ full_name
β”œβ”€β”€ phone_number
β”œβ”€β”€ role (admin/employee/customer)
β”œβ”€β”€ points
└── is_verified

user_profiles      # Extended profile
β”œβ”€β”€ user_id (FK)
β”œβ”€β”€ avatar_url
β”œβ”€β”€ date_of_birth
β”œβ”€β”€ gender
└── address

Cinema Management

movies             # Movie information
β”œβ”€β”€ id (PK)
β”œβ”€β”€ name
β”œβ”€β”€ description
β”œβ”€β”€ duration_minutes
β”œβ”€β”€ release_date
β”œβ”€β”€ poster_url
β”œβ”€β”€ trailer_url
β”œβ”€β”€ age_rating
└── is_deleted

cinema_rooms       # Cinema rooms
β”œβ”€β”€ id (PK)
β”œβ”€β”€ cinema_room_name
└── is_deleted

seats              # Seats
β”œβ”€β”€ id (PK)
β”œβ”€β”€ seat_row
β”œβ”€β”€ seat_column
β”œβ”€β”€ cinema_room_id (FK)
β”œβ”€β”€ seat_type_id (FK)
└── is_deleted

schedules          # Schedules
β”œβ”€β”€ id (PK)
β”œβ”€β”€ movie_id (FK)
β”œβ”€β”€ cinema_room_id (FK)
β”œβ”€β”€ version_id (FK)
β”œβ”€β”€ start_movie_time
β”œβ”€β”€ end_movie_time
└── is_deleted

Booking & Orders

orders             # Orders
β”œβ”€β”€ id (PK)
β”œβ”€β”€ user_id (FK)
β”œβ”€β”€ total_prices
β”œβ”€β”€ status
β”œβ”€β”€ payment_method_id (FK)
β”œβ”€β”€ promotion_id (FK)
β”œβ”€β”€ created_at
└── updated_at

order_details      # Order details
β”œβ”€β”€ id (PK)
β”œβ”€β”€ order_id (FK)
β”œβ”€β”€ schedule_id (FK)
β”œβ”€β”€ ticket_id (FK)
β”œβ”€β”€ total_each_ticket
└── created_at

tickets            # Tickets
β”œβ”€β”€ id (PK)
β”œβ”€β”€ schedule_id (FK)
β”œβ”€β”€ seat_id (FK)
β”œβ”€β”€ ticket_type_id (FK)
β”œβ”€β”€ price
β”œβ”€β”€ qr_code
β”œβ”€β”€ status
└── created_at

transactions       # Payment transactions
β”œβ”€β”€ id (PK)
β”œβ”€β”€ order_id (FK)
β”œβ”€β”€ payment_gateway
β”œβ”€β”€ transaction_code
β”œβ”€β”€ amount
β”œβ”€β”€ status
β”œβ”€β”€ payment_url
└── created_at

πŸ” Authentication & Authorization

JWT Token Structure

{
  "access_token": {
    "user_id": "uuid",
    "email": "user@example.com",
    "role": "customer",
    "exp": "timestamp"
  },
  "refresh_token": {
    "user_id": "uuid",
    "token_id": "uuid",
    "exp": "timestamp"
  }
}

Role-based Permissions

Endpoint Customer Employee Admin
Movies (Read) βœ… βœ… βœ…
Movies (Write) ❌ ❌ βœ…
Cinema Rooms (Read) βœ… βœ… βœ…
Cinema Rooms (Write) ❌ βœ… βœ…
Schedules (Read) βœ… βœ… βœ…
Schedules (Write) ❌ βœ… βœ…
Orders (Own) βœ… βœ… βœ…
Orders (All) ❌ βœ… βœ…
Dashboard ❌ βœ… βœ…
User Management ❌ ❌ βœ…

🎯 Business Logic

Seat Booking Flow

  1. User selects seats β†’ Check availability
  2. Hold seats for 10 minutes β†’ Update status to 'HELD'
  3. User proceeds to payment β†’ Create order with PENDING status
  4. Payment processing β†’ Redirect to payment gateway
  5. Payment callback β†’ Verify signature and update order
  6. Success: Mark seats as 'BOOKED', generate QR code, send email
  7. Failure: Release seats, mark order as 'FAILED'

Auto-scheduling Algorithm

// Cron job runs daily at 2 AM
@Cron('0 2 * * *')
async handleAutoScheduleMovies() {
  // 1. Calculate revenue from last 3 days
  const movieRevenue = await this.getMovieRevenue(3);
  
  // 2. Allocate slots based on revenue percentage
  const totalSlots = 20;
  const movieSlotPlan = this.calculateSlotDistribution(movieRevenue, totalSlots);
  
  // 3. Prioritize top movies during golden hours (6 PM - 9 PM)
  // 4. Avoid schedule conflicts in the same room
  // 5. Create schedules for tomorrow
}

Payment Gateway Integration

Each payment gateway implements AbstractPaymentService:

abstract class AbstractPaymentService {
  abstract createOrder(orderData: OrderBillType): Promise<PaymentResponse>;
  abstract handleReturn(query: any): Promise<string>;
  abstract queryOrderStatus(orderId: string): Promise<PaymentStatus>;
  
  // Shared business logic
  protected async handleReturnSuccess(transaction: Transaction): Promise<string>;
  protected async handleReturnFailed(transaction: Transaction): Promise<string>;
}

πŸ“ˆ Monitoring & Performance

Metrics & Logging

  • Application logs: NestJS Logger vα»›i different levels
  • Performance monitoring: Request duration tracking
  • Error tracking: Custom exception filters
  • Database queries: TypeORM query logging
  • Cache hit rate: Redis performance metrics

Health Checks

GET /health/database    # Check database connection
GET /health/redis       # Check Redis connection
GET /health/s3          # Check AWS S3 connection
GET /health/payment     # Check payment gateways

πŸš€ Deployment

Docker Configuration

FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
RUN npm run build
EXPOSE 3001
CMD ["npm", "run", "start:prod"]

Environment Setup

  • Development: Local MySQL + Redis
  • Staging: Docker Compose with separate services
  • Production: Kubernetes cluster with LoadBalancer

🀝 Contributing

Development Workflow

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

Code Standards

  • TypeScript: Strict mode enabled
  • ESLint: Airbnb configuration with custom rules
  • Prettier: Code formatting
  • Commit messages: Conventional Commits format
  • Testing: Minimum 80% coverage required

Pre-commit Hooks

npm run lint          # ESLint checking
npm run format        # Prettier formatting
npm run test          # Run unit tests
npm run build         # Build verification

πŸ“ API Documentation

After running the application, access Swagger documentation at: http://localhost:3001/api

Sample API Requests

1. Register account

curl -X POST http://localhost:3001/auth/register \
  -H "Content-Type: application/json" \
  -d '{
    "email": "user@example.com",
    "password": "password123",
    "full_name": "Nguyen Van A",
    "phone_number": "0123456789"
  }'

2. Login

curl -X POST http://localhost:3001/auth/login \
  -H "Content-Type: application/json" \
  -d '{
    "email": "user@example.com",
    "password": "password123"
  }'

3. View movie list

curl -X GET "http://localhost:3001/movies?page=1&take=10" \
  -H "Authorization: Bearer YOUR_JWT_TOKEN"

4. Book tickets

curl -X POST http://localhost:3001/order \
  -H "Authorization: Bearer YOUR_JWT_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "schedule_id": 1,
    "seats": ["R1_A1", "R1_A2"],
    "payment_method_id": 2,
    "promotion_id": null
  }'

πŸ”§ Troubleshooting

Common Issues

1. Database Connection Error

Error: ER_ACCESS_DENIED_ERROR: Access denied for user

Solution: Check username, password and MySQL access permissions

2. Redis Connection Failed

Error: connect ECONNREFUSED 127.0.0.1:6379

Solution: Ensure Redis server is running

redis-server
# or
docker run -d -p 6379:6379 redis:alpine

3. JWT Token Invalid

Error: JsonWebTokenError: invalid token

Solution: Check JWT_SECRET in .env file

4. Payment Gateway Errors

Error: Invalid signature

Solution: Re-verify API keys and secret keys for payment gateways

Debug Mode

# Run with debug logs
DEBUG=* npm run dev

# Or only NestJS logs
DEBUG=nest:* npm run dev

πŸ“š Resources & References

Documentation

Payment Gateway APIs

Learning Resources

πŸ“„ License

🎬 Made with ❀️ for movie lovers

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors