Browse Source

Still some kinks to work out

wip-firestore
Atridad Lahiji 1 year ago
parent
commit
1f6b2e8332
  1. 60
      backend/db.js
  2. 2
      components/workflow/HomeScreen.vue
  3. 53
      functions/api.js

60
backend/db.js

@ -15,15 +15,13 @@ along with Pollo. If not, see <https://www.gnu.org/licenses/>. */
import firebase from '@firebase/app'
const fetchQuestions = function (context) {
console.log('fetchQuestions')
console.log(context.pollEventObject.pollid)
// Define a database reference for user questions based on a specific pollid and creates a Firebase watcher
const qRef = firebase.firestore().collection('questions').where('pollid', '==', context.pollEventObject.pollid)
qRef.onSnapshot((doc) => {
const tempQuestionKeys = []
doc.forEach((questionSnapshot) => {
tempQuestionKeys.push({
qid: questionSnapshot.key,
qid: questionSnapshot.id,
qname: questionSnapshot.data().qname,
qtype: (questionSnapshot.data().qtype === 'mc') ? 'Multiple Choice' : ((questionSnapshot.data().qtype === 'tf') ? 'True/False' : 'Short Answer'),
qobj: questionSnapshot.data()
@ -35,8 +33,6 @@ const fetchQuestions = function (context) {
}
const fetchSessions = function (context) {
console.log('fetchSessions')
console.log(context.pollEventObject.pollid)
// Define a database reference for sessions based on a specific pollid and creates a Firebase watcher
const sessionRef = firebase.firestore().collection('sessions').where('pollid', '==', context.pollEventObject.pollid)
sessionRef.onSnapshot((doc) => {
@ -48,8 +44,8 @@ const fetchSessions = function (context) {
})
doc.forEach((sessionSnapshot) => {
if (sessionSnapshot.key) {
tempSessionKey = sessionSnapshot.key
if (sessionSnapshot.id) {
tempSessionKey = sessionSnapshot.id
}
})
@ -70,25 +66,24 @@ const fetchPolls = function (context) {
doc.forEach((pollSnapshot) => {
// Push new poll object to an array
tempPollKeys.push({
pollid: pollSnapshot.key,
pollid: pollSnapshot.id,
pollname: pollSnapshot.data().pollname,
pollobj: pollSnapshot.data(),
sessionid: '',
deleted: false
sessionid: ''
})
})
context.pollKeys = tempPollKeys
})
}
const fetchPollSessions = function (context, pollKeys) {
const fetchPollSessions = function (context) {
// Getting session IDs for all polls if they exist
// Define database reference for sessions
const sessionUserRef = firebase.firestore().collection('sessions').where('owneruid', '==', context.GET_LOGGED_IN_USER.uid)
// Create Firebase watcher for sessions
sessionUserRef.onSnapshot((doc) => {
// If the sessions list on the DB changed at all, loop through Polls
pollKeys.forEach((poll, index, pollKeys) => {
context.pollKeys.forEach((poll, index, pollKeys) => {
// Define database reference for sessions with a given pollid
const sessionRef = firebase.firestore().collection('sessions').where('pollid', '==', poll.pollid)
// Get the session(s) with the pollid provided
@ -96,8 +91,8 @@ const fetchPollSessions = function (context, pollKeys) {
let tempSessionKey = ''
doc.forEach((sessionSnapshot) => {
// If the session has a sessionid, assign that ID to the poll object
if (sessionSnapshot.key) {
tempSessionKey = sessionSnapshot.key
if (sessionSnapshot.id) {
tempSessionKey = sessionSnapshot.id
}
})
context.$set(pollKeys[index], 'sessionid', tempSessionKey)
@ -110,17 +105,13 @@ const fetchFinalResults = function (context, isActivated) {
// Check if the dialog has been activated
if (isActivated) {
// Define a database reference for user questions based on a specific qid and creates a Firebase watcher
const sessionRef = firebase.database().ref(`/users/${context.GET_LOGGED_IN_USER.uid}/questions/${context.qid}`)
sessionRef.on('value', (snapshot) => {
const sessionRef = firebase.firestore().collection('questions').doc(context.qid)
sessionRef.onSnapshot((doc) => {
context.qobj = {
qid: snapshot.key,
question: snapshot.val()
qid: doc.id,
question: doc.data()
}
})
} else {
// If the dialog is deactivated, destroy the watcher
const sessionRef = firebase.database().ref(`/users/${context.GET_LOGGED_IN_USER.uid}/questions/${context.qid}`)
sessionRef.off('value')
}
}
@ -128,12 +119,12 @@ const fetchLiveResults = function (context, isActivated) {
// Check if the dialog has been activated
if (isActivated) {
// Define a database reference for sessions based on a specific sessionid and creates a Firebase watcher
const sessionRef = firebase.database().ref(`/sessions/${context.sessionid}`)
sessionRef.on('value', (snapshot) => {
const sessionRef = firebase.firestore().collection('sessions').doc(context.sessionid)
sessionRef.onSnapshot((doc) => {
try {
context.activeQuestion = {
activequestion: snapshot.val().activequestion,
activeqid: snapshot.val().activeqid
activequestion: doc.data().activequestion,
activeqid: doc.data().activeqid
}
} catch {
context.activeQuestion = {
@ -143,20 +134,17 @@ const fetchLiveResults = function (context, isActivated) {
context.closeLiveResultsViewDialog()
}
})
} else {
const sessionRef = firebase.database().ref(`/sessions/${context.sessionid}`)
sessionRef.off('value')
}
}
const fetchActiveQuestion = function (context) {
// Define a database reference for sessions based on a specific sessionid and creates a Firebase watcher
const sessionRef = firebase.database().ref(`/sessions/${context.sessionid}`)
sessionRef.on('value', (snapshot) => {
const sessionRef = firebase.firestore().collection('sessions').doc(context.sessionid)
sessionRef.onSnapshot((doc) => {
try {
context.activeQuestion = {
activequestion: snapshot.val().activequestion,
activeqid: snapshot.val().activeqid
activequestion: doc.data().activequestion,
activeqid: doc.data().activeqid
}
} catch {
context.activeQuestion = {
@ -171,10 +159,10 @@ const fetchActiveQuestion = function (context) {
const checkSessionExists = function (context) {
if (context.pollCode.length > 0) {
// Defines a database reference to a specific session based on a given session ID (poll code)
const sessionRef = firebase.database().ref(`/sessions/${context.pollCode.toUpperCase()}`)
const sessionRef = firebase.firestore().collection('sessions').doc(context.pollCode.toUpperCase())
// Gets the session associated with the provided session ID
sessionRef.once('value', function (snapshot) {
if (snapshot.hasChildren()) {
sessionRef.get().then((doc) => {
if (doc.exists) {
context.id = context.pollCode.toUpperCase()
context.pollCode = ''
context.studentPollViewDialog = true

2
components/workflow/HomeScreen.vue

@ -212,7 +212,7 @@ export default {
// If a user is authenticated, create a watcher for polls
if (this.GET_LOGGED_IN_USER) {
fetchPolls(this)
fetchPollSessions(this, this.pollKeys)
fetchPollSessions(this)
}
},
methods: {

53
functions/api.js

@ -139,7 +139,7 @@ module.exports.question = {
},
edit (context, qid, qobj) {
// Edits the poll with the provided qid
return firestore.collection('questions').doc('qid').set(qobj).then(() => {
return firestore.collection('questions').doc(qid).set(qobj).then(() => {
// The poll was edited
console.log('Edit success!');
return { status: 'success', code: 200, message: 'Question Edited' };
@ -166,7 +166,7 @@ module.exports.session = {
create (context, pollid) {
// Push a new session object with the current user's UID and pollid
return firestore.collection('sessions').where('pollid', '==', pollid).get().then(function(doc) {
if (!doc) {
if (!doc.exists) {
const sessionid = helpers.getNewID(5);
firestore.collection('sessions').doc(sessionid).set({
pollid: pollid,
@ -189,13 +189,13 @@ module.exports.session = {
})
},
setActiveQuestion (context, qid, pollid, sessionid) {
return firestore.collection('questions').doc(qid).then((doc) => {
return firestore.collection('questions').doc(qid).get().then((doc) => {
const inQuestion = doc.data();
delete inQuestion.responses
return firestore.collection('sessions').doc(sessionid).get().then((doc) => {
if (doc.data()) {
if (doc.data().activequestion) {
if (doc.data().activeqid) {
module.exports.question.edit(context, doc.data().activeqid, doc.data().activequestion);
}
}
@ -213,34 +213,31 @@ module.exports.session = {
});
},
stopActiveQuestion (context, qid, pollid, sessionid) {
return rtdb.ref(`/sessions/${sessionid}`).once('value').then((snapshot) => {
rtdb.ref(`/sessions/${sessionid}`).transaction((session) => {
if (session !== null) {
module.exports.question.edit(context, qid, snapshot.val().activequestion);
return {
activeqid: null,
pollid: pollid,
owneruid: context.auth.uid,
activequestion: {}
}
} else {
return {};
}
return firestore().collection('sessions').doc(sessionid).collection(responses).doc(context.auth.token.email).get().then((doc) => {
const activequestion = doc.data().activequestion;
activequestion.responses.push({
})
module.exports.question.edit(context, qid, doc.data().activequestion);
return firestore.collection('sessions').doc(sessionid).set({
activeqid: null,
pollid: pollid,
owneruid: context.auth.uid,
activequestion: {}
}).then(() => {
return { status: 'success', code: 200, message: 'Transaction sent!' };
});
return { status: 'success', code: 200, message: 'Transaction sent!' };
});
})
},
sendResponse (context, sessionid, response) {
const activeQuestionRef = rtdb.ref(`/sessions/${sessionid}/activequestion/responses/${context.auth.uid}`);
return activeQuestionRef.once('value').then((snapshot) => {
if (snapshot !== null) {
activeQuestionRef.update({
response,
email: context.auth.token.email
})
const activeQuestionRef = firestore.collection('sessions').doc(sessionid).collection('responses').doc(context.auth.token.email);
return activeQuestionRef.set({
response,
email: context.auth.token.email
}).then((doc) => {
return { status: 'success', code: 200, message: 'Response sent!' };
}
return { status: 'error', code: 500, message: 'Response failed to send!' };
}).catch((err) => {
return { status: 'error', code: 500, message: err };
});
}
}