-
Notifications
You must be signed in to change notification settings - Fork 0
/
churchsuite-api-client.js
64 lines (58 loc) · 1.4 KB
/
churchsuite-api-client.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
const fetch = require('node-fetch');
const fetchEvents = async (domain, params) => {
const response = await fetch(eventsUrl(domain, params));
return await response.json();
};
const signUpToEvent = async (eventId, data) => {
const opts = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Account': `${__GATSBY_PLUGIN_CHURCHSUITE_ACCOUNT__}`,
'X-Application': `${__GATSBY_PLUGIN_CHURCHSUITE_APPLICATION__}`,
'X-Auth': `${__GATSBY_PLUGIN_CHURCHSUITE_AUTH_KEY__}`,
},
body: JSON.stringify({
action: 'add',
signups: [
{
...data,
},
],
}),
};
return await fetch(signUpUrl(eventId), opts);
};
const signUpUrl = eventId =>
`https://api.churchsuite.co.uk/v1/calendar/event/1016/signups`; // will be event id but hardcoding for testing
const eventsUrl = (d, qp) => {
const p = Object.keys(qp)
.filter(f => !!qp[f])
.map(m => `${m}=${qp[m]}`);
return `https://${d}/embed/calendar/json?${p.join('&')}`;
};
class QueryParams {
constructor(
date_start,
date_end,
featured,
category_ids,
site_ids,
embed_signup,
public_signup
) {
this.date_start = date_start;
this.date_end = date_end;
this.featured = featured;
this.category_ids = category_ids;
this.site_ids = site_ids;
this.embed_signup = embed_signup;
this.public_signup = public_signup;
}
}
module.exports = {
QueryParams,
buildUrl: eventsUrl,
fetchEvents,
signUpToEvent,
};