A RESTful API built with NestJS that consumes and synchronizes product data from Contentful, providing endpoints for product management and reporting.
- 🔐 JWT Authentication
- 📦 Product Management
- 📊 Reporting System
- 🔄 Contentful Synchronization
- 📝 Swagger Documentation
- 🧪 Test Coverage
- 🐳 Docker Support
- Node.js (v22 or higher)
- Docker and Docker Compose
- PostgreSQL
- Contentful Account
Create a .env file in the root directory with the following variables:
# Contentful Configuration
CONTENTFUL_SPACE_ID=your_space_id
CONTENTFUL_ACCESS_TOKEN=your_access_token
CONTENTFUL_ENVIRONMENT=master
CONTENTFUL_CONTENT_TYPE=product
# Database Configuration
DB_HOST=localhost
DB_PORT=5432
DB_USERNAME=postgres
DB_PASSWORD=postgres
DB_DATABASE=contentful_rest
# JWT Configuration
JWT_SECRET=your_jwt_secret- Clone the repository:
git clone https://github.com/ChoqueCastroLD/contentful-consumer
cd contentful-consumer- Install dependencies:
npm install- Start the application using Docker:
docker-compose up -dBuild and run the application using Docker:
docker-compose up -dnpm run build- Build the applicationnpm run format- Format code using Prettiernpm run start- Start the application in production modenpm run start:dev- Start the application in development modenpm run start:debug- Start the application in debug modenpm run start:prod- Start the application in production modenpm run lint- Run ESLintnpm run test- Run testsnpm run test:watch- Run tests in watch modenpm run test:cov- Run tests with coveragenpm run test:debug- Run tests in debug modenpm run test:e2e- Run end-to-end testsnpm run generate:token- Generate a JWT token for authenticationnpm run sync:products- Synchronize products from Contentful
To run scripts inside the Docker container:
# Generate JWT token
docker-compose exec app npm run gen:token
# Synchronize products
# You should run this one while the container is running to get a initial batch of data
docker-compose exec app npm run sync:productsOnce the application is running, you can access the Swagger documentation at:
http://localhost:3000/api/docs
- Generate a JWT token:
npm run gen:token- Use the token in your requests:
Authorization: Bearer <your_token>
-
GET /products- Get all products with optional filters- Query Parameters:
name(string, optional) - Filter by product namecategory(string, optional) - Filter by categoryminPrice(number, optional) - Minimum pricemaxPrice(number, optional) - Maximum pricepage(number, optional, default: 1) - Page number for paginationlimit(number, optional, default: 10) - Items per pagesearch(string, optional) - Search querystartDate(string, optional) - Start date for filteringendDate(string, optional) - End date for filtering
- Query Parameters:
-
DELETE /products/:id- Soft delete a product by ID
GET /reports/deleted-products-percentage- Get percentage of deleted productsGET /reports/non-deleted-products-percentage- Get percentage of non-deleted products with filters- Query Parameters:
priceMin(number, optional) - Minimum pricepriceMax(number, optional) - Maximum pricestartDate(string, optional) - Start dateendDate(string, optional) - End date
- Query Parameters:
GET /reports/price-range-stats- Get product statistics by price ranges- Query Parameters:
ranges(string, optional) - Comma-separated price ranges (e.g., "0-100,101-500,501-1000")
- Query Parameters:
src/
├── auth/ # Authentication module
├── modules/
│ ├── public/ # Public endpoints
│ │ └── products/ # Product management
│ ├── private/ # Private endpoints
│ │ └── reports/ # Reporting system
│ └── common/ # Shared services
├── scheduler/ # Scheduled tasks
└── scripts/ # Utility scripts
Current test coverage report (as of latest update):
| Metric | Coverage |
|---|---|
| Statements | 56.34% |
| Branches | 54.16% |
| Functions | 63.82% |
| Lines | 55.18% |
| File | Statements | Branches | Functions | Lines |
|---|---|---|---|---|
| reports.service.ts | 100% | 100% | 100% | 100% |
| reports.controller.ts | 100% | 100% | 100% | 100% |
| app.controller.ts | 100% | 100% | 100% | 100% |
| auth.service.ts | 92.85% | 66.66% | 100% | 91.66% |
| product.dto.ts | 100% | 100% | 100% | 100% |
| scheduler.service.ts | 100% | 100% | 100% | 100% |
| File | Statements | Branches | Functions | Lines |
|---|---|---|---|---|
| products.service.ts | 74.46% | 61.29% | 100% | 72.09% |
| filter-products.dto.ts | 76.47% | 100% | 0% | 76.47% |
To run the coverage report:
npm run test:cov