Skip to content

Instantly share code, notes, and snippets.

@YonathanMeguira
Created June 6, 2018 09:57
Show Gist options
  • Save YonathanMeguira/e09affbe3b04075dee11cc5e46d25540 to your computer and use it in GitHub Desktop.
Save YonathanMeguira/e09affbe3b04075dee11cc5e46d25540 to your computer and use it in GitHub Desktop.
money bot - new expense
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