diff --git a/functions/api/routes/notice/service/index.js b/functions/api/routes/notice/service/index.js index a3f6ed7..af61f6a 100644 --- a/functions/api/routes/notice/service/index.js +++ b/functions/api/routes/notice/service/index.js @@ -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; diff --git a/functions/api/routes/notice/service/serviceGET.js b/functions/api/routes/notice/service/serviceGET.js new file mode 100644 index 0000000..a36c29a --- /dev/null +++ b/functions/api/routes/notice/service/serviceGET.js @@ -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(); + } +}; diff --git a/functions/constants/responseMessage.js b/functions/constants/responseMessage.js index 3a293c7..b153e4c 100644 --- a/functions/constants/responseMessage.js +++ b/functions/constants/responseMessage.js @@ -33,4 +33,5 @@ module.exports = { // Notice SERVICE_READ_SUCCESS: '서비스 알림 읽음처리 완료', ACTIVE_READ_SUCCESS: '활동 알림 읽음처리 완료', + SERVICE_GET_SUCCESS: '서비스 알림 조회 완료', }; diff --git a/functions/db/notice.js b/functions/db/notice.js index a66cb3c..1fc939f 100644 --- a/functions/db/notice.js +++ b/functions/db/notice.js @@ -33,4 +33,21 @@ const activeReadByUserId = async (client, userId) => { return convertSnakeToCamel.keysToCamel(rows); }; -module.exports = { serviceReadByUserId, activeReadByUserId }; +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 };