forked from Cheese-Guardians-Opensource/cheese
-
Notifications
You must be signed in to change notification settings - Fork 2
/
calendarController.js
242 lines (219 loc) · 8.08 KB
/
calendarController.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
const calendarService = require('../services/calendarService');
const path = require('path');
const calendarDate = require('../public/js/calendar.js');
const jwt = require('jsonwebtoken');
require('dotenv').config();
const querystring = require('querystring');
const baseResponse = require("../config/baseResponseStatus");
//캘린더 조회
exports.getCalendar = async function (req, res) {
const jwtSecret = process.env.jwtsecret;
console.log(jwtSecret)
const token = req.cookies.x_auth;
if (token) {
const decodedToken = jwt.verify(token, process.env.jwtsecret);
const user_id = decodedToken.user_id; // user_id를 추출
let date = req.query.selectedYear + req.query.selectedMonth + req.query.selectedDate;
if (!req.query.selectedYear || !req.query.selectedMonth || !req.query.selectedDate) {
const today = new Date();
const selectedYear = String(today.getFullYear()).padStart(4, '0');
const selectedMonth = String(today.getMonth() + 1).padStart(2, '0');
const selectedDate = String(today.getDate()).padStart(2, '0');
const existingQueryString = req.query;
if (Object.keys(existingQueryString).length === 0) {
const newURL = `${req.protocol}://${req.get('host')}${req.baseUrl}?selectedYear=${selectedYear}&selectedMonth=${selectedMonth}&selectedDate=${selectedDate}`;
return res.redirect(newURL);
}
}
// validation
if(!user_id) {
return res.send(baseResponse.USER_USERIDX_EMPTY);
}
if (user_id <= 0) {
return res.send(baseResponse.USER_USERIDX_LENGTH);
}
const calendarResult = await calendarService.retrieveCalendar(user_id, date);
const calendarDataResult = await calendarService.retrieveSelectedCalendar(user_id, date);
const MindDiaryResult = await calendarService.retrieveCalendar(user_id, date);
const MindDiaryDataResult = await calendarService.retrieveSelectedMindDiary(user_id, date);
console.log(MindDiaryDataResult)
if (calendarResult.length>0){
console.log("경우2")
return res.render('calendar/calendar.ejs', { calendarResult: calendarResult, calendarDataResult: calendarDataResult, MindDiaryResult:MindDiaryResult, MindDiaryDataResult:MindDiaryDataResult });
}
else {
console.log("경우4")
return res.render('calendar/calendar.ejs', { calendarResult: null, calendarDataResult: calendarDataResult, MindDiaryResult:MindDiaryResult, MindDiaryDataResult:MindDiaryDataResult });
}
} else {
return res.redirect('/');
}
}
exports.postCalendar = async function (req, res) {
const token = req.cookies.x_auth;
if (token) {
const decodedToken = jwt.verify(token, process.env.jwtsecret);
const user_id = decodedToken.user_id; // user_id를 추출
console.log(req.body);
const date = req.query.selectedYear + req.query.selectedMonth + req.query.selectedDate;
console.log(date);
//validation
if(!user_id) {
return res.send(errResponse(baseResponse.USER_USERIDX_EMPTY));
}
if (user_id <= 0) {
return res.send(errResponse(baseResponse.USER_USERIDX_LENGTH));
}
const {
hospital_name,
hospital_schedule,
check_content,
sleep_time,
//symptom_text,
//symptom_time,
symptom_range,
diary_text,
is_check
} = req.body;
const createCalResponse = await calendarService.createCalendar(
user_id,
date,
//hospital_name,
//hospital_schedule,
check_content,
sleep_time,
symptom_range,
diary_text,
is_check
);
console.log(diary_text);
if (createCalResponse == "성공") {
const queryString = querystring.stringify(req.query);
return res.status(200).send(`
<script>
if (confirm('캘린더 등록에 성공했습니다.')) {
window.location.href = "/calendar?${queryString}";
}
</script>
`);
} else {
const queryString = querystring.stringify(req.query);
return res.send(`
<script>
if (confirm('캘린더 등록에 실패했습니다.')) {
window.location.href = "/calendar?${queryString}";
}
</script>
`);
}
}
else {
return res.redirect('/');
}
};
exports.postFile = async function (req, res) {
const token = req.cookies.x_auth;
if (token) {
const decodedToken = jwt.verify(token, process.env.jwtsecret);
const user_id = decodedToken.user_id; // user_id를 추출
const date = req.body.fileDate;
console.log("date: "+req.body.fileDate);
const server_name = path.basename(req.file.filename, path.extname(req.file.originalname)); //서버증상
const user_name = path.basename(req.file.originalname, path.extname(req.file.originalname));
const extension = path.extname(req.file.filename);
console.log(server_name + user_name + extension);
var attachFileResponse;
//const queryString = querystring.stringify(req.query);
const selectedYear = String(date).slice(0, 4); // 처음 4글자는 년도
const selectedMonth = String(date).slice(4, 6); // 다음 2글자는 월
const selectedDate = String(date).slice(6, 9); // 다음 2글자는 일
const newURL = `${req.protocol}://${req.get('host')}${req.baseUrl}?selectedYear=${selectedYear}&selectedMonth=${selectedMonth}&selectedDate=${selectedDate}`;
// 사진 확장자인 경우에만 처리
if (['.png', '.jpg', '.jpeg', '.tiff', '.tif','.gif', '.webp', '.heif', '.heic'].includes(extension.toLowerCase())) {
attachFileResponse = await calendarService.createFileMem(
user_id,
date,
server_name,
user_name,
extension
);
}
else{
return res.send(`
<script>
if (confirm('png, jpg, jpeg, tif, tiff, gif, webp, heif, heic의 사진 확장자인 파일만 업로드할 수 있습니다. 확인을 누르면 메인 페이지로 돌아갑니다.')) {
window.location.href = "/calendar";
}
</script>
`);
}
if (!req.file) {
return res.send(`
<script>
if (confirm('파일이 없습니다. 확인을 누르면 메인 페이지로 돌아갑니다.')) {
window.location.href = "/calendar";
}
</script>
`);
}
// Code for handling file upload and database query goes here
if (attachFileResponse == "성공") {
return res.redirect(newURL);
}
else res.send(attachFileResponse);
//return res.redirect('/calendar');
//res.send(attachFileResponse);
}
else {
return res.redirect('/');
}
};
exports.postMindDiary = async function (req, res) {
const token = req.cookies.x_auth;
if (token) {
const decodedToken = jwt.verify(token, process.env.jwtsecret);
const user_id = decodedToken.user_id; // user_id를 추출
const date = req.query.selectedYear + req.query.selectedMonth + req.query.selectedDate; //쿼리스트링에서 날짜 추출
//const date = '2023-09-20'
console.log(req.query);
const {
keyword,
matter,
change,
solution,
compliment
} = req.body;
console.log(req.body)
const createMindDiaryResponse = await calendarService.createMindDiary(
user_id,
date,
keyword,
matter,
change,
solution,
compliment
);
if (createMindDiaryResponse == "성공") {
const queryString = querystring.stringify(req.query);
return res.status(200).send(`
<script>
if (confirm('마음 일기 등록에 성공했습니다.')) {
window.location.href = "/calendar?${queryString}";
}
</script>
`);
} else {
const queryString = querystring.stringify(req.query);
return res.send(`
<script>
if (confirm('마음 일기 등록에 실패했습니다.')) {
window.location.href = "/calendar?${queryString}";
}
</script>
`);
}
}
else {
return res.redirect('/');
}
}