Skip to content

Commit

Permalink
Merge pull request #23 from SDOS-Winter2021/testing-fixes
Browse files Browse the repository at this point in the history
Testing fixes in cloud functions
  • Loading branch information
vishwesh-D-kumar authored Apr 14, 2021
2 parents bcc022c + 947292b commit 004fd2f
Showing 1 changed file with 136 additions and 84 deletions.
220 changes: 136 additions & 84 deletions App/functions/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable spaced-comment */
/* eslint-disable max-len */
/* eslint-disable eol-last */
/* eslint-disable no-var */
Expand All @@ -10,43 +9,68 @@ const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
// const url = 'https://testdb-cloudfn.firebaseio.com/'
const url = 'https://testfortls.firebaseio.com/'
// const url = 'https://tls-op-default-rtdb.firebaseio.com/'
// Create and Deploy Your First Cloud Functions
// https://firebase.google.com/docs/functions/write-firebase-functions
function deleteAllMatchingKey(table,key, childKey) {
const db_ref = admin.app().database(url).ref('InternalDb/'+table+'/');
db_ref.orderByChild(childKey).equalTo(key).once("value", function(snapshot) {
console.log('starting to remove from table '+table)
snapshot.forEach(function(child){
console.log(child.key);
child.ref.remove();

async function getURLFromPasscode(passCode){
const db_ref = admin.app().database(url).ref('InternalDb/Courses/');
let courseURL;
await db_ref.orderByChild("passCode").equalTo(passCode).once("value",
function(snapshot) {
courseURL = Object.keys(snapshot.val())[0].replace(' ', '');
},
function (errorObject) {
console.log("The read failed: " + errorObject.code);
}
);
return courseURL
}

async function deleteAllMatchingKey(table,key, childKey) {
const db_ref = admin.app().database(url).ref('InternalDb/'+table+'/');
db_ref.orderByChild(childKey).equalTo(key).once("value", function(snapshot) {
console.log('starting to remove from table '+table)
snapshot.forEach(function(child){
console.log(child.key);
child.ref.remove().then(()=>{
console.log()
});
});
}, function (errorObject) {
console.log("The read failed: " + errorObject.code);

}).then(()=>{
console.log("Done");
}).catch((error)=>{
console.log(error);
});
}, function (errorObject) {
console.log("The read failed: " + errorObject.code);

});
}

function deleteCourseHelper(passCode){
deleteAllMatchingKey('Courses',passCode, "passCode")
deleteAllMatchingKey('Announcements',passCode, "passCode")
deleteAllMatchingKey('KBC',passCode, "passCode")
deleteAllMatchingKey('KBCResponse',passCode, "passCode")
deleteAllMatchingKey('Feedback',passCode, "passCode")
deleteAllMatchingKey('FeedbackResponse',passCode, "passCode")
removeFromStudentList(passCode)
removeCourseFromFacultyList(passCode)
async function deleteCourseHelper(passCode, courseURL){
await deleteAllMatchingKey('Courses',passCode, "passCode")
await deleteAllMatchingKey('Announcements',passCode, "passCode")
await deleteAllMatchingKey('KBC',passCode, "passCode")
await deleteAllMatchingKey('KBCResponse',passCode, "passCode")
await deleteAllMatchingKey('Feedback',passCode, "passCode")
await deleteAllMatchingKey('FeedbackResponse',passCode, "passCode")
console.log("Starting remove from student list");
removeFromStudentList(courseURL);
console.log("Starting remove from faculty list");
removeCourseFromFacultyList(courseURL);
}
exports.deleteCourse = functions.https.onCall((data,context) => {

// flow : 1. del announcements
// 2. del student registerations ,faculty registrations
// getting all announcements and deleting them
// const passCode = req.body['passCode'];
const passCode = data.passCode;
console.log("Got passCode to delete "+ passCode)
deleteCourseHelper(passCode);
// res.send("Done deleting")
return 'done';

exports.deleteCourse = functions.https.onCall(async (data,context) => {

// flow : 1. del announcements
// 2. del student registerations ,faculty registrations
// getting all announcements and deleting them
const passCode = data.passCode;
console.log("Got passCode to delete "+ passCode)
let courseURL = await getURLFromPasscode(passCode);
await deleteCourseHelper(passCode, courseURL);
return 'done';
});

function removeFromStudentList(courseKey){
Expand All @@ -58,27 +82,40 @@ function removeFromStudentList(courseKey){
thisStudent.once("value", function(snapshot){
snapshot.forEach((el)=>{
if(el.val() === courseKey){
el.ref.remove();
el.ref.remove().then(()=>{
console.log();
});
}
})
}).then(()=>{
console.log();
})
})
}).then(()=>{
console.log();
})
}

function removeCourseFromFacultyList(courseKey){
const student = admin.app().database(url).ref('InternalDb/Faculty/');
student.once("value", function(snapshot){
snapshot.forEach(el=>{
let facultyKey = el.ref.path.pieces_.reverse()[0];
const thisStudent = admin.app().database().ref('InternalDb/Faculty/'+facultyKey+'/courses');
const thisStudent = admin.app().database(url).ref('InternalDb/Faculty/'+facultyKey+'/courses');
thisStudent.once("value", function(snapshot){
snapshot.forEach((el)=>{
if(el.val() === courseKey){
el.ref.remove();
el.ref.remove().then(()=>{
console.log();
});
}
})
}).then(()=>{
console.log();
})
})
}).then(()=>{
console.log();
})
}

Expand All @@ -87,8 +124,12 @@ function removeFromFacultyList(key){
faculty.once("value", function(snapshot){
snapshot.forEach((el)=>{
removeFromStudentList(el.val())
el.ref.remove();
el.ref.remove().then(()=>{
console.log();
});
})
}).then(()=>{
console.log();
})
}

Expand All @@ -113,7 +154,9 @@ exports.deleteStudent = functions.https.onCall((data, context) =>{
dbRef.once("value", function(snapshot){
if (snapshot.val()){
deleteStudentHelper(studentID);
snapshot.ref.remove();
snapshot.ref.remove().then(()=>{
console.log();
});
return "removed";
}
else{
Expand All @@ -122,6 +165,8 @@ exports.deleteStudent = functions.https.onCall((data, context) =>{
}, function (errorObject) {
console.log("The student read failed: " + errorObject.code);
return "Error";
}).then(()=>{
console.log();
});
admin
.auth()
Expand All @@ -135,64 +180,69 @@ exports.deleteStudent = functions.https.onCall((data, context) =>{
});

exports.deleteFaculty = functions.https.onCall((data,context) => {
// key = req.body['key'];
key = data.key
uid = data.uid
console.log("Faculty KEY "+ key )
console.log("recieved data")
console.log(data)
console.log(context)
db_ref = admin.app().database(url).ref('InternalDb/Faculty/'+key)
db_ref.once("value", function(snapshot)
{
console.log(snapshot.val());
if(snapshot.val()['courses']){
snapshot.val()['courses'].forEach(function(child)
// key = data.body['key'];
let key = data.key;
let userUID = data.uid;
console.log("Faculty KEY "+ key )
console.log("recieved data")
console.log(data)
console.log(context)
db_ref = admin.app().database(url).ref('InternalDb/Faculty/'+key)
db_ref.once("value", function(snapshot)
{
console.log("Removing course of key " + child);
course_ref = admin.app().database(url).ref('InternalDb/Courses/'+child)
course_ref.once("value",
function(courseSnapshot){
var passcode = courseSnapshot.val()['passCode'];
deleteCourseHelper(passcode);
console.log(snapshot.val());
if(snapshot.val()['courses']){
snapshot.val()['courses'].forEach(function(child)
{
console.log("Removing course of key " + child);
course_ref = admin.app().database(url).ref('InternalDb/Courses/'+child)
course_ref.once("value",
function(courseSnapshot){
if(courseSnapshot.val()){
var passcode = courseSnapshot.val()['passCode'];
deleteCourseHelper(passcode, child);
}
}
,
function (errorObject) {
console.log("The Course read failed: " + errorObject.code);
// res.send("ERROR");
return "Error";
}
,
function (errorObject) {
console.log("The Course read failed: " + errorObject.code);
// res.send("ERROR");
return "Error";
}
);
).then(()=>{
console.log();
});
});
delCoursesOfFaculty(key);
snapshot.ref.remove().then(()=>{
console.log();
});
context.send("removed");
return "removed"
}
else{
// res.send("error while removing");
return "error while removing"
}
}, function (errorObject) {
console.log("The faculty read failed: " + errorObject.code);
// res.send("ERROR")
return "Error";
}).then(()=>{
console.log();
});
delCoursesOfFaculty(key);
snapshot.ref.remove();
// res.send("removed");
return "removed"
}
else{
// res.send("error while removing");
return "error while removing"
}
}, function (errorObject) {
console.log("The faculty read failed: " + errorObject.code);
// res.send("ERROR")
return "Error";
});

admin
admin
.auth()
.deleteUser(uid)
.deleteUser(userUID)
.then(() => {
console.log('Successfully deleted user from firebase auth');
})
.catch((error) => {
console.log('Error deleting user from firebase auth:', error);
});
});


}
);

exports.sendNotificationToTopic_New = functions.firestore
.document('Course/{uid}')
.onWrite(async (event) => {
Expand All @@ -207,12 +257,12 @@ exports.sendNotificationToTopic_New = functions.firestore
topic: 'Course',
};

console.log(message);
const response = await admin.messaging().send(message);
console.log(response);
});


exports.sendPushNotification = functions.database
exports.sendPushNotification = functions.database
.ref('InternalDb/Student/{sid}') // Put your path here with the params.
.onWrite(async (change, context) => {
try {
Expand All @@ -236,3 +286,5 @@ exports.sendPushNotification = functions.database
}
});



0 comments on commit 004fd2f

Please sign in to comment.