Created
June 6, 2018 09:57
-
-
Save YonathanMeguira/e09affbe3b04075dee11cc5e46d25540 to your computer and use it in GitHub Desktop.
money bot - new expense
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Firebase = require("./firebase.init"); | |
const addExpense = (request, res) => { | |
const body = request.body; | |
const UID = body["messenger user id"]; | |
const ref = Firebase.FireDB.ref(UID); | |
ref.once("value").then(snapshot => { | |
const user = snapshot.val() || "undefined"; | |
if (user !== "undefined") { | |
const date = new Date(); | |
const month = date.getMonth(); | |
const monthExpense = ref.child(month); | |
monthExpense.push({ | |
amount: body["expense"], | |
poste: body["poste"] | |
}); | |
let total = 0; | |
const currentMonth = user[month]; | |
for (let i in currentMonth){ | |
total += parseInt(currentMonth[i].amount) | |
} | |
total += parseInt(body["expense"]); | |
const answer = `Depuis le debut du mois tu as dépensé ${total} ₪`; | |
const message = {"messages": [{"text": answer}]}; | |
res.send(message); | |
} | |
}); | |
}; | |
module.exports = { addExpense }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment