From 3cc2db4ddcbc15f76cad42d5a090c05e9b31437d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Orb=C3=A1n?= Date: Thu, 10 Jan 2019 22:03:48 +0100 Subject: [PATCH] update feedbackChanged #5 --- functions/src/lib/feedbacks/changed.js | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/functions/src/lib/feedbacks/changed.js b/functions/src/lib/feedbacks/changed.js index d50252f..d41bdf8 100644 --- a/functions/src/lib/feedbacks/changed.js +++ b/functions/src/lib/feedbacks/changed.js @@ -16,17 +16,21 @@ export const feedbackChanged = FEEDBACKS_FUNC_REF feedbacks.forEach(feedback => { const {roomId, ratings} = feedback.data() - if (Array.isArray(rooms[roomId])) { - rooms[roomId].push(getRatingAverage(ratings)) - } else { - rooms[roomId] = [(getRatingAverage(ratings))] - } + roomId.forEach(r => { + if (Array.isArray(rooms[r])) { + rooms[r].push(getRatingAverage(ratings)) + } else { + rooms[r] = [(getRatingAverage(ratings))] + } + }) }) const averages = {} - Object.entries(rooms).forEach(([roomId, room]) => - averages[roomId] = room.reduce((acc, rating) => acc + rating) / room.length - ) + Object.entries(rooms).forEach(([roomId, room]) => { + roomId.forEach(roomId => { + averages[roomId] = room.reduce((acc, rating) => acc + rating) / room.length + }) + }) await FEEDBACKS_DB_REF.set(averages)