import { DailyStats, LunchChoice, WeeklyStats } from "../../types"; import { getStatsMock } from "./mock"; import { getClientData } from "./service"; import getStorage from "./storage"; const storage = getStorage(); /** * Vypočte a vrátí statistiky jednotlivých možností pro předaný rozsah dat. * * @param startDate počáteční datum * @param endDate koncové datum * @returns statistiky pro zadaný rozsah dat */ export async function getStats(startDate: string, endDate: string): Promise { if (process.env.MOCK_DATA === 'true') { return getStatsMock(); } const start = new Date(startDate); const end = new Date(endDate); // Dočasná validace, aby to někdo ručně neshodil const daysDiff = ((end as any) - (start as any)) / (1000 * 60 * 60 * 24); if (daysDiff > 4) { throw Error('Neplatný rozsah'); } const result = []; for (const date = start; date <= end; date.setDate(date.getDate() + 1)) { const locationsStats: DailyStats = { // TODO vytáhnout do utils funkce date: `${String(date.getDate()).padStart(2, "0")}.${String(date.getMonth() + 1).padStart(2, "0")}.`, locations: {} } const data = await getClientData(date); if (data?.choices) { Object.keys(data.choices).forEach(locationKey => { if (!locationsStats.locations) { locationsStats.locations = {} } // TODO dořešit, tohle je zmatek a té hlášce Sonaru nerozumím locationsStats.locations[locationKey as LunchChoice] = Object.keys(data.choices[locationKey as LunchChoice]!).length; }) } result.push(locationsStats); } return result as WeeklyStats; }