Skip to content

Commit

Permalink
[add] 아이템 추가/수정 시 알림 날짜 validate 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
hyejungg committed Dec 16, 2023
1 parent 7d554d1 commit b268e3c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 28 deletions.
58 changes: 32 additions & 26 deletions src/controllers/itemController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -25,27 +26,29 @@ module.exports = {
if (!req.body.item_name) {
throw new BadRequest(ErrorMessage.itemNameMiss);
}

await Items.insertItem(req).then((itemId) => {
if (
req.body.item_notification_date &&
req.body.item_notification_type
) {
// 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,
Expand Down Expand Up @@ -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) => {
Expand Down
3 changes: 2 additions & 1 deletion src/utils/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ const ErrorMessage = {
notiReadStateUpdate: '수정된 알림 읽음 상태 없음',
notiInsert: '추가된 알림 없음',
notiUpsert: '추가되거나 수정된 알림 없음',
notiDateBadRequest: '알림 날짜의 분은 00 또는 30',
notiDateMinuteBadRequest: '알림 날짜의 분은 00 또는 30',
notiDateBadRequest: '알림 날짜는 현재 날짜보다 같거나 미래만 가능',

/* 사용자*/
validateNickname: '이미 존재하는 닉네임',
Expand Down
24 changes: 23 additions & 1 deletion src/utils/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };

0 comments on commit b268e3c

Please sign in to comment.