You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
1.8 KiB

const { graphql_endpoint } = require('./constants');
const {
map_event,
get_past_events_from_page,
get_upcoming_events_from_page,
} = require('./logic');
const fetch = require('node-fetch');
const defaultRes = { edges: [] };
const get_past_events = async () => defaultRes;
const fetch_upcoming_events = async (page_id, cursor = null) => {
const params = new URLSearchParams();
let variables = {
pageID: `${page_id}`,
allowedStates: ['PUBLISHED', 'CANCELED'],
cacheBreaker: 0,
};
if (cursor !== null) {
variables = { ...variables, count: 3, cursor };
}
params.append('doc_id', '3636086023161977');
params.append('variables', JSON.stringify(variables));
const fetch_options = {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: params,
method: 'POST',
};
let res = defaultRes;
try {
res = await fetch(graphql_endpoint, fetch_options);
} catch (e) {
console.error(e);
return defaultRes;
}
if (!res.ok) {
return defaultRes;
}
try {
res = await res.json();
} catch (e) {}
return res;
};
const get_upcoming_events = async (page_id) => {
return defaultRes;
};
const get_reoccuring_events = () => {};
const get_page_events = async (opt) => {
let past_events = defaultRes;
let upcoming_events = defaultRes;
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);
console.log(upcoming_events);
}
const responses = [upcoming_events, past_events];
const nodes = responses.reduce(
(res, current) => [...res, ...current.edges],
[],
);
return nodes.map(map_event);
};
module.exports = {
get_page_events,
};