diff --git a/src/targets/services/consumptionAlert.ts b/src/targets/services/consumptionAlert.ts index b34de110bd51705caea590d50adc2844d8fc1379..890094a4f556d14284177812efb1c25cf606c889 100644 --- a/src/targets/services/consumptionAlert.ts +++ b/src/targets/services/consumptionAlert.ts @@ -1,104 +1,109 @@ -import logger from 'cozy-logger' -import { Client } from 'cozy-client' -import get from 'lodash/get' -import { runService } from './service' -import ProfileService from 'services/profile.service' -import MailService from 'services/mail.service' -import { DateTime } from 'luxon' -const consumptionLimit = require('notifications/consumptionLimit.hbs') -import mjml2html from 'mjml' -import { FluidType } from 'enum/fluid.enum' -import ConsumptionService from 'services/consumption.service' -import { getPreviousMonthName } from 'utils/utils' -import EnvironmentService from 'services/environment.service' - -const log = logger.namespace('alert') - -interface ConsumptionAlertProps { - client: Client -} - -// Only monitoring WATER fluid for now -const consumptionAlert = async ({ client }: ConsumptionAlertProps) => { - log('info', 'Fetching user profile...') - const upm = new ProfileService(client) - const consumptionService = new ConsumptionService(client) - const userProfil = await upm.getProfile() - if ( - !userProfil || - !userProfil.sendConsumptionAlert || - userProfil.waterDailyConsumptionLimit === 0 - ) { - log( - 'info', - 'End of process - Alert report notification is disabled or lack informations from user profile to run' - ) - return - } - - let username = '' - - log('info', 'water limit is :' + userProfil.waterDailyConsumptionLimit) - - log('info', 'Fetching fluid data...') - // Retrieve public name from the stack - const settings = await client - .getStackClient() - .fetchJSON('GET', '/settings/instance') - const publicName = get(settings, 'data.attributes.public_name') - if (publicName) { - username = publicName - } - - // Retrieve link to ecolyo app from the stack - const apps = await client.getStackClient().fetchJSON('GET', '/apps/ecolyo') - const appLink = get(apps, 'data.links.related') - - const fetchedData = await consumptionService.getLastDataload(FluidType.WATER) - let lastDayValue = 0 - if (fetchedData && fetchedData.length > 0) { - fetchedData.forEach(element => { - if (element.value) { - lastDayValue = element.value - } - }) - } - if (lastDayValue <= userProfil.waterDailyConsumptionLimit) { - log( - 'info', - 'End of process - Limit consumption set by the user has not been passed.' - ) - return - } - - log('info', 'Creation of mail...') - const mailService = new MailService() - const today = DateTime.local().setZone('utc', { keepLocalTime: true }) - - const environmentService = new EnvironmentService() - const template = consumptionLimit({ - title: 'Ça déborde !', - baseUrl: environmentService.getPublicURL(), - username: username, - clientUrl: `${appLink}/#/consumption/water`, - unsubscribeUrl: `${appLink}/#/options`, - userLimit: userProfil.waterDailyConsumptionLimit, - limitDate: `${today.day} ${getPreviousMonthName(today)}`, - }) - - const mailData = { - mode: 'noreply', - subject: '[Ecolyo] - Ça déborde !', - parts: [ - { - type: 'text/html', - body: mjml2html(template).html, - }, - ], - } - - log('info', 'Sending mail...') - mailService.SendMail(client, mailData) -} - -runService(consumptionAlert) +import logger from 'cozy-logger' +import { Client } from 'cozy-client' +import get from 'lodash/get' +import { runService } from './service' +import ProfileService from 'services/profile.service' +import MailService from 'services/mail.service' +import { DateTime } from 'luxon' +const consumptionLimit = require('notifications/consumptionLimit.hbs') +import mjml2html from 'mjml' +import { FluidType } from 'enum/fluid.enum' +import ConsumptionService from 'services/consumption.service' +import { getPreviousMonthName } from 'utils/utils' +import EnvironmentService from 'services/environment.service' + +const log = logger.namespace('alert') + +interface ConsumptionAlertProps { + client: Client +} + +// Only monitoring WATER fluid for now +const consumptionAlert = async ({ client }: ConsumptionAlertProps) => { + log('info', 'Fetching user profile...') + const upm = new ProfileService(client) + const consumptionService = new ConsumptionService(client) + const userProfil = await upm.getProfile() + if ( + !userProfil || + !userProfil.sendConsumptionAlert || + userProfil.waterDailyConsumptionLimit === 0 + ) { + log( + 'info', + 'End of process - Alert report notification is disabled or lack informations from user profile to run' + ) + return + } + + let username = '' + + log('info', 'water limit is :' + userProfil.waterDailyConsumptionLimit) + + log('info', 'Fetching fluid data...') + // Retrieve public name from the stack + const settings = await client + .getStackClient() + .fetchJSON('GET', '/settings/instance') + const publicName = get(settings, 'data.attributes.public_name') + if (publicName) { + username = publicName + } + + // Retrieve link to ecolyo app from the stack + const apps = await client.getStackClient().fetchJSON('GET', '/apps/ecolyo') + const appLink = get(apps, 'data.links.related') + + const fetchedData = await consumptionService.getLastDataload(FluidType.WATER) + let lastDayValue = 0 + let alertDay: DateTime = DateTime.local().setZone('utc', { + keepLocalTime: true, + }) + if (fetchedData && fetchedData.length > 0) { + fetchedData.forEach(element => { + if (element.value) { + lastDayValue = element.value + } + if (element.date) { + alertDay = element.date + } + }) + } + if (lastDayValue <= userProfil.waterDailyConsumptionLimit) { + log( + 'info', + 'End of process - Limit consumption set by the user has not been passed.' + ) + return + } + + log('info', 'Creation of mail...') + const mailService = new MailService() + + const environmentService = new EnvironmentService() + const template = consumptionLimit({ + title: 'Ça déborde !', + baseUrl: environmentService.getPublicURL(), + username: username, + clientUrl: `${appLink}/#/consumption/water`, + unsubscribeUrl: `${appLink}/#/options`, + userLimit: userProfil.waterDailyConsumptionLimit, + limitDate: `${alertDay.day} ${getPreviousMonthName(alertDay)}`, + }) + + const mailData = { + mode: 'noreply', + subject: '[Ecolyo] - Ça déborde !', + parts: [ + { + type: 'text/html', + body: mjml2html(template).html, + }, + ], + } + + log('info', 'Sending mail...') + mailService.SendMail(client, mailData) +} + +runService(consumptionAlert)