diff --git a/index.js b/index.js index a034ef0..13b623e 100644 --- a/index.js +++ b/index.js @@ -91,9 +91,11 @@ function updateRecords(callStats, records) { if (!records.callRecord) { records.callRecord = { date: yesterdayDateString, count: callStats.totalCallsMade }; isNewRecord = true; + console.warn("No previous call record found, initializing new record."); } else if (allTimeRecord.count < callStats.totalCallsThisMonth) { allTimeRecord.count = callStats.totalCallsThisMonth; isNewRecord = true; + console.log(`New all-time record: ${allTimeRecord.count} calls on ${yesterdayDateString}, previous record was ${records.callRecord.count} calls on ${records.callRecord.date}`); } callStats.allTimeRecord = `${allTimeRecord.count} calls on ${allTimeRecord.date}`; diff --git a/records.js b/records.js index 5cef7a4..8cbac55 100644 --- a/records.js +++ b/records.js @@ -2,16 +2,19 @@ import fs from "fs"; export class Records { constructor(records = {}) { - if (typeof records.records === "object") for (const [ key, value ] of Object.entries(records.records)) { - const oldTableMatches = key.match(/^monthly_total_(\d{4})-(\d{2})$/); - if (oldTableMatches) { - const year = oldTableMatches[1]; - const month = oldTableMatches[2]; - if (!this.monthlyTotals) this.monthlyTotals = {}; - if (!this.monthlyTotals[year]) this.monthlyTotals[year] = {}; - this.monthlyTotals[year][month] = value; - } else if (key === "record_calls" && typeof value === "object" && value !== null) this.callRecord = new CallRecord(value.date, value.count); else if (key === "total_calls_ever_placed" && typeof value === "number") this.totalCallsEverPlaced = value; else throw new Error(`Unknown legacy record key: ${key}`); - } else for (const [ key, value ] of Object.entries(records)) this[key] = value; + if (typeof records.records === "object") + for (const [ key, value ] of Object.entries(records.records)) { + const oldTableMatches = key.match(/^monthly_total_(\d{4})-(\d{2})$/); + if (oldTableMatches) { + const year = oldTableMatches[1]; + const month = oldTableMatches[2]; + if (!this.monthlyTotals) this.monthlyTotals = {}; + if (!this.monthlyTotals[year]) this.monthlyTotals[year] = {}; + this.monthlyTotals[year][month] = value; + } else if (key === "record_calls" && typeof value === "object" && value !== null) this.callRecord = new CallRecord(value.date, value.count); + else if (key === "total_calls_ever_placed" && typeof value === "number") this.totalCallsEverPlaced = value; + else throw new Error(`Unknown legacy record key: ${key}`); + } else for (const [ key, value ] of Object.entries(records)) this[key] = value; } static fromJSONFile(path) {