Adding server-side functions to my tiny responsive web apps

The business problem

The new requirement

Firebase triggers

exports.countStreaks = functions.database.ref('diary/{userUid}/entries')
.onUpdate(async (snapshot, context) => {
const afterEntries = Object.entries(snapshot.after.val());
const processedEntries = [];

for (const [key, value] of afterEntries) {
const finishTime = DateTime.fromMillis(value.finishTime);
const diaryEntry = {finishTime, totalSeconds: value.totalSeconds};
processedEntries.push(diaryEntry);
}
const stats = statscalculator.longestStreak(userTimezone, processedEntries);
if(stats){
return snapshot.after.ref.parent.child('stats').set(stats);
}
});

Summary

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store