Skip to content
Snippets Groups Projects
Commit 50b9a811 authored by Bastien DUMONT's avatar Bastien DUMONT :angel:
Browse files

Merge branch '225-alerte-eau-pb-date-dans-template' into 'dev'

Resolve "Alerte Eau - Pb date dans template"

Closes #225

See merge request web-et-numerique/llle_project/ecolyo!572
parents 4d19de73 3e4f2984
No related branches found
No related tags found
2 merge requests!584chore(release): 1.8.1,!572Resolve "Alerte Eau - Pb date dans template"
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)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment