Skip to content

Commit

Permalink
Fix for updating the history document
Browse files Browse the repository at this point in the history
  • Loading branch information
de-ich committed May 20, 2024
1 parent f4f3a08 commit 5a82195
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions functions/src/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,27 @@ export const pushToHistory = onCall({ maxInstances: 1 }, async (request) => {

const historyDocRef = db.doc(`aggregates/history`);

historyDocRef.get().
then(snapshot => snapshot.data() as History).
then(history => {
var newEntries = history.entries.filter(entry => entry.recipeId !== recipeId);
newEntries.unshift({"recipeId": recipeId, "timestamp": Timestamp.now()});
return newEntries.slice(0, 10);
}).
then(newEntries => {
historyDocRef.set({entries: newEntries});
});
let history : History = {
entries: []
};

try {
history = await historyDocRef.get().then(snapshot => snapshot.data() as History);
if (typeof(history.entries) !== 'object') {
history.entries = [];
}
} catch (e) {
console.error("Unable to convert document 'aggregates/history' to an instance of History. " +
"The following error occurred: " + e);
}

console.log(`Old history: ${history}`);

var newEntries = history.entries.filter(entry => entry.recipeId !== recipeId);
newEntries.unshift({"recipeId": recipeId, "timestamp": Timestamp.now()});
history.entries = newEntries.slice(0, 10);

console.log(`New entries: ${history}`);

await historyDocRef.set(history);
});

0 comments on commit 5a82195

Please sign in to comment.