[FIX] createProgress batch write limit reached

Ensure batch write limit isn't reached when creating progress records
This commit is contained in:
2021-09-12 22:52:47 +01:00
parent 74c5401976
commit 51b21d3541

View File

@@ -180,15 +180,18 @@ exports.createProgress = functions.https.onCall((data, context) => {
return transaction.get(setsId.doc(setId)).then((setDoc) => { return transaction.get(setsId.doc(setId)).then((setDoc) => {
if (!setDoc.exists) { if (!setDoc.exists) {
throw new functions.https.HttpsError("not-found", "Set doesn't exist"); throw new functions.https.HttpsError("not-found", "Set doesn't exist");
} else if (!setDoc.data().public && setDoc.data().owner !== uid) { }
if (!setDoc.data().public && setDoc.data().owner !== uid) {
throw new functions.https.HttpsError("permission-denied", "Insufficient permissions to access set"); throw new functions.https.HttpsError("permission-denied", "Insufficient permissions to access set");
} else { }
const setVocabCollectionId = db const setVocabCollectionId = db
.collection("sets").doc(setId) .collection("sets").doc(setId)
.collection("vocab"); .collection("vocab");
return transaction.get(setVocabCollectionId).then((setVocab) => { return transaction.get(setVocabCollectionId).then((setVocab) => {
if (setVocab.docs.length < 1) throw new functions.https.HttpsError("failed-precondition", "Set must have at least one term/definition pair"); if (setVocab.docs.length < 1) {
throw new functions.https.HttpsError("failed-precondition", "Set must have at least one term/definition pair");
}
allSetTitles.push(setDoc.data().title); allSetTitles.push(setDoc.data().title);
@@ -198,7 +201,6 @@ exports.createProgress = functions.https.onCall((data, context) => {
allVocab.push(newVocabData); allVocab.push(newVocabData);
}); });
}); });
}
}); });
})); }));
@@ -238,8 +240,22 @@ exports.createProgress = functions.https.onCall((data, context) => {
}), }),
} }
shuffleArray(allVocab).forEach((doc, index, array) => { return {
let batch = db.batch(); allVocab: allVocab,
dataToSet: dataToSet,
mode: mode,
progressDocId: progressDocId,
limit: limit,
}
}).then(async (data) => {
let batches = [db.batch()];
let promises = [];
shuffleArray(data.allVocab).forEach(async (doc, index, array) => {
if (index % 248 === 0) {
promises.push(batches[batches.length - 1].commit());
batches.push(db.batch());
}
const vocabId = doc.vocabId; const vocabId = doc.vocabId;
@@ -252,45 +268,37 @@ exports.createProgress = functions.https.onCall((data, context) => {
"sound": doc.data().sound, "sound": doc.data().sound,
}; };
dataToSet.questions.push(vocabId); data.dataToSet.questions.push(vocabId);
if (index > 248) { batches[batches.length - 1].set(
batch.set( data.progressDocId.collection("terms").doc(vocabId),
progressDocId.collection("terms").doc(vocabId),
terms terms
); );
batch.set( batches[batches.length - 1].set(
progressDocId.collection("definitions").doc(vocabId), data.progressDocId.collection("definitions").doc(vocabId),
definitions definitions
); );
} else {
transaction.set(
progressDocId.collection("terms").doc(vocabId),
terms
);
transaction.set(
progressDocId.collection("definitions").doc(vocabId),
definitions
);
}
if ((mode == "questions" && index >= limit - 1) || index === array.length - 1) { if ((data.mode == "questions" && index >= data.limit - 1) || index === array.length - 1) {
array.length = index + 1; array.length = index + 1;
batch.commit();
} }
}); });
if (mode === "lives") { if (data.mode === "lives") {
dataToSet.lives = limit; data.dataToSet.lives = data.limit;
dataToSet.start_lives = limit; data.dataToSet.start_lives = data.limit;
} }
transaction.set( batches[batches.length - 1].set(
progressDocId, data.progressDocId,
dataToSet data.dataToSet
); );
return progressDocId.id; promises.push(batches[batches.length - 1].commit());
await Promise.all(promises);
return data.progressDocId.id;
}); });
}); });