Untitled
unknown
javascript
a month ago
1.5 kB
2
Indexable
Never
router.get("/", async (req, res, next) => { try { const { date } = req.query; let page = 1, hasMore = true, fixtures = [], data = []; try { while (hasMore) { const response = await sportMonkslUrl.get( `/fixtures/date/${date}?include=league.country;round.stage;participants;state;scores;periods&page=${page}` ); fixtures.length > 0 ? (fixtures = [...fixtures, ...response?.data?.data]) : (fixtures = [...response?.data?.data]); hasMore = response?.data?.pagination?.has_more; page += 1; } } catch (err) { console.log(err.stack); } // Fetch selected leagues from the database const selectedLeagues = await League.find({}).sort({ position: "asc" }); const selectedLeagueIds = new Set(selectedLeagues.map((l) => l.id)); const leagueMap = {}; fixtures.forEach((fixture) => { const leagueId = fixture?.league?.id; if (selectedLeagueIds.has(leagueId)) { if (!leagueMap[leagueId]) { leagueMap[leagueId] = { id: leagueId, name: fixture.league.name, image: fixture.league.image_path, fixtures: [] }; } leagueMap[leagueId].fixtures.push(fixture); } }); data = Object.values(leagueMap); res.send({ status: true, result: data }); } catch (error) { console.log(error.stack); next(error); } });
Leave a Comment