-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
305 lines (282 loc) · 9.47 KB
/
index.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const twilio = require('twilio');
const TWILIO_ACCOUNT_SID = functions.config().twilio.account_sid;
const TWILIO_AUTH_TOKEN = functions.config().twilio.auth_token;
const TWILIO_SMS_SID = functions.config().twilio.sms_sid;
const twilioClient = twilio(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN);
admin.initializeApp();
const unauthenticationError = new functions.https.HttpsError(
// Very important to use the codes defined in documentation
// https://firebase.google.com/docs/reference/functions/providers_https_#functionserrorcode
'unauthenticated',
'User is not authenticated'
);
const getDateStatus = (startDate, endDate) => {
const now = admin.firestore.Timestamp.now().valueOf();
const initialDate = startDate.valueOf();
const finalDate = endDate.valueOf();
if (now < initialDate) return 'upcoming';
if (now > finalDate) return 'expired';
return 'live';
}
// ****************
// ASSEMBLIES
// ****************
exports.createAssembly = functions.https.onCall((data, context) => {
if (!context.auth) {
throw unauthenticationError;
}
const initialDate = new admin.firestore.Timestamp(data.initialDate.seconds, data.initialDate.nanoseconds);
const endDate = new admin.firestore.Timestamp(data.endDate.seconds, data.endDate.nanoseconds);
return admin.firestore().collection('assemblies').add({ ...data, initialDate, endDate })
.then(doc => ({ message: 'Assembly added succesfully', payload: { id: doc.id }}))
.catch(error => {
throw new functions.https.HttpsError(
'internal',
error.message
);
})
});
exports.updateAssembly = functions.https.onCall((data, context) => {
if (!context.auth) {
throw unauthenticationError;
}
const initialDate = new admin.firestore.Timestamp(data.data.initialDate.seconds, data.data.initialDate.nanoseconds);
const endDate = new admin.firestore.Timestamp(data.data.endDate.seconds, data.data.endDate.nanoseconds);
return admin.firestore().collection('assemblies').doc(data.id).set({ ...data.data, initialDate, endDate }, { merge: true })
.then(doc => ({ message: 'Assembly updated succesfully', payload: { id: doc.id }}))
.catch(error => {
throw new functions.https.HttpsError(
'internal',
error.message
);
})
});
exports.deleteAssembly = functions.https.onCall((data, context) => {
if (!context.auth) {
throw unauthenticationError;
}
return admin.firestore().collection('assemblies').doc(data.id).delete();
});
// ****************
// USERS
// ****************
exports.createUser = functions.https.onCall((data, context) => {
if (!context.auth) {
throw unauthenticationError;
}
return admin.firestore().collection('users').doc(data.uid).set({
email: data.email,
displayName: data.displayName,
})
.then(doc => ({ message: 'User created succesfully', payload: { id: doc.id }}))
.catch(error => {
throw new functions.https.HttpsError(
'internal',
error.message
);
})
})
exports.deleteUser = functions.auth.user().onDelete(user => {
return admin.firestore().collection('users').doc(user.uid).delete();
});
// ****************
// MEMBERS
// ****************
exports.createMember = functions.https.onCall((data, context) => {
if (!context.auth) {
throw unauthenticationError;
}
return admin.firestore().collection('members').add(data)
.then(doc => ({ message: 'Member added succesfully', payload: { id: doc.id }}))
.catch(error => {
throw new functions.https.HttpsError(
'internal',
error.message
);
})
})
exports.updateMember = functions.https.onCall((data, context) => {
if (!context.auth) {
throw unauthenticationError;
}
return admin.firestore().collection('members').doc(data.id).set(data.data, { merge: true })
.then(doc => ({ message: 'Member updated succesfully', payload: { id: doc.id }}))
.catch(error => {
throw new functions.https.HttpsError(
'internal',
error.message
);
})
});
exports.deleteMember = functions.https.onCall((data, context) => {
if (!context.auth) {
throw unauthenticationError;
}
return admin.firestore().collection('members').doc(data.id).delete();
});
// ****************
// VOTING
// ****************
exports.getVoting = functions.https.onCall((data) => {
const { assemblyId, memberId } = data;
if (!assemblyId || !memberId) {
throw new functions.https.HttpsError(
'invalid-argument',
'Invalid or missing arguments'
);
}
let assembly;
return admin.firestore().collection('assemblies').doc(assemblyId).get()
.then(doc => {
assembly = { id: doc.id, ...doc.data()}
if (assembly.votes.includes(memberId)) {
throw new functions.https.HttpsError(
'aborted',
'voted'
);
}
const assemblyDateStatus = getDateStatus(assembly.initialDate, assembly.endDate);
if (assemblyDateStatus === 'expired') {
throw new functions.https.HttpsError(
'aborted',
assemblyDateStatus
);
}
if (assemblyDateStatus === 'upcoming') {
throw new functions.https.HttpsError(
'aborted',
assemblyDateStatus
);
}
return admin.firestore().collection('members').doc(memberId).get()
})
.then(doc => {
const member = { id: doc.id, ...doc.data()}
return { payload: { assembly, member }}
})
.catch(error => {
throw new functions.https.HttpsError(
'internal',
error.message
);
})
});
exports.vote = functions.https.onCall((data) => {
const { assemblyId, memberId, sectionVotes } = data;
return admin.firestore().collection('assemblies').doc(assemblyId).get()
.then(doc => {
const assembly = doc.data();
assembly.sections.forEach(section => {
section.options.forEach(option => {
if (sectionVotes[section.id].includes(option.id)) {
option.votes.push(memberId);
}
})
})
assembly.votes.push(memberId);
return admin.firestore().collection('assemblies').doc(assemblyId).set(assembly, { merge: true });
})
.then(() => ({ message: 'Vote success'}))
});
// ****************
// URLS
// ****************
exports.onAddMember = functions.firestore.document('members/{member}').onCreate((snapMember) => {
const memberData = snapMember.data();
return admin.firestore().collection('assemblies').where('church', '==', memberData.church).get()
.then(doc => {
let item = [];
doc.forEach(assemblySnap => {
admin.firestore().collection('urls').doc().set({
url: `https://idn-asambleas.web.app/votacion/${assemblySnap.id}/${snapMember.id}`,
memberId: snapMember.id,
assemblyId: assemblySnap.id
})
})
return item;
})
});
exports.onAddAssembly = functions.firestore.document('assemblies/{assembly}').onCreate((assemblySnap) => {
const assembliesData = assemblySnap.data();
return admin.firestore().collection('members').where('church', '==', assembliesData.church).get()
.then(doc => {
let item = [];
doc.forEach(memberSnap => {
admin.firestore().collection('urls').doc().set({
url: `https://idn-asambleas.web.app/votacion/${assemblySnap.id}/${memberSnap.id}`,
memberId: memberSnap.id,
assemblyId: assemblySnap.id
})
})
return item;
})
});
// ****************
// NOTIFICATIONS
// ****************
exports.onUpdateUrl = functions.firestore.document('urls/{member}').onUpdate((change) => {
const urlsData = change.after.data();
if (urlsData.memberId) {
return admin.firestore().collection('members').doc(urlsData.memberId).set({
urls: {
[urlsData.assemblyId]: urlsData.shortUrl
}
}, { merge: true })
}
});
exports.sendWhatsappMessage = functions.https.onCall((data, context) => {
if (!context.auth) {
throw unauthenticationError;
}
return admin.firestore().collection('members').doc(data.memberId).get()
.then(doc => {
const member = doc.data();
if (!member.phone) {
throw new functions.https.HttpsError(
'internal',
'User does not have phone number'
);
}
return twilioClient.messages.create({
from: 'whatsapp:+14155238886',
to: `whatsapp:${member.phone}`,
body: `${data.messge ? data.message : `Llegó la hora de votar. Puedes hacerlo en este link: ${member.urls[data.assemblyId]}`}`
})
})
.then(message => message.sid)
.catch(error => {
throw new functions.https.HttpsError(
'internal',
error.message
);
})
});
exports.sendSMSMessage = functions.https.onCall((data, context) => {
if (!context.auth) {
throw unauthenticationError;
}
return admin.firestore().collection('members').doc(data.memberId).get()
.then(doc => {
const member = doc.data();
if (!member.phone) {
throw new functions.https.HttpsError(
'internal',
'User does not have phone number'
);
}
return twilioClient.messages.create({
messagingServiceSid: TWILIO_SMS_SID,
to: `${member.phone}`,
body: `${data.messge ? data.message : `Somos la Iglesia del Nazareno de Coronado. Llegó la hora de votar. Puedes hacerlo en este link: ${member.urls[data.assemblyId]}`}`
})
})
.then(message => message.sid)
.catch(error => {
throw new functions.https.HttpsError(
'internal',
error.message
);
})
});