From a9965dbdd4a02e6fd0ba694cd8557eb1c1f491ce Mon Sep 17 00:00:00 2001
From: ppc19 <ppc19@inf.ufpr.br>
Date: Fri, 10 Dec 2021 12:24:34 -0300
Subject: [PATCH] create message route

---
 src/libs/middlewares/email.js |  3 ++-
 src/libs/routes/api.js        |  3 +++
 src/libs/routes/message.js    | 47 +++++++++++++++++++++++++++++++++++
 3 files changed, 52 insertions(+), 1 deletion(-)
 create mode 100644 src/libs/routes/message.js

diff --git a/src/libs/middlewares/email.js b/src/libs/middlewares/email.js
index 7e0403d4..610826fc 100644
--- a/src/libs/middlewares/email.js
+++ b/src/libs/middlewares/email.js
@@ -23,7 +23,8 @@ transporter.verify(function(error, success) {
 });
 
 const mailOptions = {
-    from: config.email.from
+    from: config.email.from,
+    to: config.email.from
 };
 
 module.exports = function send(options, cb) {
diff --git a/src/libs/routes/api.js b/src/libs/routes/api.js
index c96266b0..31fc9257 100644
--- a/src/libs/routes/api.js
+++ b/src/libs/routes/api.js
@@ -128,6 +128,8 @@ const disciplines = require(`${libs}/routes/disciplines`);
 
 const universityLocalOffer = require(`${libs}/routes/universityLocalOffer`);
 
+const message = require(`${libs}/routes/message`);
+
 api.get('/', (req, res) => {
     res.json({ msg: 'SimCAQ API is running' });
 });
@@ -183,4 +185,5 @@ api.use('/microregion', microregion);
 api.use('/location', location);
 api.use('/disciplines', disciplines);
 api.use('/universityLocalOffer', universityLocalOffer);
+api.use('/message', message);
 module.exports = api;
diff --git a/src/libs/routes/message.js b/src/libs/routes/message.js
new file mode 100644
index 00000000..81292a39
--- /dev/null
+++ b/src/libs/routes/message.js
@@ -0,0 +1,47 @@
+/*
+Copyright (C) 2021 Centro de Computacao Cientifica e Software Livre
+Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
+
+This file is part of simcaq-node.
+
+simcaq-node is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+simcaq-node is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with simcaq-node.  If not, see <https://www.gnu.org/licenses/>.
+*/
+
+const express = require('express');
+
+const messageApp = express.Router();
+
+const email = require(`../middlewares/email`);
+
+messageApp.post('/', (req, res, next) => {
+    var reqName = JSON.parse(req.body.name)
+    var reqEmail = JSON.parse(req.body.email)
+    var reqContents = JSON.parse(req.body.contents)
+
+    let mailOptions = {
+        from: `"${reqName} <${reqEmail}>"`,
+        text: reqContents
+    }
+
+    email(mailOptions, (err, info) => {
+        if(err) {
+            log.error(err);
+            res.json({msg: 'Undelivered Contact Mail'});
+        }
+        log.info(`Message ${info.messageId} sent: ${info.response}`);
+        res.json({msg: 'Contact Mail Successfully Delivered'});
+    });
+})
+
+module.exports = messageApp;
-- 
GitLab