Blame view

app/controllers/home.controller.js 6.42 KB
fbe7f7a7   Apichat.Tum   add api google
1
2
'use strict'

1244ae71   Apichat.Tum   new response
3
4
5
6
const console = process.console
const yamlConfig = require('node-yaml-config')
const path = require('path')
const async = require('async')
f6a4415a   Apichat.Tum   start smart-rms-c...
7
const config = yamlConfig.load(path.join(__dirname, '/../../config/config.yml'))
1244ae71   Apichat.Tum   new response
8

f6a4415a   Apichat.Tum   start smart-rms-c...
9
10
11
12
13
const lib = require('../lib')

// lib.authorize()
// lib.listEvents()
// lib.createEvent()
1244ae71   Apichat.Tum   new response
14
15

const url = config.server.url
f3f14fef   TUM.Apichat   add value config ...
16
const port = config.server.port
cf86e9a3   Apichat.Tum   - ui with oauth2 ...
17

1244ae71   Apichat.Tum   new response
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
function responseJSON(code, description, data, status) {
    return {
        "code": code,
        "description": description,
        "result": {
            "data": data,
            "status": status
        }
    }
}

function strToTime(date) {
    var strDate = new Date(date)
    return strDate.getTime()
}

function ggToKendo(data) {
    var dataToArr = []
    try {
        for (var n = 0; n < data.items.length; n++) {
            dataToArr.push({
                "TaskID": data.items[n].id,
                "EventTypeID": checkObjEmpty(data.items[n].extendedProperties, 'private'),
                "Title": data.items[n].summary,
                "Description": data.items[n].description,
                "StartTimezone": "Asia/Bangkok",
                "Start": new Date(data.items[n].start.dateTime),
77034810   Apichat.Tum   fix REST API
45
46
47
                "End": new Date(data.items[n].end.dateTime),
                "EndTimezone": "Asia/Bangkok",
                "RecurrenceRule": null,
cf86e9a3   Apichat.Tum   - ui with oauth2 ...
48
49
50
51
52
53
54
55
56
57
                "RecurrenceID": null,
                "RecurrenceException": null,
                "IsAllDay": false
            })
        }
    } catch (error) {
        console.error(error.message)
    } finally {
        return dataToArr
    }
77034810   Apichat.Tum   fix REST API
58
}
1244ae71   Apichat.Tum   new response
59
60

function home(req, res) {
cf86e9a3   Apichat.Tum   - ui with oauth2 ...
61
62
63
64
65
66
67
68
69
70
71
72
73
    res.render('index')
}

function index(req, res) {
    console.log(req.body)
    lib.getNewToken((err, authUrl) => {
        if (err) {
            console.error(err)
        } else {
            console.info(authUrl)
            res.send(authUrl)
        }
    })
1244ae71   Apichat.Tum   new response
74
}
cf86e9a3   Apichat.Tum   - ui with oauth2 ...
75
76
77
78
79
80
81
82

function setToken(req, res) {
    console.log(req.body)
    var code = req.body.code
    lib.setNewToken(code, (err, token) => {
        if (err) {
            console.error(err)
            res.send(err)
f6a4415a   Apichat.Tum   start smart-rms-c...
83
84
            res.end()
        } else {
1244ae71   Apichat.Tum   new response
85
86
87
            console.info(token)
            res.send(token)
            res.end()
cf86e9a3   Apichat.Tum   - ui with oauth2 ...
88
89
        }
    })
1244ae71   Apichat.Tum   new response
90
}
cf86e9a3   Apichat.Tum   - ui with oauth2 ...
91

1244ae71   Apichat.Tum   new response
92
function events(req, res) {
cf86e9a3   Apichat.Tum   - ui with oauth2 ...
93
    // console.log(req.body)
fbe7f7a7   Apichat.Tum   add api google
94
    lib.authorize((err, auth, authUrl) => {
cf86e9a3   Apichat.Tum   - ui with oauth2 ...
95
96
97
        if (err) {
            console.error(err)
            res.send(err)
1244ae71   Apichat.Tum   new response
98
99
100
        } else {
            lib.listEvents(auth, (err, response) => {
                if (err) {
cf86e9a3   Apichat.Tum   - ui with oauth2 ...
101
102
103
                    if (authUrl) {
                        console.info(authUrl)
                        res.json(responseJSON(res.code, "redirect to get auth code", authUrl, "redirect"))
1244ae71   Apichat.Tum   new response
104
                        res.end()
cf86e9a3   Apichat.Tum   - ui with oauth2 ...
105
                    } else {
1244ae71   Apichat.Tum   new response
106
                        console.error(err)
f6a4415a   Apichat.Tum   start smart-rms-c...
107
108
109
110
111
                        res.json(responseJSON(res.code, "response error", err.message, "failed"))
                        res.end()
                    }
                } else {
                    // console.info(response)
1244ae71   Apichat.Tum   new response
112
                    res.jsonp(ggToKendo(response))
fbe7f7a7   Apichat.Tum   add api google
113
                    res.end
cf86e9a3   Apichat.Tum   - ui with oauth2 ...
114
                }
f6a4415a   Apichat.Tum   start smart-rms-c...
115
            })
1244ae71   Apichat.Tum   new response
116
        }
cf86e9a3   Apichat.Tum   - ui with oauth2 ...
117
    })
fbe7f7a7   Apichat.Tum   add api google
118
119
120
121
122
123
124
}

function eventCreate(req, res) {
    console.log(req.body)
    let payload = req.body
    if (typeof payload.models == "string") {
        payload.models = JSON.parse(req.body.models)
cf86e9a3   Apichat.Tum   - ui with oauth2 ...
125
126
        payload = payload.models[0]
    }
77034810   Apichat.Tum   fix REST API
127

1244ae71   Apichat.Tum   new response
128
129
    // let summary = payload.summary
    // let description = payload.description
77034810   Apichat.Tum   fix REST API
130
    // let email = payload.email
1244ae71   Apichat.Tum   new response
131
    // let startDate = payload.startDate
77034810   Apichat.Tum   fix REST API
132
    // let startTimezone = payload.startTimezone
cf86e9a3   Apichat.Tum   - ui with oauth2 ...
133
    // let endDate = payload.endDate
fbe7f7a7   Apichat.Tum   add api google
134
135
    // let endTimezone = payload.endTimezone
    // // let extendedProperties = payload.extendedProperties
cf86e9a3   Apichat.Tum   - ui with oauth2 ...
136
137
    // payload.extendedProperties = {
    //     "private": {
f6a4415a   Apichat.Tum   start smart-rms-c...
138
    //         "eventTypeID": payload.EventTypeID
f6a4415a   Apichat.Tum   start smart-rms-c...
139
140
141
    //     }
    // }

77034810   Apichat.Tum   fix REST API
142
    lib.authorize((err, auth) => {
1244ae71   Apichat.Tum   new response
143
144
145
        let options = lib.eventBuilder(payload)
        if (err) {
            res.send(err)
f6a4415a   Apichat.Tum   start smart-rms-c...
146
        } else {
77034810   Apichat.Tum   fix REST API
147
148
149
150
            options.auth = auth
        }

        lib.createEvent(options, (err, result) => {
f6a4415a   Apichat.Tum   start smart-rms-c...
151
            if (err) {
f6a4415a   Apichat.Tum   start smart-rms-c...
152
                res.send(err)
a54f2679   DESKTOP-RBJDHSM\ADMIN   fix API
153
            } else {
f6a4415a   Apichat.Tum   start smart-rms-c...
154
                res.send(result)
1244ae71   Apichat.Tum   new response
155
                res.end()
a54f2679   DESKTOP-RBJDHSM\ADMIN   fix API
156
157
            }
        })
f6a4415a   Apichat.Tum   start smart-rms-c...
158
159
    })
}
a54f2679   DESKTOP-RBJDHSM\ADMIN   fix API
160

f6a4415a   Apichat.Tum   start smart-rms-c...
161
function oauth2callback(req, res) {
1244ae71   Apichat.Tum   new response
162
    var code = req.query.code
f6a4415a   Apichat.Tum   start smart-rms-c...
163
    lib.setNewToken(code, (err, token) => {
1244ae71   Apichat.Tum   new response
164
        if (err) {
f6a4415a   Apichat.Tum   start smart-rms-c...
165
166
167
168
169
            res.send(err)
            res.end()
        } else {
            res.redirect('/home')
        }
77034810   Apichat.Tum   fix REST API
170
    })
1244ae71   Apichat.Tum   new response
171
172
}

77034810   Apichat.Tum   fix REST API
173
174
175
function eventDelete(req, res) {
    console.log(req.body)
    let payload = req.body
1244ae71   Apichat.Tum   new response
176
    if (typeof payload.models == "string") {
77034810   Apichat.Tum   fix REST API
177
        payload.models = JSON.parse(req.body.models)
fbe7f7a7   Apichat.Tum   add api google
178
        payload = payload.models[0]
f3f14fef   TUM.Apichat   add value config ...
179
    }
77034810   Apichat.Tum   fix REST API
180
181
182
183
184
    lib.authorize((err, auth) => {
        let options = lib.deleteBuilder(payload)
        if (err) {
            console.error(err.message)
            res.send(err)
1244ae71   Apichat.Tum   new response
185
        } else {
21a1ff19   Apichat.Tum   fix follow smartr...
186
            console.info(auth)
1244ae71   Apichat.Tum   new response
187
            options.auth = auth
77034810   Apichat.Tum   fix REST API
188
189
190
191
192
193
194
195
        }

        lib.deleteEvent(options, (err, result) => {
            if (err) {
                console.error(err.message)
                res.send(err)
            } else {
                console.info(result)
1244ae71   Apichat.Tum   new response
196
                res.send(result)
77034810   Apichat.Tum   fix REST API
197
                res.end()
1244ae71   Apichat.Tum   new response
198
            }
77034810   Apichat.Tum   fix REST API
199
200
201
202
203
        })
    })
}

function eventUpdate(req, res) {
1244ae71   Apichat.Tum   new response
204
    console.log(req.body)
77034810   Apichat.Tum   fix REST API
205
    let payload = req.body
1244ae71   Apichat.Tum   new response
206
    if (typeof payload.models == "string") {
77034810   Apichat.Tum   fix REST API
207
208
209
210
211
212
        payload.models = JSON.parse(req.body.models)
        payload = payload.models[0]
    }
    lib.authorize((err, auth) => {
        let options = lib.updateBuilder(payload)
        if (err) {
1244ae71   Apichat.Tum   new response
213
214
215
            console.error(err.message)
            res.send(err)
        } else {
77034810   Apichat.Tum   fix REST API
216
217
218
219
220
221
222
223
            console.info(auth)
            options.auth = auth
        }

        lib.updateEvent(options, (err, result) => {
            if (err) {
                console.error(err)
                res.send(err)
1244ae71   Apichat.Tum   new response
224
            } else {
77034810   Apichat.Tum   fix REST API
225
                console.info(result)
1244ae71   Apichat.Tum   new response
226
                res.send(result)
77034810   Apichat.Tum   fix REST API
227
228
229
230
231
                res.end()
            }
        })
    })
}
1244ae71   Apichat.Tum   new response
232

77034810   Apichat.Tum   fix REST API
233
function checkObjEmpty(obj, parm) {
1244ae71   Apichat.Tum   new response
234
    if (obj) {
77034810   Apichat.Tum   fix REST API
235
236
237
238
239
        if (Object.getOwnPropertyNames(obj)) {
            return obj[parm].eventTypeID
        }
    } else {
        return
1244ae71   Apichat.Tum   new response
240
    }
7598c58f   Apichat.Tum   add eventTypeID /...
241
242

}
1244ae71   Apichat.Tum   new response
243
244
245
246
247

module.exports.index = index
module.exports.events = events
module.exports.eventCreate = eventCreate
module.exports.eventUpdate = eventUpdate
7598c58f   Apichat.Tum   add eventTypeID /...
248
249
250
251
module.exports.eventDelete = eventDelete
module.exports.home = home
module.exports.setToken = setToken
module.exports.oauth2callback = oauth2callback
77034810   Apichat.Tum   fix REST API

fbe7f7a7   Apichat.Tum   add api google

f3f14fef   TUM.Apichat   add value config ...

fbe7f7a7   Apichat.Tum   add api google