Skip to content

Commit

Permalink
feat: #1055 (endpoint) to hasura on target date;
Browse files Browse the repository at this point in the history
  • Loading branch information
migbash committed Feb 27, 2023
1 parent 918553a commit 4a5baa7
Showing 1 changed file with 89 additions and 0 deletions.
89 changes: 89 additions & 0 deletions src/routes/api/hasura/home/livescores-v2/+server.ts
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;
}

0 comments on commit 4a5baa7

Please sign in to comment.