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/#55] 자동으로 메일을 발송하는 코드 작성 #62

Merged
merged 4 commits into from
Sep 24, 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
9 changes: 9 additions & 0 deletions backend/bin/www
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,20 @@ const app = require('../app');
const debug = require('debug')('backend:server');
const http = require('http');
const mongoose = require('mongoose');
const schedule = require('node-schedule');
const {findAllTeams, getUsersCommits, getUsersEmail, sendMails} = require('../src/services/mail/mailAutoSender');

mongoose.connect(process.env.DATABASE_URL)
.then(res => console.log("MongoDB Connected"))
.catch(err => console.error(err));

const autoMailScheduler = schedule.scheduleJob('*/5 * * * * *', async () => {
const teamIds = await findAllTeams();
const users = await getUsersCommits(teamIds);
const userEmails = await getUsersEmail(users);
await sendMails(userEmails);
});

/**
* Get port from environment and store in Express.
*/
Expand Down
1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"express-session": "^1.17.2",
"mongoose": "^6.0.7",
"morgan": "^1.9.1",
"node-schedule": "^2.0.0",
"nodemailer": "^6.6.5",
"passport": "^0.4.1",
"passport-github": "^1.1.0"
Expand Down
1 change: 0 additions & 1 deletion backend/src/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const Schema = mongoose.Schema;
const userSchema = new Schema({
nickName: String,
email: String,
teamIds: Array,
githubId : String,
accessToken: String
});
Expand Down
84 changes: 84 additions & 0 deletions backend/src/services/mail/mailAutoSender.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
const Team = require("../../models/team");
const User = require("../../models/user");
const { getCommitCountOfTeam } = require("../github");
const nodemailer = require('nodemailer');

const findAllTeams = async () => {
try{
const teams = await Team.find({});
const teamIds = teams.map(el => el._id.toString());
return teamIds;
}
catch(err){
console.error(err);
}
};

const getUsersCommits = async (teamIds) => {
try{
const users = [];
for(let i = 0 ; i < teamIds.length; i++){
const commits = await getCommitCountOfTeam(teamIds[i]);
users.concat(commits);
}
return users.filter(el => parseInt(el.commit) === 0).map(el => el.userId);
}
catch(err){
console.error(err);
}
};

const getUsersEmail = async (users) => {
try{

const userIds = [];
for(let i = 0; i < users.length; i++){
const user = await User.find({githubId:`${users[i]}`});
userIds.push(user.email);
};
return userIds;
}
catch(err){
console.error(err);
}
};

const sendMails = async (userIds) => {
try{
const email = process.env.GMAIL;
const emailPw = process.env.GMAILPW;

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

userIds.forEach(el => {

let mailOptions = {
from: email,
to: el,
subject: "1일 1커밋 운동 참여 독려",
html:`
<h1> 1일 1커밋 운동에 참여하세요! </h1>
<p>자라나라 잔디잔디!</p>
<p>아직 오늘이 가지 않았습니다! 커밋을 보내 잔디밭을 키워보세요!</p>
`
};

transport.sendMail(mailOptions, async (error, info) => {
if(error) throw new Error("메일을 보내는 데 실패하였습니다.");
});
});


}
catch(err){
console.error(err);
}
};

module.exports = {findAllTeams, getUsersCommits, getUsersEmail, sendMails};
5 changes: 5 additions & 0 deletions backend/src/services/team/team.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ const Team = require("../../models/team");

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

if(req.body.password === undefined) req.body.password = null;
req.body.password === null ? req.body.isLocked = false : req.body.isLocked = true;

const result = await Team.create({...req.body});
await res.status(201).json({
code: '2001',
Expand Down Expand Up @@ -98,6 +102,7 @@ const enterTeam = async (req,res,next) => {
const teamId = req.query['teamId'];
const userId = req.query['userId'];
const team = await Team.findById(teamId);
if(team.password !== null && team.password !== req.body.password) throw new Error("비밀번호가 일치하지 않습니다.");
const userIds = [...team.userIds];
userIds.push(userId);
team.userIds = userIds;
Expand Down