Skip to content

nikkittaa/task-manager-crud

Repository files navigation

πŸ“ Task Manager API

A robust, feature-rich Task Management REST API built with NestJS, TypeScript, and MySQL. This application provides complete task management functionality with advanced features like Redis caching, rate limiting, JWT authentication, and comprehensive API documentation.

πŸš€ Features

Core Functionality

  • βœ… Complete CRUD Operations - Create, Read, Update, Delete tasks
  • πŸ” JWT Authentication - Secure user authentication and authorization
  • πŸ‘€ User Management - User registration and profile management
  • πŸ” Advanced Filtering - Filter tasks by status and search terms
  • πŸ“Š Task Status Management - OPEN, IN_PROGRESS, DONE status workflow

Advanced Features

  • πŸš€ Redis Caching - High-performance caching for improved response times
  • πŸ›‘οΈ Rate Limiting - Multi-tier throttling protection against abuse
  • πŸ“‘ Real-time Updates - Redis Pub/Sub for real-time notifications
  • πŸ“š Swagger Documentation - Complete API documentation with interactive UI
  • ✨ Input Validation - Comprehensive request validation with class-validator
  • πŸ“§ Email Notifications - Automated weekly task summaries with HTML templates
  • ⏰ Scheduled Jobs - Cron-based task scheduling for automated operations
  • πŸ§ͺ Comprehensive Testing

Security & Performance

  • πŸ”’ JWT Token Security - Stateless authentication with configurable expiration
  • πŸ›‘ Rate Limiting - Multiple throttling tiers (per-second, per-minute)
  • πŸ—„οΈ Database Security - Parameterized queries and ORM protection
  • ⚑ Optimized Caching - Smart cache invalidation and refresh strategies

πŸ› οΈ Tech Stack

Backend Framework

  • NestJS - Progressive Node.js framework
  • TypeScript - Type-safe JavaScript

Database & Caching

  • MySQL - Primary database
  • TypeORM - Object-Relational Mapping
  • Redis - Caching and Pub/Sub messaging

Authentication & Security

  • JWT - JSON Web Tokens
  • bcrypt - Password hashing
  • Passport - Authentication middleware
  • class-validator - Input validation

Development & Testing

  • Jest - Testing framework
  • Swagger - API documentation
  • ESLint - Code linting
  • Prettier - Code formatting
  • Husky - Git hooks

Email & Scheduling

  • @nestjs-modules/mailer - Email service integration
  • Nodemailer - Email transport layer
  • Handlebars - HTML email templates
  • @nestjs/schedule - Cron job scheduling

πŸ“‹ Prerequisites

  • Node.js >= 16.0.0
  • npm >= 8.0.0
  • MySQL >= 8.0
  • Redis >= 6.0

πŸš€ Quick Start

1. Clone Repository

git clone <repository-url>
cd task-manager-crud

2. Install Dependencies

npm install

3. Environment Configuration

Create environment files:

.env.stage.dev

# Database Configuration
DB_HOST=localhost
DB_PORT=3306
DB_USERNAME=root
DB_PASSWORD=your_password
DB_DATABASE=task_manager_dev

# JWT Configuration
JWT_SECRET=your-super-secret-jwt-key-here

# Redis Configuration
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_PASSWORD=
REDIS_DB=0

# Email Configuration
EMAIL_USER=your-gmail@gmail.com
EMAIL_PASS=your-app-password

# Application
PORT=3001
STAGE=dev

4. Database Setup

# Create MySQL database
mysql -u root -p
CREATE DATABASE task_manager_dev;

5. Email Setup (Gmail)

  1. Enable 2-Factor Authentication on your Gmail account
  2. Generate App Password:
    • Go to Google Account settings
    • Security β†’ App passwords
    • Generate password for "Mail"
  3. Update environment variables with Gmail credentials

6. Start Services

# Start Redis (if using local Redis)
redis-server

# Start MySQL service
# On macOS: brew services start mysql
# On Linux: sudo systemctl start mysql

7. Run Application

# Development mode with hot reload
npm run start:dev

# Production mode
npm run start:prod

# Debug mode
npm run start:debug

The API will be available at http://localhost:3001

πŸ“§ Email Features

Automated Weekly Reports

The application automatically sends weekly task summaries to users every Monday at 8:00 AM:

  • Recipients: All registered users with email addresses
  • Content: List of OPEN tasks for each user
  • Format: HTML email with professional styling
  • Template: Handlebars-based customizable templates

Email Configuration

  • Provider: Gmail SMTP (configurable)
  • Authentication: App passwords (secure)
  • Templates: Located in src/modules/tasks/templates/
  • Scheduling: Cron-based automated delivery

Customizing Email Templates

Edit the Handlebars template at src/modules/tasks/templates/weekly-tasks.hbs:

<h1>Hello {{user.username}},</h1>
<p>Here's your weekly summary of tasks that are still OPEN:</p>
<ul>
  {{#each tasks}}
    <li>{{this.title}} - {{this.description}}</li>
  {{/each}}
</ul>
<p>Keep up the good work!</p>

Manual Email Testing

// In TaskMailerService
async sendTestEmail(userEmail: string) {
  await this.mailerService.sendMail({
    to: userEmail,
    subject: 'Test Email',
    template: 'weekly-tasks',
    context: { user: { username: 'Test User' }, tasks: [] }
  });
}

πŸ“š API Documentation

Interactive Documentation

Visit http://localhost:3001/api for Swagger UI with interactive API documentation.

Authentication

All task endpoints require JWT authentication. Include the token in the Authorization header:

Authorization: Bearer <your-jwt-token>

Core Endpoints

Authentication

POST /auth/signin          # Sign in user

User Management

GET  /users/:id           # Get user by ID
POST /users               # Create new user

Task Management

GET    /tasks             # Get all tasks (with optional filters)
GET    /tasks/:id         # Get specific task
POST   /tasks             # Create new task
PATCH  /tasks/:id/status  # Update task status
DELETE /tasks/:id         # Delete task

Request/Response Examples

Create User

curl -X POST http://localhost:3000/users \
  -H "Content-Type: application/json" \
  -d '{
    "username": "john_doe",
    "password": "password123"
  }'

Sign In

curl -X POST http://localhost:3000/auth/signin \
  -H "Content-Type: application/json" \
  -d '{
    "username": "john_doe",
    "password": "password123"
  }'

Create Task

curl -X POST http://localhost:3000/tasks \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer <your-token>" \
  -d '{
    "title": "Complete project documentation",
    "description": "Write comprehensive README and API docs"
  }'

Filter Tasks

# Filter by status
GET /tasks?status=OPEN

# Search tasks
GET /tasks?search=documentation

# Combined filters
GET /tasks?status=IN_PROGRESS&search=project

πŸ§ͺ Testing

Run Tests

# Unit tests
npm test

# E2E tests
npm run test:e2e

# Test coverage
npm run test:cov

# Watch mode
npm run test:watch

# Specific test files
npm test -- --testPathPatterns="auth|users|tasks"

Test Coverage

  • 27 Unit Tests - Services and controllers
  • 100% Core Functionality - All modules covered
  • Authentication Flow - Complete auth testing
  • Caching Logic - Redis cache hit/miss scenarios
  • Email Service - Mailing functionality testing
  • Scheduled Jobs - Cron job execution testing
  • Error Handling - 401, 404, 400 responses
  • Rate Limiting - Throttling verification

πŸ—οΈ Project Structure

src/
β”œβ”€β”€ app.module.ts              # Root application module
β”œβ”€β”€ main.ts                    # Application entry point
β”œβ”€β”€ common/                    # Shared utilities
β”‚   β”œβ”€β”€ decorators/           # Custom decorators
β”‚   β”œβ”€β”€ enums/               # Application enums
β”‚   └── interfaces/          # TypeScript interfaces
β”œβ”€β”€ config/                   # Configuration files
β”‚   └── redis.config.ts      # Redis configuration
β”œβ”€β”€ modules/                 # Feature modules
β”‚   β”œβ”€β”€ auth/               # Authentication module
β”‚   β”‚   β”œβ”€β”€ auth.controller.ts
β”‚   β”‚   β”œβ”€β”€ auth.controller.spec.ts
β”‚   β”‚   β”œβ”€β”€ auth.service.ts
β”‚   β”‚   β”œβ”€β”€ auth.service.spec.ts
β”‚   β”‚   β”œβ”€β”€ auth.module.ts
β”‚   β”‚   β”œβ”€β”€ dto/           # Data Transfer Objects
β”‚   β”‚   └── strategies/    # Passport strategies
β”‚   β”œβ”€β”€ users/             # User management module
β”‚   β”‚   β”œβ”€β”€ users.controller.ts
β”‚   β”‚   β”œβ”€β”€ users.controller.spec.ts
β”‚   β”‚   β”œβ”€β”€ users.service.ts
β”‚   β”‚   β”œβ”€β”€ users.service.spec.ts
β”‚   β”‚   β”œβ”€β”€ users.module.ts
β”‚   β”‚   β”œβ”€β”€ user.entity.ts
β”‚   β”‚   β”œβ”€β”€ user.repository.ts
β”‚   β”‚   └── dto/
β”‚   β”œβ”€β”€ tasks/             # Task management module
β”‚   β”‚   β”œβ”€β”€ tasks.controller.ts
β”‚   β”‚   β”œβ”€β”€ tasks.controller.spec.ts
β”‚   β”‚   β”œβ”€β”€ tasks.service.ts
β”‚   β”‚   β”œβ”€β”€ tasks.service.spec.ts
β”‚   β”‚   β”œβ”€β”€ tasks-mailer.service.ts
β”‚   β”‚   β”œβ”€β”€ tasks.module.ts
β”‚   β”‚   β”œβ”€β”€ tasks.entity.ts
β”‚   β”‚   β”œβ”€β”€ tasks.repository.ts
β”‚   β”‚   β”œβ”€β”€ dto/
β”‚   β”‚   └── templates/
β”‚   β”‚       └── weekly-tasks.hbs
β”‚   └── redis/             # Redis module
β”‚       β”œβ”€β”€ redis.service.ts
β”‚       β”œβ”€β”€ redis.module.ts
β”‚       └── redis-pubsub.service.ts

πŸ”§ Development

Code Quality

# Lint code
npm run lint

# Format code
npm run format

# Type checking
npm run build

Git Hooks

Pre-commit hooks automatically run:

  • ESLint for code quality
  • Prettier for formatting

Environment Variables

The application uses different configurations for different environments:

  • Development: .env.stage.dev
  • Production: .env.stage.prod
  • Test: Uses in-memory/test configurations

πŸ“Š Performance Features

Redis Caching

  • Cache Strategy: Cache-first with fallback to database
  • Smart Invalidation: Automatic cache cleanup on data changes
  • Performance Gain: faster response times for cached data

Rate Limiting

  • Multi-tier Protection:
    • Short: 3 requests/second
    • Medium: 20 requests/10 seconds
    • Long: 100 requests/minute

⏰ Scheduled Jobs

Weekly Email Reports

  • Schedule: Every Monday at 8:00 AM
  • Cron Expression: '00 08 * * 1'
  • Function: Sends task summaries to all users
  • Conditions: Only sends if user has OPEN tasks

Custom Scheduled Jobs

Add new scheduled jobs using NestJS Schedule:

@Cron('0 0 * * *') // Daily at midnight
async dailyCleanup() {
  // Your cleanup logic here
}

@Interval(10000) // Every 10 seconds
async healthCheck() {
  // Health check logic
}

πŸ”’ Security Features

  • Password Hashing: bcrypt with salt rounds
  • JWT Security: Configurable expiration and refresh
  • Input Validation: Comprehensive DTO validation
  • SQL Injection Protection: Parameterized queries via TypeORM
  • Rate Limiting: Multi-tier request throttling
  • Email Security: App passwords and secure SMTP

πŸš€ Key Features Summary

Feature Description Technology
πŸ“ Task Management Complete CRUD operations NestJS + TypeORM
πŸ” Authentication JWT-based secure auth Passport + JWT
πŸš€ Caching High-performance caching Redis
πŸ“§ Email Notifications Automated weekly reports Nodemailer + Handlebars
⏰ Scheduled Jobs Cron-based automation @nestjs/schedule
πŸ›‘οΈ Rate Limiting Multi-tier protection @nestjs/throttler
πŸ“š API Docs Interactive documentation Swagger
πŸ§ͺ Testing Comprehensive test suite Jest

⚑ Built with NestJS, TypeScript, Redis, and automated email notifications for maximum performance and user engagement.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published