Add stock status history tracking and timeline visualization

- Create stock_status_history table to track status changes over time
- Add stockStatusHistoryQueries with getByProductId, recordChange, getStats
- Update scheduler to record status changes
- Update product creation and manual refresh to record initial/changed status
- Add GET /products/:id/stock-history API endpoint
- Create StockTimeline component with:
  - Visual timeline bar showing in-stock (green) vs out-of-stock (red)
  - Availability percentage
  - Outage count and duration stats
- Integrate timeline into ProductDetail page

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
clucraft 2026-01-22 14:23:55 -05:00
parent 4928d6b9d3
commit 6c2aece1e8
8 changed files with 528 additions and 6 deletions

View file

@ -1,5 +1,5 @@
import cron from 'node-cron';
import { productQueries, priceHistoryQueries, userQueries } from '../models';
import { productQueries, priceHistoryQueries, userQueries, stockStatusHistoryQueries } from '../models';
import { scrapeProduct } from './scraper';
import { sendNotifications, NotificationPayload } from './notifications';
@ -29,9 +29,13 @@ async function checkPrices(): Promise<void> {
const wasOutOfStock = product.stock_status === 'out_of_stock';
const nowInStock = scrapedData.stockStatus === 'in_stock';
// Update stock status
// Update stock status and record to history
if (scrapedData.stockStatus !== product.stock_status) {
await productQueries.updateStockStatus(product.id, scrapedData.stockStatus);
// Record the status change in history
await stockStatusHistoryQueries.recordChange(product.id, scrapedData.stockStatus);
console.log(
`Stock status changed for product ${product.id}: ${product.stock_status} -> ${scrapedData.stockStatus}`
);