Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] 푸시알림 설정 toggle API #224

Merged
merged 9 commits into from
Mar 6, 2022
2 changes: 1 addition & 1 deletion functions/api/routes/notice/active/activeGET.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ module.exports = async (req, res) => {
return notice;
});

res.status(statusCode.OK).send(util.success(statusCode.OK, responseMessage.ACTIVE_GET_SUCCESS, { notices }));
res.status(statusCode.OK).send(util.success(statusCode.OK, responseMessage.GET_ACTIVE_SUCCESS, { notices }));
} catch (error) {
functions.logger.error(`[ERROR] [${req.method.toUpperCase()}] ${req.originalUrl}`, `[CONTENT] ${error}`);
console.log(error);
Expand Down
4 changes: 3 additions & 1 deletion functions/api/routes/notice/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ const { checkUser } = require('../../../middlewares/auth');
router.use('/active', require('./active'));
router.use('/service', require('./service'));

router.delete('/:noticeId', checkUser, require('./noticeDELETE'));
router.get('/new', checkUser, require('./noticeNewGET'));
router.get('/setting', checkUser, require('./noticeSettingGET'));
router.patch('/setting', checkUser, require('./noticeSettingPATCH'));
router.delete('/:noticeId', checkUser, require('./noticeDELETE'));

module.exports = router;
2 changes: 1 addition & 1 deletion functions/api/routes/notice/noticeNewGET.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module.exports = async (req, res) => {

const numOfUnreadServiceNotice = await noticeDB.getNumberOfUnreadServiceNoticeById(client, user.userId);

res.status(statusCode.OK).send(util.success(statusCode.OK, responseMessage.NEW_NOTICE_GET_SUCCESS, { newNotice: numOfUnreadServiceNotice > 0 ? true : false }));
res.status(statusCode.OK).send(util.success(statusCode.OK, responseMessage.GET_NEW_NOTICE_SUCCESS, { newNotice: numOfUnreadServiceNotice > 0 ? true : false }));
} catch (error) {
functions.logger.error(`[ERROR] [${req.method.toUpperCase()}] ${req.originalUrl}`, `[CONTENT] ${error}`);
console.log(error);
Expand Down
38 changes: 38 additions & 0 deletions functions/api/routes/notice/noticeSettingGET.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const functions = require('firebase-functions');
const util = require('../../../lib/util');
const statusCode = require('../../../constants/statusCode');
const responseMessage = require('../../../constants/responseMessage');
const slackAPI = require('../../../middlewares/slackAPI');

/**
* @푸시알림_설정_조회
* @route GET /notice/setting
* @error
*
*/

module.exports = async (req, res) => {
const user = req.user;

try {
const { pushRoomStart, pushSpark, pushConsider, pushCertification, pushRemind } = user;

const data = {
roomStart: pushRoomStart,
spark: pushSpark,
consider: pushConsider,
certification: pushCertification,
remind: pushRemind,
};

res.status(statusCode.OK).send(util.success(statusCode.OK, responseMessage.GET_NOTICE_SETTING_SUCCESS, data));
} catch (error) {
functions.logger.error(`[ERROR] [${req.method.toUpperCase()}] ${req.originalUrl}`, `[CONTENT] ${error}`);
console.log(error);
const slackMessage = `[ERROR] [${req.method.toUpperCase()}] ${req.originalUrl} ${error} ${JSON.stringify(error)}`;
slackAPI.sendMessageToSlack(slackMessage, slackAPI.DEV_WEB_HOOK_ERROR_MONITORING);

return res.status(statusCode.INTERNAL_SERVER_ERROR).send(util.fail(statusCode.INTERNAL_SERVER_ERROR, responseMessage.INTERNAL_SERVER_ERROR));
} finally {
}
};
42 changes: 42 additions & 0 deletions functions/api/routes/notice/noticeSettingPATCH.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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 slackAPI = require('../../../middlewares/slackAPI');
const { userDB } = require('../../../db');

/**
* @푸시알림_설정_토글
* @route PATCH /notice/setting
* @error
* 1. 유효하지 않은 category
*/

module.exports = async (req, res) => {
const user = req.user;
const category = req.query.category;

// @error 1. 유효하지 않은 category
if (!['roomStart', 'spark', 'consider', 'certification', 'remind'].includes(category)) {
return res.status(statusCode.BAD_REQUEST).send(util.fail(statusCode.BAD_REQUEST, responseMessage.PUSH_CATEGORY_INVALID));
}

let client;

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

await userDB.togglePushSettingById(client, user.userId, category);

res.status(statusCode.OK).send(util.success(statusCode.OK, responseMessage.PUSH_TOGGLE_SUCCESS));
} catch (error) {
functions.logger.error(`[ERROR] [${req.method.toUpperCase()}] ${req.originalUrl}`, `[CONTENT] ${error}`);
console.log(error);
const slackMessage = `[ERROR] [${req.method.toUpperCase()}] ${req.originalUrl} ${error} ${JSON.stringify(error)}`;
slackAPI.sendMessageToSlack(slackMessage, slackAPI.DEV_WEB_HOOK_ERROR_MONITORING);

return res.status(statusCode.INTERNAL_SERVER_ERROR).send(util.fail(statusCode.INTERNAL_SERVER_ERROR, responseMessage.INTERNAL_SERVER_ERROR));
} finally {
}
};
2 changes: 1 addition & 1 deletion functions/api/routes/notice/service/serviceGET.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ module.exports = async (req, res) => {
return notice;
});

res.status(statusCode.OK).send(util.success(statusCode.OK, responseMessage.SERVICE_GET_SUCCESS, { notices }));
res.status(statusCode.OK).send(util.success(statusCode.OK, responseMessage.GET_SERVICE_SUCCESS, { notices }));
} catch (error) {
functions.logger.error(`[ERROR] [${req.method.toUpperCase()}] ${req.originalUrl}`, `[CONTENT] ${error}`);
console.log(error);
Expand Down
9 changes: 6 additions & 3 deletions functions/constants/responseMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,15 @@ module.exports = {
// Notice
SERVICE_READ_SUCCESS: '서비스 알림 읽음처리 완료',
ACTIVE_READ_SUCCESS: '활동 알림 읽음처리 완료',
SERVICE_GET_SUCCESS: '서비스 알림 조회 완료',
ACTIVE_GET_SUCCESS: '활동 알림 조회 완료',
GET_SERVICE_SUCCESS: '서비스 알림 조회 완료',
GET_ACTIVE_SUCCESS: '활동 알림 조회 완료',
NOTICE_DELETE_SUCCESS: '알림 삭제 완료',
NOTICE_ID_NOT_VALID: '유효하지 않은 알림 ID 입니다',
PUSH_SEND_SUCCESS: '푸시알림 전송 완료',
NEW_NOTICE_GET_SUCCESS: '새로운 알림 조회 완료',
GET_NEW_NOTICE_SUCCESS: '새로운 알림 조회 완료',
GET_NOTICE_SETTING_SUCCESS: '푸시알림 설정 조회 완료',
PUSH_CATEGORY_INVALID: '유효하지 않은 category입니다',
PUSH_TOGGLE_SUCCESS: '푸시알림 설정 변경 완료',

// Status
INVALID_USER_STATUS: '유효하지 않은 status type입니다',
Expand Down
16 changes: 16 additions & 0 deletions functions/db/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,21 @@ const updateProfileById = async (client, userId, nickname, profileImg) => {
return convertSnakeToCamel.keysToCamel(rows[0]);
};

const togglePushSettingById = async (client, userId, category) => {
const categoryColumn = 'push_' + convertSnakeToCamel.keysToSnake([category])[0];
const now = dayjs().add(9, 'hour');
const { rows } = await client.query(
`
UPDATE spark.user
SET ${categoryColumn} = NOT ${categoryColumn}, updated_at = $2
WHERE user_id = $1
RETURNING *
`,
[userId, now],
);
return convertSnakeToCamel.keysToCamel(rows[0]);
};

module.exports = {
getAllUsers,
getUserById,
Expand All @@ -97,4 +112,5 @@ module.exports = {
addUser,
updateDeviceTokenById,
updateProfileById,
togglePushSettingById,
};