feat(TTRPreview): add standalone TTR preview endpoint and enhance loading logic in MatchReportApiDialog
All checks were successful
Deploy tt-tagebuch / deploy (push) Successful in 58s
All checks were successful
Deploy tt-tagebuch / deploy (push) Successful in 58s
This commit is contained in:
@@ -216,20 +216,23 @@ export const getMatchPlayers = async (req, res) => {
|
||||
export const getTtrPreviewPlayers = async (req, res) => {
|
||||
try {
|
||||
const { authcode: userToken } = req.headers;
|
||||
const match = await Match.findByPk(req.params.matchId);
|
||||
if (!match) return res.status(404).json({ error: 'Spiel nicht gefunden' });
|
||||
await checkAccess(userToken, match.clubId);
|
||||
const isStandalonePreview = req.params.matchId === 'standalone';
|
||||
const match = isStandalonePreview ? null : await Match.findByPk(req.params.matchId);
|
||||
if (!isStandalonePreview && !match) return res.status(404).json({ error: 'Spiel nicht gefunden' });
|
||||
const clubId = isStandalonePreview ? Number(req.body?.clubId) : match.clubId;
|
||||
if (!Number.isInteger(clubId) || clubId <= 0) return res.status(400).json({ error: 'Verein fehlt für die TTR-Vorschau' });
|
||||
await checkAccess(userToken, clubId);
|
||||
|
||||
// The product uses German match dates; do not roll the cache over at
|
||||
// UTC midnight while a late-evening match is still in progress.
|
||||
const snapshotDate = new Intl.DateTimeFormat('sv-SE', { timeZone: 'Europe/Berlin' }).format(new Date());
|
||||
const [cachedRows] = await sequelize.query(
|
||||
'SELECT payload FROM match_ttr_preview_cache WHERE match_id = :matchId AND snapshot_date = :snapshotDate LIMIT 1',
|
||||
{ replacements: { matchId: match.id, snapshotDate } }
|
||||
);
|
||||
if (cachedRows.length) {
|
||||
const payload = typeof cachedRows[0].payload === 'string' ? JSON.parse(cachedRows[0].payload) : cachedRows[0].payload;
|
||||
return res.json({ ...payload, cached: true, snapshotDate });
|
||||
if (match) {
|
||||
const [cachedRows] = await sequelize.query(
|
||||
'SELECT payload FROM match_ttr_preview_cache WHERE match_id = :matchId AND snapshot_date = :snapshotDate LIMIT 1',
|
||||
{ replacements: { matchId: match.id, snapshotDate } }
|
||||
);
|
||||
if (cachedRows.length) {
|
||||
const payload = typeof cachedRows[0].payload === 'string' ? JSON.parse(cachedRows[0].payload) : cachedRows[0].payload;
|
||||
return res.json({ ...payload, cached: true, snapshotDate });
|
||||
}
|
||||
}
|
||||
|
||||
const sides = ['home', 'guest'];
|
||||
@@ -283,11 +286,13 @@ export const getTtrPreviewPlayers = async (req, res) => {
|
||||
const value = resolved.get(`${side}:${normalizeTtrName(playerName(player))}`);
|
||||
return { side, name: playerName(player), nuLigaPersonId: player?.nuLigaPersonId || null, ttr: value?.ttr ?? null, adjustmentConstant: value?.adjustmentConstant ?? null, source: value?.source || 'unavailable' };
|
||||
}) };
|
||||
await sequelize.query(
|
||||
`INSERT INTO match_ttr_preview_cache (match_id, snapshot_date, payload, created_at, updated_at)
|
||||
VALUES (:matchId, :snapshotDate, :payload, NOW(), NOW())`,
|
||||
{ replacements: { matchId: match.id, snapshotDate, payload: JSON.stringify(payload) } }
|
||||
);
|
||||
if (match) {
|
||||
await sequelize.query(
|
||||
`INSERT INTO match_ttr_preview_cache (match_id, snapshot_date, payload, created_at, updated_at)
|
||||
VALUES (:matchId, :snapshotDate, :payload, NOW(), NOW())`,
|
||||
{ replacements: { matchId: match.id, snapshotDate, payload: JSON.stringify(payload) } }
|
||||
);
|
||||
}
|
||||
return res.json({ ...payload, cached: false, snapshotDate });
|
||||
} catch (error) {
|
||||
console.error('[getTtrPreviewPlayers]', error);
|
||||
|
||||
Reference in New Issue
Block a user