Skip to content

Commit

Permalink
update leaves every 15 minutes
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxime-Bott committed May 28, 2021
1 parent 5294f24 commit 9d76369
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"build:client": "npx webpack --progress --profile --colors",
"work:client": "npx webpack --env=dev --progress",
"build": "npm run build:server && npm run build:client",
"start": "node bin/server/index.js"
"start": "nodemon bin/server/index.js"
},
"lint-staged": {
"*.js": [
Expand Down
16 changes: 13 additions & 3 deletions src/server/helpers/handleLeaves.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
const UserModel = require('../models/UserModel')
const TreeModel = require('../models/TreeSchema')

const sendLeaves = async () => {

try {
const users = await UserModel.find()
const users = await UserModel.find().select('trees leaves')

for(const user of users){
await UserModel.findOneAndUpdate({_id: user._id}, {leaves: Math.round(user.leaves + user.treesCount)})
}
const trees = user.trees
let totalValue = 0

for(const treeId of trees){
const tree = await TreeModel.find({_id: treeId})
const value = tree[0].value
totalValue += value
}

await UserModel.findOneAndUpdate({_id: user._id}, {leaves: Math.round(user.leaves + totalValue)})
}
} catch (err) {
console.log(err)
}
Expand All @@ -28,6 +37,7 @@ const removeLeaves = async () => {
}
}


module.exports = {
sendLeaves,
removeLeaves
Expand Down

0 comments on commit 9d76369

Please sign in to comment.