Skip to content

This is the Agroplus project API, built with Node.js with Express, SequelizeORM, PostgreSQL, Docker, JWT, and entity-based MVC architecture.

License

Notifications You must be signed in to change notification settings

OscarS05/Agroplus-api

Repository files navigation

πŸ„ Agroplus-API

Agroplus-API is a GraphQL-enabled RESTful API designed to help farmers manage all relevant information about their livestock. It allows users to record, track, and manage animal data, including parentage, vaccination, deworming records, and personal notes.


Node.js Express GraphQL PostgreSQL Sequelize Docker Git GitHub Jest


πŸ§‘β€πŸ’» Tech Stack

  • Backend: Node.js + Express.js with GraphQL
  • ORM: Sequelize
  • Database: PostgreSQL
  • Container: Docker
  • Authentication: JWT with cookies
  • Docs: Swagger
  • Containerization: Docker
  • Architecture: MVC (based on entities)
  • Testing: Jest + Supertest

βš™οΈ Features

  • πŸ§‘ Register and login a user
  • πŸ„ CRUD operations for animals:
    • Include full animal details.
    • Define animal parentage (link to mother/father).
  • πŸ’‰ CRUD for vaccinations.
  • πŸ’Š CRUD for deworming treatments.
  • πŸ“ CRUD for notes.
  • Full support for GraphQL

πŸ™ˆ Planned

  • πŸ”œ : Live chat with other users using Socket.io

πŸ” Authentication

  • System: JWT access token saved in cookies.

  • Flow:

    1. User registers.
    2. On login, a unique accessToken is issued and saved in a cookie.
    3. Token has a lifespan of 60 minutes and is stored in the DB for validation.
    4. Security Note: This implementation ensures simplicity and control via token invalidation by removing it from the DB.
  • Roles: No role-based access control; every user has full access to their data.

  • Important: In GraphQL, the accessToken is implemented in the headers using "Bearer" strategy

Testing

The project has been tested in a robust way using Jest + Supertest. The implemented tests were unit tests(services) and E2E tests(endpoints).


πŸ“‚ Project Structure

api/
β”œβ”€β”€ index.js
β”œβ”€β”€ app.js
β”œβ”€β”€ config/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ graphql/
β”‚   β”‚    β”œβ”€β”€ resolvers/
β”‚   β”‚    β”‚   └── [entity]/   for example: animal/
β”‚   β”‚    β”‚        β”œβ”€β”€ animal.resolvers.js
β”‚   β”‚    β”‚        └── index.js
β”‚   β”‚    β”œβ”€β”€ schemas/
β”‚   β”‚    β”‚   β”œβ”€β”€ animal.schemas.graphql
β”‚   β”‚    β”‚   └── [entity].schemas.graphql
β”‚   β”‚    β”œβ”€β”€ resolvers.js (Centralization of resolvers)
β”‚   β”‚    └── index.js
β”‚   β”‚
β”‚   β”œβ”€β”€ middlewares/
β”‚   β”œβ”€β”€ utils/
β”‚   β”‚   └── docs/
β”‚   β”‚       └── swagger.js
β”‚   β”œβ”€β”€ store/
β”‚   β”‚   └── (DB setup, Sequelize initialization)
β”‚   β”œβ”€β”€ modules/
β”‚       β”œβ”€β”€ animal/
β”‚       β”‚   β”œβ”€β”€ animal.controller.js
β”‚       β”‚   β”œβ”€β”€ animal.routes.js
β”‚       β”‚   β”œβ”€β”€ animal.schema.js
β”‚       β”‚   β”œβ”€β”€ animal.service.js
β”‚       β”‚   └── animal.repository.js
β”‚       β”œβ”€β”€ user/
β”‚       β”œβ”€β”€ deworming/
β”‚       β”œβ”€β”€ vaccination/
β”‚       β”œβ”€β”€ note/
β”‚       └── routes.js (main router)

/tests/
β”œβ”€β”€ unit/
β”‚   └── [ServiceName].test.js   # Ej: user-service.test.js
β”‚
β”œβ”€β”€ e2e/
β”‚   └── [entity].e2e.js            # Ej: vaccination.e2e.js β€” all vaccination endpoints
  • MVC pattern is followed per module/entity.
  • Each module contains all necessary logic (schema, controller, service, repo, route).

πŸ› οΈ Installation & Setup

βœ… Requirements

  • Docker Desktop
  • Node.js (v18+ recommended)
  • NPM

βš™οΈ Steps to Run Locally

  1. Clone the repo

    git clone [email protected]:OscarS05/Agroplus-api.git
    cd agroplus-api
    
  2. Install dependencies

npm install
  1. Setup environment variables
    Copy the .env.example file to .env and update it with database values. Use the docker-compose.yml file to get the DB credentials.
cp .env.example .env
  1. Run Docker containers (for DB)
docker-compose up -d
  1. Run migrations to set up DB schema
npm run migrations:run
  1. Start the server
    for development:
npm run dev

πŸ“₯ Example Requests & Responses

Here are example requests and responses for some endpoints:

πŸ”Έ POST /animals/

Description: Creates a new animal record in the database.

Request Body (example):

{
  "livestockType": "Bovino",
  "animalType": "Vaca",
  "breed": "Angus",
  "code": "BOV-001",
  "sex": "Female",
  "father": "d0759f76-a119-4be0-ad33-e93595f56be8"
}

Response (example):

{
  "message": "Animal was successfully created",
  "success": true,
  "newAnimal": {
    "id": "c36b4f45-35f9-4d77-9669-e55eb7d33405",
    "livestockType": "Bovino",
    "animalType": "Vaca",
    "breed": "Angus",
    "code": "BOV-001",
    "sex": "Female",
    "mother": null,
    "father": "OPP-TT1",
    "birthDate": null,
    "registeredAt": "2025-04-16"
  }
}

πŸ”Έ GET /animals/vaccination/

Description: Retrieves the list of all vaccinations associated with animals.

Response (example):

{
  "vaccinations": [
    {
      "id": "065f441b-64ba-411a-97d1-7cbf15443732",
      "vaccine": "Ivermectin 0.5%",
      "description": "Updated",
      "animal": "abc-123",
      "registeredAt": "2025-04-16"
    }
  ],
  "success": true
}

πŸ§ͺ API Documentation

  • All endpoints are fully documented using Swagger. Be sure to have the right server when trying on Swagger.

πŸ“Œ Information to test the API

  • Development Swagger URL: http://localhost:4000/api-docs (port may vary depending on your .env configuration)

πŸŽ₯ Demo Video: Watch how to use the API via Swagger


πŸ‘€ Author & License

  • Author: Oscar Santiago Monsalve
  • GitHub: OscarS05
  • License: Creative Commons BY-NC 4.0

About

This is the Agroplus project API, built with Node.js with Express, SequelizeORM, PostgreSQL, Docker, JWT, and entity-based MVC architecture.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •