From b268e3ce96b1afd53df6d2ec60c1bddb7cc81c91 Mon Sep 17 00:00:00 2001 From: hyejungg Date: Sun, 17 Dec 2023 00:02:35 +0900 Subject: [PATCH] =?UTF-8?q?[add]=20=EC=95=84=EC=9D=B4=ED=85=9C=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80/=EC=88=98=EC=A0=95=20=EC=8B=9C=20=EC=95=8C=EB=A6=BC?= =?UTF-8?q?=20=EB=82=A0=EC=A7=9C=20validate=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/itemController.js | 58 +++++++++++++++++-------------- src/utils/response.js | 3 +- src/utils/util.js | 24 ++++++++++++- 3 files changed, 57 insertions(+), 28 deletions(-) diff --git a/src/controllers/itemController.js b/src/controllers/itemController.js index 8ac4b34..d431319 100644 --- a/src/controllers/itemController.js +++ b/src/controllers/itemController.js @@ -8,6 +8,7 @@ const { ErrorMessage, } = require('../utils/response'); const { Strings } = require('../utils/strings'); +const { isValidDateFormat } = require('../utils/util'); const existEmptyData = (obj) => { if (obj.constructor !== Object) { @@ -25,7 +26,6 @@ module.exports = { if (!req.body.item_name) { throw new BadRequest(ErrorMessage.itemNameMiss); } - await Items.insertItem(req).then((itemId) => { if ( req.body.item_notification_date && @@ -33,19 +33,22 @@ module.exports = { ) { // TODO request DTO 분리하기 const itemNotiDate = req.body.item_notification_date; + const date = itemNotiDate.slice(0, 10); const minute = Number( itemNotiDate.slice(-5, itemNotiDate.length - 3), ); - if (minute === 0 || minute === 30) { - Noti.insertNoti(req, itemId).then(() => { - return res.status(StatusCode.CREATED).json({ - success: true, - message: SuccessMessage.itemAndNotiInsert, - }); - }); - } else { + if (!isValidDateFormat(date)) { throw new BadRequest(ErrorMessage.notiDateBadRequest); } + if (!(minute === 0 || minute === 30)) { + throw new BadRequest(ErrorMessage.notiDateMinuteBadRequest); + } + Noti.insertNoti(req, itemId).then(() => { + return res.status(StatusCode.CREATED).json({ + success: true, + message: SuccessMessage.itemAndNotiInsert, + }); + }); } else { return res.status(StatusCode.CREATED).json({ success: true, @@ -97,29 +100,32 @@ module.exports = { ) { // TODO request DTO 분리하기 const itemNotiDate = req.body.item_notification_date; + const date = itemNotiDate.slice(0, 10); const minute = Number( itemNotiDate.slice(-5, itemNotiDate.length - 3), ); - if (minute === 0 || minute === 30) { - //* item 수정 후 item_noti_~에 따라 알림여부를 noti에 수정/추가 - Noti.upsertNoti(req).then((state) => { - if (state === Strings.INSERT) { - return res.status(StatusCode.CREATED).json({ - success: true, - message: SuccessMessage.itemUpdateAndNotiInsert, - }); - } - if (state === Strings.UPSERT) { - return res.status(StatusCode.OK).json({ - success: true, - message: SuccessMessage.itemAndNotiUpdate, - }); - } - }); - } else { + if (!isValidDateFormat(date)) { throw new BadRequest(ErrorMessage.notiDateBadRequest); } + if (!(minute === 0 || minute === 30)) { + throw new BadRequest(ErrorMessage.notiDateMinuteBadRequest); + } + Noti.upsertNoti(req).then((state) => { + if (state === Strings.INSERT) { + return res.status(StatusCode.CREATED).json({ + success: true, + message: SuccessMessage.itemUpdateAndNotiInsert, + }); + } + + if (state === Strings.UPSERT) { + return res.status(StatusCode.OK).json({ + success: true, + message: SuccessMessage.itemAndNotiUpdate, + }); + } + }); } else { //* item 수정 후 item_noti_~가 null인 경우, noti에 존재하면 삭제 Noti.deleteNoti(req).then((result) => { diff --git a/src/utils/response.js b/src/utils/response.js index 101344a..26b7074 100644 --- a/src/utils/response.js +++ b/src/utils/response.js @@ -93,7 +93,8 @@ const ErrorMessage = { notiReadStateUpdate: '수정된 알림 읽음 상태 없음', notiInsert: '추가된 알림 없음', notiUpsert: '추가되거나 수정된 알림 없음', - notiDateBadRequest: '알림 날짜의 분은 00 또는 30', + notiDateMinuteBadRequest: '알림 날짜의 분은 00 또는 30', + notiDateBadRequest: '알림 날짜는 현재 날짜보다 같거나 미래만 가능', /* 사용자*/ validateNickname: '이미 존재하는 닉네임', diff --git a/src/utils/util.js b/src/utils/util.js index d8b2f50..640206d 100644 --- a/src/utils/util.js +++ b/src/utils/util.js @@ -4,4 +4,26 @@ const trimToString = (str) => { } }; -module.exports = { trimToString }; +const isValidDateFormat = (str) => { + const datePattern = /^\d{4}-\d{2}-\d{2}$/; + if (datePattern.test(str)) { + const currentDate = new Date(); + const currentYear = currentDate.getFullYear(); + const currentMonth = currentDate.getMonth() + 1; // 월은 0부터 시작 + const currentDay = currentDate.getDate(); + + const inputDate = new Date(str); + const inputYear = inputDate.getFullYear(); + const inputMonth = inputDate.getMonth() + 1; // 월은 0부터 시작 + const inputDay = inputDate.getDate(); + + return ( + inputYear >= currentYear && + inputMonth >= currentMonth && + inputDay >= currentDay + ); + } + return false; +}; + +module.exports = { trimToString, isValidDateFormat };