From 4a5baa73d11fde6b3ac70d20348422d8c26b548f Mon Sep 17 00:00:00 2001 From: migbash <20924663+migbash@users.noreply.github.com> Date: Mon, 27 Feb 2023 15:38:01 +0000 Subject: [PATCH] feat: #1055 (endpoint) to hasura on target date; --- .../api/hasura/home/livescores-v2/+server.ts | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 src/routes/api/hasura/home/livescores-v2/+server.ts diff --git a/src/routes/api/hasura/home/livescores-v2/+server.ts b/src/routes/api/hasura/home/livescores-v2/+server.ts new file mode 100644 index 000000000..f29c04ad9 --- /dev/null +++ b/src/routes/api/hasura/home/livescores-v2/+server.ts @@ -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 { + 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; +} \ No newline at end of file