deckhearth/lib/pricing/card-price.js
Randall Stillwell d0468015eb
Some checks failed
CI / Lint (pull_request) Has been cancelled
CI / Schema map up to date (pull_request) Has been cancelled
CI / Forbidden patterns (9 checks) (pull_request) Has been cancelled
CI / Migrations apply (node-pg-migrate) (pull_request) Has been cancelled
Visual diff / Should run? (pull_request) Has been cancelled
CI / Unit tests (vitest) (pull_request) Has been cancelled
Convoy metrics gate / Require role-event telemetry on convoy PRs (pull_request) Has been cancelled
PR Health rollup / Aggregate gate status (pull_request) Has been cancelled
Preview smoke / Should run? (pull_request) Has been cancelled
Preview smoke / Playwright smoke (pull_request) Has been cancelled
Visual diff / Screenshot diff (pull_request) Has been cancelled
feat(pricing): convoy for online prices and local stores
Add pricing schema, free API integrations (MTG bulk, TCGdex, Foolio),
valuation and sort-by-price, card detail pricing UI, and nearby store
discovery via home zip and OSM geo clients.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-08 22:47:46 -05:00

31 lines
1.1 KiB
JavaScript

/** Hours after which a stored price is considered stale for live refresh. */
export const PRICE_STALE_HOURS = 24;
/**
* Resolve the unit price for a owned card row (foil-aware).
* MTG: current_price = nonfoil USD, market_price = foil USD (Scryfall mapping).
*
* @param {{ currentPrice?: number | null, marketPrice?: number | null, isFoil?: boolean }} params
* @returns {number | null}
*/
export function resolveUnitPrice({ currentPrice, marketPrice, isFoil = false }) {
const current = currentPrice != null ? Number(currentPrice) : null;
const market = marketPrice != null ? Number(marketPrice) : null;
if (isFoil) {
return market ?? current ?? null;
}
return current ?? market ?? null;
}
/**
* @param {Date | string | null | undefined} pricesUpdatedAt
* @returns {boolean}
*/
export function isPriceStale(pricesUpdatedAt) {
if (!pricesUpdatedAt) return true;
const updated = pricesUpdatedAt instanceof Date ? pricesUpdatedAt : new Date(pricesUpdatedAt);
if (Number.isNaN(updated.getTime())) return true;
const ageMs = Date.now() - updated.getTime();
return ageMs > PRICE_STALE_HOURS * 60 * 60 * 1000;
}