Session 28: Parlay builder, line movement tracker, book comparison — 3 features, zero credits (1623 tests)

This commit is contained in:
Kev
2026-06-13 12:37:08 -04:00
parent 66fafd8429
commit c48aecd510
23 changed files with 1567 additions and 1 deletions
+78
View File
@@ -0,0 +1,78 @@
'use strict';
/**
* /api/books (Session 28)
*
* Book comparison views over the CACHED odds props — zero odds-api
* credits (it never triggers a fetch; it reads what's already cached).
*
* GET /api/books/:sport → best lines tonight (sorted by savings)
* GET /api/books/:sport/:player/:stat → book-by-book for one prop
*
* `?side=over|under` selects which side to optimize (default over).
*/
const express = require('express');
const bookComparison = require('../services/bookComparisonService');
const { cacheGet } = require('../utils/redis');
const router = express.Router();
const MISSION_HEADER = { 'X-VYNDR-Mission': 'Never leave money on the table' };
const SUPPORTED = new Set(['nba', 'wnba', 'mlb', 'soccer', 'nfl', 'nhl']);
// Read cached grouped props for a sport without triggering a fetch.
// oddsService caches `odds:{sport}:{utcDate}` = { updated_at, props, spreads }.
async function readCachedProps(sport) {
const utcDate = new Date().toISOString().split('T')[0];
const cache =
(await cacheGet(`odds:${sport}:${utcDate}`)) ??
(await cacheGet(`odds:${sport}`));
if (!cache) return [];
return Array.isArray(cache.props) ? cache.props : [];
}
router.get('/:sport', async (req, res) => {
const sport = String(req.params.sport || '').toLowerCase();
if (!SUPPORTED.has(sport)) {
return res.status(404).set(MISSION_HEADER).json({ error: `No book comparison for sport: ${sport}` });
}
const side = req.query.side === 'under' ? 'under' : 'over';
const limit = req.query.limit ? Math.max(0, parseInt(req.query.limit, 10) || 0) : 20;
try {
const props = await readCachedProps(sport);
const lines = bookComparison.bestLines(props, { side, limit });
return res.set(MISSION_HEADER).json({ sport, side, bestLines: lines, source: 'odds-cache' });
} catch (err) {
console.error(`[books/${sport}]`, err.message);
return res.set(MISSION_HEADER).json({ sport, side, bestLines: [], source: 'odds-cache' });
}
});
router.get('/:sport/:player/:stat', async (req, res) => {
const sport = String(req.params.sport || '').toLowerCase();
const player = decodeURIComponent(req.params.player);
const stat = req.params.stat;
const side = req.query.side === 'under' ? 'under' : 'over';
try {
const props = await readCachedProps(sport);
const prop = props.find(
(p) => (p.player || '').toLowerCase() === player.toLowerCase() &&
(p.stat_type || p.stat || '').toLowerCase() === stat.toLowerCase(),
);
if (!prop) {
return res.status(404).set(MISSION_HEADER).json({ error: 'Prop not found in current slate.' });
}
const comparison = bookComparison.compareProp(prop, side);
if (!comparison) {
return res.set(MISSION_HEADER).json({ sport, player, stat, side, books: [], bestBook: null });
}
return res.set(MISSION_HEADER).json({ sport, ...comparison });
} catch (err) {
console.error(`[books/${sport}/prop]`, err.message);
return res.status(500).set(MISSION_HEADER).json({ error: 'Comparison failed' });
}
});
module.exports = router;
+49
View File
@@ -0,0 +1,49 @@
'use strict';
/**
* /api/lines (Session 28)
*
* Read-only views over the line-snapshot history (Redis). Zero credits.
*
* GET /api/lines/:sport/movers → biggest movers today
* GET /api/lines/:sport/:gameId/:player/:stat → one prop's history + classification
*/
const express = require('express');
const lineSnapshots = require('../services/lineSnapshotService');
const router = express.Router();
const MISSION_HEADER = { 'X-VYNDR-Mission': 'The market confirms the grade' };
const SUPPORTED = new Set(['nba', 'wnba', 'mlb', 'soccer', 'nfl', 'nhl']);
router.get('/:sport/movers', async (req, res) => {
const sport = String(req.params.sport || '').toLowerCase();
if (!SUPPORTED.has(sport)) {
return res.status(404).set(MISSION_HEADER).json({ error: `No line tracking for sport: ${sport}` });
}
const limit = req.query.limit ? Math.max(0, parseInt(req.query.limit, 10) || 0) : 20;
try {
const movers = await lineSnapshots.getBiggestMovers(sport, { limit });
return res.set(MISSION_HEADER).json({ sport, movers, source: 'snapshots' });
} catch (err) {
console.error(`[lines/${sport}/movers]`, err.message);
return res.set(MISSION_HEADER).json({ sport, movers: [], source: 'snapshots' });
}
});
router.get('/:sport/:gameId/:player/:stat', async (req, res) => {
const sport = String(req.params.sport || '').toLowerCase();
const { gameId, player, stat } = req.params;
try {
const history = await lineSnapshots.getLineHistory(sport, gameId, decodeURIComponent(player), stat);
const classification = lineSnapshots.classifyMovement(history);
return res.set(MISSION_HEADER).json({ sport, gameId, player, stat, ...classification });
} catch (err) {
console.error(`[lines/${sport}/prop]`, err.message);
return res.set(MISSION_HEADER).json({ sport, gameId, player, stat, movement: 'stable', delta: 0, snapshots: [] });
}
});
module.exports = router;
+46
View File
@@ -0,0 +1,46 @@
'use strict';
/**
* /api/parlay (Session 28)
*
* Builder-side parlay math — combined odds, grade, and correlation flags
* for a set of user-selected legs. Distinct from the existing parlay
* GRADING path (parlayGrader); this is the lightweight, zero-credit
* combination used by the live parlay builder.
*
* POST /api/parlay/calculate { legs: [...] } → combined analysis
* POST /api/parlay/suggestions { props, legs?, max? } → suggested combos
*/
const express = require('express');
const parlayService = require('../services/parlayService');
const router = express.Router();
const MISSION_HEADER = { 'X-VYNDR-Mission': 'Catch the legs that fight each other' };
router.post('/calculate', (req, res) => {
try {
const legs = req.body?.legs;
const result = parlayService.calculateParlay(legs);
return res.set(MISSION_HEADER).json(result);
} catch (err) {
const status = err.statusCode || 400;
return res.status(status).set(MISSION_HEADER).json({ error: err.message });
}
});
router.post('/suggestions', (req, res) => {
try {
const { props, legs, max } = req.body || {};
const suggestions = parlayService.suggestParlays(props || [], {
legs: Number(legs) || 3,
max: Number(max) || 3,
});
return res.set(MISSION_HEADER).json({ suggestions });
} catch (err) {
return res.status(400).set(MISSION_HEADER).json({ error: err.message });
}
});
module.exports = router;