Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/JFKoryy/autopart-pro/llms.txt

Use this file to discover all available pages before exploring further.

AutoPart Pro uses a lightweight, server-side event pipeline to detect when a product’s stock level drops to a critical threshold. Whenever an admin updates a product through PUT /api/products/:id, the controller re-reads the saved record and compares stock against min_stock. If the current stock is at or below the threshold the controller emits a low-stock event on a shared Node.js EventEmitter singleton. In the current implementation the event triggers a console log; the architecture is intentionally designed to be extended with real-time push notifications in a future iteration.

The stockEmitter Module

backend/src/events/stockEmitter.js exports a single EventEmitter instance. Because Node.js caches require() calls, every file that imports this module receives the same object — making it a singleton across the entire server process.
// backend/src/events/stockEmitter.js
const EventEmitter = require('events');

const stockEmitter = new EventEmitter();

module.exports = stockEmitter;

The Listener in app.js

app.js registers the low-stock listener once at startup. It receives the product object (containing at minimum name, sku, and stock) and logs a formatted warning to the server console:
// backend/src/app.js
const stockEmitter = require('./events/stockEmitter');

stockEmitter.on('low-stock', (product) => {
  console.log(
    `⚠️ Alerta de stock bajo: "${product.name}" (SKU: ${product.sku}) tiene stock de ${product.stock}.`
  );
});

When the Event Fires

The updateProduct controller emits the event immediately after a successful PUT /api/products/:id save. It fetches the freshly updated record from the database and checks the threshold:
// backend/src/controllers/productController.js — updateProduct
const updatedProduct = await ProductModel.getById(id);

if (updatedProduct.stock <= updatedProduct.min_stock) {
  stockEmitter.emit('low-stock', updatedProduct);
}
The event fires whenever stock is at or below min_stock — including exactly equal. For example, if min_stock is 5 and you update stock to 5, the alert triggers.
Each product has its own min_stock value set at creation time, so you can configure a higher threshold for fast-moving SKUs (e.g. min_stock: 20 for a popular oil filter) and a lower threshold for slow-moving specialty parts (e.g. min_stock: 2).

The min_stock Field

min_stock defaults to 5 at the database level and is set when a product is created via POST /api/products. The productModel.update() function does not currently include min_stock as an updatable field — only sku, name, brand, compatible_cars, price, stock, image_url, description, and category can be changed through PUT /api/products/:id. The alert threshold is therefore fixed at the value supplied during product creation. To trigger a low-stock event, update stock to a value at or below the product’s existing min_stock:
curl -X PUT http://localhost:5000/api/products/42 \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer <ADMIN_TOKEN>" \
  -d '{"stock": 4}'
In this example, if min_stock is 5, the save operation will immediately emit low-stock because 4 <= 5.

The GET /api/products/low-stock Endpoint

In addition to real-time event emission, any authenticated user can poll the low-stock list at any time. The endpoint queries all products where stock <= min_stock:
SELECT name, stock FROM products WHERE stock <= min_stock;
curl http://localhost:5000/api/products/low-stock \
  -H "Authorization: Bearer <TOKEN>"
Response:
{
  "success": true,
  "data": [
    { "name": "Filtro de aceite", "stock": 3 },
    { "name": "Pastilla de freno trasera", "stock": 5 }
  ]
}
The GET endpoint returns only name and stock (as defined by the getLowStock query). The full product record — including sku, min_stock, price, and other fields — is available from GET /api/products/:id if you need the complete detail for a flagged item.

Event Lifecycle Summary

Admin sends PUT /api/products/:id


validateProductData middleware (SKU normalisation, type checks)


ProductModel.update(id, body)  ──► MySQL UPDATE products SET ...


ProductModel.getById(id)       ──► re-reads updated row


stock <= min_stock?
   YES  ──► stockEmitter.emit('low-stock', updatedProduct)


           app.js listener  ──► console.log(⚠️ alert)
   NO   ──► (no event)

Frontend Visibility

The Inventory page (/admin/inventario) surfaces low-stock items inline: each row in the product table renders the stock count as a badge. When stock <= min_stock the badge switches to a red background and prepends an AlertTriangle icon so warehouse staff can spot critical items at a glance without querying the API directly.

Extending the Alert System

The current implementation logs low-stock events to the server console. The project roadmap includes integrating Socket.io for real-time browser push notifications and optionally triggering serverless functions (e.g. AWS Lambda) to send email or SMS alerts when stock falls below threshold. Because all alert logic is isolated behind the stockEmitter singleton, adding new subscribers requires only a new stockEmitter.on('low-stock', handler) call — no changes to the controllers or models are needed.

Inventory Management

Learn about the min_stock field, how products are updated, and the full product data model.

User Roles

See which roles can access the low-stock endpoint and update product thresholds.

Build docs developers (and LLMs) love