Improve testing and store more progress data

This commit is contained in:
2021-10-15 20:56:49 +01:00
parent 5c095ba249
commit 796680f357
2 changed files with 1182 additions and 240 deletions

View File

@@ -66,8 +66,7 @@ exports.userDeleted = functions.auth.user().onDelete((user) => {
* NOTE: can't be unit tested
*/
exports.getGroupMembers = functions.https.onCall((data, context) => {
const uid = context.auth.uid;
// const uid = "M3JPrFRH6Fdo8XMUbF0l2zVZUCH3";
const uid = LOCAL_TESTING ? "M3JPrFRH6Fdo8XMUbF0l2zVZUCH3" : context.auth.uid;
if (context.app == undefined && !LOCAL_TESTING) {
throw new functions.https.HttpsError(
@@ -148,9 +147,7 @@ exports.getGroupMembers = functions.https.onCall((data, context) => {
* @return {string} The ID of the created progress document.
*/
exports.createProgress = functions.https.onCall((data, context) => {
const uid = context.auth.uid;
// const uid = "M3JPrFRH6Fdo8XMUbF0l2zVZUCH3";
const uid = LOCAL_TESTING ? "M3JPrFRH6Fdo8XMUbF0l2zVZUCH3" : context.auth.uid;
if (context.app == undefined && !LOCAL_TESTING) {
throw new functions.https.HttpsError(
"failed-precondition",
@@ -214,7 +211,7 @@ exports.createProgress = functions.https.onCall((data, context) => {
let setTitle;
if (allSetTitles.length > 1) {
setTitle = allSetTitles.slice(0, -1).join(", ") + " & " + allSetTitles.slice(-1);
setTitle = allSetTitles.sort().slice(0, -1).join(", ") + " & " + allSetTitles.sort().slice(-1);
} else {
setTitle = allSetTitles[0];
}
@@ -407,8 +404,7 @@ function cleanseVocabString(item) {
* @return {boolean} typo Whether the inputted answer is likely to include a typo (using Levenshtein distance or by detecting a null answer).
*/
exports.processAnswer = functions.https.onCall((data, context) => {
const uid = context.auth.uid;
// const uid = "M3JPrFRH6Fdo8XMUbF0l2zVZUCH3";
const uid = LOCAL_TESTING ? "M3JPrFRH6Fdo8XMUbF0l2zVZUCH3" : context.auth.uid;
if (context.app == undefined && !LOCAL_TESTING) {
throw new functions.https.HttpsError(
@@ -547,7 +543,6 @@ exports.processAnswer = functions.https.onCall((data, context) => {
prompt = transaction.get(progressDoc.data().switch_language ? definitionDocId : termDocId).then((doc) => doc.data().item);
}
if (!returnData.moreAnswers) {
if (docData.progress >= docData.questions.length || (mode === "lives" && docData.lives <= 0)) {
const duration = Date.now() - docData.start_time;
@@ -559,6 +554,8 @@ exports.processAnswer = functions.https.onCall((data, context) => {
const completedProgressDocId = db.collection("completed_progress").doc(progressDoc.data().setIds.sort().join("__"));
return transaction.get(completedProgressDocId).then(async (completedProgressDoc) => {
if (!completedProgressDoc.exists) throw new Error("Completed progress doc doesn't exist");
if (!isCorrectAnswer) transaction.set(incorrectAnswerDoc, {
uid: uid,
groups: await userGroups,
@@ -566,6 +563,7 @@ exports.processAnswer = functions.https.onCall((data, context) => {
definition: progressDoc.data().switch_language ? await prompt : correctAnswers,
answer: inputAnswer.trim(),
switch_language: progressDoc.data().switch_language,
setIds: progressDoc.data().setIds,
});
const totalPercentage = completedProgressDoc.data().total_percentage + (docData.correct.length / docData.questions.length * 100);
@@ -573,12 +571,19 @@ exports.processAnswer = functions.https.onCall((data, context) => {
transaction.set(completedProgressDocId, {
attempts: attempts,
total_percentage: totalPercentage,
setIds: progressDoc.data().setIds,
set_title: completedProgressDoc.data().set_title,
});
returnData.averagePercentage = (totalPercentage / attempts).toFixed(2);
transaction.set(progressDocId, docData);
return returnData;
}).catch(async (error) => {
const allSetTitles = await Promise.all(progressDoc.data().setIds.map((setId) =>
transaction.get(db.collection("sets")
.doc(setId))
.then((setDoc) => setDoc.data().title)
.catch((error) => ""))
);
const setTitle = allSetTitles.slice(0, -1).join(", ") + " & " + allSetTitles.sort().slice(-1);
if (!isCorrectAnswer) transaction.set(incorrectAnswerDoc, {
uid: uid,
groups: await userGroups,
@@ -586,12 +591,14 @@ exports.processAnswer = functions.https.onCall((data, context) => {
definition: progressDoc.data().switch_language ? await prompt : correctAnswers,
answer: inputAnswer.trim(),
switch_language: progressDoc.data().switch_language,
setIds: progressDoc.data().setIds,
});
const totalPercentage = docData.correct.length / docData.questions.length * 100;
transaction.set(completedProgressDocId, {
attempts: 1,
total_percentage: totalPercentage,
set_title: setTitle,
});
returnData.averagePercentage = totalPercentage.toFixed(2);
transaction.set(progressDocId, docData);
@@ -614,6 +621,7 @@ exports.processAnswer = functions.https.onCall((data, context) => {
definition: progressDoc.data().switch_language ? await prompt : correctAnswers,
answer: inputAnswer.trim(),
switch_language: progressDoc.data().switch_language,
setIds: progressDoc.data().setIds,
});
returnData.nextPrompt = {
@@ -636,6 +644,7 @@ exports.processAnswer = functions.https.onCall((data, context) => {
definition: progressDoc.data().switch_language ? await prompt : correctAnswers,
answer: inputAnswer.trim(),
switch_language: progressDoc.data().switch_language,
setIds: progressDoc.data().setIds,
});
const sound = promptDoc.data().sound;
@@ -654,7 +663,6 @@ exports.processAnswer = functions.https.onCall((data, context) => {
return returnData;
}
});
}
});
});
});
@@ -668,10 +676,8 @@ exports.processAnswer = functions.https.onCall((data, context) => {
* @return {promise} The promise from setting the target user's admin custom auth claim.
*/
exports.setAdmin = functions.https.onCall(async (data, context) => {
const uid = context.auth.uid;
const isAdmin = context.auth.tokens.admin;
// const uid = "M3JPrFRH6Fdo8XMUbF0l2zVZUCH3";//nobVRmshkZNkrPbwgmPqNYrk55v2
// const isAdmin = true;
const uid = LOCAL_TESTING ? "M3JPrFRH6Fdo8XMUbF0l2zVZUCH3" : context.auth.uid;
const isAdmin = LOCAL_TESTING ? true : context.auth.token.admin;
if (context.app == undefined && !LOCAL_TESTING) {
throw new functions.https.HttpsError(
@@ -703,12 +709,9 @@ exports.setAdmin = functions.https.onCall(async (data, context) => {
* @return {boolean} true, to show the function has succeeded.
*/
exports.addSetToGroup = functions.https.onCall((data, context) => {
const uid = context.auth.uid;
const isAdmin = context.auth.token.admin;
const auth = context.auth;
// const uid = "M3JPrFRH6Fdo8XMUbF0l2zVZUCH3";
// const isAdmin = false;
// const auth = { uid: uid };
const uid = LOCAL_TESTING ? "M3JPrFRH6Fdo8XMUbF0l2zVZUCH3" : context.auth.uid;
const isAdmin = LOCAL_TESTING ? false : context.auth.token.admin;
const auth = LOCAL_TESTING ? { uid: uid } : context.auth;
if (context.app == undefined && !LOCAL_TESTING) {
throw new functions.https.HttpsError(
@@ -775,12 +778,9 @@ exports.addSetToGroup = functions.https.onCall((data, context) => {
* @return {promise} The promise from setting the group's updated data.
*/
exports.removeSetFromGroup = functions.https.onCall((data, context) => {
const uid = context.auth.uid;
const isAdmin = context.auth.token.admin;
const auth = context.auth;
// const uid = "M3JPrFRH6Fdo8XMUbF0l2zVZUCH3";
// const isAdmin = false;
// const auth = { uid: uid };
const uid = LOCAL_TESTING ? "M3JPrFRH6Fdo8XMUbF0l2zVZUCH3" : context.auth.uid;
const isAdmin = LOCAL_TESTING ? false : context.auth.token.admin;
const auth = LOCAL_TESTING ? { uid: uid } : context.auth;
if (context.app == undefined && !LOCAL_TESTING) {
throw new functions.https.HttpsError(
@@ -905,8 +905,7 @@ async function generateJoinCode() {
* @return {string} The ID of the new group's document in the groups collection.
*/
exports.createGroup = functions.https.onCall(async (data, context) => {
const uid = context.auth.uid;
// const uid = "M3JPrFRH6Fdo8XMUbF0l2zVZUCH3";
const uid = LOCAL_TESTING ? "M3JPrFRH6Fdo8XMUbF0l2zVZUCH3" : context.auth.uid;
if (context.app == undefined && !LOCAL_TESTING) {
throw new functions.https.HttpsError(

File diff suppressed because it is too large Load Diff