-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypeform.js
173 lines (152 loc) · 4.55 KB
/
typeform.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
const { createClient } = require('@typeform/api-client')
const refresh_interval = 1000 * 60; // Check for new responses every minute
const typeformAPI = createClient({ token: process.env.TYPEFORM_TOKEN });
// Cached users
let emails = {};
let mentored_emails = {}
let checked_in_emails = {}
let doe_workshop_emails = {}
function updateUserDoc(db, uid) {
db.collection("users").doc(uid).update({
app_status: "Pending Review"
})
.then(function(doc) {
// console.log("Automatic user status update complete");
})
.catch(function(error) {
console.log("Error: ", error)
})
}
function UserCreated(user)
{
}
// Queries the typeform every minute to get new responses for those who applied
function StartTypeformCheck(db, firebase)
{
// console.log("Starting typeform checks...");
CheckTypeformResponses(db, firebase);
setInterval(() => {
CheckTypeformResponses(db, firebase);
}, refresh_interval);
}
function CheckTypeformResponses(db, firebase)
{
/* First, query all users in DB. (in StartTypeformCheck)
If the user's application status is not "Not Applied Yet",
then add them to the users table (above).
Next, query all responses from the typeform.
If the user exists in the users object (above),
then don't do anything because their status is already updated.
If the user does not exist in the table above, then
add them and also update their application status in the database.
*/
typeformAPI
.responses
.list({uid: process.env.TYPEFORM_FORM_ID, pageSize: 1000, until: "2020-12-28T12:00:00"})
.then(response => {
response.items.forEach((form_response) =>
{
emails[RemovePeriodsInEmail(form_response.answers[2].email.toLowerCase())] = true;
})
})
.then(() =>
{
// Because there are over 1000 responses, get the next few responses
// See https://developer.typeform.com/responses/reference/retrieve-responses/ for details
typeformAPI
.responses
.list({uid: process.env.TYPEFORM_FORM_ID, pageSize: 1000, since: "2020-12-28T12:00:00"})
.then(response => {
response.items.forEach((form_response) =>
{
emails[RemovePeriodsInEmail(form_response.answers[2].email.toLowerCase())] = true;
})
})
.then(() =>
{
})
.catch((reason) =>
{
console.log(reason);
})
})
.catch((reason) =>
{
console.log(reason);
})
// Also query the mentored responses typeform
typeformAPI
.responses
.list({uid: process.env.TYPEFORM_MENTORED_ID, pageSize: 1000})
.then(response => {
response.items.forEach((form_response) =>
{
mentored_emails[RemovePeriodsInEmail(form_response.answers[4].email.toLowerCase())] = true;
})
})
.then(() =>
{
})
.catch((reason) =>
{
console.log(reason);
})
// Also query the checked-in responses typeform
typeformAPI
.responses
.list({uid: process.env.TYPEFORM_CHECKIN_ID, pageSize: 1000})
.then(response => {
response.items.forEach((form_response) =>
{
checked_in_emails[form_response.answers[2].email.toLowerCase()] = true;
})
})
.then(() =>
{
})
.catch((reason) =>
{
console.log(reason);
})
// Also query the workshop attendance responses typeform
typeformAPI
.responses
.list({uid: process.env.TYPEFORM_WORKSHOP_ID, pageSize: 1000})
.then(response => {
response.items.forEach((form_response) =>
{
doe_workshop_emails[RemovePeriodsInEmail(form_response.answers[0].email.toLowerCase())] = true;
})
})
.then(() =>
{
})
.catch((reason) =>
{
console.log(reason);
})
}
// When there are periods in the email, it messes up the format so you should remove them first
function RemovePeriodsInEmail(email)
{
const split = email.toLowerCase().split("@");
return `${split[0].replace(".", "")}@${split[1]}`
}
function GetAppliedEmails()
{
return emails;
}
// Gets a list of emails of people who filled out the mentored typeform for the badge
function GetMentoredEmails()
{
return mentored_emails;
}
function GetCheckedInEmails()
{
return checked_in_emails;
}
function GetDoEWorkshopEmails()
{
return doe_workshop_emails;
}
module.exports = {StartTypeformCheck, GetAppliedEmails, GetMentoredEmails, GetCheckedInEmails, GetDoEWorkshopEmails}