Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat/#31] GET /mail/:userId를 통해 메일 보내는 기능 구현 #39

Merged
merged 5 commits into from
Sep 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions backend/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const indexRouter = require('./src/routes/index');
const userRouter = require('./src/routes/user');
const authRouter = require('./src/routes/auth');
const teamRouter = require('./src/routes/team');
const mailRouter = require('./src/routes/mail');

const app = express();

Expand All @@ -31,5 +32,6 @@ app.use('/', indexRouter);
app.use('/user', userRouter);
app.use('/auth', authRouter);
app.use('/study',teamRouter);
app.use('/mail',mailRouter);

module.exports = app;
3 changes: 2 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
"debug": "^2.6.9",
"dotenv": "^10.0.0",
"express": "^4.16.1",
"mongoose": "^6.0.7",
"express-session": "^1.17.2",
"mongoose": "^6.0.7",
"morgan": "^1.9.1",
"nodemailer": "^6.6.5",
"passport": "^0.4.1",
"passport-github": "^1.1.0"
},
Expand Down
7 changes: 7 additions & 0 deletions backend/src/routes/mail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const {findUser, sendMail} = require('../services/mail/mail');
const express = require('express');
const router = express.Router();

router.get('/:userId',[findUser, sendMail]);

module.exports = router;
67 changes: 67 additions & 0 deletions backend/src/services/mail/mail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
const nodemailer = require("nodemailer");
const User = require("../../models/user");

const findUser = async (req, res, next) => {
try{
const userId = req.params['userId'];
const user = await User.findById(userId);
req.params["email"] = user["email"];
if(req.params["email"] === undefined) throw new Error("사용자의 이메일이 없습니다.");
next();
}
catch(err){
console.error(err);
await res.status(500).json({
code: '5000',
status: '에러 : 서버 에러',
message : `메일 발송 요청 처리 중 사용자를 찾지 못했습니다.`
});
}
};

const sendMail = async (req, res, next) => {

// Gmail
const email = process.env.GMAIL;
const emailPw = process.env.GMAILPW;

let transport = nodemailer.createTransport({
service: "gmail",
auth:{
user: email,
pass: emailPw
}
});

let mailOptions = {
from: email,
to: req.params["email"],
subject: "1일 1커밋 운동 참여 독려",
html:`
<h1> 1일 1커밋 운동에 참여하세요! </h1>
<p>자라나라 잔디잔디!</p>
<p>아직 오늘이 가지 않았습니다! 커밋을 보내 잔디밭을 키워보세요!</p>
`
};
try{
transport.sendMail(mailOptions, async (error, info) => {
if(error) throw new Error("메일을 보내는 데 실패하였습니다.");
await res.status(200).json({
code: '2000',
status: '성공 : 메세지 발송',
message: '메세지 발송이 정상적으로 수행되었습니다.',
info: info
});
});
}
catch(err){
console.error(err);
await res.status(500).json({
code: '5000',
status: '에러 : 서버 에러',
message : `${err}`
});
}
};

module.exports = {findUser, sendMail};
14 changes: 11 additions & 3 deletions backend/src/services/team/team.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,17 @@ const updateTeam = async (req, res, next) => {

const searchTeams = async (req, res, next) => {
try{

let title = req.query["title"];
if(title === undefined) title = '';
const teams = await Team.find({title: new RegExp(`${title}`,'i')}).exec();
let userId = req.query["userId"];
let teams;

if(title !== undefined && userId === undefined) teams = await Team.find({title: new RegExp(`${title}`,'i')}).exec();
else if(title === undefined && userId !== undefined) teams = await Team.find({userIds : [`${userId}`]}).exec();
else if(title === undefined && userId === undefined) teams = await Team.find({});
else{
throw new Error("query로 보내는 변수가 잘못되었습니다.");
}

await res.status(200).json({
code: '2000',
Expand All @@ -80,7 +88,7 @@ const searchTeams = async (req, res, next) => {
await res.status(500).json({
code: '5000',
status: '에러 : 서버 에러',
message : '검색 요청 처리 중 서버에서 문제가 발생했습니다.'
message : `${err}`
});
}
};
Expand Down