diff --git a/kbb.js b/kbb.js index c8cdb4a..f128494 100644 --- a/kbb.js +++ b/kbb.js @@ -62,7 +62,7 @@ async function getKBB(URL) { if (diff != 0) { const daily = parseInt(getTagValue(note, 'kbbDailyMileage')); if (mileage && daily) { - let lastDate = await getLastTransactionDate(account); + let lastDate = await getLastTransactionDate(account, undefined, true); const parts = lastDate.split('-'); lastDate = new Date(parts[0], parts[1] - 1, parts[2]); if (lastDate < new Date()) { diff --git a/utils.js b/utils.js index 45b3f26..4550d61 100644 --- a/utils.js +++ b/utils.js @@ -65,18 +65,21 @@ const Utils = { return data.data; }, - getLastTransactionDate: async function (account, cutoffDate=undefined) { - if (cutoffDate === undefined) { + getLastTransactionDate: async function (account, cutoffDate=undefined, inbound=false) { + if (cutoffDate === undefined || cutoffDate === null) { cutoffDate = new Date(); cutoffDate.setDate(cutoffDate.getDate() + 1); } + const filters = { + 'account': account.id, + 'date': { $lt: cutoffDate }, + }; + if (!inbound) { + filters['amount'] = { $gt: 0 }; + } const data = await api.runQuery( api.q('transactions') - .filter({ - 'account': account.id, - 'date': { $lt: cutoffDate }, - 'amount': { $gt: 0 }, - }) + .filter(filters) .select('date') .orderBy({ 'date': 'desc' }) .limit(1)