[FIX] createProgress batch write limit reached
Ensure batch write limit isn't reached when creating progress records
This commit is contained in:
@@ -180,25 +180,27 @@ 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) {
|
|
||||||
throw new functions.https.HttpsError("permission-denied", "Insufficient permissions to access set");
|
|
||||||
} else {
|
|
||||||
const setVocabCollectionId = db
|
|
||||||
.collection("sets").doc(setId)
|
|
||||||
.collection("vocab");
|
|
||||||
|
|
||||||
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");
|
|
||||||
|
|
||||||
allSetTitles.push(setDoc.data().title);
|
|
||||||
|
|
||||||
return setVocab.docs.map((vocabDoc) => {
|
|
||||||
let newVocabData = vocabDoc;
|
|
||||||
newVocabData.vocabId = setDoc.data().owner + "__" + vocabDoc.id;
|
|
||||||
allVocab.push(newVocabData);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
if (!setDoc.data().public && setDoc.data().owner !== uid) {
|
||||||
|
throw new functions.https.HttpsError("permission-denied", "Insufficient permissions to access set");
|
||||||
|
}
|
||||||
|
const setVocabCollectionId = db
|
||||||
|
.collection("sets").doc(setId)
|
||||||
|
.collection("vocab");
|
||||||
|
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
|
||||||
|
allSetTitles.push(setDoc.data().title);
|
||||||
|
|
||||||
|
return setVocab.docs.map((vocabDoc) => {
|
||||||
|
let newVocabData = vocabDoc;
|
||||||
|
newVocabData.vocabId = setDoc.data().owner + "__" + vocabDoc.id;
|
||||||
|
allVocab.push(newVocabData);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}));
|
}));
|
||||||
|
|
||||||
@@ -238,9 +240,23 @@ 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;
|
||||||
|
|
||||||
const terms = {
|
const terms = {
|
||||||
@@ -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
|
);
|
||||||
);
|
batches[batches.length - 1].set(
|
||||||
batch.set(
|
data.progressDocId.collection("definitions").doc(vocabId),
|
||||||
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;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user