-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
202 lines (171 loc) · 7.31 KB
/
app.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
const express = require('express')
const request = require('request-promise')
const bodyParser = require('body-parser')
const app = express()
const port = process.env.PORT || 3000
// Load environment variables in development
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config()
}
// Body parser - support parsing of application/json type post data
app.use(bodyParser.json());
app.get('/', (req, res) => {
res.send("Plan a Visit at XR.church!")
})
app.post('/', (req, res) => {
const data = req.body
const campusID = (data.campus === "North Fayette") ? "9929" :
(data.campus === "Boyce Road") ? "9930" :
(data.campus === "Cranberry") ? "9931" :
(data.campus === "East Liberty") ? "9932" :
(data.campus === "Weirton") ? "9933" :
"9929"
// household is an array of Promises - each promise being a person being created
let household = []
// Check that we have data for first person
if (data.your_firstname && data.your_lastname) {
// Add person to Planning Center
const postPerson = request.post({
uri: 'https://api.planningcenteronline.com/people/v2/people/',
method: 'POST',
json: {
"data": {
"attributes": {
"first_name": data.your_firstname,
"last_name": data.your_lastname,
"primary_campus_id": campusID,
"membership": "Planned Visit Online"
}
}
}
}).auth(process.env.PCO_APP_ID, process.env.PCO_SECRET, false).catch(error => { console.log(error.message) })
household.push(postPerson) // Add the Promise to the households array
// Add their email to their profile, if provided
if (data.your_email) {
const postEmail = postPerson.then(body => {
const personID = body["data"]["id"]
return request.post({
uri: `https://api.planningcenteronline.com/people/v2/people/${personID}/emails`,
method: 'POST',
json: {
"data": {
"attributes": {
"address": data.your_email,
"location": "Home",
"primary": true
}
}
}
}).auth(process.env.PCO_APP_ID, process.env.PCO_SECRET, false).catch(error => { console.log(error.message) })
})
}
// Add their phone number to their profile, if provided
if (data.your_phone) {
const postPhone = postPerson.then(body => {
const personID = body["data"]["id"]
return request.post({
uri: `https://api.planningcenteronline.com/people/v2/people/${personID}/phone_numbers`,
method: 'POST',
json: {
"data": {
"attributes": {
"number": data.your_phone,
"location": "Mobile",
"primary": true
}
}
}
}).auth(process.env.PCO_APP_ID, process.env.PCO_SECRET, false).catch(error => { console.log(error.message) })
})
}
}
// Check if we have spouse
if (data.spouse_firstname && data.spouse_lastname) {
// Add spouse to Planning Center
const postSpouse = request.post({
uri: 'https://api.planningcenteronline.com/people/v2/people/',
method: 'POST',
json: {
"data": {
"attributes": {
"first_name": data.spouse_firstname,
"last_name": data.spouse_lastname,
"primary_campus_id": campusID,
"membership": "Planned Visit Online"
}
}
}
}).auth(process.env.PCO_APP_ID, process.env.PCO_SECRET, false).catch(error => { console.log(error.message) })
household.push(postSpouse) // Add the Promise to the households array
}
// Check for first child
if (data.child1_firstname && data.child1_lastname) {
// Check to see if profile should be marked as child=true
let isChild = true
if (data.child1_dob) {
if (getAge(data.child1_dob) >= 18) isChild = false
}
// Add first child to Planning Center
const postChild1 = request.post({
uri: 'https://api.planningcenteronline.com/people/v2/people/',
method: 'POST',
json: {
"data": {
"attributes": {
"first_name": data.child1_firstname,
"last_name": data.child1_lastname,
"birthdate": data.child1_dob,
"medical_notes": data.child1_info,
"child": isChild,
"primary_campus_id": campusID,
"membership": "Planned Visit Online"
}
}
}
}).auth(process.env.PCO_APP_ID, process.env.PCO_SECRET, false).catch(error => { console.log(error.message) })
household.push(postChild1) // Add the Promise to the households array
}
// Put all the people in a household!
if (household.length > 0 && data.your_lastname) {
// Once all the people are created (every Promise in the household array has been resolved)
Promise.all(household).then(householdBodys => {
// Create the json to be POSTed
let householdJson = {
"data": {
"attributes": {},
"relationships": {
"people": {
"data": []
},
"primary_contact": {
"data": {}
}
}
}
}
householdJson.data.attributes = { "name": data.your_lastname }
householdBodys.forEach((body) => {
householdJson.data.relationships.people.data.push({ "type": "Person", "id": `${body["data"]["id"]}` })
})
householdJson.data.relationships.primary_contact.data = { "type": "Person", "id": `${householdBodys[0]["data"]["id"]}` }
const postHousehold = request.post({
uri: 'https://api.planningcenteronline.com/people/v2/households/',
method: 'POST',
json: householdJson
}).auth(process.env.PCO_APP_ID, process.env.PCO_SECRET, false).catch(error => { console.log(error.message) })
})
}
res.status(200)
res.send("Success")
})
function getAge(DOB) {
const today = new Date();
const birthDate = new Date(DOB);
let age = today.getFullYear() - birthDate.getFullYear();
const m = today.getMonth() - birthDate.getMonth();
if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
age = age - 1;
}
return age;
}
app.listen(port, () => console.log(`PlanAVisit app listening on port ${port}!`))