Blame view

app/controllers/home.controller.js 8.71 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
var URLCALENDAR = config.urlcalendar
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 rmsRes(res) {
    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
    }
}

cf86e9a3   Apichat.Tum   - ui with oauth2 ...
48
49
50
51
52
53
54
55
56
57
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({
77034810   Apichat.Tum   fix REST API
58
                "TaskID": data.items[n].id,
1244ae71   Apichat.Tum   new response
59
60
                "EventTypeID": checkObjEmpty(data.items[n].extendedProperties, 'private', 'EventTypeID'),
                "PropertyID": checkObjEmpty(data.items[n].extendedProperties, 'private', 'PropertyID'),
cf86e9a3   Apichat.Tum   - ui with oauth2 ...
61
62
63
64
65
66
67
68
69
70
71
72
73
                "Title": data.items[n].summary,
                "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) {
1244ae71   Apichat.Tum   new response
74
        console.tag().time().file().error(error.message)
cf86e9a3   Apichat.Tum   - ui with oauth2 ...
75
76
77
78
79
80
81
82
    } finally {
        return dataToArr
    }
}

function home(req, res) {
    res.render('index')
}
f6a4415a   Apichat.Tum   start smart-rms-c...
83
84

function index(req, res) {
1244ae71   Apichat.Tum   new response
85
86
87
    var rms = new rmsRes(res)

    console.tag().time().file().log(req.body)
cf86e9a3   Apichat.Tum   - ui with oauth2 ...
88
89
    lib.getNewToken((err, authUrl) => {
        if (err) {
1244ae71   Apichat.Tum   new response
90
            rms._50000(err)
cf86e9a3   Apichat.Tum   - ui with oauth2 ...
91
        } else {
1244ae71   Apichat.Tum   new response
92
            rms._20000(authUrl)
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) {
1244ae71   Apichat.Tum   new response
98
99
100
    var rms = new rmsRes(res)

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

function events(req, res) {
1244ae71   Apichat.Tum   new response
112
    var rms = new rmsRes(res)
fbe7f7a7   Apichat.Tum   add api google
113
    console.tag().time().file().log(req.body)
cf86e9a3   Apichat.Tum   - ui with oauth2 ...
114
    lib.authorize((err, auth, authUrl) => {
f6a4415a   Apichat.Tum   start smart-rms-c...
115
        if (err) {
1244ae71   Apichat.Tum   new response
116
            rms._50000(err)
cf86e9a3   Apichat.Tum   - ui with oauth2 ...
117
        } else {
fbe7f7a7   Apichat.Tum   add api google
118
119
120
121
122
123
124
            lib.listCalendar(auth, (err, response) => {
                if (err) {
                    console.error(err);
                } else {
                    console.info(response);
                }
            })
cf86e9a3   Apichat.Tum   - ui with oauth2 ...
125
126
            lib.listEvents(auth, (err, response) => {
                if (err) {
77034810   Apichat.Tum   fix REST API
127
                    if (authUrl) {
1244ae71   Apichat.Tum   new response
128
129
                        console.tag().time().file().info(authUrl)
                        rms._20000(authUrl)
77034810   Apichat.Tum   fix REST API
130
                    } else {
1244ae71   Apichat.Tum   new response
131
                        rms._50000(err)
77034810   Apichat.Tum   fix REST API
132
                    }
cf86e9a3   Apichat.Tum   - ui with oauth2 ...
133
                } else {
fbe7f7a7   Apichat.Tum   add api google
134
135
                    res.json(ggToKendo(response));
                    res.end();
cf86e9a3   Apichat.Tum   - ui with oauth2 ...
136
137
                }
            })
f6a4415a   Apichat.Tum   start smart-rms-c...
138
        }
f6a4415a   Apichat.Tum   start smart-rms-c...
139
140
141
    })
}

77034810   Apichat.Tum   fix REST API
142
function eventCreate(req, res) {
1244ae71   Apichat.Tum   new response
143
144
145
    var rms = new rmsRes(res)

    console.tag().time().file().log(req.body)
f6a4415a   Apichat.Tum   start smart-rms-c...
146
    let payload = req.body
77034810   Apichat.Tum   fix REST API
147
148
149
150
    if (typeof payload.models == "string") {
        payload.models = JSON.parse(req.body.models)
        payload = payload.models[0]
    }
f6a4415a   Apichat.Tum   start smart-rms-c...
151

f6a4415a   Apichat.Tum   start smart-rms-c...
152
    lib.authorize((err, auth) => {
a54f2679   DESKTOP-RBJDHSM\ADMIN   fix API
153
        let options = lib.eventBuilder(payload)
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 {
            options.auth = auth
f6a4415a   Apichat.Tum   start smart-rms-c...
158
159
        }

a54f2679   DESKTOP-RBJDHSM\ADMIN   fix API
160
        lib.createEvent(options, (err, result) => {
f6a4415a   Apichat.Tum   start smart-rms-c...
161
            if (err) {
1244ae71   Apichat.Tum   new response
162
                rms._50000(err)
f6a4415a   Apichat.Tum   start smart-rms-c...
163
            } else {
1244ae71   Apichat.Tum   new response
164
                rms._20000(result)
f6a4415a   Apichat.Tum   start smart-rms-c...
165
166
167
168
169
            }
        })
    })
}

77034810   Apichat.Tum   fix REST API
170
function oauth2callback(req, res) {
1244ae71   Apichat.Tum   new response
171
172
    var rms = new rmsRes(res)

77034810   Apichat.Tum   fix REST API
173
174
175
    var code = req.query.code
    lib.setNewToken(code, (err, token) => {
        if (err) {
1244ae71   Apichat.Tum   new response
176
            rms._50000(err)
77034810   Apichat.Tum   fix REST API
177
        } else {
fbe7f7a7   Apichat.Tum   add api google
178

f3f14fef   TUM.Apichat   add value config ...
179
            res.redirect(URLCALENDAR)
77034810   Apichat.Tum   fix REST API
180
181
182
183
184
        }
    })
}

function eventDelete(req, res) {
1244ae71   Apichat.Tum   new response
185
    var rms = new rmsRes(res)
21a1ff19   Apichat.Tum   fix follow smartr...
186

1244ae71   Apichat.Tum   new response
187
    console.tag().time().file().log(req.body)
77034810   Apichat.Tum   fix REST API
188
189
190
191
192
193
194
195
    let payload = req.body
    if (typeof payload.models == "string") {
        payload.models = JSON.parse(req.body.models)
        payload = payload.models[0]
    }
    lib.authorize((err, auth) => {
        let options = lib.deleteBuilder(payload)
        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
            console.tag().time().file().info(auth)
77034810   Apichat.Tum   fix REST API
199
200
201
202
203
            options.auth = auth
        }

        lib.deleteEvent(options, (err, result) => {
            if (err) {
1244ae71   Apichat.Tum   new response
204
                rms._50000(err)
77034810   Apichat.Tum   fix REST API
205
            } else {
1244ae71   Apichat.Tum   new response
206
                rms._20000(result)
77034810   Apichat.Tum   fix REST API
207
208
209
210
211
212
            }
        })
    })
}

function eventUpdate(req, res) {
1244ae71   Apichat.Tum   new response
213
214
215
    var rms = new rmsRes(res)

    console.tag().time().file().log(req.body)
77034810   Apichat.Tum   fix REST API
216
217
218
219
220
221
222
223
    let payload = req.body
    if (typeof payload.models == "string") {
        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
224
            rms._50000(err)
77034810   Apichat.Tum   fix REST API
225
        } else {
1244ae71   Apichat.Tum   new response
226
            console.tag().time().file().info(auth)
77034810   Apichat.Tum   fix REST API
227
228
229
230
231
            options.auth = auth
        }

        lib.updateEvent(options, (err, result) => {
            if (err) {
1244ae71   Apichat.Tum   new response
232
                rms._50000(err)
77034810   Apichat.Tum   fix REST API
233
            } else {
1244ae71   Apichat.Tum   new response
234
                rms._20000(result)
77034810   Apichat.Tum   fix REST API
235
236
237
238
239
            }
        })
    })
}

1244ae71   Apichat.Tum   new response
240
function checkObjEmpty(obj, parm, name) {
7598c58f   Apichat.Tum   add eventTypeID /...
241
242
    if (obj) {
        if (Object.getOwnPropertyNames(obj)) {
1244ae71   Apichat.Tum   new response
243
244
245
246
247
            if (name == 'EventTypeID') {
                return obj[parm].eventTypeID
            } else if (name == 'PropertyID') {
                return obj[parm].propertyID
            }
7598c58f   Apichat.Tum   add eventTypeID /...
248
249
250
251
252
253
        }
    } else {
        return
    }

}
77034810   Apichat.Tum   fix REST API
254

fbe7f7a7   Apichat.Tum   add api google
255
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
function createCalendar(req, res) {
    var rms = new rmsRes(res)
    let options = {};
    lib.authorize((err, auth) => {
        if (err) {
            rms._50000(err)
        } else {
            options.auth = auth;
            options.summary = req.body.summary;
            lib.createCalendar(options, (err, result) => {
                if (err) {
                    rms._50000(err)
                } else {
                    rms._20000(result)
                }
            })
        }
    })
}

function getCalendar(req, res) {
    var rms = new rmsRes(res);

    lib.authorize((err, auth) => {
        if (err) {
            rms._50000(err)
        } else {
            lib.getCalendar(auth, (err, response) => {
                if (err) {
                    rms._50000(err)
                } else {
                    rms._20000(response)
                }
            })
        }
    })
}

function deleteCalendar(req, res) {
    var rms = new rmsRes(res);
    var options = {};
    lib.authorize((err, auth) => {
        if (err) {
            rms._50000(err)
        } else {
            options.auth = auth;
            options.calendarId = req.body.id;
            lib.deleteCalendar(options, (err, response) => {
                if (err) {
                    rms._50000(err)
                } else {
                    rms._20000(response)
                }
            })
        }
    })
}

function refreshToken(req, res) {
    var rms = new rmsRes(res);
f3f14fef   TUM.Apichat   add value config ...
315
    var options = {};
fbe7f7a7   Apichat.Tum   add api google
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
    lib.authorize((err, auth) => {
        if (err) {
            rms._50000(err)
        } else {
            options.auth = auth;
            lib.refreshToken(options, (err, response) => {
                if (err) {
                    rms._50000(err)
                } else {
                    rms._20000(response)
                }
            })
        }
    })
}

module.exports.index = index;
module.exports.events = events;
module.exports.eventCreate = eventCreate;
module.exports.eventUpdate = eventUpdate;
module.exports.eventDelete = eventDelete;
module.exports.home = home;
module.exports.setToken = setToken;
module.exports.oauth2callback = oauth2callback;
module.exports.createCalendar = createCalendar;
module.exports.getCalendar = getCalendar;
module.exports.deleteCalendar = deleteCalendar;
module.exports.refreshToken = refreshToken;