Yann
9 months ago
18 changed files with 6561 additions and 3280 deletions
File diff suppressed because it is too large
@ -0,0 +1,16 @@ |
|||||
|
/*! |
||||
|
* Bootstrap v5.3.2 (https://getbootstrap.com/) |
||||
|
* Copyright 2011-2023 The Bootstrap Authors (https://github.com/twbs/bootstrap/graphs/contributors) |
||||
|
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE) |
||||
|
*/ |
||||
|
|
||||
|
/*! |
||||
|
* jQuery JavaScript Library v3.7.1 |
||||
|
* https://jquery.com/ |
||||
|
* |
||||
|
* Copyright OpenJS Foundation and other contributors |
||||
|
* Released under the MIT license |
||||
|
* https://jquery.org/license |
||||
|
* |
||||
|
* Date: 2023-08-28T13:37Z |
||||
|
*/ |
@ -0,0 +1,17 @@ |
|||||
|
{ |
||||
|
"sendgrid": { |
||||
|
"to": [ |
||||
|
{ |
||||
|
"email": "onja@blastream.com" |
||||
|
}, |
||||
|
{ |
||||
|
"email": "onja.asmad@gmail.com" |
||||
|
} |
||||
|
], |
||||
|
"from": "contact@blastream.com", |
||||
|
"fromName": "Blastream", |
||||
|
"uri": "https://api.sendgrid.com/v3/mail/send", |
||||
|
"application_key": "YABUthwqTla79YQbR7gToQ", |
||||
|
"application_secret": "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" |
||||
|
} |
||||
|
} |
@ -0,0 +1,110 @@ |
|||||
|
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: to, |
||||
|
subject: subject |
||||
|
} |
||||
|
], |
||||
|
content: [ |
||||
|
{ |
||||
|
type: "text/html", |
||||
|
value: message |
||||
|
} |
||||
|
], |
||||
|
from: { |
||||
|
email: sendgridParams.from, |
||||
|
name: sendgridParams.fromName, |
||||
|
} |
||||
|
}; |
||||
|
|
||||
|
return null |
||||
|
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) }); |
||||
|
} |
||||
|
}); |
||||
|
}; |
@ -0,0 +1,8 @@ |
|||||
|
<p>Bonjour, </p> |
||||
|
<br /> |
||||
|
<p>Le traitement de votre fichier depuis l'url <strong>{{url}}</strong> est terminé</p> |
||||
|
<p><strong>{{count}} lignes</strong> ont été générés</p> |
||||
|
|
||||
|
<br /> |
||||
|
<p>Voici le lien pour télécharger le fichier généré: <strong>{{host}}/csv/{{generated}}</strong></p> |
||||
|
<p>Copier le lien ci-dessus et coller le dans votre navigateur</p> |
@ -0,0 +1,13 @@ |
|||||
|
<div class="email-layout"> |
||||
|
<div class="email-layout__inner"> |
||||
|
<div class="email-layout__header"> |
||||
|
|
||||
|
</div> |
||||
|
<div class="email-layout__body"> |
||||
|
{{{body}}} |
||||
|
</div> |
||||
|
<div class="email-layout__footer"> |
||||
|
|
||||
|
</div> |
||||
|
</div> |
||||
|
</div> |
Loading…
Reference in new issue