diff --git a/backend/bin/www b/backend/bin/www index f8e47c7..7cf50d9 100644 --- a/backend/bin/www +++ b/backend/bin/www @@ -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. */ diff --git a/backend/package.json b/backend/package.json index 9accb41..6c86553 100644 --- a/backend/package.json +++ b/backend/package.json @@ -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" diff --git a/backend/src/models/user.js b/backend/src/models/user.js index 82d2515..6714fa9 100644 --- a/backend/src/models/user.js +++ b/backend/src/models/user.js @@ -5,7 +5,6 @@ const Schema = mongoose.Schema; const userSchema = new Schema({ nickName: String, email: String, - teamIds: Array, githubId : String, accessToken: String }); diff --git a/backend/src/services/mail/mailAutoSender.js b/backend/src/services/mail/mailAutoSender.js new file mode 100644 index 0000000..985c702 --- /dev/null +++ b/backend/src/services/mail/mailAutoSender.js @@ -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:` +
자라나라 잔디잔디!
+아직 오늘이 가지 않았습니다! 커밋을 보내 잔디밭을 키워보세요!
+ ` + }; + + transport.sendMail(mailOptions, async (error, info) => { + if(error) throw new Error("메일을 보내는 데 실패하였습니다."); + }); + }); + + + } + catch(err){ + console.error(err); + } +}; + +module.exports = {findAllTeams, getUsersCommits, getUsersEmail, sendMails}; \ No newline at end of file diff --git a/backend/src/services/team/team.js b/backend/src/services/team/team.js index 41785bc..fd372f6 100644 --- a/backend/src/services/team/team.js +++ b/backend/src/services/team/team.js @@ -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', @@ -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;