Blame view

app/controllers/home.controller.js 6.03 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,
                "OwnerID": 2,
                "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

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

        lib.createEvent(options, (err, result) => {
f6a4415a   Apichat.Tum   start smart-rms-c...
146
            if (err) {
77034810   Apichat.Tum   fix REST API
147
148
149
150
                res.send(err)
            } else {
                res.send(result)
                res.end()
f6a4415a   Apichat.Tum   start smart-rms-c...
151
            }
f6a4415a   Apichat.Tum   start smart-rms-c...
152
        })
a54f2679   DESKTOP-RBJDHSM\ADMIN   fix API
153
    })
f6a4415a   Apichat.Tum   start smart-rms-c...
154
}
1244ae71   Apichat.Tum   new response
155

a54f2679   DESKTOP-RBJDHSM\ADMIN   fix API
156
157
function oauth2callback(req, res) {
    var code = req.query.code
f6a4415a   Apichat.Tum   start smart-rms-c...
158
159
    lib.setNewToken(code, (err, token) => {
        if (err) {
a54f2679   DESKTOP-RBJDHSM\ADMIN   fix API
160
            res.send(err)
f6a4415a   Apichat.Tum   start smart-rms-c...
161
            res.end()
1244ae71   Apichat.Tum   new response
162
        } else {
f6a4415a   Apichat.Tum   start smart-rms-c...
163
            res.redirect('/home')
1244ae71   Apichat.Tum   new response
164
        }
f6a4415a   Apichat.Tum   start smart-rms-c...
165
166
167
168
169
    })
}

function eventDelete(req, res) {
    console.log(req.body)
77034810   Apichat.Tum   fix REST API
170
    let payload = req.body
1244ae71   Apichat.Tum   new response
171
172
    if (typeof payload.models == "string") {
        payload.models = JSON.parse(req.body.models)
77034810   Apichat.Tum   fix REST API
173
174
175
        payload = payload.models[0]
    }
    lib.authorize((err, auth) => {
1244ae71   Apichat.Tum   new response
176
        let options = lib.deleteBuilder(payload)
77034810   Apichat.Tum   fix REST API
177
        if (err) {
fbe7f7a7   Apichat.Tum   add api google
178
            console.error(err.message)
f3f14fef   TUM.Apichat   add value config ...
179
            res.send(err)
77034810   Apichat.Tum   fix REST API
180
181
182
183
184
        } else {
            console.info(auth)
            options.auth = auth
        }

1244ae71   Apichat.Tum   new response
185
        lib.deleteEvent(options, (err, result) => {
21a1ff19   Apichat.Tum   fix follow smartr...
186
            if (err) {
1244ae71   Apichat.Tum   new response
187
                console.error(err.message)
77034810   Apichat.Tum   fix REST API
188
189
190
191
192
193
194
195
                res.send(err)
            } else {
                console.info(result)
                res.send(result)
                res.end()
            }
        })
    })
1244ae71   Apichat.Tum   new response
196
}
77034810   Apichat.Tum   fix REST API
197

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

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


module.exports.index = index
module.exports.events = events
module.exports.eventCreate = eventCreate
1244ae71   Apichat.Tum   new response
232
module.exports.eventUpdate = eventUpdate
77034810   Apichat.Tum   fix REST API
233
module.exports.eventDelete = eventDelete
1244ae71   Apichat.Tum   new response
234
module.exports.home = home
77034810   Apichat.Tum   fix REST API
235
236
module.exports.setToken = setToken
module.exports.oauth2callback = oauth2callback
1244ae71   Apichat.Tum   new response

7598c58f   Apichat.Tum   add eventTypeID /...

1244ae71   Apichat.Tum   new response

7598c58f   Apichat.Tum   add eventTypeID /...

77034810   Apichat.Tum   fix REST API

fbe7f7a7   Apichat.Tum   add api google

f3f14fef   TUM.Apichat   add value config ...

fbe7f7a7   Apichat.Tum   add api google