Created
March 8, 2020 16:09
-
-
Save Rajdeep-Das/894ac662635a8fc07031d03826bd04fc to your computer and use it in GitHub Desktop.
FireBase Cloud Function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const functions = require('firebase-functions'); | |
const express = require('express'); | |
const path = require('path'); | |
// The Firebase Admin SDK to access the Firebase Realtime Database. | |
const admin = require('firebase-admin'); | |
admin.initializeApp() | |
/* This function may break now*/ | |
// exports.onUserCreated = functions.firestore.collection("users").onCreate((change, context)=>{ | |
// return console.log(change); | |
// }); | |
const app = express(); | |
// Test Start | |
// app.use(express.static(path.join(__dirname, 'build'))); | |
// app.get('/admin', (req, res) => { | |
// res.sendFile(path.join(__dirname, 'build', 'index.html')); | |
// }); | |
// Test End | |
app.get('/timestamp', (request, response) => { | |
response.send(`${Date.now()}`); | |
}); | |
app.get('/updateday', (request, response) => { | |
const users = []; | |
const question = []; | |
var db = admin.firestore(); | |
var docRefQuestion = db.collection("questions").get().then((snapShot) => { | |
snapShot.forEach((doc) => { | |
console.log(doc.id, "=>", doc.data()); | |
question.push(doc.data().id); | |
}); | |
response.send("not working") | |
return ""; | |
}).catch((error) => { | |
console.log(error); | |
}); | |
var docRefUser = db.collection("users").get().then((snapShot) => { | |
snapShot.forEach((doc) => { | |
const currentUser = doc.data(); | |
var QuidRef = db.collection('users').doc(currentUser.uid); | |
const setWithMerge = QuidRef.update({ | |
questionId: `${question[0]}`, | |
isAttempt: false | |
}); | |
}); | |
// All set Sucess | |
response.send(`${question[0]}`); | |
return ""; | |
}).catch((error) => { | |
// all failed error | |
response.send(error); | |
}); | |
}); | |
app.get('/updatedayv2', (request, response) => { | |
const users = []; | |
const question = []; | |
var db = admin.firestore(); | |
var docRefQuestion = db.collection("questions").get().then((snapShot) => { | |
snapShot.forEach((doc) => { | |
console.log(doc.id, "=>", doc.data()); | |
question.push(doc.data().id); | |
}) | |
// eslint-disable-next-line promise/no-nesting | |
var docRefUser = db.collection("users").get().then((snapShot) => { | |
snapShot.forEach((doc) => { | |
const currentUser = doc.data(); | |
var QuidRef = db.collection('users').doc(currentUser.uid); | |
const setWithMerge = QuidRef.update({ | |
questionId: `${question[0]}`, | |
isAttempt: false | |
}); | |
}); | |
// All set Sucess | |
response.send(`${question[0]}`); | |
return ""; | |
}).catch((error) => { | |
// all failed error | |
response.send(error); | |
}); | |
return ""; | |
}).catch((error) => { | |
console.log(error); | |
}); | |
}); | |
// Updatw With Question offset | |
// Trigger by https://cron-job.org/ | |
app.get('/updatedayv3', async (request, response) => { | |
const users = []; | |
const question = []; | |
var myoffset = -1; | |
var db = admin.firestore(); | |
var docRef = db.collection("last_question_set_to_user_id").doc("1"); | |
await docRef.get().then((doc)=>{ | |
myoffset = doc.data().offset; | |
return null | |
}).catch((error)=>{ | |
console.log(error); | |
}); | |
var docRefQuestion = db.collection("questions").doc(myoffset.toString()).get().then((doc) => { | |
// snapShot.forEach((doc) => { | |
// console.log(doc.id, "=>", doc.data()); | |
// question.push(doc.data().id); | |
// }) | |
console.log(doc.data()); | |
question.push(doc.data().id); | |
// eslint-disable-next-line promise/no-nesting | |
var docRefUser = db.collection("users").get().then((snapShot) => { | |
snapShot.forEach((doc) => { | |
const currentUser = doc.data(); | |
var QuidRef = db.collection('users').doc(currentUser.uid); | |
const setWithMerge = QuidRef.update({ | |
questionId: `${question[0]}`, | |
isAttempt: false | |
}); | |
}); | |
docRef.update({offset:myoffset+1}); | |
// All set Sucess | |
response.send(`${question[0]}`); | |
return ""; | |
}).catch((error) => { | |
// all failed error | |
response.send(error); | |
}); | |
return ""; | |
}).catch((error) => { | |
console.log(error); | |
}); | |
}); | |
app.get('/updateLeaderBoard', (request, response) => { | |
var db = admin.firestore(); | |
// Read all users and store them to leaderBoard | |
var docRefUser = db.collection("users").get().then((snapShot) => { | |
snapShot.forEach((doc) => { | |
const currentUser = doc.data(); | |
// eslint-disable-next-line promise/no-nesting | |
var QuidRef = db.collection('leaderboard').doc(currentUser.uid).set({ | |
points: `${currentUser.points}` | |
}).then(() => { | |
// Avoid Using Nesting Promises This method may not required. Only .Catch() is enough and each promise must return value | |
console.log("Document successfully written!"); | |
return ''; | |
}) | |
.catch((error) => { | |
console.error("Error writing document: ", error); | |
return ''; | |
}); | |
}); | |
// All set Sucess | |
response.send('Ok Leader Board Updated'); | |
return ""; | |
}).catch((error) => { | |
// all failed error | |
response.send(error); | |
}); | |
}); | |
// Get qestion from qestion buffer and set to main db | |
// Trigger by https://cron-job.org/ | |
app.get('/transfer', (request, response) => { | |
var myoffset = -1; | |
var db = admin.firestore(); | |
var docRef = db.collection("last_add_question_main").doc("1"); | |
docRef.get().then((doc) => { | |
if (doc.exists) { | |
myoffset = doc.data().offset; | |
console.log("Document data:", doc.data()); | |
// Now get the question from question_buffer | |
var docRef_2 = db.collection("question_buffer").doc(myoffset.toString()); | |
docRef_2.get().then((Qdoc) => { | |
if (Qdoc.exists) { | |
console.log(Qdoc.data()); | |
// Now Save the Document(Question) to main DB; | |
var docRef_3 = db.collection("questions").doc(Qdoc.data().id.toString()); | |
docRef_3.set(Qdoc.data()); | |
myoffset = myoffset + 1; | |
docRef.update({ offset: myoffset }) | |
} else { | |
console.log("No Question found! Wiht id = ", myoffset); | |
} | |
return ""; | |
}).catch((_error) => { | |
}); | |
} else { | |
// doc.data() will be undefined in this case | |
console.log("No such document!"); | |
} | |
return "" | |
}).catch((error) => { | |
console.log("Error getting document:", error); | |
}); | |
response.send('Transfer Complete'); | |
});//end get reques function | |
exports.app = functions.https.onRequest(app); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment