schedule.controller.js 13.4 KB
Newer Older
Kim, Subin's avatar
Kim, Subin committed
1
2
import { KU, Schedule } from "../db/index.js";
import sequelize from 'sequelize';
3

Kim, Subin's avatar
Kim, Subin committed
4
5
6
7
8
const { Op } = sequelize

const findbyId = async (req, res, next) => {
    try {
        const id = req.scheduleId
Kim, Subin's avatar
context    
Kim, Subin committed
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
        const userId = req.userId
        let findSchedule = null
        let startDate = null
        let endDate = null
        if (id && userId) {
            if (userId === "ku") {
                findSchedule = await KU.findOne({ where: { id: id } })
                if (!findSchedule) throw new Error("해당 일정을 찾지 못했습니다.")
                else {
                    const { id, title, start, end, memo } = findSchedule
                    startDate = dateToString(start, "full")
                    endDate = dateToString(end, "full")
                    req.schedule = { id, title, startDate: startDate, endDate: endDate, memo }
                }
            } else {
                findSchedule = await Schedule.findOne({ where: { [Op.and]: [{ id: id }, { userId: userId }] } })
                if (!findSchedule) throw new Error("해당 일정을 찾지 못했습니다.")
                else {
Choi Ga Young's avatar
Choi Ga Young committed
27
                    const { id, title, start, end, allDay, location, memo, userId } = findSchedule
Kim, Subin's avatar
context    
Kim, Subin committed
28
29
30
31
                    startDate = dateToString(start, "full")
                    endDate = dateToString(end, "full")
                    const startTime = dateToString(start, "time")
                    const endTime = dateToString(end, "time")
Choi Ga Young's avatar
Choi Ga Young committed
32
                    req.schedule = { id, title, startDate, endDate, startTime, endTime, allDay: allDay ? "on" : "off", location, memo, userId }
Kim, Subin's avatar
context    
Kim, Subin committed
33
                }
Kim, Subin's avatar
Kim, Subin committed
34
35
36
37
38
39
40
41
42
            }
            next()
        } else next()
    } catch (error) {
        return res.status(500).send(error.message || "일정 가져오는 중 에러 발생")
    }
}

const findbyDate = async (req, res, next) => {
43
    try {
Kim, Subin's avatar
Kim, Subin committed
44
45
        if (req.date || req.month) {
            let date = ""
Kim, Subin's avatar
Kim, Subin committed
46
47
            let startDate = null
            let endDate = null
Kim, Subin's avatar
context    
Kim, Subin committed
48
            let findList = null
Kim, Subin's avatar
Kim, Subin committed
49
50
            let findKUList = null
            let findIndividualList = null
Kim, Subin's avatar
Kim, Subin committed
51
            if (req.date) {
Kim, Subin's avatar
Kim, Subin committed
52
                // 날짜 기준
Kim, Subin's avatar
Kim, Subin committed
53
                date = new Date(req.date)
Kim, Subin's avatar
Kim, Subin committed
54
55
56
57
                startDate = new Date(req.date)
                startDate.setHours(24, 0, 0, 0)
                endDate = new Date(req.date)
                endDate.setHours(0, 0, 0, 0)
Kim, Subin's avatar
context    
Kim, Subin committed
58
                if (req.userId === "ku") {
Kim, Subin's avatar
Kim, Subin committed
59
                    findKUList = await KU.findAll({
Kim, Subin's avatar
context    
Kim, Subin committed
60
61
62
63
64
65
66
67
68
69
                        where: {
                            [Op.and]: [
                                {
                                    start: {
                                        [Op.lte]: date
                                    }
                                }, {
                                    end: {
                                        [Op.gte]: date
                                    }
Kim, Subin's avatar
Kim, Subin committed
70
                                }
Kim, Subin's avatar
context    
Kim, Subin committed
71
72
73
                            ]
                        }, order: [['updatedAt', 'DESC']]
                    })
Kim, Subin's avatar
Kim, Subin committed
74
75
76
                    findKUList.forEach(schedule => {
                        schedule.dataValues.start = dateToString(schedule.dataValues.start, "twoYear")
                        schedule.dataValues.end = dateToString(schedule.dataValues.end, "twoYear")
Kim, Subin's avatar
context    
Kim, Subin committed
77
                    })
Kim, Subin's avatar
Kim, Subin committed
78
                    findList = findKUList
Kim, Subin's avatar
context    
Kim, Subin committed
79
                } else {
Kim, Subin's avatar
Kim, Subin committed
80
                    findIndividualList = await Schedule.findAll({
Kim, Subin's avatar
context    
Kim, Subin committed
81
82
83
84
                        where: {
                            [Op.and]: [
                                {
                                    start: {
Kim, Subin's avatar
Kim, Subin committed
85
                                        [Op.lte]: startDate
Kim, Subin's avatar
context    
Kim, Subin committed
86
87
88
                                    }
                                }, {
                                    end: {
Kim, Subin's avatar
Kim, Subin committed
89
                                        [Op.gte]: endDate
Kim, Subin's avatar
context    
Kim, Subin committed
90
                                    }
Choi Ga Young's avatar
Choi Ga Young committed
91
                                }, { userId: req.userId }
Kim, Subin's avatar
context    
Kim, Subin committed
92
93
94
                            ]
                        }, order: [['updatedAt', 'DESC']]
                    })
Kim, Subin's avatar
Kim, Subin committed
95
96
97
98
99
100
101
102
103
                    findIndividualList.forEach(schedule => {
                        schedule.dataValues.startDate = dateToString(schedule.dataValues.start, "twoYear")
                        schedule.dataValues.endDate = dateToString(schedule.dataValues.end, "twoYear")
                        if (!schedule.dataValues.allDay) {
                            schedule.dataValues.startTime = dateToString(schedule.dataValues.start, "time")
                            schedule.dataValues.endTime = dateToString(schedule.dataValues.end, "time")
                        }
                    })
                    findList = findIndividualList
Kim, Subin's avatar
context    
Kim, Subin committed
104
                }
Kim, Subin's avatar
Kim, Subin committed
105
            } else {
Kim, Subin's avatar
Kim, Subin committed
106
                // 달 기준
Kim, Subin's avatar
Kim, Subin committed
107
108
109
                date = new Date(req.month)
                const year = dateToString(date, "year")
                const month = dateToString(date, "month")
Kim, Subin's avatar
Kim, Subin committed
110
                findKUList = await KU.findAll({
Kim, Subin's avatar
Kim, Subin committed
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
                    where: {
                        [Op.or]: [
                            {
                                [Op.and]: [
                                    sequelize.where(sequelize.fn('date_part', 'year', sequelize.col('start')), year),
                                    sequelize.where(sequelize.fn('date_part', 'month', sequelize.col('start')), month)
                                ]
                            }, {
                                [Op.and]: [
                                    sequelize.where(sequelize.fn('date_part', 'year', sequelize.col('end')), year),
                                    sequelize.where(sequelize.fn('date_part', 'month', sequelize.col('end')), month)
                                ]
                            }
                        ]
                    }, attributes: ['id', 'title', 'start', 'end']
                    , order: [['start']]
                })
Kim, Subin's avatar
Kim, Subin committed
128
129
130
131
132
                findKUList.forEach(schedule => {
                    schedule.dataValues.end.setDate(schedule.dataValues.end.getDate() + 1)
                    schedule.dataValues.end = dateToString(schedule.dataValues.end, "full")
                    schedule.dataValues.start = dateToString(schedule.dataValues.start, "full")
                    schedule.dataValues.allDay = true
133
                    schedule.dataValues.className = ['text', 'first']
Kim, Subin's avatar
Kim, Subin committed
134
                })
Kim, Subin's avatar
context    
Kim, Subin committed
135
136
137
                if (req.userId === "ku") {
                    findList = findKUList
                } else {
Kim, Subin's avatar
Kim, Subin committed
138
                    findIndividualList = await Schedule.findAll({
Kim, Subin's avatar
context    
Kim, Subin committed
139
                        where: {
Choi Ga Young's avatar
Choi Ga Young committed
140
141
                            [Op.and]: [
                                { userId: req.userId },
Kim, Subin's avatar
context    
Kim, Subin committed
142
                                {
Choi Ga Young's avatar
Choi Ga Young committed
143
144
145
146
147
148
149
150
151
152
153
154
                                    [Op.or]: [
                                        {
                                            [Op.and]: [
                                                sequelize.where(sequelize.fn('date_part', 'year', sequelize.col('start')), year),
                                                sequelize.where(sequelize.fn('date_part', 'month', sequelize.col('start')), month)
                                            ]
                                        }, {
                                            [Op.and]: [
                                                sequelize.where(sequelize.fn('date_part', 'year', sequelize.col('end')), year),
                                                sequelize.where(sequelize.fn('date_part', 'month', sequelize.col('end')), month)
                                            ]
                                        }
Kim, Subin's avatar
context    
Kim, Subin committed
155
156
157
                                    ]
                                }
                            ]
Choi Ga Young's avatar
Choi Ga Young committed
158
                        }, attributes: ['id', 'title', 'start', 'end', 'userId']
Kim, Subin's avatar
context    
Kim, Subin committed
159
160
                        , order: [['start']]
                    })
Kim, Subin's avatar
Kim, Subin committed
161
162
163
164
165
                    findIndividualList.forEach(schedule => {
                        schedule.dataValues.end.setDate(schedule.dataValues.end.getDate() + 1)
                        schedule.dataValues.end = dateToString(schedule.dataValues.end, "full")
                        schedule.dataValues.start = dateToString(schedule.dataValues.start, "full")
                        schedule.dataValues.allDay = true
Choi Ga Young's avatar
Choi Ga Young committed
166
                        schedule.dataValues.className = ['text', 'indi']
Kim, Subin's avatar
Kim, Subin committed
167
                    })
Kim, Subin's avatar
context    
Kim, Subin committed
168
169
                    findList = { KU: findKUList, individual: findIndividualList }
                }
Kim, Subin's avatar
Kim, Subin committed
170
171
172
173
174
175
176
177
            }
            req.scheduleList = findList
            next()
        } else next()
    } catch (error) {
        return res.status(500).send(error.message || "일정 가져오는 중 에러 발생")
    }
}
178

Kim, Subin's avatar
Kim, Subin committed
179
180
181
const create = async (req, res) => {
    try {
        let newSchedule = null
Kim, Subin's avatar
context    
Kim, Subin committed
182
183
        let start = null
        let end = null
Kim, Subin's avatar
Kim, Subin committed
184
        let allDay_v = false
Kim, Subin's avatar
Kim, Subin committed
185
186
187
        const userId = req.userId
        if (userId === "ku") {
            const { title, startDate, endDate, memo } = req.body
Kim, Subin's avatar
context    
Kim, Subin committed
188
189
            start = new Date(startDate)
            end = new Date(endDate)
Kim, Subin's avatar
Kim, Subin committed
190
191
192
            newSchedule = await KU.create({ title: title, start: start, end: end, memo: memo })
        } else {
            const { title, startDate, endDate, startTime, endTime, allDay, location, memo } = req.body
Kim, Subin's avatar
context    
Kim, Subin committed
193
194
195
            if (allDay === "on") {
                start = new Date(startDate)
                end = new Date(endDate)
Kim, Subin's avatar
Kim, Subin committed
196
                allDay_v = true
Kim, Subin's avatar
context    
Kim, Subin committed
197
198
199
200
            } else {
                start = new Date(startDate + " " + startTime)
                end = new Date(endDate + " " + endTime)
            }
Kim, Subin's avatar
Kim, Subin committed
201
            newSchedule = await Schedule.create({ title: title, start: start, end: end, allDay: allDay_v, location: location, memo: memo, userId: userId })
Kim, Subin's avatar
Kim, Subin committed
202
203
        }
        return res.json(newSchedule)
204
205
206
207
208
209
210
    } catch (error) {
        return res.status(500).send(error.message || "일정 등록 중 에러 발생")
    }
}

const edit = async (req, res) => {
    try {
Kim, Subin's avatar
Kim, Subin committed
211
        let updated = null
Kim, Subin's avatar
context    
Kim, Subin committed
212
213
        let start = null
        let end = null
Kim, Subin's avatar
Kim, Subin committed
214
        let allDay_v = false
Kim, Subin's avatar
Kim, Subin committed
215
216
217
218
        const userId = req.userId
        const { scheduleId } = req.query
        if (userId === "ku") {
            const { title, startDate, endDate, memo } = req.body
Kim, Subin's avatar
context    
Kim, Subin committed
219
220
221
            start = new Date(startDate)
            end = new Date(endDate)
            updated = await KU.update({ title: title, start: start, end: end, memo: memo }, { where: { id: scheduleId } })
Kim, Subin's avatar
Kim, Subin committed
222
223
        } else {
            const { title, startDate, endDate, startTime, endTime, allDay, location, memo } = req.body
Kim, Subin's avatar
context    
Kim, Subin committed
224
225
226
            if (allDay === "on") {
                start = new Date(startDate)
                end = new Date(endDate)
Kim, Subin's avatar
Kim, Subin committed
227
                allDay_v = true
Kim, Subin's avatar
context    
Kim, Subin committed
228
229
230
231
            } else {
                start = new Date(startDate + " " + startTime)
                end = new Date(endDate + " " + endTime)
            }
Kim, Subin's avatar
Kim, Subin committed
232
            updated = await Schedule.update({ title: title, start: start, end: end, allDay: allDay_v, location: location, memo: memo },
Kim, Subin's avatar
context    
Kim, Subin committed
233
                { where: { [Op.and]: [{ id: scheduleId }, { userId: userId }] } })
Kim, Subin's avatar
Kim, Subin committed
234
235
236
        }
        if (!updated) throw new Error("해당 일정의 일부 정보를 수정하는데 실패하였습니다.")
        else return res.send(200)
237
238
239
240
241
242
243
    } catch (error) {
        return res.status(500).send(error.message || "일정 수정 중 에러 발생")
    }
}

const remove = async (req, res) => {
    try {
Kim, Subin's avatar
Kim, Subin committed
244
245
246
247
        let deleted = null
        const userId = req.userId
        const { scheduleId } = req.query
        if (userId === "ku") deleted = await KU.destroy({ where: { id: scheduleId } })
Kim, Subin's avatar
context    
Kim, Subin committed
248
        else deleted = await Schedule.destroy({ where: { [Op.and]: [{ id: scheduleId }, { userId: userId }] } })
Kim, Subin's avatar
Kim, Subin committed
249
250
        if (!deleted) throw new Error("해당 일정을 삭제하는데 실패하였습니다.")
        else return res.send(200)
251
252
253
254
255
    } catch (error) {
        return res.status(500).send(error.message || "일정 삭제 중 에러 발생")
    }
}

Kim, Subin's avatar
Kim, Subin committed
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
const getParams = async (req, res, next) => {
    try {
        const { userId } = req.params
        req.userId = userId
        next()
    } catch (error) {
        return res.status(500).send(error.message || "일정 가져오는 중 에러 발생")
    }
}

const querySeparation = async (req, res, next) => {
    try {
        const { scheduleId, date, dateMonth } = req.query
        req.scheduleId = scheduleId
        req.date = date
        req.month = dateMonth
        next()
    } catch (error) {
        return res.status(500).send(error.message || "일정 가져오는 중 에러 발생")
    }
}

const send = async (req, res) => {
    try {
        const result = req.schedule || req.scheduleList
        return res.json(result)
    } catch (error) {
        return res.status(500).send(error.message || "일정 가져오는 중 에러 발생")
    }
}

Choi Ga Young's avatar
Choi Ga Young committed
287
export function dateToString(dateObj, method) {
Kim, Subin's avatar
Kim, Subin committed
288
289
290
291
    const year = dateObj.getFullYear()
    const year_disit = String(year).substring(2, 4)
    const month = dateObj.getMonth() + 1
    const date = dateObj.getDate()
Kim, Subin's avatar
context    
Kim, Subin committed
292
293
    const hour = dateObj.getHours()
    const minute = dateObj.getMinutes()
Kim, Subin's avatar
Kim, Subin committed
294

Kim, Subin's avatar
context    
Kim, Subin committed
295
296
297
298
    switch (method) {
        case "full":
            return [year, (month > 9 ? "" : "0") + month, (date > 9 ? "" : "0") + date].join("-")
        case "twoYear":
Kim, Subin's avatar
Kim, Subin committed
299
            return [year_disit, (month > 9 ? "" : "0") + month, (date > 9 ? "" : "0") + date].join(".")
Kim, Subin's avatar
context    
Kim, Subin committed
300
301
302
303
304
305
306
        case "time":
            return [(hour > 9 ? "" : "0") + hour, (minute > 9 ? "" : "0") + minute].join(":")
        case "year":
            return year
        case "month":
            return month
    }
Kim, Subin's avatar
Kim, Subin committed
307
308
}

309
export default {
Kim, Subin's avatar
Kim, Subin committed
310
311
    findbyId,
    findbyDate,
312
313
    create,
    edit,
Kim, Subin's avatar
Kim, Subin committed
314
315
316
317
    remove,
    getParams,
    querySeparation,
    send
318
}