-
Notifications
You must be signed in to change notification settings - Fork 1
/
draw.js
69 lines (60 loc) · 1.65 KB
/
draw.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import axios from 'axios';
import meetupRandomizer from 'meetup-randomizer';
import {
getParamsFromRequest,
getIdFromEvent,
getRsvpsUrl,
parseEventsResponse,
validateStatus,
} from './helpers';
const handler = async (request, context) => {
const headers =
process.env.NODE_ENV === 'development'
? { 'Access-Control-Allow-Origin': '*' }
: {};
let meetup;
let count;
let specificEventId;
let meetupApiKey;
try {
({ meetup, count, specificEventId, meetupApiKey } = getParamsFromRequest(
request,
));
} catch (err) {
return {
headers,
statusCode: 400,
body: JSON.stringify({ error: { message: err.message } }),
};
}
try {
const baseUrl = `https://api.meetup.com/${encodeURIComponent(
meetup,
)}/events`;
const eventUrlSuffix = '?status=upcoming&only=id,visibility';
const eventUrl = `${baseUrl}/${encodeURIComponent(
specificEventId,
)}${eventUrlSuffix}`;
const response = await axios.get(eventUrl, { validateStatus });
const eventId = getIdFromEvent(parseEventsResponse(response));
meetupRandomizer.setCustomApiUrl(
getRsvpsUrl(baseUrl, eventId, meetupApiKey),
);
const winners = await meetupRandomizer.run(meetup, eventId, count);
if (Array.isArray(winners) && winners.length) {
return {
headers,
statusCode: 200,
body: JSON.stringify({ winners }),
};
}
throw new Error('Sorry, we received unexpected data for that request.');
} catch (err) {
return {
headers,
statusCode: 404,
body: JSON.stringify({ error: { message: err.message } }),
};
}
};
export { handler };