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

[GET] /notice/service?lastid=&size= 서비스 알림 조회 #22

Merged
merged 8 commits into from
Jan 12, 2022
34 changes: 34 additions & 0 deletions functions/api/routes/notice/active/activeReadPATCH.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/active/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.activeReadByUserId(client, userId);

res.status(statusCode.OK).send(util.success(statusCode.OK, responseMessage.ACTIVE_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();
}
};
7 changes: 7 additions & 0 deletions functions/api/routes/notice/active/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('./activeReadPATCH'));

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

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

module.exports = router;
44 changes: 44 additions & 0 deletions functions/api/routes/notice/service/serviceGET.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
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/service?lastid=&size=
* @error
*/

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

let client;

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

const services = await noticeDB.getServicesByUserId(client, userId, lastid, size);

const notices = services.map((s) => {
const notice = {};
notice['noticeId'] = s.notificationId;
notice['noticeTitle'] = s.title;
notice['noticeImg'] = s.thumbnail;
notice['noticeContent'] = s.content;
notice['createdAt'] = s.createdAt.toISOString().split('T')[0].replace(/-/g, '/');
return notice;
});

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

// Notice
SERVICE_READ_SUCCESS: '서비스 알림 읽음처리 완료',
ACTIVE_READ_SUCCESS: '활동 알림 읽음처리 완료',
SERVICE_GET_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'),
};
53 changes: 53 additions & 0 deletions functions/db/notice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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()
WHERE is_read = FALSE
AND is_deleted = FALSE
AND is_service = TRUE
AND receiver_id = $1
RETURNING *
`,
[userId],
);
return convertSnakeToCamel.keysToCamel(rows);
};

const activeReadByUserId = async (client, userId) => {
const { rows } = await client.query(
`
UPDATE spark.notification
SET is_read = TRUE, read_at = now(), updated_at = now()
WHERE is_read = FALSE
AND is_deleted = FALSE
AND is_service = FALSE
AND receiver_id = $1
RETURNING *
`,
[userId],
);
return convertSnakeToCamel.keysToCamel(rows);
};

const getServicesByUserId = async (client, userId, lastid, size) => {
const { rows } = await client.query(
`
SELECT * FROM spark.notification
WHERE receiver_id = $1
AND is_deleted = FALSE
AND is_service = TRUE
AND notification_id < $2
AND created_at > current_timestamp + '-7 days'
ORDER BY notification_id DESC
LIMIT $3
`,
[userId, lastid, size],
);
return convertSnakeToCamel.keysToCamel(rows);
};

module.exports = { serviceReadByUserId, activeReadByUserId, getServicesByUserId };