1
- /* eslint-disable spaced-comment */
2
1
/* eslint-disable max-len */
3
2
/* eslint-disable eol-last */
4
3
/* eslint-disable no-var */
@@ -10,43 +9,68 @@ const admin = require('firebase-admin');
10
9
admin . initializeApp ( functions . config ( ) . firebase ) ;
11
10
// const url = 'https://testdb-cloudfn.firebaseio.com/'
12
11
const url = 'https://testfortls.firebaseio.com/'
12
+ // const url = 'https://tls-op-default-rtdb.firebaseio.com/'
13
13
// Create and Deploy Your First Cloud Functions
14
14
// https://firebase.google.com/docs/functions/write-firebase-functions
15
- function deleteAllMatchingKey ( table , key , childKey ) {
16
- const db_ref = admin . app ( ) . database ( url ) . ref ( 'InternalDb/' + table + '/' ) ;
17
- db_ref . orderByChild ( childKey ) . equalTo ( key ) . once ( "value" , function ( snapshot ) {
18
- console . log ( 'starting to remove from table ' + table )
19
- snapshot . forEach ( function ( child ) {
20
- console . log ( child . key ) ;
21
- child . ref . remove ( ) ;
15
+
16
+ async function getURLFromPasscode ( passCode ) {
17
+ const db_ref = admin . app ( ) . database ( url ) . ref ( 'InternalDb/Courses/' ) ;
18
+ let courseURL ;
19
+ await db_ref . orderByChild ( "passCode" ) . equalTo ( passCode ) . once ( "value" ,
20
+ function ( snapshot ) {
21
+ courseURL = Object . keys ( snapshot . val ( ) ) [ 0 ] . replace ( ' ' , '' ) ;
22
+ } ,
23
+ function ( errorObject ) {
24
+ console . log ( "The read failed: " + errorObject . code ) ;
25
+ }
26
+ ) ;
27
+ return courseURL
28
+ }
29
+
30
+ async function deleteAllMatchingKey ( table , key , childKey ) {
31
+ const db_ref = admin . app ( ) . database ( url ) . ref ( 'InternalDb/' + table + '/' ) ;
32
+ db_ref . orderByChild ( childKey ) . equalTo ( key ) . once ( "value" , function ( snapshot ) {
33
+ console . log ( 'starting to remove from table ' + table )
34
+ snapshot . forEach ( function ( child ) {
35
+ console . log ( child . key ) ;
36
+ child . ref . remove ( ) . then ( ( ) => {
37
+ console . log ( )
38
+ } ) ;
39
+ } ) ;
40
+ } , function ( errorObject ) {
41
+ console . log ( "The read failed: " + errorObject . code ) ;
42
+
43
+ } ) . then ( ( ) => {
44
+ console . log ( "Done" ) ;
45
+ } ) . catch ( ( error ) => {
46
+ console . log ( error ) ;
22
47
} ) ;
23
- } , function ( errorObject ) {
24
- console . log ( "The read failed: " + errorObject . code ) ;
25
48
26
- } ) ;
27
49
}
28
50
29
- function deleteCourseHelper ( passCode ) {
30
- deleteAllMatchingKey ( 'Courses' , passCode , "passCode" )
31
- deleteAllMatchingKey ( 'Announcements' , passCode , "passCode" )
32
- deleteAllMatchingKey ( 'KBC' , passCode , "passCode" )
33
- deleteAllMatchingKey ( 'KBCResponse' , passCode , "passCode" )
34
- deleteAllMatchingKey ( 'Feedback' , passCode , "passCode" )
35
- deleteAllMatchingKey ( 'FeedbackResponse' , passCode , "passCode" )
36
- removeFromStudentList ( passCode )
37
- removeCourseFromFacultyList ( passCode )
51
+ async function deleteCourseHelper ( passCode , courseURL ) {
52
+ await deleteAllMatchingKey ( 'Courses' , passCode , "passCode" )
53
+ await deleteAllMatchingKey ( 'Announcements' , passCode , "passCode" )
54
+ await deleteAllMatchingKey ( 'KBC' , passCode , "passCode" )
55
+ await deleteAllMatchingKey ( 'KBCResponse' , passCode , "passCode" )
56
+ await deleteAllMatchingKey ( 'Feedback' , passCode , "passCode" )
57
+ await deleteAllMatchingKey ( 'FeedbackResponse' , passCode , "passCode" )
58
+ console . log ( "Starting remove from student list" ) ;
59
+ removeFromStudentList ( courseURL ) ;
60
+ console . log ( "Starting remove from faculty list" ) ;
61
+ removeCourseFromFacultyList ( courseURL ) ;
38
62
}
39
- exports . deleteCourse = functions . https . onCall ( ( data , context ) => {
40
-
41
- // flow : 1. del announcements
42
- // 2. del student registerations ,faculty registrations
43
- // getting all announcements and deleting them
44
- // const passCode = req.body['passCode'];
45
- const passCode = data . passCode ;
46
- console . log ( "Got passCode to delete " + passCode )
47
- deleteCourseHelper ( passCode ) ;
48
- // res.send("Done deleting")
49
- return 'done' ;
63
+
64
+ exports . deleteCourse = functions . https . onCall ( async ( data , context ) => {
65
+
66
+ // flow : 1. del announcements
67
+ // 2. del student registerations ,faculty registrations
68
+ // getting all announcements and deleting them
69
+ const passCode = data . passCode ;
70
+ console . log ( "Got passCode to delete " + passCode )
71
+ let courseURL = await getURLFromPasscode ( passCode ) ;
72
+ await deleteCourseHelper ( passCode , courseURL ) ;
73
+ return 'done' ;
50
74
} ) ;
51
75
52
76
function removeFromStudentList ( courseKey ) {
@@ -58,27 +82,40 @@ function removeFromStudentList(courseKey){
58
82
thisStudent . once ( "value" , function ( snapshot ) {
59
83
snapshot . forEach ( ( el ) => {
60
84
if ( el . val ( ) === courseKey ) {
61
- el . ref . remove ( ) ;
85
+ el . ref . remove ( ) . then ( ( ) => {
86
+ console . log ( ) ;
87
+ } ) ;
62
88
}
63
89
} )
90
+ } ) . then ( ( ) => {
91
+ console . log ( ) ;
64
92
} )
65
93
} )
94
+ } ) . then ( ( ) => {
95
+ console . log ( ) ;
66
96
} )
67
97
}
98
+
68
99
function removeCourseFromFacultyList ( courseKey ) {
69
100
const student = admin . app ( ) . database ( url ) . ref ( 'InternalDb/Faculty/' ) ;
70
101
student . once ( "value" , function ( snapshot ) {
71
102
snapshot . forEach ( el => {
72
103
let facultyKey = el . ref . path . pieces_ . reverse ( ) [ 0 ] ;
73
- const thisStudent = admin . app ( ) . database ( ) . ref ( 'InternalDb/Faculty/' + facultyKey + '/courses' ) ;
104
+ const thisStudent = admin . app ( ) . database ( url ) . ref ( 'InternalDb/Faculty/' + facultyKey + '/courses' ) ;
74
105
thisStudent . once ( "value" , function ( snapshot ) {
75
106
snapshot . forEach ( ( el ) => {
76
107
if ( el . val ( ) === courseKey ) {
77
- el . ref . remove ( ) ;
108
+ el . ref . remove ( ) . then ( ( ) => {
109
+ console . log ( ) ;
110
+ } ) ;
78
111
}
79
112
} )
113
+ } ) . then ( ( ) => {
114
+ console . log ( ) ;
80
115
} )
81
116
} )
117
+ } ) . then ( ( ) => {
118
+ console . log ( ) ;
82
119
} )
83
120
}
84
121
@@ -87,8 +124,12 @@ function removeFromFacultyList(key){
87
124
faculty . once ( "value" , function ( snapshot ) {
88
125
snapshot . forEach ( ( el ) => {
89
126
removeFromStudentList ( el . val ( ) )
90
- el . ref . remove ( ) ;
127
+ el . ref . remove ( ) . then ( ( ) => {
128
+ console . log ( ) ;
129
+ } ) ;
91
130
} )
131
+ } ) . then ( ( ) => {
132
+ console . log ( ) ;
92
133
} )
93
134
}
94
135
@@ -113,7 +154,9 @@ exports.deleteStudent = functions.https.onCall((data, context) =>{
113
154
dbRef . once ( "value" , function ( snapshot ) {
114
155
if ( snapshot . val ( ) ) {
115
156
deleteStudentHelper ( studentID ) ;
116
- snapshot . ref . remove ( ) ;
157
+ snapshot . ref . remove ( ) . then ( ( ) => {
158
+ console . log ( ) ;
159
+ } ) ;
117
160
return "removed" ;
118
161
}
119
162
else {
@@ -122,6 +165,8 @@ exports.deleteStudent = functions.https.onCall((data, context) =>{
122
165
} , function ( errorObject ) {
123
166
console . log ( "The student read failed: " + errorObject . code ) ;
124
167
return "Error" ;
168
+ } ) . then ( ( ) => {
169
+ console . log ( ) ;
125
170
} ) ;
126
171
admin
127
172
. auth ( )
@@ -135,64 +180,69 @@ exports.deleteStudent = functions.https.onCall((data, context) =>{
135
180
} ) ;
136
181
137
182
exports . deleteFaculty = functions . https . onCall ( ( data , context ) => {
138
- // key = req.body['key'];
139
- key = data . key
140
- uid = data . uid
141
- console . log ( "Faculty KEY " + key )
142
- console . log ( "recieved data" )
143
- console . log ( data )
144
- console . log ( context )
145
- db_ref = admin . app ( ) . database ( url ) . ref ( 'InternalDb/Faculty/' + key )
146
- db_ref . once ( "value" , function ( snapshot )
147
- {
148
- console . log ( snapshot . val ( ) ) ;
149
- if ( snapshot . val ( ) [ 'courses' ] ) {
150
- snapshot . val ( ) [ 'courses' ] . forEach ( function ( child )
183
+ // key = data.body['key'];
184
+ let key = data . key ;
185
+ let userUID = data . uid ;
186
+ console . log ( "Faculty KEY " + key )
187
+ console . log ( "recieved data" )
188
+ console . log ( data )
189
+ console . log ( context )
190
+ db_ref = admin . app ( ) . database ( url ) . ref ( 'InternalDb/Faculty/' + key )
191
+ db_ref . once ( "value" , function ( snapshot )
151
192
{
152
- console . log ( "Removing course of key " + child ) ;
153
- course_ref = admin . app ( ) . database ( url ) . ref ( 'InternalDb/Courses/' + child )
154
- course_ref . once ( "value" ,
155
- function ( courseSnapshot ) {
156
- var passcode = courseSnapshot . val ( ) [ 'passCode' ] ;
157
- deleteCourseHelper ( passcode ) ;
193
+ console . log ( snapshot . val ( ) ) ;
194
+ if ( snapshot . val ( ) [ 'courses' ] ) {
195
+ snapshot . val ( ) [ 'courses' ] . forEach ( function ( child )
196
+ {
197
+ console . log ( "Removing course of key " + child ) ;
198
+ course_ref = admin . app ( ) . database ( url ) . ref ( 'InternalDb/Courses/' + child )
199
+ course_ref . once ( "value" ,
200
+ function ( courseSnapshot ) {
201
+ if ( courseSnapshot . val ( ) ) {
202
+ var passcode = courseSnapshot . val ( ) [ 'passCode' ] ;
203
+ deleteCourseHelper ( passcode , child ) ;
204
+ }
205
+ }
206
+ ,
207
+ function ( errorObject ) {
208
+ console . log ( "The Course read failed: " + errorObject . code ) ;
209
+ // res.send("ERROR");
210
+ return "Error" ;
158
211
}
159
- ,
160
- function ( errorObject ) {
161
- console . log ( "The Course read failed: " + errorObject . code ) ;
162
- // res.send("ERROR");
163
- return "Error" ;
164
- }
165
- ) ;
212
+ ) . then ( ( ) => {
213
+ console . log ( ) ;
214
+ } ) ;
215
+ } ) ;
216
+ delCoursesOfFaculty ( key ) ;
217
+ snapshot . ref . remove ( ) . then ( ( ) => {
218
+ console . log ( ) ;
219
+ } ) ;
220
+ context . send ( "removed" ) ;
221
+ return "removed"
222
+ }
223
+ else {
224
+ // res.send("error while removing");
225
+ return "error while removing"
226
+ }
227
+ } , function ( errorObject ) {
228
+ console . log ( "The faculty read failed: " + errorObject . code ) ;
229
+ // res.send("ERROR")
230
+ return "Error" ;
231
+ } ) . then ( ( ) => {
232
+ console . log ( ) ;
166
233
} ) ;
167
- delCoursesOfFaculty ( key ) ;
168
- snapshot . ref . remove ( ) ;
169
- // res.send("removed");
170
- return "removed"
171
- }
172
- else {
173
- // res.send("error while removing");
174
- return "error while removing"
175
- }
176
- } , function ( errorObject ) {
177
- console . log ( "The faculty read failed: " + errorObject . code ) ;
178
- // res.send("ERROR")
179
- return "Error" ;
180
- } ) ;
181
-
182
- admin
234
+ admin
183
235
. auth ( )
184
- . deleteUser ( uid )
236
+ . deleteUser ( userUID )
185
237
. then ( ( ) => {
186
238
console . log ( 'Successfully deleted user from firebase auth' ) ;
187
239
} )
188
240
. catch ( ( error ) => {
189
241
console . log ( 'Error deleting user from firebase auth:' , error ) ;
190
242
} ) ;
243
+ } ) ;
191
244
192
245
193
- }
194
- ) ;
195
-
196
246
exports . sendNotificationToTopic_New = functions . firestore
197
247
. document ( 'Course/{uid}' )
198
248
. onWrite ( async ( event ) => {
@@ -207,12 +257,12 @@ exports.sendNotificationToTopic_New = functions.firestore
207
257
topic : 'Course' ,
208
258
} ;
209
259
260
+ console . log ( message ) ;
210
261
const response = await admin . messaging ( ) . send ( message ) ;
211
262
console . log ( response ) ;
212
263
} ) ;
213
264
214
-
215
- exports . sendPushNotification = functions . database
265
+ exports . sendPushNotification = functions . database
216
266
. ref ( 'InternalDb/Student/{sid}' ) // Put your path here with the params.
217
267
. onWrite ( async ( change , context ) => {
218
268
try {
@@ -236,3 +286,5 @@ exports.sendPushNotification = functions.database
236
286
}
237
287
} ) ;
238
288
289
+
290
+
0 commit comments