diff --git a/src/modules/matches/matches.service.ts b/src/modules/matches/matches.service.ts index d608197..f7be42f 100755 --- a/src/modules/matches/matches.service.ts +++ b/src/modules/matches/matches.service.ts @@ -586,7 +586,40 @@ export class MatchesService { date: new Date(Number(liveMatch.mstUtc)), // Fill missing relations with empty arrays teamStats: [], - playerParticipations: [], + playerParticipations: (() => { + const parsed: Array<{ teamId: string; isStarting: boolean; shirtNumber: string | number | null; position: string | null; player: { id: string; name: string } }> = []; + if (liveMatch.lineups && typeof liveMatch.lineups === 'object') { + const lu = liveMatch.lineups as Record; + const addPlayers = (teamLu: any, teamId: string | null) => { + if (!teamLu || !teamId) return; + if (teamLu.xi && Array.isArray(teamLu.xi)) { + teamLu.xi.forEach((p: any) => { + parsed.push({ + teamId, + isStarting: true, + shirtNumber: p.shirtNumber || p.number, + position: p.position || p.pos, + player: { id: p.personId || p.id || p.playerId || 'unknown', name: p.matchName || p.name || p.playerName || 'Bilinmiyor' } + }); + }); + } + if (teamLu.subs && Array.isArray(teamLu.subs)) { + teamLu.subs.forEach((p: any) => { + parsed.push({ + teamId, + isStarting: false, + shirtNumber: p.shirtNumber || p.number, + position: p.position || p.pos, + player: { id: p.personId || p.id || p.playerId || 'unknown', name: p.matchName || p.name || p.playerName || 'Bilinmiyor' } + }); + }); + } + }; + addPlayers(lu.home, liveMatch.homeTeamId); + addPlayers(lu.away, liveMatch.awayTeamId); + } + return parsed; + })(), playerEvents: [], oddCategories: [], // Will handle odds parsing below officials: [], diff --git a/src/tasks/data-fetcher.task.ts b/src/tasks/data-fetcher.task.ts index c6b6112..82da6e1 100755 --- a/src/tasks/data-fetcher.task.ts +++ b/src/tasks/data-fetcher.task.ts @@ -1,4 +1,4 @@ -import { Injectable, Logger } from "@nestjs/common"; +import { Injectable, Logger } from "@nestjs/common"; import { Cron } from "@nestjs/schedule"; import { HttpService } from "@nestjs/axios"; import { PrismaService } from "../database/prisma.service"; @@ -182,7 +182,9 @@ export class DataFetcherTask { this.logger.log("syncLiveMatches START"); const today = getDateStringInTimeZone(new Date(), this.timeZone); + const tomorrow = getShiftedDateStringInTimeZone(1, this.timeZone); await this.syncMatchList(today); + await this.syncMatchList(tomorrow); await this.updateLiveScores(); await this.fetchOddsForMatches(); await this.fillMissingLineups(); @@ -432,7 +434,10 @@ export class DataFetcherTask { for (const match of toUpdate) { try { - const formation = await this.scraper.fetchStartingFormation(match.id); + const [formation, substitutions] = await Promise.all([ + this.scraper.fetchStartingFormation(match.id), + this.scraper.fetchSubstitutions(match.id), + ]); const sidelined = match.matchSlug ? await this.scraper.fetchSidelinedPlayers( match.id, @@ -440,11 +445,26 @@ export class DataFetcherTask { ) : null; + // Normalize to same home.xi/away.xi format used by processMatchOdds + let normalizedLineups: Record | null = null; + if (formation || substitutions) { + normalizedLineups = { + home: { + xi: formation?.stats?.home || [], + subs: substitutions?.stats?.home || [], + }, + away: { + xi: formation?.stats?.away || [], + subs: substitutions?.stats?.away || [], + }, + }; + } + await this.prisma.liveMatch.update({ where: { id: match.id }, data: { - lineups: formation - ? JSON.parse(JSON.stringify(formation)) + lineups: normalizedLineups + ? JSON.parse(JSON.stringify(normalizedLineups)) : Prisma.JsonNull, sidelined: sidelined ? JSON.parse(JSON.stringify(sidelined)) @@ -810,8 +830,8 @@ export class DataFetcherTask { const matchTime = Number(match.mstUtc); const diffHours = (matchTime - now) / (1000 * 60 * 60); - // Fetch if between -3 hours (started) and +4 hours (upcoming) - if (diffHours < 4 && diffHours > -3) { + // Fetch if between -3 hours (started) and +24 hours (upcoming) + if (diffHours < 24 && diffHours > -3) { // Lineups try { const [startingFormation, substitutions] = await Promise.all([ @@ -1269,3 +1289,4 @@ export class DataFetcherTask { return new Promise((resolve) => setTimeout(resolve, ms)); } } +