feat: add node-cron daily summary job at 10:00
This commit is contained in:
parent
f7bc98eaed
commit
2dabcf0a8e
3 changed files with 33 additions and 3 deletions
12
package-lock.json
generated
12
package-lock.json
generated
|
|
@ -10,7 +10,8 @@
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"axios": "^1.6.0",
|
"axios": "^1.6.0",
|
||||||
"dotenv": "^16.4.0",
|
"dotenv": "^16.4.0",
|
||||||
"express": "^4.18.0"
|
"express": "^4.18.0",
|
||||||
|
"node-cron": "^4.2.1"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/accepts": {
|
"node_modules/accepts": {
|
||||||
|
|
@ -658,6 +659,15 @@
|
||||||
"node": ">= 0.6"
|
"node": ">= 0.6"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/node-cron": {
|
||||||
|
"version": "4.2.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/node-cron/-/node-cron-4.2.1.tgz",
|
||||||
|
"integrity": "sha512-lgimEHPE/QDgFlywTd8yTR61ptugX3Qer29efeyWw2rv259HtGBNn1vZVmp8lB9uo9wC0t/AT4iGqXxia+CJFg==",
|
||||||
|
"license": "ISC",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=6.0.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/object-inspect": {
|
"node_modules/object-inspect": {
|
||||||
"version": "1.13.4",
|
"version": "1.13.4",
|
||||||
"resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.13.4.tgz",
|
"resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.13.4.tgz",
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"axios": "^1.6.0",
|
"axios": "^1.6.0",
|
||||||
"dotenv": "^16.4.0",
|
"dotenv": "^16.4.0",
|
||||||
"express": "^4.18.0"
|
"express": "^4.18.0",
|
||||||
|
"node-cron": "^4.2.1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
21
src/index.js
21
src/index.js
|
|
@ -1,7 +1,10 @@
|
||||||
require('dotenv').config()
|
require('dotenv').config()
|
||||||
|
const cron = require('node-cron')
|
||||||
const express = require('express')
|
const express = require('express')
|
||||||
const { handleWebhook } = require('./messageHandler')
|
const { handleWebhook } = require('./messageHandler')
|
||||||
const { loadDiensten } = require('./dienstService')
|
const { loadDiensten } = require('./dienstService')
|
||||||
|
const { resetDay, getYesterdayTotal } = require('./dailyStats')
|
||||||
|
const { sendTextMessage } = require('./whatsappApi')
|
||||||
|
|
||||||
const app = express()
|
const app = express()
|
||||||
app.use(express.json({ limit: '10mb' }))
|
app.use(express.json({ limit: '10mb' }))
|
||||||
|
|
@ -24,8 +27,24 @@ const PORT = process.env.WEBHOOK_PORT || 3002
|
||||||
|
|
||||||
loadDiensten()
|
loadDiensten()
|
||||||
|
|
||||||
|
cron.schedule('0 10 * * *', () => {
|
||||||
|
const adminNumber = process.env.ADMIN_NUMBER
|
||||||
|
if (!adminNumber) {
|
||||||
|
console.error('ADMIN_NUMBER not configured, skipping daily summary')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
resetDay()
|
||||||
|
const total = getYesterdayTotal()
|
||||||
|
|
||||||
|
sendTextMessage(adminNumber, `Aantal dienstaanvragen gisteren: ${total}`)
|
||||||
|
.then(() => console.log(`Daily summary sent: ${total} requests`))
|
||||||
|
.catch(err => console.error('Failed to send daily summary:', err.message))
|
||||||
|
})
|
||||||
|
|
||||||
app.listen(PORT, () => {
|
app.listen(PORT, () => {
|
||||||
console.log(`dienst-bot listening on port ${PORT}`)
|
console.log(`dienst-bot listening on port ${PORT}`)
|
||||||
console.log(`Allowed group: ${process.env.ALLOWED_GROUP}`)
|
console.log(`Allowed group: ${process.env.ALLOWED_GROUP}`)
|
||||||
console.log(`Bot name: ${process.env.BOT_NAME}`)
|
console.log(`Bot name: ${process.env.BOT_NAME}`)
|
||||||
})
|
console.log('Daily summary cron job scheduled for 10:00')
|
||||||
|
})
|
||||||
Loading…
Reference in a new issue