Fix receipt date offset issue
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
module.exports = {
|
||||
timeout: 1000,
|
||||
immediate: false,
|
||||
timeout: 10000,
|
||||
immediate: true,
|
||||
name: 'Receipt Day Announcements',
|
||||
data: {
|
||||
channelId: '1462060674766344370',
|
||||
@@ -9,23 +9,35 @@ module.exports = {
|
||||
},
|
||||
async tick(client, timer) {
|
||||
|
||||
//format date because isostring is utc duh
|
||||
const formatDate = (date) => {
|
||||
const year = date.getFullYear();
|
||||
const month = String(date.getMonth() + 1).padStart(2, '0');
|
||||
const day = String(date.getDate()).padStart(2, '0');
|
||||
return `${year}-${month}-${day}`;
|
||||
};
|
||||
|
||||
try {
|
||||
const today = new Date();
|
||||
const lastMonday = new Date(today);
|
||||
lastMonday.setDate(today.getDate() - ((today.getDay() === 0) ? 6 : today.getDay() - 1));
|
||||
const startDate = lastMonday.toISOString().split('T')[0];
|
||||
const startDate = formatDate(lastMonday);
|
||||
|
||||
const nextSunday = new Date(lastMonday);
|
||||
nextSunday.setDate(lastMonday.getDate() + 6);
|
||||
const endDate = nextSunday.toISOString().split('T')[0];
|
||||
const nextMonday = new Date(lastMonday);
|
||||
nextMonday.setDate(lastMonday.getDate() + 7);
|
||||
const endDate = formatDate(nextMonday);
|
||||
// 0 1 2 3 4 5 6
|
||||
// S M T W T F S
|
||||
let db = await client.localDB;
|
||||
const channel = await client.channels.fetch(timer.data.channelId);
|
||||
await channel.send(`Format date last monday ${lastMonday} Format date next monday ${nextMonday}`);
|
||||
|
||||
const config = await (await 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);
|
||||
|
||||
await channel.send(`\`\`\`SELECT id FROM weekly_budgets WHERE start_date = ${startDate}\`\`\`
|
||||
${JSON.stringify(existingBudget)}`);
|
||||
|
||||
if (!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`);
|
||||
@@ -41,6 +53,9 @@ module.exports = {
|
||||
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}.`);
|
||||
}
|
||||
@@ -52,6 +67,24 @@ module.exports = {
|
||||
) as was_sent_today
|
||||
`).get();
|
||||
|
||||
const debug = db.prepare(`
|
||||
SELECT
|
||||
date(last_date_msg_receipts) AS last_date,
|
||||
datetime('now', 'localtime') AS current_time_full,
|
||||
(date(last_date_msg_receipts) = date('now', 'localtime')) AS was_sent_today
|
||||
FROM bot_config
|
||||
LIMIT 1;
|
||||
`).get();
|
||||
|
||||
await channel.send(`\`\`\`SELECT
|
||||
date(last_date_msg_receipts) AS last_date,
|
||||
datetime('now', 'localtime') AS current_time_full,
|
||||
(date(last_date_msg_receipts) = date('now', 'localtime')) AS was_sent_today
|
||||
FROM bot_config
|
||||
LIMIT 1;\`\`\`
|
||||
|
||||
${JSON.stringify(debug)}`);
|
||||
|
||||
if (!notification.was_sent_today) {
|
||||
const currentDate = new Date().toLocaleDateString('en-GB', {
|
||||
weekday: 'long',
|
||||
@@ -98,7 +131,7 @@ module.exports = {
|
||||
}, delay);
|
||||
|
||||
const channel = await client.channels.fetch(timer.data.channelId);
|
||||
//if (channel) await channel.send(`Next message scheduled for: ${next.toLocaleString()} with ms delta of ${delay} / ${delay/3600000}`);
|
||||
if (channel) await channel.send(`Next message scheduled for: ${next.toLocaleString()} with ms delta of ${delay} / ${delay/3600000}`);
|
||||
console.log(`[TIMER] Next message scheduled for: ${next.toLocaleString()}`);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user