Skip to content

Commit

Permalink
feat(history): init mail alert ✨
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreBrisorgueil committed Apr 22, 2020
1 parent d44cbaa commit 0da3fb4
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 4 deletions.
10 changes: 10 additions & 0 deletions config/defaults/montaineDev.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,14 @@ module.exports = _.merge(defaultConfig, {
secret: 'LouNodeDevSecret', // secret for hash
expiresIn: 360, // token expire in x sec
},
mailer: {
from: 'lou.worker@gmail.com',
options: {
service: 'gmail',
auth: {
user: 'lou.worker@gmail.com',
pass: 'zmvhmfqjzuzftpth',
},
},
},
});
4 changes: 3 additions & 1 deletion config/templates/api-failed-alert.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@

<body>
<p>Dear Mr, Mrs,</p>
<p> You have configured to be alerted on this mail if api call to <b>{{apiTitle}}</b> fails.</p>
<p> You have configured to be alerted if <b>{{apiTitle}}</b> api call failed.</p>
<pre style='background: #f5f2f0; color:#72972c; padding: 15px; border-radius: 7px;'>{{result}}</pre>
<br />
<p>More informations <a href="{{link}}">here</a>.</p>
<br />
<p>The {{appName}} Support Team.</p>
<br/>
<i style='color:#9b9b9b'>Please do not reply to this email, you can contact us here {{appContact}}.</i>
Expand Down
3 changes: 2 additions & 1 deletion lib/helpers/montaineRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ exports.request = async (api, params) => {
* @param {Object} data - api result
* @return {Object} mail status
*/
exports.sendMailAlert = (data, api) => mails.sendMail({
exports.sendMailAlert = (data, api, history) => mails.sendMail({
template: 'api-failed-alert',
from: config.mailer.from,
to: api.alert,
Expand All @@ -90,6 +90,7 @@ exports.sendMailAlert = (data, api) => mails.sendMail({
apiTitle: api.title,
appName: config.app.title,
appContact: config.app.appContact,
link: `${config.cors.origin}/historys/${history.id}`,
},
});

Expand Down
1 change: 1 addition & 0 deletions modules/history/services/historys.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ exports.historize = async (result, start, api, user) => {
const history = await HistorysRepository.create(montaineRequest.setApiHistory(result, start, user, api));
await HistorysRepository.apiHistorize(api, history);
api.history.push(history);
if (!history.status) await montaineRequest.sendMailAlert(result, api, history);
return Promise.resolve(api);
} catch (err) {
console.log(err);
Expand Down
2 changes: 0 additions & 2 deletions scripts/crons/apis.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ const cronJobs = async () => {
_.forEach(apis, (_api) => {
if (cron.validate(_api.cron)) {
console.log(chalk.blue(`- Init ${_api.title} (${_api.id}) : ${_api.cron}`));

cron.schedule(_api.cron, async () => {
console.log(chalk.blue(`- Running ${_api.title} (${_api.id}) : ${_api.cron}`));

const api = await ApiService.get(_api.id);
await ApiService.load(api, 'cron');
});
Expand Down

0 comments on commit 0da3fb4

Please sign in to comment.