Fix receipts bugs: budget queries, undo/transfer desync, timer spam
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
const { formatDate } = require('../core/utils.js');
|
||||
|
||||
module.exports = {
|
||||
timeout: 10000,
|
||||
// scheduleNext() below drives the real daily schedule via setTimeout;
|
||||
// this is just a safety-net interval in case that chain ever breaks.
|
||||
timeout: 24 * 60 * 60 * 1000,
|
||||
immediate: true,
|
||||
name: 'Receipt Day Announcements',
|
||||
data: {
|
||||
@@ -26,30 +28,24 @@ module.exports = {
|
||||
let db = await client.localDB;
|
||||
const channel = await client.channels.fetch(timer.data.channelId);
|
||||
|
||||
const config = await (await db.prepare(`SELECT weekly_budget, last_date_msg_receipts FROM bot_config`).get());
|
||||
const config = db.prepare(`SELECT weekly_budget, last_date_msg_receipts FROM bot_config`).get();
|
||||
const existingBudget = db.prepare(`SELECT id FROM weekly_budgets WHERE start_date = ?`).get(startDate);
|
||||
|
||||
|
||||
if (!existingBudget) {
|
||||
await channel.send(`Format date last monday ${lastMonday} as startDate Format date next sunday ${nextSunday} as endDate`);
|
||||
await channel.send(`\`\`\`SELECT id FROM weekly_budgets WHERE start_date = ${startDate}\`\`\`
|
||||
${JSON.stringify(existingBudget)}`);
|
||||
console.log(`No budget found for week starting ${startDate}.`);
|
||||
const response = await fetch(`https://api.frankfurter.dev/v1/latest?amount=1&from=EUR&to=SEK`);
|
||||
|
||||
|
||||
if (!response.ok) await channel.send(`Failed to fetch exchange rate:\n${response.statusText}`);
|
||||
if (!response.ok) {
|
||||
await channel.send(`Failed to fetch exchange rate:\n${response.statusText}`);
|
||||
return;
|
||||
}
|
||||
const data = await response.json();
|
||||
//await channel.send(`Fetched new exchange rate:\n${JSON.stringify(data)}`);
|
||||
await channel.send(`Reset weekly budget to ${config.weekly_budget.toFixed(2)}€ / ${(config.weekly_budget * data.rates.SEK).toFixed(2)} kr`);
|
||||
|
||||
|
||||
db.prepare(`
|
||||
INSERT INTO weekly_budgets (start_date, end_date, budget_amount, exchange_rate)
|
||||
VALUES (?, ?, ?, ?)
|
||||
`).run(startDate, endDate, config.weekly_budget, data.rates.SEK);
|
||||
|
||||
await channel.send(`\`\`\`INSERT INTO weekly_budgets (start_date, end_date, budget_amount, exchange_rate)
|
||||
VALUES (${startDate}, ${endDate}, ${config.weekly_budget}, ${data.rates.SEK})\`\`\``);
|
||||
} else {
|
||||
console.log(`budget found for week starting ${startDate}.`);
|
||||
}
|
||||
@@ -61,25 +57,7 @@ module.exports = {
|
||||
) as was_sent_today
|
||||
`).get();
|
||||
|
||||
const debug = db.prepare(`
|
||||
SELECT
|
||||
date(last_date_msg_receipts, 'localtime') AS last_date,
|
||||
datetime('now', 'localtime') AS current_time_full,
|
||||
(date(last_date_msg_receipts, 'localtime') = date('now', 'localtime')) AS was_sent_today
|
||||
FROM bot_config
|
||||
LIMIT 1;
|
||||
`).get();
|
||||
|
||||
|
||||
if (!notification.was_sent_today) {
|
||||
await channel.send(`\`\`\`SELECT
|
||||
date(last_date_msg_receipts, 'localtime') AS last_date,
|
||||
datetime('now', 'localtime') AS current_time_full,
|
||||
(date(last_date_msg_receipts, 'localtime') = date('now', 'localtime')) AS was_sent_today
|
||||
FROM bot_config
|
||||
LIMIT 1;\`\`\`
|
||||
|
||||
${JSON.stringify(debug)}`);
|
||||
const currentDate = new Date().toLocaleDateString('en-GB', {
|
||||
weekday: 'long',
|
||||
day: '2-digit',
|
||||
|
||||
Reference in New Issue
Block a user