Skip to content

Farmproduct-app Backend is a RESTful API built with Node.js, TypeScript, and Sequelize, designed to manage users, authentication, and product data for food and agriculture-related applications. It provides secure endpoints for user registration, OTP-based authentication, and CRUD operations on products, supporting scalable and maintainable backend

License

Notifications You must be signed in to change notification settings

frckbrice/farming-product-REST-api

Repository files navigation

🌾 Farming Products REST API

Node.js TypeScript PostgreSQL Docker CI/CD Test Coverage

Farming Product
Farming Product

πŸš€ Live Demo

πŸ“‹ Table of Contents

🎯 Overview

This project demonstrates a production-ready REST API for a farming marketplace platform. It showcases modern software development practices including:

  • Microservices Architecture with clean separation of concerns
  • TypeScript for type safety and better developer experience
  • PostgreSQL with Sequelize ORM for robust data management
  • JWT Authentication with secure token-based sessions
  • File Upload with Cloudinary integration
  • Real-time Notifications using Expo Push Notifications
  • Payment Processing with Adwa payment gateway
  • Comprehensive Testing with 80%+ code coverage
  • Docker Containerization for consistent deployment
  • CI/CD Pipeline with automated testing and deployment

✨ Key Features

πŸ” Authentication & Security

  • JWT-based authentication with refresh tokens
  • Role-based access control (Farmers, Buyers, Admins)
  • Input validation with Zod schemas
  • Rate limiting and security middleware
  • Password hashing with bcrypt

πŸ›’ Functionality

  • Product catalog with search and filtering
  • Order management system
  • Secure payment processing
  • User reviews and ratings
  • Real-time order tracking

πŸ“± User Experience

  • Responsive web interface
  • Push notifications for order updates
  • File upload for product images
  • User profile management
  • Custom 404 error pages

πŸ—οΈ Developer Experience

  • Comprehensive API documentation (Swagger/OpenAPI)
  • Automated testing with Vitest
  • Code quality tools (ESLint, Prettier)
  • Hot reloading for development
  • Detailed error logging

πŸ› οΈ Tech Stack

Backend

  • Runtime: Node.js 20
  • Language: TypeScript 5.0
  • Framework: Express.js
  • Database: PostgreSQL 15
  • ORM: Sequelize with TypeScript
  • Authentication: JWT + bcrypt
  • Validation: Zod schemas
  • File Upload: Multer + Cloudinary

Testing & Quality

  • Testing Framework: Vitest
  • HTTP Testing: Supertest
  • Code Coverage: V8 Coverage
  • Linting: ESLint
  • Formatting: Prettier

DevOps & Deployment

  • Containerization: Docker + Docker Compose
  • CI/CD: GitHub Actions
  • Database Migrations: Sequelize CLI
  • Environment Management: dotenv

External Services

  • Cloud Storage: Cloudinary
  • Push Notifications: Expo
  • Payment Gateway: Adwa
  • Email Service: Nodemailer

πŸ›οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Client Apps   β”‚    β”‚   Web Browser   β”‚    β”‚   Mobile Apps   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜
          β”‚                      β”‚                      β”‚
          β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                 β”‚
                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚    Express.js Server      β”‚
                    β”‚   (TypeScript + Node.js)  β”‚
                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                  β”‚
                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚    Business Logic Layer   β”‚
                    β”‚  (Controllers + Services) β”‚
                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                  β”‚
                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚    Data Access Layer      β”‚
                    β”‚   (Sequelize + Models)    β”‚
                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                  β”‚
                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚      PostgreSQL DB        β”‚
                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸš€ Quick Start

Prerequisites

  • Node.js 20+
  • Docker & Docker Compose
  • PostgreSQL (or use Docker)

1. Clone & Install

git clone <repository-url>
cd farming-product-REST-api
yarn install

2. Environment Setup

cp .env.example .env
# Edit .env with your configuration

3. Run with Docker (Recommended)

# Start all services
docker-compose up -d

# View logs
docker-compose logs -f app

# Stop services
docker-compose down

4. Run Locally

# Start database
docker-compose up db -d

# Run migrations
npx sequelize-cli db:migrate

# Start development server
yarn dev

πŸ“‘ API Endpoints

Authentication

POST   /api/v1/auth/signup     # User registration
POST   /api/v1/auth/login      # User login
POST   /api/v1/auth/verifyOTP  # OTP verification

Products

GET    /api/v1/products        # Get all products
POST   /api/v1/products        # Create product (Farmers only)
GET    /api/v1/products/:id    # Get product by ID
PUT    /api/v1/products/:id    # Update product
DELETE /api/v1/products/:id    # Delete product

Orders & Payments

POST   /api/v1/orders          # Create order
GET    /api/v1/orders          # Get user orders
POST   /api/v1/payments        # Process payment

User Management

GET    /api/v1/users/profile   # Get user profile
PUT    /api/v1/users/profile   # Update profile
POST   /api/v1/users/upload-avatar # Upload profile picture

System

GET    /api/v1/health          # Health check with DB status
GET    /api-docs               # API documentation (Swagger)

πŸ§ͺ Testing

Run All Tests

yarn test              # Run tests
yarn coverage          # Generate coverage report
yarn vitest --ui       # Interactive test UI

Test Coverage

  • Unit Tests: 80%+ coverage
  • Integration Tests: API endpoints
  • Mocking: External services (email, payments)
  • Database: Test database with migrations

Example Test

describe('Auth Controller', () => {
  it('should create user and send OTP on successful signup', async () => {
    const response = await request(app)
      .post('/api/v1/auth/signup')
      .send({
        email: 'test@example.com',
        password: 'password123',
        role: 'farmer'
      });
    
    expect(response.status).toBe(201);
    expect(response.body).toHaveProperty('message', 'User created successfully');
  });
});

🐳 Deployment

Docker Deployment

# Build and run
docker-compose up --build -d

# Production build
docker build -t farming-api .
docker run -p 3000:3000 farming-api

Environment Variables

# Database
DATABASE_URL=postgresql://user:pass@host:5432/dbname

# JWT
JWT_SECRET=your_jwt_secret
JWT_SECRET_REFRESH=your_refresh_secret

# External Services
CLOUDINARY_CLOUD_NAME=your_cloud_name
ADWA_MERCHANT_KEY=your_merchant_key

πŸ“ Project Structure

farming-product-REST-api/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ controllers/          # API controllers
β”‚   β”‚   β”œβ”€β”€ auth.controller.ts
β”‚   β”‚   β”œβ”€β”€ product.controller.ts
β”‚   β”‚   └── user.controller.ts
β”‚   β”œβ”€β”€ middleware/           # Express middleware
β”‚   β”‚   β”œβ”€β”€ auth-check.ts
β”‚   β”‚   β”œβ”€β”€ errorHandler.ts
β”‚   β”‚   └── rateLimiter.ts
β”‚   β”œβ”€β”€ models/              # Database models
β”‚   β”‚   β”œβ”€β”€ user.ts
β”‚   β”‚   β”œβ”€β”€ product.ts
β”‚   β”‚   └── order.ts
β”‚   β”œβ”€β”€ routes/              # API routes
β”‚   β”‚   β”œβ”€β”€ auth.routes.ts
β”‚   β”‚   β”œβ”€β”€ product.routes.ts
β”‚   β”‚   └── user.routes.ts
β”‚   β”œβ”€β”€ config/              # Configuration
β”‚   β”‚   └── config.ts
β”‚   └── utils/               # Utility functions
β”‚       └── runMigrations.ts
β”œβ”€β”€ public/                  # Static files
β”‚   β”œβ”€β”€ index.html          # Welcome page
β”‚   β”œβ”€β”€ 404.html            # Custom 404 page
β”‚   └── assets/             # Static assets
β”‚       β”œβ”€β”€ styles.css
β”‚       β”œβ”€β”€ script.js
β”‚       └── images/
β”œβ”€β”€ tests/                   # Test files
β”‚   β”œβ”€β”€ controllers/
β”‚   β”œβ”€β”€ mocks/
β”‚   └── setup.ts
β”œβ”€β”€ migrations/             # Database migrations
β”œβ”€β”€ docker-compose.yml      # Docker services
β”œβ”€β”€ Dockerfile             # Docker configuration
└── README.md              # This file

🎯 Key Achievements

  • Scalable Architecture: Designed for high-traffic e-commerce operations
  • Security First: Implemented comprehensive security measures
  • Developer Experience: Full CI/CD pipeline with automated testing
  • Production Ready: Dockerized deployment with health monitoring
  • Documentation: Complete API documentation with Swagger
  • Code Quality: 80%+ test coverage with TypeScript

🀝 Contributing

  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

πŸ“„ License

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


πŸ‘¨β€πŸ’» Developer

Developed by Avom Brice

Full-Stack Developer | TypeScript | Node.js | React | DevOps


Ready to scale your e-commerce platform? This API demonstrates enterprise-level development practices with modern technologies and best practices.

About

Farmproduct-app Backend is a RESTful API built with Node.js, TypeScript, and Sequelize, designed to manage users, authentication, and product data for food and agriculture-related applications. It provides secure endpoints for user registration, OTP-based authentication, and CRUD operations on products, supporting scalable and maintainable backend

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published