Skip to content

Commit

Permalink
Merge pull request #304 from sparcs-kaist/#303-introduce-aws-ses
Browse files Browse the repository at this point in the history
#303 introduce aws ses
  • Loading branch information
14KGun authored Aug 5, 2023
2 parents 48dbabf + 84ca67c commit ca31167
Show file tree
Hide file tree
Showing 17 changed files with 277 additions and 89 deletions.
1 change: 1 addition & 0 deletions loadenv.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ module.exports = {
JSON.parse(process.env.GOOGLE_APPLICATION_CREDENTIALS),
testAccounts:
process.env.TEST_ACCOUNTS && JSON.parse(process.env.TEST_ACCOUNTS),
slackWebhookUrl: { report: process.env.SLACK_REPORT_WEBHOOK_URL || "" },
};
49 changes: 26 additions & 23 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"ajv": "^8.12.0",
"ajv-errors": "^3.0.0",
"ajv-formats": "^2.1.1",
"aws-sdk": "^2.1182.0",
"aws-sdk": "^2.1386.0",
"axios": "^0.27.2",
"ci": "^2.2.0",
"connect-mongo": "^4.6.0",
Expand Down
28 changes: 28 additions & 0 deletions src/modules/slackNotification.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const { slackWebhookUrl: slackUrl } = require("../../loadenv");
const axios = require("axios");
const logger = require("../modules/logger");

module.exports.notifyToReportChannel = (reportUser, report) => {
if (!slackUrl.report) return;

const data = {
text: `${reportUser}님으로부터 신고가 접수되었습니다.
신고자 ID: ${report.creatorId}
신고 ID: ${report.reportedId}
방 ID: ${report.roomId ?? ""}
사유: ${report.type}
기타: ${report.etcDetail}
`,
};
const config = { "Content-Type": "application/json" };

axios
.post(slackUrl.report, data, config)
.then((res) => {
logger.info("Slack webhook sent successfully");
})
.catch((err) => {
logger.error("Fail to send slack webhook", err);
});
};
4 changes: 2 additions & 2 deletions src/modules/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const sessionMiddleware = require("../middlewares/session");
const logger = require("./logger");
const { getLoginInfo } = require("./auths/login");
const { roomModel, userModel, chatModel } = require("./stores/mongo");
const { getS3Url } = require("./stores/awsS3");
const { getS3Url } = require("./stores/aws");
const { getTokensOfUsers, sendMessageByTokens } = require("./fcm");

const { frontUrl } = require("../../loadenv");
Expand Down Expand Up @@ -53,7 +53,7 @@ const transformChatsForRoom = async (chats) => {
inOutNames: chat.inOutNames,
});
}

return chatsToSend;
};

Expand Down
37 changes: 37 additions & 0 deletions src/modules/stores/awsS3.js → src/modules/stores/aws.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
const { aws: awsEnv } = require("../../../loadenv");

const logger = require("../logger");
// Load the AWS-SDK and s3
const AWS = require("aws-sdk");
AWS.config.update({
region: "ap-northeast-2",
signatureVersion: "v4",
});

const s3 = new AWS.S3({ apiVersion: "2006-03-01" });
const ses = new AWS.SES({ apiVersion: "2010-12-01" });

// function to list Object
module.exports.getList = (directoryPath, cb) => {
Expand Down Expand Up @@ -80,3 +83,37 @@ module.exports.foundObject = (filePath, cb) => {
module.exports.getS3Url = (filePath) => {
return `${awsEnv.s3Url}${filePath}`;
};

module.exports.sendReportEmail = (reportedEmail, report, html) => {
const reportTypeMap = {
"no-settlement": "정산을 하지 않음",
"no-show": "택시에 동승하지 않음",
"etc-reason": "기타 사유",
};

const params = {
Destination: {
ToAddresses: [reportedEmail],
},
Message: {
Body: {
Html: {
Data: html,
},
},
Subject: {
Charset: "UTF-8",
Data: `[SPARCS TAXI] 신고가 접수되었습니다 (사유: ${reportTypeMap[report.type]})`,
},
},
Source: "taxi.sparcs@gmail.com",
};

ses.sendEmail(params, (err, data) => {
if (err) {
logger.error("Fail to send email", err);
} else {
logger.info("Email sent successfully");
}
});
};
1 change: 1 addition & 0 deletions src/modules/stores/mongo.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ const reportSchema = Schema({
},
etcDetail: { type: String, default: "" }, // 기타 세부 사유
time: { type: Date, required: true },
roomId: { type: Schema.Types.ObjectId, ref: "Room" }, // 신고한 방 id
});

const adminIPWhitelistSchema = Schema({
Expand Down
7 changes: 6 additions & 1 deletion src/routes/docs/reportsSchema.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const reportsSchema = {
createHandler: {
type: "object",
required: ["reportedId", "type", "time"],
required: ["reportedId", "type", "time", "roomId"],
properties: {
reportedId: {
type: "string",
Expand All @@ -14,11 +14,16 @@ const reportsSchema = {
etcDetail: {
type: "string",
maxLength: 30,
default: "",
},
time: {
type: "string",
format: "date-time",
},
roomId: {
type: "string",
pattern: "^[a-fA-F\\d]{24}$",
},
},
errorMessage: "validation: bad request",
},
Expand Down
51 changes: 2 additions & 49 deletions src/services/auth.replace.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,54 +11,7 @@ const logger = require("../modules/logger");
const jwt = require("../modules/auths/jwt");

const { registerDeviceTokenHandler } = require("../services/auth");

const loginHtmlBuilder = (redirectPath) => `
<!DOCTYPE html>
<html lang="ko">
<head>
<title>replace Login</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1.0,minium-scale=1.0,maxinum-scale=1.0,user-scalable=no" />
<script src="https://code.jquery.com/jquery-latest.min.js"></script>
<script>
$(document).ready(function(){
const post = (path, params) => {
var form = document.createElement("form");
form.setAttribute("method", "post");
form.setAttribute("action", path);
for(var key in params) {
var hiddenField = document.createElement("input");
hiddenField.setAttribute("type", "hidden");
hiddenField.setAttribute("name", key);
hiddenField.setAttribute("value", params[key]);
form.appendChild(hiddenField);
}
document.body.appendChild(form);
form.submit();
}
const submitHandler = () => {
const value = document.getElementById("input-id").value;
if(value) post('/auth/login/replace', {
id: value,
redirect: "${encodeURIComponent(redirectPath)}",
});
}
const enterHandler = (e) => {
if (e.keyCode === 13) submitHandler();
}
$('#btn').click(submitHandler);
$('#input-id').on("keyup", enterHandler);
$('#input-id').focus();
});
</script>
</head>
<body>
<div>아이디 입력</div>
<input id="input-id">
<div id="btn">로그인</div>
</body>
</html>
`;
const loginReplacePage = require("../views/loginReplacePage");

const makeInfo = (id) => {
const info = {
Expand Down Expand Up @@ -144,7 +97,7 @@ const sparcsssoHandler = (req, res) => {
const isApp = !!req.query.isApp;

req.session.isApp = isApp;
res.end(loginHtmlBuilder(redirectPath));
res.end(loginReplacePage(redirectPath));
};

const logoutHandler = async (req, res) => {
Expand Down
6 changes: 3 additions & 3 deletions src/services/chats.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { chatModel, userModel, roomModel } = require("../modules/stores/mongo");
const { chatPopulateOption } = require("../modules/populates/chats");
const awsS3 = require("../modules/stores/awsS3");
const aws = require("../modules/stores/aws");
const { transformChatsForRoom, emitChatEvent } = require("../modules/socket");

const chatCount = 60;
Expand Down Expand Up @@ -188,7 +188,7 @@ const uploadChatImgGetPUrlHandler = async (req, res) => {
});
const chat = await chatDocument.save();
const key = `chat-img/${chat._id}`;
awsS3.getUploadPUrlPost(key, type, (err, data) => {
aws.getUploadPUrlPost(key, type, (err, data) => {
if (err) {
return res
.status(500)
Expand Down Expand Up @@ -234,7 +234,7 @@ const uploadChatImgDoneHandler = async (req, res) => {
});
}
const key = `chat-img/${chat._id}`;
awsS3.foundObject(key, async (err, data) => {
aws.foundObject(key, async (err, data) => {
if (err) {
return res
.status(500)
Expand Down
Loading

0 comments on commit ca31167

Please sign in to comment.