Fix receipts bugs: budget queries, undo/transfer desync, timer spam
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
|
||||
const { HOUSEHOLD_IDS } = require('../../core/constants.js');
|
||||
|
||||
module.exports = {
|
||||
category: 'utility',
|
||||
@@ -44,46 +45,42 @@ module.exports = {
|
||||
.setDescription('User to view stats for'))
|
||||
),
|
||||
async execute(interaction) {
|
||||
console.log('budget command entrypoint');
|
||||
let db = await interaction.client.localDB;
|
||||
let targetUser = interaction.options.getUser('user') ?? interaction.user;
|
||||
let discordId = interaction.member.id;
|
||||
console.log(db, discordId);
|
||||
let allowed = ['372115788498468864', '222457277708369928'].includes(discordId);
|
||||
console.log(allowed);
|
||||
let allowed = [HOUSEHOLD_IDS.MIFFY, HOUSEHOLD_IDS.MINZ].includes(discordId);
|
||||
if (!allowed) {
|
||||
console.log('budget command denied');
|
||||
await interaction.reply('This command may only be invoked by Miffy!');
|
||||
return;
|
||||
}
|
||||
console.log(`Switcching on sub ${interaction.options.getSubcommand()}`);
|
||||
switch (interaction.options.getSubcommand()) {
|
||||
case 'set':
|
||||
console.log('budget command set');
|
||||
let override = interaction.options.getBoolean('override') ?? false;
|
||||
let budget = interaction.options.get('amount').value;
|
||||
db.prepare(`INSERT OR REPLACE INTO bot_config (id, weekly_budget) VALUES (1, ?)`).run(budget);
|
||||
let overrideApplied = true;
|
||||
if (override) {
|
||||
db.prepare(`
|
||||
UPDATE weekly_budgets
|
||||
const result = db.prepare(`
|
||||
UPDATE weekly_budgets
|
||||
SET budget_amount = ?
|
||||
WHERE id = (
|
||||
SELECT id FROM weekly_budgets
|
||||
SELECT id FROM weekly_budgets
|
||||
WHERE date('now', 'localtime') BETWEEN start_date AND end_date
|
||||
ORDER BY id DESC
|
||||
LIMIT 1
|
||||
)`).run(budget);
|
||||
|
||||
overrideApplied = result.changes > 0;
|
||||
}
|
||||
await interaction.reply(`Budget set for ${targetUser.username} to ${Number(budget).toFixed(2)}.`);
|
||||
const overrideNote = override && !overrideApplied ? " (no active budget period to override this week)" : "";
|
||||
await interaction.reply(`Budget set for ${targetUser.username} to ${Number(budget).toFixed(2)}.${overrideNote}`);
|
||||
break;
|
||||
case 'view':
|
||||
const lastWeek = interaction.options.getBoolean('lastweek') ?? false;
|
||||
const currentBudget = db.prepare(`
|
||||
SELECT id, budget_amount, exchange_rate, start_date, end_date
|
||||
FROM weekly_budgets
|
||||
WHERE ${lastWeek ? "start_date < date('now', 'localtime')" : "date('now', 'localtime') BETWEEN start_date AND end_date"}
|
||||
ORDER BY start_date DESC
|
||||
WHERE ${lastWeek ? "end_date < date('now', 'localtime')" : "date('now', 'localtime') BETWEEN start_date AND end_date"}
|
||||
ORDER BY start_date DESC, created_at DESC
|
||||
LIMIT 1
|
||||
`).get();
|
||||
|
||||
@@ -123,11 +120,11 @@ module.exports = {
|
||||
break
|
||||
case 'stats':
|
||||
const allBudgets = db.prepare(`
|
||||
SELECT id, budget_amount, exchange_rate, start_date, end_date
|
||||
FROM weekly_budgets
|
||||
ORDER BY start_date ASC
|
||||
SELECT id, budget_amount, exchange_rate, start_date, end_date
|
||||
FROM weekly_budgets
|
||||
ORDER BY start_date DESC
|
||||
LIMIT 4
|
||||
`).all();
|
||||
`).all().reverse();
|
||||
|
||||
let statsDescription = "";
|
||||
let cumulativeSavingsEur = 0;
|
||||
|
||||
Reference in New Issue
Block a user