From a4f6d47a80682d4d396635f6cfe46183570dfa8c Mon Sep 17 00:00:00 2001 From: hokim Date: Mon, 30 Aug 2021 18:28:10 +0900 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20contact=20=ED=8E=98=EC=9D=B4?= =?UTF-8?q?=EC=A7=80=20=EA=B5=AC=ED=98=84=20-=EB=A9=94=EC=9D=BC=EC=9D=84?= =?UTF-8?q?=20=EB=B3=B4=EB=82=BC=20=EC=88=98=20=EC=9E=88=EB=8A=94=20contac?= =?UTF-8?q?t=20=ED=8E=98=EC=9D=B4=EC=A7=80=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- saver-web/.env.sample | 2 + .../controllers/user/settingController.js | 43 +++++-- saver-web/lib/sendContactMail.js | 30 +++++ saver-web/public/javascripts/buildSetting.js | 65 +++++----- .../stylesheets/user/pages/bookmark_alarm.css | 104 ++++++++++++---- saver-web/routes/setting.js | 11 +- saver-web/views/user/contact.ejs | 113 ++++++++++++++++++ saver-web/views/user/setting.ejs | 4 +- 8 files changed, 294 insertions(+), 78 deletions(-) create mode 100644 saver-web/lib/sendContactMail.js create mode 100644 saver-web/views/user/contact.ejs diff --git a/saver-web/.env.sample b/saver-web/.env.sample index ee37c0e7..1a9541f2 100644 --- a/saver-web/.env.sample +++ b/saver-web/.env.sample @@ -8,6 +8,8 @@ MYSQL_PASSWORD= SALT_ROUNDS=10 GMAIL_USER_NAME= GMAIL_PASSWORD= +CONTACT_USER_NAME= +CONTACT_PASSWORD= S3= S3_BUCKET= KAKAO_JS_KEY= diff --git a/saver-web/controllers/user/settingController.js b/saver-web/controllers/user/settingController.js index 0e98713b..467b19d4 100644 --- a/saver-web/controllers/user/settingController.js +++ b/saver-web/controllers/user/settingController.js @@ -1,15 +1,13 @@ -const {Subscriber } = require('../../models'); +const { Subscriber } = require('../../models'); const sequelize = require('../../models').sequelize; -const Sequelize = require("sequelize"); +const Sequelize = require('sequelize'); const Op = Sequelize.Op; +const sendContactMail = require('../../lib/sendContactMail'); const getSettingRequest = async (req, res) => { const user = await Subscriber.findAll({ where: { id: req.decoded.userId }, - attributes: [ - 'email', - 'alarmStatus', - ], + attributes: ['email', 'alarmStatus'], }); try { res.status(200).json({ @@ -24,23 +22,41 @@ const getSettingRequest = async (req, res) => { const AlarmOnOffRequest = async (req, res) => { var flag = req.params.flag; - try{ + try { const result = await Subscriber.update( - {alarmStatus: flag}, - {where: { id: req.decoded.userId}} + { alarmStatus: flag }, + { where: { id: req.decoded.userId } }, ); - + res.status(200).json({ success: true, }); - } catch (error){ + } catch (error) { res.status(400).json({ succes: false, - error + error, }); } -} +}; +const postContactMail = async (req, res) => { + try { + await sendContactMail( + req.body.title, + `문의자 이메일: ${req.body.email}\n ---\n${req.body.contents}`, + ); + + res.status(200).json({ + success: true, + }); + } catch (error) { + console.log(error); + res.status(400).json({ + succes: false, + error, + }); + } +}; module.exports = { section: (req, res, next) => { @@ -50,4 +66,5 @@ module.exports = { getUser: getSettingRequest, OnOff: AlarmOnOffRequest, }, + postContactMail, }; diff --git a/saver-web/lib/sendContactMail.js b/saver-web/lib/sendContactMail.js new file mode 100644 index 00000000..fc8864ab --- /dev/null +++ b/saver-web/lib/sendContactMail.js @@ -0,0 +1,30 @@ +const nodemailer = require('nodemailer'); + +module.exports = async (title, contents) => { + const myEmail = nodemailer.createTransport({ + service: 'gmail', + auth: { + user: process.env.CONTACT_USER_NAME, + pass: process.env.CONTACT_PASSWORD, + }, + tls: { + rejectUnauthorized: false, + }, + }); + + await myEmail.sendMail( + { + from: `${process.env.CONTACT_USER_NAME}`, + to: `${process.env.CONTACT_USER_NAME}`, + subject: title, + text: contents, + }, + function (error, info) { + if (error) { + console.log(error); + } else { + return info.response; + } + }, + ); +}; diff --git a/saver-web/public/javascripts/buildSetting.js b/saver-web/public/javascripts/buildSetting.js index dba317b0..79d94c56 100644 --- a/saver-web/public/javascripts/buildSetting.js +++ b/saver-web/public/javascripts/buildSetting.js @@ -1,28 +1,25 @@ var buildPoint = document.querySelector('.bookmark_alarm-page'); -const buildStart = async() => { - await $.ajax({ - url: `/setting/getuser`, - type: 'get', - headers: { 'x-access-token': token }, - success: function (user) { - console.log(user.user); - getListTemplate(user.user[0]); +const buildStart = async () => { + await $.ajax({ + url: `/setting/getuser`, + type: 'get', + headers: { 'x-access-token': token }, + success: function (user) { + console.log(user.user); + getListTemplate(user.user[0]); + }, + error: function (error) { + alert(error.response.data.message); + }, + }); +}; - }, - error: function (error) { - alert(error.response.data.message); - }, - }); - }; +buildStart(); - buildStart(); - - function getListTemplate(user) { - - - buildPoint.insertAdjacentHTML( - 'beforeend', - ` +function getListTemplate(user) { + buildPoint.insertAdjacentHTML( + 'beforeend', + `