Skip to content

Commit

Permalink
[FEAT] 커뮤니티 게시글 신고 시 슬랙 알림 발송 (#361)
Browse files Browse the repository at this point in the history
* [FIX] slack message block/text type에 따른 body 수정

* [FEAT] 커뮤니티 게시글 신고 시 slack 알림 발송

* [FIX] DB 신고 쿼리에서 게시글 정보 반환하도록 수정
  • Loading branch information
jokj624 authored Jun 25, 2024
1 parent 62299a4 commit 390a066
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 23 deletions.
27 changes: 23 additions & 4 deletions functions/api/routes/community/communityReportPOST.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const responseMessage = require('../../../constants/responseMessage');
const db = require('../../../db/db');
const { communityDB } = require('../../../db');
const asyncWrapper = require('../../../lib/asyncWrapper');
const { getCommunityReportMessage } = require('../../../lib/slackMessage');
const slackAPI = require('../../../middlewares/slackAPI');

/**
* @route POST /community/reports
Expand All @@ -18,10 +20,27 @@ module.exports = asyncWrapper(async (req, res) => {
const dbConnection = await db.connect(req);
req.dbConnection = dbConnection;

const communityPostReport = await communityDB.reportCommunityPost(dbConnection, userId, communityPostId);
const communityPostReport = await communityDB.reportCommunityPost(
dbConnection,
userId,
communityPostId,
);
if (!communityPostReport) {
return res.status(statusCode.BAD_REQUEST).send(util.fail(statusCode.BAD_REQUEST, responseMessage.NO_COMMUNITY_POST))
return res
.status(statusCode.BAD_REQUEST)
.send(util.fail(statusCode.BAD_REQUEST, responseMessage.NO_COMMUNITY_POST));
}

res.status(statusCode.CREATED).send(util.success(statusCode.CREATED, responseMessage.REPORT_COMMUNITY_POST_SUCCESS))

const slackReportMessage = getCommunityReportMessage(
userId,
communityPostId,
communityPostReport.title,
communityPostReport.postUserId,
);

slackAPI.sendMessageToSlack(slackReportMessage, slackAPI.WEB_HOOK_ERROR_MONITORING, true);

res
.status(statusCode.CREATED)
.send(util.success(statusCode.CREATED, responseMessage.REPORT_COMMUNITY_POST_SUCCESS));
});
9 changes: 8 additions & 1 deletion functions/db/community.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ const reportCommunityPost = async (client, userId, communityPostId) => {
[communityPostId],
);
if (!existingCommunityPosts[0]) return existingCommunityPosts[0];

const { title, user_id: postUserId } = existingCommunityPosts[0];

const { rows: communityPostReports } = await client.query(
`
INSERT INTO community_post_report_user
Expand All @@ -194,7 +197,11 @@ const reportCommunityPost = async (client, userId, communityPostId) => {
`,
[userId, communityPostId],
);
return convertSnakeToCamel.keysToCamel(communityPostReports[0]);
return {
...convertSnakeToCamel.keysToCamel(communityPostReports[0]),
title,
postUserId,
};
};

const getCommunityPostById = async (client, communityPostId) => {
Expand Down
28 changes: 28 additions & 0 deletions functions/lib/slackMessage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const getCommunityReportMessage = (userId, postId, title, postUserId) => `
{
"blocks": [
{
"type": "header",
"text": {
"type": "plain_text",
"text": "🚨 게시글 신고 알림 🚨",
"emoji": true
}
},
{
"type": "divider"
},
{
"type": "context",
"elements": [
{
"type": "mrkdwn",
"text": "*신고 유저 ID*: ${userId} \n *신고 게시글 ID: ${postId} * \n *게시글 제목:* ${title} \n *게시글 작성자 ID*: ${postUserId}"
}
]
}
]
}
`;

module.exports = { getCommunityReportMessage };
42 changes: 24 additions & 18 deletions functions/middlewares/slackAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,31 @@ const axios = require('axios');
// endpoint 자체는 깃허브에 올라가면 안 되기 때문!
const WEB_HOOK_ERROR_MONITORING = process.env.WEB_HOOK_ERROR_MONITORING;

const sendMessageToSlack = (message, apiEndPoint = WEB_HOOK_ERROR_MONITORING) => {
// 슬랙 Webhook을 이용해 슬랙에 메시지를 보내는 코드
try {
axios
.post(apiEndPoint, { text: message })
.then((response) => { })
.catch((e) => {
throw e;
});
} catch (e) {
console.error(e);
// 슬랙 Webhook 자체에서 에러가 났을 경우,
// Firebase 콘솔에 에러를 찍는 코드
functions.logger.error('[slackAPI 에러]', { error: e });
}
const sendMessageToSlack = (
message,
apiEndPoint = WEB_HOOK_ERROR_MONITORING,
isBlockMessage = false,
) => {
// 슬랙 Webhook을 이용해 슬랙에 메시지를 보내는 코드
const body = isBlockMessage ? message : { text: message };

try {
axios
.post(apiEndPoint, body)
.then((response) => {})
.catch((e) => {
throw e;
});
} catch (e) {
console.error(e);
// 슬랙 Webhook 자체에서 에러가 났을 경우,
// Firebase 콘솔에 에러를 찍는 코드
functions.logger.error('[slackAPI 에러]', { error: e });
}
};

// 이 파일에서 정의한 변수 / 함수를 export 해서, 다른 곳에서 사용할 수 있게 해주는 코드
module.exports = {
sendMessageToSlack,
WEB_HOOK_ERROR_MONITORING,
};
sendMessageToSlack,
WEB_HOOK_ERROR_MONITORING,
};

0 comments on commit 390a066

Please sign in to comment.