1- /* eslint-disable spaced-comment */
21/* eslint-disable max-len */
32/* eslint-disable eol-last */
43/* eslint-disable no-var */
@@ -10,43 +9,68 @@ const admin = require('firebase-admin');
109admin . initializeApp ( functions . config ( ) . firebase ) ;
1110// const url = 'https://testdb-cloudfn.firebaseio.com/'
1211const url = 'https://testfortls.firebaseio.com/'
12+ // const url = 'https://tls-op-default-rtdb.firebaseio.com/'
1313// Create and Deploy Your First Cloud Functions
1414// 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 ) ;
2247 } ) ;
23- } , function ( errorObject ) {
24- console . log ( "The read failed: " + errorObject . code ) ;
2548
26- } ) ;
2749}
2850
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 ) ;
3862}
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' ;
5074} ) ;
5175
5276function removeFromStudentList ( courseKey ) {
@@ -58,27 +82,40 @@ function removeFromStudentList(courseKey){
5882 thisStudent . once ( "value" , function ( snapshot ) {
5983 snapshot . forEach ( ( el ) => {
6084 if ( el . val ( ) === courseKey ) {
61- el . ref . remove ( ) ;
85+ el . ref . remove ( ) . then ( ( ) => {
86+ console . log ( ) ;
87+ } ) ;
6288 }
6389 } )
90+ } ) . then ( ( ) => {
91+ console . log ( ) ;
6492 } )
6593 } )
94+ } ) . then ( ( ) => {
95+ console . log ( ) ;
6696 } )
6797}
98+
6899function removeCourseFromFacultyList ( courseKey ) {
69100 const student = admin . app ( ) . database ( url ) . ref ( 'InternalDb/Faculty/' ) ;
70101 student . once ( "value" , function ( snapshot ) {
71102 snapshot . forEach ( el => {
72103 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' ) ;
74105 thisStudent . once ( "value" , function ( snapshot ) {
75106 snapshot . forEach ( ( el ) => {
76107 if ( el . val ( ) === courseKey ) {
77- el . ref . remove ( ) ;
108+ el . ref . remove ( ) . then ( ( ) => {
109+ console . log ( ) ;
110+ } ) ;
78111 }
79112 } )
113+ } ) . then ( ( ) => {
114+ console . log ( ) ;
80115 } )
81116 } )
117+ } ) . then ( ( ) => {
118+ console . log ( ) ;
82119 } )
83120}
84121
@@ -87,8 +124,12 @@ function removeFromFacultyList(key){
87124 faculty . once ( "value" , function ( snapshot ) {
88125 snapshot . forEach ( ( el ) => {
89126 removeFromStudentList ( el . val ( ) )
90- el . ref . remove ( ) ;
127+ el . ref . remove ( ) . then ( ( ) => {
128+ console . log ( ) ;
129+ } ) ;
91130 } )
131+ } ) . then ( ( ) => {
132+ console . log ( ) ;
92133 } )
93134}
94135
@@ -113,7 +154,9 @@ exports.deleteStudent = functions.https.onCall((data, context) =>{
113154 dbRef . once ( "value" , function ( snapshot ) {
114155 if ( snapshot . val ( ) ) {
115156 deleteStudentHelper ( studentID ) ;
116- snapshot . ref . remove ( ) ;
157+ snapshot . ref . remove ( ) . then ( ( ) => {
158+ console . log ( ) ;
159+ } ) ;
117160 return "removed" ;
118161 }
119162 else {
@@ -122,6 +165,8 @@ exports.deleteStudent = functions.https.onCall((data, context) =>{
122165 } , function ( errorObject ) {
123166 console . log ( "The student read failed: " + errorObject . code ) ;
124167 return "Error" ;
168+ } ) . then ( ( ) => {
169+ console . log ( ) ;
125170 } ) ;
126171 admin
127172 . auth ( )
@@ -135,64 +180,69 @@ exports.deleteStudent = functions.https.onCall((data, context) =>{
135180} ) ;
136181
137182exports . 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 )
151192 {
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" ;
158211 }
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 ( ) ;
166233 } ) ;
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
183235 . auth ( )
184- . deleteUser ( uid )
236+ . deleteUser ( userUID )
185237 . then ( ( ) => {
186238 console . log ( 'Successfully deleted user from firebase auth' ) ;
187239 } )
188240 . catch ( ( error ) => {
189241 console . log ( 'Error deleting user from firebase auth:' , error ) ;
190242 } ) ;
243+ } ) ;
191244
192245
193- }
194- ) ;
195-
196246exports . sendNotificationToTopic_New = functions . firestore
197247 . document ( 'Course/{uid}' )
198248 . onWrite ( async ( event ) => {
@@ -207,12 +257,12 @@ exports.sendNotificationToTopic_New = functions.firestore
207257 topic : 'Course' ,
208258 } ;
209259
260+ console . log ( message ) ;
210261 const response = await admin . messaging ( ) . send ( message ) ;
211262 console . log ( response ) ;
212263 } ) ;
213264
214-
215- exports . sendPushNotification = functions . database
265+ exports . sendPushNotification = functions . database
216266 . ref ( 'InternalDb/Student/{sid}' ) // Put your path here with the params.
217267 . onWrite ( async ( change , context ) => {
218268 try {
@@ -236,3 +286,5 @@ exports.sendPushNotification = functions.database
236286 }
237287 } ) ;
238288
289+
290+
0 commit comments