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', + `