mirror of
https://github.com/clucraft/PriceGhost.git
synced 2026-05-18 13:55:16 +02:00
Add out-of-stock detection and display
- Add stock_status column to products table (in_stock/out_of_stock/unknown) - Detect out-of-stock status on Amazon by checking: - #availability text for "currently unavailable" - #outOfStock element presence - Missing "Add to Cart" button - Add generic stock status detection for other sites - Allow adding out-of-stock products (they just won't have a price) - Update background scheduler to track stock status changes - Display stock status badge in product list and detail pages - Dim out-of-stock products in the dashboard - Show "Currently Unavailable" badge instead of price when out of stock Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
bf111e13d8
commit
8c5d20707d
9 changed files with 274 additions and 44 deletions
|
|
@ -1,6 +1,6 @@
|
|||
import cron from 'node-cron';
|
||||
import { productQueries, priceHistoryQueries } from '../models';
|
||||
import { scrapePrice } from './scraper';
|
||||
import { scrapeProduct } from './scraper';
|
||||
|
||||
let isRunning = false;
|
||||
|
||||
|
|
@ -22,25 +22,35 @@ async function checkPrices(): Promise<void> {
|
|||
try {
|
||||
console.log(`Checking price for product ${product.id}: ${product.url}`);
|
||||
|
||||
const priceData = await scrapePrice(product.url);
|
||||
const scrapedData = await scrapeProduct(product.url);
|
||||
|
||||
if (priceData) {
|
||||
// Update stock status
|
||||
if (scrapedData.stockStatus !== product.stock_status) {
|
||||
await productQueries.updateStockStatus(product.id, scrapedData.stockStatus);
|
||||
console.log(
|
||||
`Stock status changed for product ${product.id}: ${product.stock_status} -> ${scrapedData.stockStatus}`
|
||||
);
|
||||
}
|
||||
|
||||
if (scrapedData.price) {
|
||||
// Get the latest recorded price to compare
|
||||
const latestPrice = await priceHistoryQueries.getLatest(product.id);
|
||||
|
||||
// Only record if price has changed or it's the first entry
|
||||
if (!latestPrice || latestPrice.price !== priceData.price) {
|
||||
if (!latestPrice || latestPrice.price !== scrapedData.price.price) {
|
||||
await priceHistoryQueries.create(
|
||||
product.id,
|
||||
priceData.price,
|
||||
priceData.currency
|
||||
scrapedData.price.price,
|
||||
scrapedData.price.currency
|
||||
);
|
||||
console.log(
|
||||
`Recorded new price for product ${product.id}: ${priceData.currency} ${priceData.price}`
|
||||
`Recorded new price for product ${product.id}: ${scrapedData.price.currency} ${scrapedData.price.price}`
|
||||
);
|
||||
} else {
|
||||
console.log(`Price unchanged for product ${product.id}`);
|
||||
}
|
||||
} else if (scrapedData.stockStatus === 'out_of_stock') {
|
||||
console.log(`Product ${product.id} is out of stock, no price available`);
|
||||
} else {
|
||||
console.warn(`Could not extract price for product ${product.id}`);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue