Repository navigation
Expand file tree
/
Copy pathExternalLinkController.js
More file actions
272 lines (247 loc) · 9.18 KB
/
Copy pathExternalLinkController.js
File metadata and controls
272 lines (247 loc) · 9.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/**
* ExternalLinkController
*
* @module :: Controller
* @description :: A set of functions called `actions`.
*
* Actions contain code telling Sails how to respond to a certain type of request.
* (i.e. do stuff, then send some JSON, show an HTML page, or redirect to another URL)
*
* You can configure the blueprint URLs which trigger these actions (`config/controllers.js`)
* and/or override them with custom routes (`config/routes.js`)
*
* NOTE: The code you write here supports both HTTP and Socket.io automatically.
*
* @docs :: http://sailsjs.org/#!documentation/controllers
*/
"use strict";
var async = require("async");
module.exports = {
/**
* Overrides for the settings in `config/controllers.js`
* (specific to ExternalLinkController)
*/
_config: {},
/**
* External links list action. This action will show user project specified external link list
* GUI and user can interact with those.
*
* @param {Request} request Request object
* @param {Response} response Response object
*/
list: function(request, response) {
var projectId = parseInt(request.param("projectId"), 10);
// Parallel jobs to fetch project and actual external link data.
async.parallel(
{
// Get project data
project: function(callback) {
DataService.getProject(projectId, callback);
},
// Get external links for current project
links: function(callback) {
DataService.getProjectLinks(projectId, callback);
}
},
/**
* Main callback function which is called after all parallel jobs are done.
*
* @param {null|Error} error
* @param {{}} results
*/
function(error, results) {
if (error) {
ResponseService.makeError(error, request, response);
} else {
processLinks(results);
}
}
);
/**
* Private function to process links. Basically this will just map all founded links
* and fetch actual count for those.
*
* @param {{}} results Project and links data
*/
function processLinks(results) {
// Map founded links
async.map(
results.links,
/**
* Iterator function which is called to every link in array. This will just add
* attached link count to each link object.
*
* @param {sails.model.link} link
* @param {Function} callback
*/
function(link, callback) {
Link
.count({externalLinkId: link.id})
.exec(function (error, count) {
link.attachedLinksCount = count;
callback(error, link);
});
},
/**
* Main callback function which is called after all array values are processed.
*
* @param {null|Error} error
*/
function(error) {
if (error) {
ResponseService.makeError(error, request, response);
} else {
response.view(_.extend({
projectId: projectId
}, results));
}
}
);
}
},
/**
* External link add action. This will render GUI for adding new link.
*
* @param {Request} request Request object
* @param {Response} response Response object
*/
add: function(request, response) {
var projectId = parseInt(request.param("projectId"), 10);
// Fetch project data
DataService.getProject(projectId, function(error, project) {
if (error) {
ResponseService.makeError(error, request, response);
} else {
response.view({
project: project,
projectId: projectId
});
}
});
},
/**
* External link edit action. This will render GUI for editing specified link.
*
* @param {Request} request Request object
* @param {Response} response Response object
*/
edit: function(request, response) {
var linkId = parseInt(request.param("linkId"), 10);
// Fetch project data
DataService.getProjectLink(linkId, function(error, link) {
if (error) {
ResponseService.makeError(error, request, response);
} else {
response.view({
link: link
});
}
});
},
/**
* Action to show all links that area attached to specified external link.
*
* @param {Request} request Request object
* @param {Response} response Response object
*/
links: function(request, response) {
var linkId = parseInt(request.param("id"), 10);
// Make parallel jobs to fetch basic data
async.parallel(
{
// Fetch external link data
externalLink: function(callback) {
DataService.getProjectLink(linkId, callback);
},
// Fetch user data
users: function(callback) {
DataService.getUsers({}, callback);
},
// Fetch attached links
links: function(callback) {
Link
.find()
.where({ externalLinkId: linkId })
.sort("objectName ASC")
.sort("objectId ASC")
.sort("link ASC")
.exec(function(error, links) {
callback(error, links);
});
}
},
/**
* Main callback function which is called after all parallel jobs are processed.
*
* @param {null|Error} error
* @param {{}} results
*/
function(error, results) {
if (error) {
ResponseService.makeError(error, request, response);
} else {
processData(results);
}
}
);
/**
* Private function to process actual link data for GUI render.
*
* @param {{}} results Object that contains all necessary data for rendering view
*/
function processData(results) {
// Map founded links
async.map(
results.links,
/**
* Iterator function which is called with every link.
*
* @param {sails.model.link} link
* @param {Function} callback
*/
function(link, callback) {
// Set author to link
link.author = _.find(results.users, function(user) {
return user.id === link.createdUserId;
});
switch (link.objectName) {
case "Story":
DataService.getStory(link.objectId, function(error, story) {
if (error) {
callback(error, null);
} else {
link.objectTitle = story.objectTitle();
callback(null, link);
}
});
break;
case "Task":
DataService.getTask(link.objectId, function(error, task) {
if (error) {
callback(error, null);
} else {
link.objectTitle = task.objectTitle();
callback(null, link);
}
});
break;
}
},
/**
* Callback function which is called after all links are mapped.
*
* @param {null|Error} error
*/
function(error) {
if (error) {
ResponseService.makeError(error, request, response);
} else {
response.view(_.extend({
linkId: linkId
}, results));
}
}
);
}
}
};