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

[PATCH] /notice/service/read 서비스 알림 읽음 처리 #18

Merged
merged 3 commits into from
Jan 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions functions/api/routes/notice/active/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const express = require('express');
const router = express.Router();

module.exports = router;
3 changes: 2 additions & 1 deletion functions/api/routes/notice/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const express = require('express');
const router = express.Router();

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

module.exports = router;
7 changes: 7 additions & 0 deletions functions/api/routes/notice/service/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const express = require('express');
const router = express.Router();
const { checkUser } = require('../../../../middlewares/auth');

router.patch('/read', checkUser, require('./serviceReadPATCH'));

module.exports = router;
34 changes: 34 additions & 0 deletions functions/api/routes/notice/service/serviceReadPATCH.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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 PATCH /notice/service/read
* @body
* @error
*/

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

let client;

try {
client = await db.connect(req);
await noticeDB.serviceReadByUserId(client, userId);

res.status(statusCode.OK).send(util.success(statusCode.OK, responseMessage.SERVICE_READ_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();
}
};
3 changes: 3 additions & 0 deletions functions/constants/responseMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,7 @@ module.exports = {
GET_WAITROOM_DATA_ALREADY: '이미 사용자가 참가중인 방입니다',
GET_WAITROOM_DATA_FAIL: '대기방 정보 확인 실패',
GET_WAITROOM_DATA_KICKED: '습관 방 생성자에 의해 내보내진 방입니다',

// Notice
SERVICE_READ_SUCCESS: '서비스 알림 읽음처리 완료',
};
1 change: 1 addition & 0 deletions functions/db/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = {
userDB: require('./user'),
roomDB: require('./room'),
noticeDB: require('./notice'),
};
20 changes: 20 additions & 0 deletions functions/db/notice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const _ = require('lodash');
const convertSnakeToCamel = require('../lib/convertSnakeToCamel');

const serviceReadByUserId = async (client, userId) => {
const { rows } = await client.query(
`
UPDATE spark.notification
SET is_read = TRUE, read_at = now(), updated_at = now()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이거 dayjs 라이브러리 써서 timezone 한국으로 설정해주면 좋을 것 같아요!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xxeol2
아 근데 이거는 DB Timezone을 한국으로 맞춰주는게 좋을거라구 생각했어요!!
매번 코드에서 한국 시간대로 변경하는 것도 비용 낭비인거 같구, 누락되는 부분도 생길 수 있을거같아서~!!
혹시 어떻게 생각하세요~??

WHERE is_read = FALSE
AND is_deleted = FALSE
AND is_service = TRUE
AND receiver_id = $1
RETURNING *
`,
[userId],
);
return convertSnakeToCamel.keysToCamel(rows);
};

module.exports = { serviceReadByUserId };