Skip to content

Commit

Permalink
Merge branch 'develop' into feature/#25
Browse files Browse the repository at this point in the history
  • Loading branch information
youngkwon02 authored Jan 12, 2022
2 parents 25386ea + 4f5451a commit 2f03197
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 1 deletion.
44 changes: 44 additions & 0 deletions functions/api/routes/notice/active/activeGET.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/active?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 actives = await noticeDB.getActivesByUserId(client, userId, lastid, size);

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

res.status(statusCode.OK).send(util.success(statusCode.OK, responseMessage.ACTIVE_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();
}
};
1 change: 1 addition & 0 deletions functions/api/routes/notice/active/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const express = require('express');
const router = express.Router();
const { checkUser } = require('../../../../middlewares/auth');

router.get('', checkUser, require('./activeGET'));
router.patch('/read', checkUser, require('./activeReadPATCH'));

module.exports = router;
1 change: 1 addition & 0 deletions functions/api/routes/notice/service/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ 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();
}
};
2 changes: 2 additions & 0 deletions functions/constants/responseMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ module.exports = {
// Notice
SERVICE_READ_SUCCESS: '서비스 알림 읽음처리 완료',
ACTIVE_READ_SUCCESS: '활동 알림 읽음처리 완료',
SERVICE_GET_SUCCESS: '서비스 알림 조회 완료',
ACTIVE_GET_SUCCESS: '활동 알림 조회 완료',
NOTICE_DELETE_SUCCESS: '알림 삭제 완료',
NOTICE_ID_NOT_VALID: '유효하지 않은 알림 ID 입니다',
};
36 changes: 35 additions & 1 deletion functions/db/notice.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,38 @@ const deleteNoticeByNoticeId = async (client, noticeId) => {
return convertSnakeToCamel.keysToCamel(rows[0]);
};

module.exports = { serviceReadByUserId, activeReadByUserId, getNoticeByNoticeId, deleteNoticeByNoticeId };
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);
};

const getActivesByUserId = 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 = FALSE
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, getNoticeByNoticeId, deleteNoticeByNoticeId, getServicesByUserId, getActivesByUserId };

0 comments on commit 2f03197

Please sign in to comment.