diff --git a/.vscode/settings.json b/.vscode/settings.json index 3de1abc07c8902b3a41b3d0d4cd57a7cd011669c..6fda2456642daca22c9653b8d876a76dbfd76b0d 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -38,6 +38,7 @@ "Derniere", "Detaillees", "enedissgegrandlyon", + "EPGL", "Etage", "etat", "faultstring", diff --git a/src/helpers/utils.js b/src/helpers/utils.js index 1b2656675e7566716d1fabeafa9b43af9945bb71..1e4f4a08ce56638e18c461300b4974bbfa3f8509 100644 --- a/src/helpers/utils.js +++ b/src/helpers/utils.js @@ -66,21 +66,22 @@ function aggregateYearlyLoad(data) { } /** - * Removes the month aggregate that has the same year and month as startDate from the input array if it already exists in database. + * Removes aggregates matching the same year and month as the first data retrieved from EPGL if it is already in database. * * This step is needed to avoid updating the 3 years old month aggregate with incomplete data * - * @param {DateTime} startDate + * @param {number} firstMonth + * @param {number} firstYear * @param {FormattedData[]} yearlyLoads * @returns {Promise<FormattedData[]>} */ -async function filterStartDateMonthlyLoad(startDate, monthlyLoads) { +async function filterFirstMonthlyLoad(firstMonth, firstYear, monthlyLoads) { const monthlyLoadRef = await cozyClient.data.defineIndex( rangeDate.month.doctype, rangeDate.month.keys ) const starDateMonthlyLoad = await cozyClient.data.query(monthlyLoadRef, { - selector: { year: startDate.year, month: startDate.month }, + selector: { year: firstYear, month: firstMonth }, limit: 1, }) @@ -95,21 +96,21 @@ async function filterStartDateMonthlyLoad(startDate, monthlyLoads) { } /** - * Removes the year aggregate that has the same year as startDate from the given array if it already exists in database. + * Removes aggregates matching the same year as the first data retrieved from EPGL if it is already in database. * * This step is needed to avoid updating the 3 years old year aggregate with incomplete data * - * @param {DateTime} startDate + * @param {number} firstYear * @param {FormattedData[]} yearlyLoads * @returns {Promise<FormattedData[]>} */ -async function filterStartDateYearlyLoad(startDate, yearlyLoads) { +async function filterFirstYearlyLoad(firstYear, yearlyLoads) { const yearlyLoadRef = await cozyClient.data.defineIndex( rangeDate.year.doctype, rangeDate.year.keys ) const startDateYearlyLoad = await cozyClient.data.query(yearlyLoadRef, { - selector: { year: startDate.year }, + selector: { year: firstYear }, limit: 1, }) if (!startDateYearlyLoad.length) { @@ -123,6 +124,6 @@ async function filterStartDateYearlyLoad(startDate, yearlyLoads) { module.exports = { aggregateMonthlyLoad, aggregateYearlyLoad, - filterStartDateMonthlyLoad, - filterStartDateYearlyLoad, + filterFirstMonthlyLoad, + filterFirstYearlyLoad, } diff --git a/src/index.js b/src/index.js index 61e49279809610d19658468f656b03c9627a88a2..6c065ec2eca71623d30c43db5181fb514de3ab26 100644 --- a/src/index.js +++ b/src/index.js @@ -16,9 +16,9 @@ const { isDev } = require('./helpers/env') const { rangeDate } = require('./types/constants') const { aggregateMonthlyLoad, - filterStartDateMonthlyLoad, + filterFirstMonthlyLoad, aggregateYearlyLoad, - filterStartDateYearlyLoad, + filterFirstYearlyLoad, } = require('./helpers/utils') require('./types/types') @@ -110,12 +110,15 @@ async function start(fields, cozyParameters) { rangeDate.day.doctype, rangeDate.day.keys ) + const { year: firstYear, month: firstMonth } = eglData[0] + log('debug', 'Aggregate EGL yearly load data') const monthlyLoads = aggregateMonthlyLoad(eglData) log('debug', 'Filter first month aggregate if already in database') - const filteredMonthlyLoads = filterStartDateMonthlyLoad( - startDate, + const filteredMonthlyLoads = filterFirstMonthlyLoad( + firstMonth, + firstYear, monthlyLoads ) @@ -130,10 +133,7 @@ async function start(fields, cozyParameters) { const yearlyLoads = aggregateYearlyLoad(monthlyLoads) log('debug', 'Filter first year aggregate if already in database') - const filteredYearlyLoads = filterStartDateYearlyLoad( - startDate, - yearlyLoads - ) + const filteredYearlyLoads = filterFirstYearlyLoad(firstYear, yearlyLoads) log('debug', 'Store aggregated EGL yearly load data') await updateOrCreate(