const { graphql_endpoint } = require('./constants'); const { map_event, get_past_events_from_page, get_upcoming_events_from_page, get_page_info, get_edges, sleep, } = require('./logic'); const fetch = require('node-fetch'); const last = require('ramda/src/last'); const fetch_past_events = async (page_id, cursor = null) => { let doc_id; if (cursor === null) { doc_id = '3636086023161977'; } else { doc_id = '3972530646159673'; } const res = await do_events_request(page_id, doc_id, cursor); return get_past_events_from_page(res); }; const get_past_events = async () => []; const do_events_request = async (page_id, doc_id, cursor = null) => { const params = new URLSearchParams(); let variables = { pageID: `${page_id}`, allowedStates: ['PUBLISHED', 'CANCELED'], cacheBreaker: 0, }; if (cursor !== null) { variables = { ...variables, count: 20, cursor }; } params.append('doc_id', doc_id); params.append('variables', JSON.stringify(variables)); const fetch_options = { headers: { 'Content-Type': 'application/x-www-form-urlencoded', }, body: params, method: 'POST', }; let res = null; try { res = await fetch(graphql_endpoint, fetch_options); } catch (e) { console.error(e); return null; } if (!res.ok) { return null; } try { res = await res.json(); } catch (e) { console.error(e); return null; } return res; }; const fetch_upcoming_events = async (page_id, cursor = null) => { let doc_id; if (cursor === null) { doc_id = '3636086023161977'; } else { doc_id = '3911675102281316'; } const res = await do_events_request(page_id, doc_id, cursor); return get_upcoming_events_from_page(res); }; const get_upcoming_events = async (page_id) => { let next = true; let cursor; let edges = []; while (next) { const res = await fetch_upcoming_events(page_id, cursor); edges = [...edges, ...get_edges(res)]; const { has_next_page } = get_page_info(res); next = has_next_page; cursor = last(edges).cursor; if (has_next_page === true) { await sleep(2); } } return edges; }; const get_reoccuring_events = () => {}; const get_page_events = async (opt) => { let past_events = []; let upcoming_events = []; const { page_id } = opt; if (opt.get_past_events) { past_events = await get_past_events(page_id); } if (opt.get_upcoming_events) { upcoming_events = await get_upcoming_events(page_id); } const nodes = [...upcoming_events, ...past_events]; return nodes.map(map_event); }; module.exports = { get_page_events, };