From 96a16e8be7bfeefa48a597ba75ae91548505a205 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=B0=AC=ED=98=B8?= Date: Fri, 24 Sep 2021 13:31:24 +0900 Subject: [PATCH 1/4] =?UTF-8?q?=F0=9F=97=83=EF=B8=8F=20user=20model?= =?UTF-8?q?=EC=97=90=EC=84=9C=20=ED=95=84=EC=9A=94=20=EC=97=86=EB=8A=94=20?= =?UTF-8?q?column=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/src/models/user.js | 1 - 1 file changed, 1 deletion(-) 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 }); From 6db1911d70a37e757757132297042da62484d099 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=B0=AC=ED=98=B8?= Date: Fri, 24 Sep 2021 13:32:07 +0900 Subject: [PATCH 2/4] =?UTF-8?q?=F0=9F=91=94=20=EC=82=AC=EC=9A=A9=EC=9E=90?= =?UTF-8?q?=20=EC=9E=85=EB=A0=A5=EC=97=90=20=EB=8C=80=ED=95=9C=20=EC=9C=A0?= =?UTF-8?q?=ED=9A=A8=EC=84=B1=20=EA=B2=80=EC=82=AC=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/src/services/team/team.js | 5 +++++ 1 file changed, 5 insertions(+) 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; From b5ca588e03b8b524bd7a45dbe7938476c9e481e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=B0=AC=ED=98=B8?= Date: Fri, 24 Sep 2021 13:33:03 +0900 Subject: [PATCH 3/4] =?UTF-8?q?=E2=9C=A8=20=ED=8A=B9=EC=A0=95=20=EC=8B=9C?= =?UTF-8?q?=EA=B0=84=EB=A7=88=EB=8B=A4=20=EC=82=AC=EB=9E=8C=EB=93=A4?= =?UTF-8?q?=EC=97=90=EA=B2=8C=20=EC=9E=90=EB=8F=99=EC=9C=BC=EB=A1=9C=20?= =?UTF-8?q?=EB=A9=94=EC=9D=BC=EC=9D=84=20=EB=B0=9C=EC=86=A1=ED=95=98?= =?UTF-8?q?=EB=8A=94=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/bin/www | 9 +++ backend/package.json | 1 + backend/src/services/mail/mailAutoSender.js | 85 +++++++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 backend/src/services/mail/mailAutoSender.js 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/services/mail/mailAutoSender.js b/backend/src/services/mail/mailAutoSender.js new file mode 100644 index 0000000..a537b02 --- /dev/null +++ b/backend/src/services/mail/mailAutoSender.js @@ -0,0 +1,85 @@ +const Team = require("../../models/team"); +const User = require("../../models/user"); +const fetch = require('node-fetch'); +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; + } + catch(err){ + console.error(err); + } +}; + +const getUsersEmail = async (users) => { + try{ + + const userIds = []; + for(let i = 0; i < users.length; i++){ + const user = 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:` +

1일 1커밋 운동에 참여하세요!

+

자라나라 잔디잔디!

+

아직 오늘이 가지 않았습니다! 커밋을 보내 잔디밭을 키워보세요!

+ ` + }; + + 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 From 854a34605de8cdf8c4654f978febfd84afae97c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=B0=AC=ED=98=B8?= Date: Fri, 24 Sep 2021 13:48:16 +0900 Subject: [PATCH 4/4] =?UTF-8?q?=F0=9F=90=9B=20commit=20=EC=88=98=EB=A1=9C?= =?UTF-8?q?=20=ED=95=84=ED=84=B0=EB=A7=81=EC=9D=B4=20=EB=90=98=EC=A7=80=20?= =?UTF-8?q?=EC=95=8A=EB=8A=94=20=EB=B2=84=EA=B7=B8=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/src/services/mail/mailAutoSender.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/backend/src/services/mail/mailAutoSender.js b/backend/src/services/mail/mailAutoSender.js index a537b02..985c702 100644 --- a/backend/src/services/mail/mailAutoSender.js +++ b/backend/src/services/mail/mailAutoSender.js @@ -1,6 +1,5 @@ const Team = require("../../models/team"); const User = require("../../models/user"); -const fetch = require('node-fetch'); const { getCommitCountOfTeam } = require("../github"); const nodemailer = require('nodemailer'); @@ -22,7 +21,7 @@ const getUsersCommits = async (teamIds) => { const commits = await getCommitCountOfTeam(teamIds[i]); users.concat(commits); } - return users; + return users.filter(el => parseInt(el.commit) === 0).map(el => el.userId); } catch(err){ console.error(err); @@ -34,7 +33,7 @@ const getUsersEmail = async (users) => { const userIds = []; for(let i = 0; i < users.length; i++){ - const user = User.find({githubId:`${users[i]}`}); + const user = await User.find({githubId:`${users[i]}`}); userIds.push(user.email); }; return userIds;