-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: #1055 (endpoint) to hasura on target date;
- Loading branch information
Showing
1 changed file
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { json } from '@sveltejs/kit'; | ||
|
||
import { initGrapQLClient } from '$lib/graphql/init_graphQL'; | ||
import { generate_historic_fixtures_day_group_map, generate_leagues_map, get_target_date_fixtures, get_target_leagues } from 'betarena-types/dist/functions/func.livescores-v2.js'; | ||
import type { B_LS2_D, LS2_C_FixtureDateGroup } from 'betarena-types/types/livescores-v2'; | ||
|
||
// [ℹ] debug info | ||
const logs = []; | ||
|
||
// ~~~~~~~~~~~~~~~~~~~~~~~~ | ||
// [MAIN] ENDPOINT METHOD | ||
// ~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
export async function GET(req): Promise<unknown> { | ||
const date: string = | ||
req.url['searchParams'].get('date'); | ||
const targetData = await main( | ||
date | ||
); | ||
return json(targetData); | ||
} | ||
|
||
// ~~~~~~~~~~~~~~~~~~~~~~~~ | ||
// [MAIN] METHOD | ||
// ~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
/** | ||
* @description gathers target data for fixtures | ||
* from a target date supplied - straight from | ||
* hasura/historic-fixtures (db); | ||
* @param _date | ||
* @returns {Promise < B_LS2_D | null >} Promise < B_LS2_D | null > | ||
*/ | ||
async function main( | ||
_date: string | ||
): Promise < B_LS2_D | null > { | ||
|
||
const graphQlInstance = initGrapQLClient() | ||
|
||
const FIXTURE_DATE = _date; | ||
|
||
const fixture_dates = [FIXTURE_DATE] | ||
|
||
const current_week_fixtures = await get_target_date_fixtures( | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
//@ts-ignore | ||
graphQlInstance, | ||
fixture_dates | ||
) | ||
|
||
/** | ||
* [ℹ] organize (group)(map) fixtures by fixture day | ||
* [ℹ] -> generate (final) (array) data | ||
* [ℹ] organize (map) leagues by id's | ||
* [ℹ] -> generate (final) (array) data | ||
*/ | ||
const fixturesGroupedByDateMap = | ||
await generate_historic_fixtures_day_group_map( | ||
current_week_fixtures?.historic_fixtures | ||
) | ||
let fixtures_by_date: LS2_C_FixtureDateGroup[] = [] | ||
for (const [key, value] of fixturesGroupedByDateMap) { | ||
fixtures_by_date.push({ | ||
date: key, | ||
fixtures: value | ||
}) | ||
} | ||
fixtures_by_date = fixtures_by_date.sort((a, b) => new Date(a.date).getTime() - new Date(b.date).getTime()) | ||
|
||
// eslint-disable-next-line prefer-const | ||
let leagues_ids_arr: number[] = current_week_fixtures?.historic_fixtures?.map(a => a.league_id) | ||
const leagues_data = await get_target_leagues( | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
//@ts-ignore | ||
graphQlInstance, | ||
leagues_ids_arr | ||
) | ||
const league_map = await generate_leagues_map(leagues_data) | ||
|
||
/** | ||
* [ℹ] cache (data) persist | ||
*/ | ||
const data: B_LS2_D = { | ||
fixtures_by_date, | ||
leagues: Array.from(league_map.values()) | ||
} | ||
|
||
return data; | ||
} |