Skip to content

Commit b9f187b

Browse files
somereticalYankai ZhuDomin0de
authored
I2 17/events api hotfix (#61)
* Added backend events API * polished code * Fix to read fetched events data correctly * Update eventInfo properly * Update event fetching error handling * fix type error * fix event concat --------- Co-authored-by: Yankai Zhu <[email protected]> Co-authored-by: Domin0de <[email protected]>
1 parent 49600e0 commit b9f187b

File tree

2 files changed

+13
-18
lines changed

2 files changed

+13
-18
lines changed

backend/src/data/eventData.ts

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ export async function fetchEvents() {
5959
`https://graph.facebook.com/${FB_API_VERSION}/${process.env.FB_EVENT_PAGE_ID}/events?access_token=${process.env.FB_ACCESS_TOKEN}&fields=id,name,cover,place,start_time,end_time`
6060
);
6161

62-
if (!response || response.type === ResultType.Err) {
63-
console.log(`No events found...\n${response}`);
64-
return [];
62+
if (!response.ok) {
63+
throw new Error(JSON.stringify(response.json()));
64+
6565
}
6666
const res: FacebookEventsResponse = await response.json();
6767

@@ -85,22 +85,17 @@ export async function fetchEvent(id: string) {
8585
`https://graph.facebook.com/${FB_API_VERSION}/${id}?access_token=${process.env.FB_ACCESS_TOKEN}&fields=id,name,cover,place,start_time,end_time`
8686
);
8787

88-
const res: Result<FacebookEvent, FacebookError> = await response.json();
89-
90-
if (!res || res.type === ResultType.Err) {
91-
throw new Error(
92-
`Couldn't fetch details for event ${id}\n${inspect(
93-
Object.getOwnPropertyDescriptor(res, "error")?.value
94-
)}`
95-
);
88+
if (!response.ok) {
89+
throw new Error(`Couldn't fetch details for event ${id}\n${JSON.stringify(response.json())}`);
9690
}
91+
const res: FacebookEvent = await response.json();
9792

9893
return new EventInfo(
99-
res.value.id,
100-
res.value.name,
101-
res.value.start_time,
102-
res.value.end_time,
103-
res.value.place?.name ?? DEFAULT_EVENT_LOCATION,
104-
res.value.cover?.source ?? DEFAULT_EVENT_IMAGE
94+
res.id,
95+
res.name,
96+
res.start_time,
97+
res.end_time,
98+
res.place?.name ?? DEFAULT_EVENT_LOCATION,
99+
res.cover?.source ?? DEFAULT_EVENT_IMAGE
105100
);
106101
}

backend/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ dotenv.config();
1111
(async () => {
1212
try {
1313
const events = await fetchEvents();
14-
eventInfoMutex.runExclusive(() => eventInfo.concat(events));
14+
eventInfoMutex.runExclusive(() => eventInfo.push(...events));
1515
console.log("Events fetched successfully");
1616
} catch (error) {
1717
// do we ungracefully bail out here???

0 commit comments

Comments
 (0)