Skip to content

Commit

Permalink
Merge pull request #22 from TeamSparker/feature/#21
Browse files Browse the repository at this point in the history
[GET] /notice/service?lastid=&size= 서비스 알림 조회
  • Loading branch information
youngkwon02 authored Jan 12, 2022
2 parents 0b4ba83 + bb067c0 commit a2ffe6a
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 1 deletion.
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();
}
};
1 change: 1 addition & 0 deletions functions/constants/responseMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ module.exports = {
// Notice
SERVICE_READ_SUCCESS: '서비스 알림 읽음처리 완료',
ACTIVE_READ_SUCCESS: '활동 알림 읽음처리 완료',
SERVICE_GET_SUCCESS: '서비스 알림 조회 완료',
};
19 changes: 18 additions & 1 deletion functions/db/notice.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };

0 comments on commit a2ffe6a

Please sign in to comment.