Commit 1244ae71fc8856721da7cfb22de25e66f86a91cb

Authored by Apichat.Tum
1 parent 48a9a246
Exists in master

new response

app/controllers/home.controller.js
1 -'use strict' 1 +"use strict"
  2 +var console = process.console
  3 +var yamlConfig = require('node-yaml-config')
  4 +var path = require('path')
  5 +var async = require('async')
  6 +var config = yamlConfig.load(path.join(__dirname, '/../../config/config.yml'))
2 7
3 -const console = process.console  
4 -const yamlConfig = require('node-yaml-config')  
5 -const path = require('path')  
6 -const async = require('async')  
7 -const config = yamlConfig.load(path.join(__dirname, '/../../config/config.yml'))  
8 -  
9 -const lib = require('../lib') 8 +var lib = require('../lib')
10 9
11 // lib.authorize() 10 // lib.authorize()
12 // lib.listEvents() 11 // lib.listEvents()
13 // lib.createEvent() 12 // lib.createEvent()
14 13
15 -const url = config.server.url  
16 -const port = config.server.port 14 +var url = config.server.url
  15 +var port = config.server.port
17 16
18 -function responseJSON(code, description, data, status) {  
19 - return {  
20 - "code": code,  
21 - "description": description,  
22 - "result": {  
23 - "data": data,  
24 - "status": status  
25 - } 17 +function rmsRes(res) {
  18 + this._40401 = function (data) {
  19 + res.json({
  20 + resultCode: '40401',
  21 + resultDescription: 'Data not found'
  22 + }).end()
  23 + }
  24 + this._50000 = function (data) {
  25 + console.tag('ERROR!').time().file().error(data)
  26 + res.json({
  27 + resultCode: '50000',
  28 + resultDescription: 'System Error',
  29 + }).end()
  30 + }
  31 + this._40301 = function (data) {
  32 + res.json({
  33 + resultCode: '40301',
  34 + resultDescription: 'Missing or invalid parameter'
  35 + }).end()
  36 + }
  37 + this._20000 = function (data) {
  38 + console.tag().time().file().info(data)
  39 + res.json({
  40 + resultCode: '20000',
  41 + resultDescription: 'Success',
  42 + data: data
  43 + }).end()
26 } 44 }
27 } 45 }
28 46
@@ -37,7 +55,8 @@ function ggToKendo(data) { @@ -37,7 +55,8 @@ function ggToKendo(data) {
37 for (var n = 0; n < data.items.length; n++) { 55 for (var n = 0; n < data.items.length; n++) {
38 dataToArr.push({ 56 dataToArr.push({
39 "TaskID": data.items[n].id, 57 "TaskID": data.items[n].id,
40 - "EventTypeID": checkObjEmpty(data.items[n].extendedProperties, 'private'), 58 + "EventTypeID": checkObjEmpty(data.items[n].extendedProperties, 'private', 'EventTypeID'),
  59 + "PropertyID": checkObjEmpty(data.items[n].extendedProperties, 'private', 'PropertyID'),
41 "Title": data.items[n].summary, 60 "Title": data.items[n].summary,
42 "Description": data.items[n].description, 61 "Description": data.items[n].description,
43 "StartTimezone": "Asia/Bangkok", 62 "StartTimezone": "Asia/Bangkok",
@@ -51,7 +70,7 @@ function ggToKendo(data) { @@ -51,7 +70,7 @@ function ggToKendo(data) {
51 }) 70 })
52 } 71 }
53 } catch (error) { 72 } catch (error) {
54 - console.error(error.message) 73 + console.tag().time().file().error(error.message)
55 } finally { 74 } finally {
56 return dataToArr 75 return dataToArr
57 } 76 }
@@ -62,53 +81,49 @@ function home(req, res) { @@ -62,53 +81,49 @@ function home(req, res) {
62 } 81 }
63 82
64 function index(req, res) { 83 function index(req, res) {
65 - console.log(req.body) 84 + var rms = new rmsRes(res)
  85 +
  86 + console.tag().time().file().log(req.body)
66 lib.getNewToken((err, authUrl) => { 87 lib.getNewToken((err, authUrl) => {
67 if (err) { 88 if (err) {
68 - console.error(err) 89 + rms._50000(err)
69 } else { 90 } else {
70 - console.info(authUrl)  
71 - res.send(authUrl) 91 + rms._20000(authUrl)
72 } 92 }
73 }) 93 })
74 } 94 }
75 95
76 function setToken(req, res) { 96 function setToken(req, res) {
77 - console.log(req.body) 97 + var rms = new rmsRes(res)
  98 +
  99 + console.tag().time().file().log(req.body)
78 var code = req.body.code 100 var code = req.body.code
79 lib.setNewToken(code, (err, token) => { 101 lib.setNewToken(code, (err, token) => {
80 if (err) { 102 if (err) {
81 - console.error(err)  
82 - res.send(err)  
83 - res.end() 103 + rms._50000(err)
84 } else { 104 } else {
85 - console.info(token)  
86 - res.send(token)  
87 - res.end() 105 + rms._20000(token)
88 } 106 }
89 }) 107 })
90 } 108 }
91 109
92 function events(req, res) { 110 function events(req, res) {
93 - // console.log(req.body) 111 + var rms = new rmsRes(res)
  112 +
94 lib.authorize((err, auth, authUrl) => { 113 lib.authorize((err, auth, authUrl) => {
95 if (err) { 114 if (err) {
96 - console.error(err)  
97 - res.send(err) 115 + rms._50000(err)
98 } else { 116 } else {
99 lib.listEvents(auth, (err, response) => { 117 lib.listEvents(auth, (err, response) => {
100 if (err) { 118 if (err) {
101 if (authUrl) { 119 if (authUrl) {
102 - console.info(authUrl)  
103 - res.json(responseJSON(res.code, "redirect to get auth code", authUrl, "redirect"))  
104 - res.end() 120 + console.tag().time().file().info(authUrl)
  121 + rms._20000(authUrl)
105 } else { 122 } else {
106 - console.error(err)  
107 - res.json(responseJSON(res.code, "response error", err.message, "failed"))  
108 - res.end() 123 + rms._50000(err)
109 } 124 }
110 } else { 125 } else {
111 - // console.info(response) 126 + // console.tag().time().file().info(response)
112 res.jsonp(ggToKendo(response)) 127 res.jsonp(ggToKendo(response))
113 res.end() 128 res.end()
114 } 129 }
@@ -118,53 +133,40 @@ function events(req, res) { @@ -118,53 +133,40 @@ function events(req, res) {
118 } 133 }
119 134
120 function eventCreate(req, res) { 135 function eventCreate(req, res) {
121 - console.log(req.body) 136 + var rms = new rmsRes(res)
  137 +
  138 + console.tag().time().file().log(req.body)
122 let payload = req.body 139 let payload = req.body
123 if (typeof payload.models == "string") { 140 if (typeof payload.models == "string") {
124 payload.models = JSON.parse(req.body.models) 141 payload.models = JSON.parse(req.body.models)
125 payload = payload.models[0] 142 payload = payload.models[0]
126 } 143 }
127 144
128 - // let summary = payload.summary  
129 - // let description = payload.description  
130 - // let email = payload.email  
131 - // let startDate = payload.startDate  
132 - // let startTimezone = payload.startTimezone  
133 - // let endDate = payload.endDate  
134 - // let endTimezone = payload.endTimezone  
135 - // // let extendedProperties = payload.extendedProperties  
136 - // payload.extendedProperties = {  
137 - // "private": {  
138 - // "eventTypeID": payload.EventTypeID  
139 - // }  
140 - // }  
141 -  
142 lib.authorize((err, auth) => { 145 lib.authorize((err, auth) => {
143 let options = lib.eventBuilder(payload) 146 let options = lib.eventBuilder(payload)
144 if (err) { 147 if (err) {
145 - res.send(err) 148 + rms._50000(err)
146 } else { 149 } else {
147 options.auth = auth 150 options.auth = auth
148 } 151 }
149 152
150 lib.createEvent(options, (err, result) => { 153 lib.createEvent(options, (err, result) => {
151 if (err) { 154 if (err) {
152 - res.send(err) 155 + rms._50000(err)
153 } else { 156 } else {
154 - console.info(result)  
155 - res.send(result)  
156 - res.end() 157 + rms._20000(result)
157 } 158 }
158 }) 159 })
159 }) 160 })
160 } 161 }
161 162
162 function oauth2callback(req, res) { 163 function oauth2callback(req, res) {
  164 + var rms = new rmsRes(res)
  165 +
163 var code = req.query.code 166 var code = req.query.code
164 lib.setNewToken(code, (err, token) => { 167 lib.setNewToken(code, (err, token) => {
165 if (err) { 168 if (err) {
166 - res.send(err)  
167 - res.end() 169 + rms._50000(err)
168 } else { 170 } else {
169 res.redirect('http://localhost:3030/home') 171 res.redirect('http://localhost:3030/home')
170 } 172 }
@@ -172,8 +174,9 @@ function oauth2callback(req, res) { @@ -172,8 +174,9 @@ function oauth2callback(req, res) {
172 } 174 }
173 175
174 function eventDelete(req, res) { 176 function eventDelete(req, res) {
175 - console.log(req.body) 177 + var rms = new rmsRes(res)
176 178
  179 + console.tag().time().file().log(req.body)
177 let payload = req.body 180 let payload = req.body
178 if (typeof payload.models == "string") { 181 if (typeof payload.models == "string") {
179 payload.models = JSON.parse(req.body.models) 182 payload.models = JSON.parse(req.body.models)
@@ -182,28 +185,26 @@ function eventDelete(req, res) { @@ -182,28 +185,26 @@ function eventDelete(req, res) {
182 lib.authorize((err, auth) => { 185 lib.authorize((err, auth) => {
183 let options = lib.deleteBuilder(payload) 186 let options = lib.deleteBuilder(payload)
184 if (err) { 187 if (err) {
185 - console.error(err.message)  
186 - res.send(err) 188 + rms._50000(err)
187 } else { 189 } else {
188 - console.info(auth) 190 + console.tag().time().file().info(auth)
189 options.auth = auth 191 options.auth = auth
190 } 192 }
191 193
192 lib.deleteEvent(options, (err, result) => { 194 lib.deleteEvent(options, (err, result) => {
193 if (err) { 195 if (err) {
194 - console.error(err.message)  
195 - res.send(err) 196 + rms._50000(err)
196 } else { 197 } else {
197 - console.info(result)  
198 - res.send(result)  
199 - res.end() 198 + rms._20000(result)
200 } 199 }
201 }) 200 })
202 }) 201 })
203 } 202 }
204 203
205 function eventUpdate(req, res) { 204 function eventUpdate(req, res) {
206 - console.log(req.body) 205 + var rms = new rmsRes(res)
  206 +
  207 + console.tag().time().file().log(req.body)
207 let payload = req.body 208 let payload = req.body
208 if (typeof payload.models == "string") { 209 if (typeof payload.models == "string") {
209 payload.models = JSON.parse(req.body.models) 210 payload.models = JSON.parse(req.body.models)
@@ -212,30 +213,30 @@ function eventUpdate(req, res) { @@ -212,30 +213,30 @@ function eventUpdate(req, res) {
212 lib.authorize((err, auth) => { 213 lib.authorize((err, auth) => {
213 let options = lib.updateBuilder(payload) 214 let options = lib.updateBuilder(payload)
214 if (err) { 215 if (err) {
215 - console.error(err.message)  
216 - res.send(err) 216 + rms._50000(err)
217 } else { 217 } else {
218 - console.info(auth) 218 + console.tag().time().file().info(auth)
219 options.auth = auth 219 options.auth = auth
220 } 220 }
221 221
222 lib.updateEvent(options, (err, result) => { 222 lib.updateEvent(options, (err, result) => {
223 if (err) { 223 if (err) {
224 - console.error(err)  
225 - res.send(err) 224 + rms._50000(err)
226 } else { 225 } else {
227 - console.info(result)  
228 - res.send(result)  
229 - res.end() 226 + rms._20000(result)
230 } 227 }
231 }) 228 })
232 }) 229 })
233 } 230 }
234 231
235 -function checkObjEmpty(obj, parm) { 232 +function checkObjEmpty(obj, parm, name) {
236 if (obj) { 233 if (obj) {
237 if (Object.getOwnPropertyNames(obj)) { 234 if (Object.getOwnPropertyNames(obj)) {
238 - return obj[parm].eventTypeID 235 + if (name == 'EventTypeID') {
  236 + return obj[parm].eventTypeID
  237 + } else if (name == 'PropertyID') {
  238 + return obj[parm].propertyID
  239 + }
239 } 240 }
240 } else { 241 } else {
241 return 242 return
app/lib/index.js
@@ -257,6 +257,9 @@ module.exports = { @@ -257,6 +257,9 @@ module.exports = {
257 } 257 }
258 } 258 }
259 } 259 }
  260 + if (payload.PropertyID) {
  261 + buildPayload.extendedProperties.private.propertyID = payload.PropertyID;
  262 + }
260 } catch (error) { 263 } catch (error) {
261 console.error(error.message) 264 console.error(error.message)
262 } finally { 265 } finally {
@@ -312,6 +315,9 @@ module.exports = { @@ -312,6 +315,9 @@ module.exports = {
312 } 315 }
313 } 316 }
314 } 317 }
  318 + if (payload.PropertyID) {
  319 + buildPayload.extendedProperties.private.propertyID = payload.PropertyID;
  320 + }
315 if (payload.extendedProperties) { 321 if (payload.extendedProperties) {
316 buildPayload.extendedProperties = payload.extendedProperties 322 buildPayload.extendedProperties = payload.extendedProperties
317 } 323 }