Skip to content

Commit

Permalink
[DELETE] /notice/:noticeId 알림 삭제
Browse files Browse the repository at this point in the history
[DELETE] /notice/:noticeId 알림 삭제
  • Loading branch information
xxeol2 authored Jan 13, 2022
2 parents 375b60c + 2f03197 commit 3bc19fd
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 1 deletion.
3 changes: 3 additions & 0 deletions functions/api/routes/notice/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
const express = require('express');
const router = express.Router();
const { checkUser } = require('../../../middlewares/auth');

router.use('/active', require('./active'));
router.use('/service', require('./service'));

router.delete('/:noticeId', checkUser, require('./noticeDELETE'));

module.exports = router;
54 changes: 54 additions & 0 deletions functions/api/routes/notice/noticeDELETE.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const functions = require('firebase-functions');
const util = require('../../../lib/util');
const statusCode = require('../../../constants/statusCode');
const responseMessage = require('../../../constants/responseMessage');
const db = require('../../../db/db');
const { noticeDB } = require('../../../db');

/**
* @서비스_및_활동_알림_삭제
* @route GET /notice/:noticeId
* @error
* 1. 알림 id가 전달되지 않음
* 2. 올바르지 않은 noticeId가 전달된 경우
* 3. 알림 삭제 권한이 없는 사용자가 요청을 보낸 경우
*/

module.exports = async (req, res) => {
const { noticeId } = req.params;
const user = req.user;
const userId = user.userId;

// @error 1. 알림 id가 전달되지 않음
if (!noticeId) {
return res.status(statusCode.BAD_REQUEST).send(util.fail(statusCode.BAD_REQUEST, responseMessage.NULL_VALUE));
}

let client;

try {
client = await db.connect(req);

const notice = await noticeDB.getNoticeByNoticeId(client, noticeId);

// @error 2. 올바르지 않은 noticeId가 전달된 경우
if (!notice) {
return res.status(statusCode.BAD_REQUEST).send(util.fail(statusCode.BAD_REQUEST, responseMessage.NOTICE_ID_NOT_VALID));
}

// @error 3. 알림 삭제 권한이 없는 사용자가 요청을 보낸 경우
if (userId !== notice.receiverId) {
return res.status(statusCode.BAD_REQUEST).send(util.fail(statusCode.BAD_REQUEST, responseMessage.PRIV_NOT_FOUND));
}

await noticeDB.deleteNoticeByNoticeId(client, noticeId);

res.status(statusCode.OK).send(util.success(statusCode.OK, responseMessage.NOTICE_DELETE_SUCCESS));
} catch (error) {
functions.logger.error(`[ERROR] [${req.method.toUpperCase()}] ${req.originalUrl}`, `[CONTENT] ${error}`);
console.log(error);
res.status(statusCode.INTERNAL_SERVER_ERROR).send(util.fail(statusCode.INTERNAL_SERVER_ERROR, responseMessage.INTERNAL_SERVER_ERROR));
} finally {
client.release();
}
};
2 changes: 2 additions & 0 deletions functions/constants/responseMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,6 @@ module.exports = {
ACTIVE_READ_SUCCESS: '활동 알림 읽음처리 완료',
SERVICE_GET_SUCCESS: '서비스 알림 조회 완료',
ACTIVE_GET_SUCCESS: '활동 알림 조회 완료',
NOTICE_DELETE_SUCCESS: '알림 삭제 완료',
NOTICE_ID_NOT_VALID: '유효하지 않은 알림 ID 입니다',
};
28 changes: 27 additions & 1 deletion functions/db/notice.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const dayjs = require('dayjs');
const _ = require('lodash');
const convertSnakeToCamel = require('../lib/convertSnakeToCamel');

Expand Down Expand Up @@ -33,6 +34,31 @@ const activeReadByUserId = async (client, userId) => {
return convertSnakeToCamel.keysToCamel(rows);
};

const getNoticeByNoticeId = async (client, noticeId) => {
const { rows } = await client.query(
`
SELECT * FROM spark.notification
WHERE notification_id = $1
`,
[noticeId],
);
return convertSnakeToCamel.keysToCamel(rows[0]);
};

const deleteNoticeByNoticeId = async (client, noticeId) => {
const now = dayjs().add(9, 'h');
const { rows } = await client.query(
`
UPDATE spark.notification
SET is_deleted = TRUE, deleted_at = $2, updated_at = $2
WHERE notification_id = $1
RETURNING *
`,
[noticeId, now],
);
return convertSnakeToCamel.keysToCamel(rows[0]);
};

const getServicesByUserId = async (client, userId, lastid, size) => {
const { rows } = await client.query(
`
Expand Down Expand Up @@ -67,4 +93,4 @@ const getActivesByUserId = async (client, userId, lastid, size) => {
return convertSnakeToCamel.keysToCamel(rows);
};

module.exports = { serviceReadByUserId, activeReadByUserId, getServicesByUserId, getActivesByUserId };
module.exports = { serviceReadByUserId, activeReadByUserId, getNoticeByNoticeId, deleteNoticeByNoticeId, getServicesByUserId, getActivesByUserId };

0 comments on commit 3bc19fd

Please sign in to comment.