Onja
1 year ago
2 changed files with 117 additions and 0 deletions
@ -0,0 +1,114 @@ |
|||
const emitter = require('../services/eventEmitter'); |
|||
const axios = require('axios'); |
|||
|
|||
const params = require('../config/params'); |
|||
const TemplateService = require('../services/template'); |
|||
const templateService = new TemplateService(); |
|||
|
|||
|
|||
const errorResponse = (error) => { |
|||
if (error.response) { |
|||
return error.response.status + ' - ' + error.response.statusText; |
|||
} else if (error.request) { |
|||
// The request was made but no response was received
|
|||
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
|
|||
// http.ClientRequest in node.js
|
|||
return error.request; |
|||
} else { |
|||
return error.message; |
|||
} |
|||
}; |
|||
|
|||
module.exports = (app) => { |
|||
|
|||
// Get app url
|
|||
let host = ''; |
|||
app.use((req, res, next) => { |
|||
console.log('--- APP_URL ---', req.protocol + '://' + req.get('host')); |
|||
host = req.protocol + '://' + req.get('host'); |
|||
next(); |
|||
}); |
|||
|
|||
// Check if sendgrid params exists
|
|||
if ( !params.sendgrid ) { |
|||
console.log('--- No sendgrid params ---'); |
|||
return; |
|||
} |
|||
console.log('--- sendgrid params exists ---'); |
|||
|
|||
const sendgridParams = params.sendgrid; |
|||
const defaultParams = { |
|||
url: sendgridParams.uri, |
|||
headers: { |
|||
Authorization: 'Bearer ' + sendgridParams.application_secret, |
|||
'Content-Type': 'application/json', |
|||
}, |
|||
}; |
|||
|
|||
const sendEmail = async (to, subject, message) => { |
|||
const data = { |
|||
personalizations: [ |
|||
{ |
|||
to: [ |
|||
{ |
|||
email: to, |
|||
} |
|||
], |
|||
subject: subject |
|||
} |
|||
], |
|||
content: [ |
|||
{ |
|||
type: "text/html", |
|||
value: message |
|||
} |
|||
], |
|||
from: { |
|||
email: sendgridParams.from, |
|||
name: sendgridParams.fromName, |
|||
} |
|||
}; |
|||
|
|||
|
|||
const response = await axios.post(defaultParams.url, data, { headers: defaultParams.headers }); |
|||
return response; |
|||
} |
|||
|
|||
// Create a new listener for the parse.end event
|
|||
emitter.on('parse.end', async ({ url, columns, count, generated }) => { |
|||
console.log('HOST', host); |
|||
try { |
|||
const message = templateService.renderEmail('parse-success', { |
|||
host, |
|||
generated, |
|||
count, |
|||
columns, |
|||
url, |
|||
}); |
|||
const response = await sendEmail(sendgridParams.to, 'BODACC - Traitement terminé', message); |
|||
emitter.emit('mailer.parse.end.success', { response: response.data }); |
|||
} catch (error) { |
|||
emitter.emit('mailer.parse.end.error', { error: errorResponse(error) }); |
|||
} |
|||
}); |
|||
|
|||
// Create a new listener for the parse.error event
|
|||
emitter.on('parse.error', async ({ filepath, columns, error }) => { |
|||
try { |
|||
const reponse = await sendEmail(sendgridParams.to, 'BODACC - Erreur traitement', '<p>Erreur traitement</p><br /><p>Voici le message d\'erreur: ' + error + '</p>') |
|||
emitter.emit('mailer.parse.error.success', { response: response }); |
|||
} catch (error) { |
|||
emitter.emit('mailer.parse.error.error', { error: errorResponse(error) }); |
|||
} |
|||
}); |
|||
|
|||
// Create a new listener for the parseFromUrl.error event
|
|||
emitter.on('parseFromUrl.error', async ({ url, columns, error }) => { |
|||
try { |
|||
const response = await sendEmail(sendgridParams.to, 'BODACC - Erreur génération fichier', '<p>Erreur génération fichier</p><br /><p>Voici le message d\'erreur: ' + error + '</p>') |
|||
emitter.emit('mailer.parseFromUrl.error.success', { response: response.data }); |
|||
} catch (error) { |
|||
emitter.emit('mailer.parseFromUrl.error.error', { error: errorResponse(error) }); |
|||
} |
|||
}); |
|||
}; |
Loading…
Reference in new issue