A robust backend service for managing agricultural storage facility bookings, built with Node.js, TypeScript, and Prisma. This service connects farmers with storage facility operators to facilitate efficient storage of agricultural produce.
AgriCon provides a secure and scalable API for managing bookings between farmers and storage facility operators. The system enables farmers to book storage facilities for their agricultural produce while providing operators with tools to manage their facilities and bookings.
- User Management: Support for farmers and facility operators with role-based authentication
- Facility Management: Operators can register and manage storage facilities with pricing and availability
- Booking System: Farmers can book storage facilities with flexible date ranges
- Payment Tracking: Integrated payment status tracking for bookings
- Role-based Authorization: Secure access control based on user roles
- Database Integration: PostgreSQL database with Prisma ORM
- Runtime: Node.js
- Language: TypeScript
- Framework: Express.js
- Database: PostgreSQL
- ORM: Prisma
- Authentication: JWT
- Testing: Jest
- Validation: Express Validator
- Deployment: Render
- Node.js β₯ 16.x
- npm or yarn
- PostgreSQL database
- Prisma CLI
1οΈβ£ Clone the repository:
git clone https://github.com/InternPulse/agricon-express-backend.git2οΈβ£ Change into the directory:
cd agricon-express-backend3οΈβ£ Install dependencies:
npm install4οΈβ£ Set up environment variables:
Create a .env file in the root directory with the following variables:
# Database connection string
DATABASE_URL="postgresql://username:password@localhost:5432/agricon_db"
# JWT secret for authentication
JWT_SECRET="your-secret-key-here"
# Server port
PORT=4000
# Environment
NODE_ENV=development
# Render deployment (optional)
RENDER_SERVICE_ID=""
RENDER_API_KEY=""5οΈβ£ Set up the database:
# Generate Prisma client
npx prisma generate
# Run database migrations
npx prisma migrate deploy
# (Optional) Seed with mock data
npm run seed6οΈβ£ Start the development server:
npm run devThe API will be accessible at http://localhost:4000
http://localhost:4000/api/v1
Most endpoints require authentication via JWT token in the Authorization header:
Authorization: Bearer <your-jwt-token>
This API implements comprehensive rate limiting to prevent abuse and ensure fair usage. Different endpoints have specific rate limits based on their usage patterns:
| Endpoint Type | Rate Limit | Window | Description |
|---|---|---|---|
| General | 100 requests | 15 minutes | Applied globally to all endpoints |
| Booking Operations | 10 requests | 1 hour | For booking creation, updates, and management |
| Facility Operations | 20 requests | 15 minutes | For facility management and queries |
| Health Check | 30 requests | 1 minute | For health monitoring endpoints |
| Database Operations | 5 requests | 5 minutes | For database initialization and maintenance |
When rate limits are exceeded, the API returns:
- Status Code:
429 Too Many Requests - Headers:
RateLimit-*headers with limit information - Response: JSON with error message and retry time
{
"success": false,
"message": "Too many requests from this IP, please try again later.",
"retryAfter": "15 minutes"
}GET /api/v1/health - Health check endpoint
POST /api/v1/init-db - Initialize database connection
POST /api/v1/facilities - Create a new storage facility (Operator only)
GET /api/v1/facilities - List all facilities
GET /api/v1/facilities/:facilityId - Get specific facility details
PUT /api/v1/facilities/:facilityId - Update facility (Facility owner only)
DELETE /api/v1/facilities/:facilityId - Remove facility (Facility owner only)
POST /api/v1/bookings - Create a new booking (Farmer only)
GET /api/v1/bookings/farmer/me - Get farmer's bookings
GET /api/v1/bookings/operator/me - Get operator's facility bookings
GET /api/v1/bookings/:bookingId - Get specific booking details
PATCH /api/v1/bookings/:bookingId - Update booking
DELETE /api/v1/bookings/:bookingId - Cancel booking
PATCH /api/v1/bookings/:bookingId/expire - Expire booking
The application uses the following main entities:
- users_user: Core user accounts with email, password, and role
- Farmer: Farmer profiles with personal information
- Operator: Facility operator profiles with business information
- Facility: Storage facilities with location, type, capacity, and pricing
- FacilityType: Enum (DRYER, STORAGE, PROCESSING, OTHERS)
- Booking: Storage bookings with dates, amounts, and payment status
- Transaction: Payment transactions linked to bookings
Run the test suite:
npm testThe application is configured for deployment on Render. The deployment process:
- Builds the TypeScript code
- Fixes import statements for production
- Starts the application on the configured port
# Development build
npm run build
# Production build for Render
npm run build:render
# Start production server
npm startnpm run dev # Start development server with hot reload
npm run start # Start production server
npm run start:dev # Start development server without hot reload
npm run build # Build TypeScript to JavaScript
npm run build:render # Build for Render deployment
npm run test # Run tests
npm run lint # Run ESLint
npm run seed # Seed database with mock data
npm run migrate # Run database migrationssrc/
βββ config/ # Configuration files
βββ controllers/ # Request handlers
βββ middlewares/ # Express middlewares
βββ routes/ # API route definitions
βββ services/ # Business logic
βββ types/ # TypeScript type definitions
βββ utils/ # Utility functions
βββ errors/ # Error handling
βββ __tests__/ # Test files
βββ app.ts # Express app configuration
βββ index.ts # Application entry point
- JWT-based authentication
- Role-based authorization (Farmer, Operator, Admin)
- Input validation using Express Validator
- Secure password handling
- CORS protection
- Request rate limiting
- Fork this repository
- Create your feature branch (
git checkout -b feat/feature-name) - Commit your changes (
git commit -m 'Add feature') - Push to the branch (
git push origin feat/feature-name) - Open a Pull Request
This project is licensed under the ISC License.
If you encounter any issues, please report them on the GitHub Issues page.