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 throughDocumentation 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.
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.
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:
When the Event Fires
TheupdateProduct 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:
min_stock — including exactly equal. For example, if min_stock is 5 and you update stock to 5, the alert triggers.
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:
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:
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
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.Related Pages
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.