Blame view

app/controllers/home.controller.js 6.47 KB
fbe7f7a7   Apichat.Tum   add api google
1
2
"use strict"
var console = process.console
1244ae71   Apichat.Tum   new response
3
4
5
6
var yamlConfig = require('node-yaml-config')
var path = require('path')
var async = require('async')
var config = yamlConfig.load(path.join(__dirname, '/../../config/config.yml'))
f6a4415a   Apichat.Tum   start smart-rms-c...
7

1244ae71   Apichat.Tum   new response
8
var lib = require('../lib')
f6a4415a   Apichat.Tum   start smart-rms-c...
9
10
11
12
13

// lib.authorize()
// lib.listEvents()
// lib.createEvent()

1244ae71   Apichat.Tum   new response
14
15
var url = config.server.url
var port = config.server.port
f3f14fef   TUM.Apichat   add value config ...
16

cf86e9a3   Apichat.Tum   - ui with oauth2 ...
17
function rmsRes(res) {
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
    this._40401 = function (data) {
        res.json({
            resultCode: '40401',
            resultDescription: 'Data not found'
        }).end()
    }
    this._50000 = function (data) {
        console.tag('ERROR!').time().file().error(data)
        res.json({
            resultCode: '50000',
            resultDescription: 'System Error',
        }).end()
    }
    this._40301 = function (data) {
        res.json({
            resultCode: '40301',
            resultDescription: 'Missing or invalid parameter'
        }).end()
    }
    this._20000 = function (data) {
        console.tag().time().file().info(data)
        res.json({
            resultCode: '20000',
            resultDescription: 'Success',
            data: data
        }).end()
    }
77034810   Apichat.Tum   fix REST API
45
46
47
}

function strToTime(date) {
cf86e9a3   Apichat.Tum   - ui with oauth2 ...
48
49
50
51
52
53
54
55
56
57
    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,
77034810   Apichat.Tum   fix REST API
58
                "EventTypeID": checkObjEmpty(data.items[n].extendedProperties, 'private', 'EventTypeID'),
1244ae71   Apichat.Tum   new response
59
60
                "PropertyID": checkObjEmpty(data.items[n].extendedProperties, 'private', 'PropertyID'),
                "Title": data.items[n].summary,
cf86e9a3   Apichat.Tum   - ui with oauth2 ...
61
62
63
64
65
66
67
68
69
70
71
72
73
                "Description": data.items[n].description,
                "StartTimezone": "Asia/Bangkok",
                "Start": new Date(data.items[n].start.dateTime),
                "End": new Date(data.items[n].end.dateTime),
                "EndTimezone": "Asia/Bangkok",
                "RecurrenceRule": null,
                "RecurrenceID": null,
                "RecurrenceException": null,
                "IsAllDay": false
            })
        }
    } catch (error) {
        console.tag().time().file().error(error.message)
1244ae71   Apichat.Tum   new response
74
    } finally {
cf86e9a3   Apichat.Tum   - ui with oauth2 ...
75
76
77
78
79
80
81
82
        return dataToArr
    }
}

function home(req, res) {
    res.render('index')
}

f6a4415a   Apichat.Tum   start smart-rms-c...
83
84
function index(req, res) {
    var rms = new rmsRes(res)
1244ae71   Apichat.Tum   new response
85
86
87

    console.tag().time().file().log(req.body)
    lib.getNewToken((err, authUrl) => {
cf86e9a3   Apichat.Tum   - ui with oauth2 ...
88
89
        if (err) {
            rms._50000(err)
1244ae71   Apichat.Tum   new response
90
        } else {
cf86e9a3   Apichat.Tum   - ui with oauth2 ...
91
            rms._20000(authUrl)
1244ae71   Apichat.Tum   new response
92
        }
cf86e9a3   Apichat.Tum   - ui with oauth2 ...
93
    })
fbe7f7a7   Apichat.Tum   add api google
94
}
cf86e9a3   Apichat.Tum   - ui with oauth2 ...
95
96
97

function setToken(req, res) {
    var rms = new rmsRes(res)
1244ae71   Apichat.Tum   new response
98
99
100

    console.tag().time().file().log(req.body)
    var code = req.body.code
cf86e9a3   Apichat.Tum   - ui with oauth2 ...
101
102
103
    lib.setNewToken(code, (err, token) => {
        if (err) {
            rms._50000(err)
1244ae71   Apichat.Tum   new response
104
        } else {
cf86e9a3   Apichat.Tum   - ui with oauth2 ...
105
            rms._20000(token)
1244ae71   Apichat.Tum   new response
106
        }
f6a4415a   Apichat.Tum   start smart-rms-c...
107
108
109
110
111
    })
}

function events(req, res) {
    var rms = new rmsRes(res)
1244ae71   Apichat.Tum   new response
112

fbe7f7a7   Apichat.Tum   add api google
113
    lib.authorize((err, auth, authUrl) => {
cf86e9a3   Apichat.Tum   - ui with oauth2 ...
114
        if (err) {
f6a4415a   Apichat.Tum   start smart-rms-c...
115
            rms._50000(err)
1244ae71   Apichat.Tum   new response
116
        } else {
cf86e9a3   Apichat.Tum   - ui with oauth2 ...
117
            lib.listEvents(auth, (err, response) => {
fbe7f7a7   Apichat.Tum   add api google
118
119
120
121
122
123
124
                if (err) {
                    if (authUrl) {
                        console.tag().time().file().info(authUrl)
                        rms._20000(authUrl)
                    } else {
                        rms._50000(err)
                    }
cf86e9a3   Apichat.Tum   - ui with oauth2 ...
125
126
                } else {
                    // console.tag().time().file().info(response)
77034810   Apichat.Tum   fix REST API
127
                    res.jsonp(ggToKendo(response))
1244ae71   Apichat.Tum   new response
128
129
                    res.end()
                }
77034810   Apichat.Tum   fix REST API
130
            })
1244ae71   Apichat.Tum   new response
131
        }
77034810   Apichat.Tum   fix REST API
132
    })
cf86e9a3   Apichat.Tum   - ui with oauth2 ...
133
}
fbe7f7a7   Apichat.Tum   add api google
134
135

function eventCreate(req, res) {
cf86e9a3   Apichat.Tum   - ui with oauth2 ...
136
137
    var rms = new rmsRes(res)

f6a4415a   Apichat.Tum   start smart-rms-c...
138
    console.tag().time().file().log(req.body)
f6a4415a   Apichat.Tum   start smart-rms-c...
139
140
141
    let payload = req.body
    if (typeof payload.models == "string") {
        payload.models = JSON.parse(req.body.models)
77034810   Apichat.Tum   fix REST API
142
        payload = payload.models[0]
1244ae71   Apichat.Tum   new response
143
144
145
    }

    lib.authorize((err, auth) => {
f6a4415a   Apichat.Tum   start smart-rms-c...
146
        let options = lib.eventBuilder(payload)
77034810   Apichat.Tum   fix REST API
147
148
149
150
        if (err) {
            rms._50000(err)
        } else {
            options.auth = auth
f6a4415a   Apichat.Tum   start smart-rms-c...
151
        }
f6a4415a   Apichat.Tum   start smart-rms-c...
152

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

f6a4415a   Apichat.Tum   start smart-rms-c...
163
function oauth2callback(req, res) {
1244ae71   Apichat.Tum   new response
164
    var rms = new rmsRes(res)
f6a4415a   Apichat.Tum   start smart-rms-c...
165
166
167
168
169

    var code = req.query.code
    lib.setNewToken(code, (err, token) => {
        if (err) {
            rms._50000(err)
77034810   Apichat.Tum   fix REST API
170
        } else {
1244ae71   Apichat.Tum   new response
171
172
            res.redirect('http://localhost:3030/home')
        }
77034810   Apichat.Tum   fix REST API
173
174
175
    })
}

1244ae71   Apichat.Tum   new response
176
function eventDelete(req, res) {
77034810   Apichat.Tum   fix REST API
177
    var rms = new rmsRes(res)
fbe7f7a7   Apichat.Tum   add api google
178

f3f14fef   TUM.Apichat   add value config ...
179
    console.tag().time().file().log(req.body)
77034810   Apichat.Tum   fix REST API
180
181
182
183
184
    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
185
    lib.authorize((err, auth) => {
21a1ff19   Apichat.Tum   fix follow smartr...
186
        let options = lib.deleteBuilder(payload)
1244ae71   Apichat.Tum   new response
187
        if (err) {
77034810   Apichat.Tum   fix REST API
188
189
190
191
192
193
194
195
            rms._50000(err)
        } else {
            console.tag().time().file().info(auth)
            options.auth = auth
        }

        lib.deleteEvent(options, (err, result) => {
            if (err) {
1244ae71   Apichat.Tum   new response
196
                rms._50000(err)
77034810   Apichat.Tum   fix REST API
197
            } else {
1244ae71   Apichat.Tum   new response
198
                rms._20000(result)
77034810   Apichat.Tum   fix REST API
199
200
201
202
203
            }
        })
    })
}

1244ae71   Apichat.Tum   new response
204
function eventUpdate(req, res) {
77034810   Apichat.Tum   fix REST API
205
    var rms = new rmsRes(res)
1244ae71   Apichat.Tum   new response
206

77034810   Apichat.Tum   fix REST API
207
208
209
210
211
212
    console.tag().time().file().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
213
214
215
    lib.authorize((err, auth) => {
        let options = lib.updateBuilder(payload)
        if (err) {
77034810   Apichat.Tum   fix REST API
216
217
218
219
220
221
222
223
            rms._50000(err)
        } else {
            console.tag().time().file().info(auth)
            options.auth = auth
        }

        lib.updateEvent(options, (err, result) => {
            if (err) {
1244ae71   Apichat.Tum   new response
224
                rms._50000(err)
77034810   Apichat.Tum   fix REST API
225
            } else {
1244ae71   Apichat.Tum   new response
226
                rms._20000(result)
77034810   Apichat.Tum   fix REST API
227
228
229
230
231
            }
        })
    })
}

1244ae71   Apichat.Tum   new response
232
function checkObjEmpty(obj, parm, name) {
77034810   Apichat.Tum   fix REST API
233
    if (obj) {
1244ae71   Apichat.Tum   new response
234
        if (Object.getOwnPropertyNames(obj)) {
77034810   Apichat.Tum   fix REST API
235
236
237
238
239
            if (name == 'EventTypeID') {
                return obj[parm].eventTypeID
            } else if (name == 'PropertyID') {
                return obj[parm].propertyID
            }
1244ae71   Apichat.Tum   new response
240
        }
7598c58f   Apichat.Tum   add eventTypeID /...
241
242
    } else {
        return
1244ae71   Apichat.Tum   new response
243
244
245
246
247
    }

}

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

f3f14fef   TUM.Apichat   add value config ...

fbe7f7a7   Apichat.Tum   add api google