Repository navigation
Expand file tree
/
Copy pathDataService.js
More file actions
1123 lines (998 loc) · 37.1 KB
/
Copy pathDataService.js
File metadata and controls
1123 lines (998 loc) · 37.1 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
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
315
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* /api/services/DataService.js
*
* Generic data service, which is used to fetch generic data and call defined callback after data fetching.
* This contains basically all data fetch that Taskboard needs. Services contains fetch of one and multiple
* objects.
*
* Single object fetch:
* get{ObjectName}(terms, next, [noExistCheck])
*
* Multiple object fetch
* get{ObjectName}s(terms, next)
*
* All data service methods will write error log if some error occurs. In all cases callback function 'next'
* is called with two arguments: possible error and actual result.
*
* Note that with multiple object fetch service will attach "default" sort conditions for results.
*/
var async = require("async");
/**
* Service to fetch single project data from database.
*
* @param {Number|{}} where Used query conditions
* @param {Function} next Callback function to call after query
* @param {Boolean} [noExistsCheck] If data is not found, skip error
*/
exports.getProject = function(where, next, noExistsCheck) {
noExistsCheck = noExistsCheck || false;
Project
.findOne(where)
.exec(function(error, /** sails.model.project */ project) {
if (error) {
sails.log.error(__filename + ":" + __line + " [Failed to fetch project data]");
sails.log.error(error);
} else if (!project && !noExistsCheck) {
error = new Error();
error.message = "Project not found.";
error.status = 404;
}
next(error, project);
});
};
/**
* Service to fetch project data from database.
*
* @param {{}} where Used query conditions
* @param {Function} next Callback function to call after query
*/
exports.getProjects = function(where, next) {
Project
.find()
.where(where)
.sort("title ASC")
.exec(function(error, /** sails.model.project[] */ projects) {
if (error) {
sails.log.error(__filename + ":" + __line + " [Failed to fetch project data]");
sails.log.error(error);
}
next(error, projects);
});
};
/**
* Service to fetch project object that belongs to specified object (name + id). Basically service this
* just fetches necessary data by specified object and process it until we have project.
*
* Note that this only support following objects at this time:
* - Story
* - Task
*
* @param {String} objectName Name of the object
* @param {Number} objectId Object id
* @param {Function} next
*/
exports.getProjectByLink = function(objectName, objectId, next) {
// Make necessary jobs as waterfall
async.waterfall(
[
/**
* First job in main water fall jobs. With this we will determine project id by
* specified object. Actual determination is done in separate processes which
* uses own async water fall jobs to determine project id.
*
* @param {Function} callback
*/
function(callback) {
switch (objectName) {
case "Story":
// In case of story we just needed to fetch story data
DataService.getStory(objectId, callback);
break;
case "Task":
// With task we need to get actual task object and then story object
async.waterfall(
[
// Fetch task
function(cb) {
DataService.getTask(objectId, cb);
},
// Fetch story object via task object.
function(task, cb) {
DataService.getStory(task.storyId, cb);
}
],
/**
* Task specified water fall callback function which is called after we have
* determined project id via task data or some error has been occurred.
*
* This will call main water fall async job callback function.
*
* Yo dawg, i herd like y ar doing callbacks in your callbacks!
*
* @param {null|Error} error Possible error
* @param {sails.model.story} story Story object
*/
function(error, story) {
callback(error, story);
}
);
break;
default:
callback("Not supported link object '" + objectName + "' given", null);
break;
}
},
/**
* Callback function to fetch actual project object, this is called after we have
* determined project id from specified object.
*
* @param {sails.model.story} story Story object
* @param {Function} callback Callback function to call after job is done
*/
function(story, callback) {
DataService.getProject(story.projectId, callback);
}
],
/**
* Main callback which is call after all main water fall jobs are done
*
* @param {null|Error} error
* @param {sails.model.project} project
*/
function(error, project) {
if (error) {
sails.log.error(__filename + ":" + __line + " [Failed to fetch project data by link, see errors above.]");
}
next(error, project);
}
)
};
/**
* Service to fetch single project user from database by given conditions.
*
* @param {Number|{}} where Used query conditions
* @param {Function} next Callback function to after query
* @param {Boolean} [noExistsCheck] If data is not found, skip error
*/
exports.getProjectUser = function(where, next, noExistsCheck) {
noExistsCheck = noExistsCheck || false;
ProjectUser
.findOne(where)
.exec(function(error, /** sails.model.projectUser */ projectUser) {
if (error) {
sails.log.error(__filename + ":" + __line + " [Failed to fetch project user data]");
sails.log.error(error);
} else if (!projectUser && !noExistsCheck) {
error = new Error();
error.message = "Project user not found.";
error.status = 404;
}
next(error, projectUser);
});
};
/**
* Service to fetch project users from database by given conditions.
*
* @param {{}} where Used query conditions
* @param {Function} next Callback function to after query
*/
exports.getProjectUsers = function(where, next) {
ProjectUser
.find()
.where(where)
.exec(function(error, /** sails.model.projectUser[] */ projectUsers) {
if (error) {
sails.log.error(__filename + ":" + __line + " [Failed to fetch project user data]");
sails.log.error(error);
}
next(error, projectUsers);
});
};
/**
* Service to fetch single project external link from database.
*
* @param {Number|{}} where Used query conditions
* @param {Function} next Callback function to call after query
* @param {Boolean} [noExistsCheck] If data is not found, skip error
*/
exports.getProjectLink = function(where, next, noExistsCheck) {
noExistsCheck = noExistsCheck || false;
ExternalLink
.findOne(where)
.exec(function(error, /** sails.model.externalLink */ externalLink) {
if (error) {
sails.log.error(__filename + ":" + __line + " [Failed to fetch external link data]");
sails.log.error(error);
} else if (!externalLink && !noExistsCheck) {
error = new Error();
error.message = "External link not found.";
error.status = 404;
}
next(error, externalLink);
});
};
/**
* Service to fetch specified project external link data from database.
*
* @param {Number} projectId Project id
* @param {Function} next Callback function to call after query
*/
exports.getProjectLinks = function(projectId, next) {
ExternalLink
.find()
.where({projectId: projectId})
.sort("title ASC")
.exec(function(error, /** sails.model.externalLink[] */ links) {
if (error) {
sails.log.error(__filename + ":" + __line + " [Failed to fetch external link data]");
sails.log.error(error);
}
next(error, links);
});
};
/**
* Service to fetch single sprint data from database.
*
* @param {Number|{}} where Used query conditions
* @param {Function} next Callback function to call after query
* @param {Boolean} [noExistsCheck] If data is not found, skip error
*/
exports.getSprint = function(where, next, noExistsCheck) {
noExistsCheck = noExistsCheck || false;
Sprint
.findOne(where)
.exec(function(error, /** sails.model.sprint */ sprint) {
if (error) {
sails.log.error(__filename + ":" + __line + " [Failed to fetch sprint data]");
sails.log.error(error);
} else if (!sprint && !noExistsCheck) {
error = new Error();
error.message = "Sprint not found.";
error.status = 404;
}
next(error, sprint);
});
};
/**
* Service to fetch sprint data from database.
*
* @param {{}} where Used query conditions
* @param {Function} next Callback function to call after query
*/
exports.getSprints = function(where, next) {
Sprint
.find()
.where(where)
.sort("dateStart ASC")
.sort("dateEnd ASC")
.sort("title ASC")
.exec(function(error, /** sails.model.sprint[] */ sprints) {
if (error) {
sails.log.error(__filename + ":" + __line + " [Failed to fetch sprint data]");
sails.log.error(error);
}
next(error, sprints);
});
};
/**
* Service to fetch sprint exclude day objects from database.
*
* @param {Number} sprintId Sprint ID
* @param {Function} next Callback function to call after query
*/
exports.getSprintExcludeDays = function(sprintId, next) {
ExcludeSprintDay
.find()
.where({sprintId: sprintId})
.sort("day ASC")
.exec(function(error, /** sails.model.excludeSprintDay[] */ excludeSprintDay) {
if (error) {
sails.log.error(__filename + ":" + __line + " [Failed to fetch sprint exclude day data]");
sails.log.error(error);
}
next(error, excludeSprintDay);
});
};
/**
* Service to fetch single milestone data from database.
*
* @param {Number|{}} where Used query conditions
* @param {Function} next Callback function to call after query
* @param {Boolean} [noExistsCheck] If data is not found, skip error
*/
exports.getMilestone = function(where, next, noExistsCheck) {
noExistsCheck = noExistsCheck || false;
Milestone
.findOne(where)
.exec(function(error, /** sails.model.milestone */ milestone) {
if (error) {
sails.log.error(__filename + ":" + __line + " [Failed to fetch milestone data]");
sails.log.error(error);
} else if (!milestone && !noExistsCheck) {
error = new Error();
error.message = "Milestone not found.";
error.status = 404;
}
next(error, milestone);
});
};
/**
* Service to fetch milestone data from database.
*
* @param {{}} where Used query conditions
* @param {Function} next Callback function to call after query
*/
exports.getMilestones = function(where, next) {
Milestone
.find()
.where(where)
.sort("deadline ASC")
.sort("title ASC")
.exec(function(error, /** sails.model.milestone[] */ milestones) {
if (error) {
sails.log.error(__filename + ":" + __line + " [Failed to fetch milestone data]");
sails.log.error(error);
}
next(error, milestones);
});
};
/**
* Service to fetch single story data from database.
*
* @param {Number|{}} where Used query conditions
* @param {Function} next Callback function to call after query
* @param {Boolean} [noExistsCheck] If data is not found, skip error
*/
exports.getStory = function(where, next, noExistsCheck) {
noExistsCheck = noExistsCheck || false;
Story
.findOne(where)
.exec(function(error, /** sails.model.story */ story) {
if (error) {
sails.log.error(__filename + ":" + __line + " [Failed to fetch story data]");
sails.log.error(error);
} else if (!story && !noExistsCheck) {
error = new Error();
error.message = "Story not found.";
error.status = 404;
}
next(error, story);
});
};
/**
* Service to fetch story data from database.
*
* @param {{}} where Used query conditions
* @param {Function} next Callback function to call after query
*/
exports.getStories = function(where, next) {
Story
.find()
.where(where)
.sort("priority ASC")
.sort("title ASC")
.exec(function(error, /** sails.model.story[] */ stories) {
if (error) {
sails.log.error(__filename + ":" + __line + " [Failed to fetch story data]");
sails.log.error(error);
}
next(error, stories);
});
};
/**
* Service to fetch single task data from database.
*
* @param {Number|{}} where Used query conditions
* @param {Function} next Callback function to call after query
* @param {Boolean} [noExistsCheck] If data is not found, skip error
*/
exports.getTask = function(where, next, noExistsCheck) {
noExistsCheck = noExistsCheck || false;
Task
.findOne(where)
.exec(function(error, /** sails.model.task */ task) {
if (error) {
sails.log.error(__filename + ":" + __line + " [Failed to fetch task data]");
sails.log.error(error);
} else if (!task && !noExistsCheck) {
error = new Error();
error.message = "Task not found.";
error.status = 404;
}
next(error, task);
});
};
/**
* Service to fetch task data from database.
*
* @param {{}} where Used query conditions
* @param {Function} next Callback function to call after query
*/
exports.getTasks = function(where, next) {
Task
.find()
.where(where)
.sort("priority ASC")
.sort("title ASC")
.exec(function(error, /** sails.model.task[] */ tasks) {
if (error) {
sails.log.error(__filename + ":" + __line + " [Failed to fetch task data]");
sails.log.error(error);
}
next(error, tasks);
});
};
/**
* Service to fetch single phase data from database.
*
* @param {Number|{}} where Used query conditions
* @param {Function} next Callback function to call after query
* @param {Boolean} [noExistsCheck] If data is not found, skip error
*/
exports.getPhase = function(where, next, noExistsCheck) {
noExistsCheck = noExistsCheck || false;
Phase
.findOne(where)
.exec(function(error, /** sails.model.phase */ phase) {
if (error) {
sails.log.error(__filename + ":" + __line + " [Failed to fetch phase data]");
sails.log.error(error);
} else if (!phase && !noExistsCheck) {
error = new Error();
error.message = "Phase not found.";
error.status = 404;
}
next(error, phase);
});
};
/**
* Service to fetch phase data from database.
*
* @param {{}} where Used query conditions
* @param {Function} next Callback function to call after query
*/
exports.getPhases = function(where, next) {
Phase
.find()
.where(where)
.sort("order ASC")
.exec(function(error, /** sails.model.phase[] */ phases) {
if (error) {
sails.log.error(__filename + ":" + __line + " [Failed to fetch phase data]");
sails.log.error(error);
}
next(error, phases);
});
};
/**
* Service to fetch phase data via specified task id data.
*
* @param {Number} taskId Task id whose phases are fetched
* @param {Function} next Callback function to call after query
*/
exports.getPhasesForTask = function(taskId, next) {
async.waterfall(
[
// Fetch task data
function(callback) {
DataService.getTask(taskId, callback);
},
// Fetch task story data
function(task, callback) {
DataService.getStory(task.storyId, callback);
},
// Fetch task phases data via story projectId information
function(story, callback) {
DataService.getPhases({projectId: story.projectId}, callback);
}
],
/**
* Main callback function which is called after all jobs are done and processed.
*
* @param {null|Error} error Possible error
* @param {sails.model.phase[]} phases Determined phases for task
*/
function(error, phases) {
if (error) {
sails.log.error(__filename + ":" + __line + " [Failed to fetch phases for task, check errors above]");
}
next(error, phases);
}
);
};
/**
* Service to fetch single phase duration data from database.
*
* @param {Number|{}} where Used query conditions
* @param {Function} next Callback function to call after query
* @param {Boolean} [noExistsCheck] If data is not found, skip error
*/
exports.getPhaseDuration = function(where, next, noExistsCheck) {
noExistsCheck = noExistsCheck || false;
PhaseDuration
.findOne(where)
.exec(function(error, /** sails.model.phaseDuration */ phaseDuration) {
if (error) {
sails.log.error(__filename + ":" + __line + " [Failed to fetch phase duration data]");
sails.log.error(error);
} else if (!phaseDuration && !noExistsCheck) {
error = new Error();
error.message = "Phase duration not found.";
error.status = 404;
}
next(error, phaseDuration);
});
};
/**
* Service to fetch phase duration data from database.
*
* @param {{}} where Used query conditions
* @param {Function} next Callback function to call after query
*/
exports.getPhaseDurations = function(where, next) {
PhaseDuration
.find()
.where(where)
.sort("order ASC")
.exec(function(error, /** sails.model.phaseDuration[] */ phaseDurations) {
if (error) {
sails.log.error(__filename + ":" + __line + " [Failed to fetch phase duration data]");
sails.log.error(error);
}
next(error, phaseDurations);
});
};
/**
* Service to fetch single comment from database. Note that this won't fetch comment
* siblings nor author data
*
* @param {Number|{}} where Used query conditions
* @param {Function} next Callback function to call after query
* @param {Boolean} [noExistsCheck] If data is not found, skip error
*/
exports.getComment = function(where, next, noExistsCheck) {
noExistsCheck = noExistsCheck || false;
Comment
.findOne(where)
.exec(function(error, /** sails.model.comment */ comment) {
if (error) {
sails.log.error(__filename + ":" + __line + " [Failed to fetch comment data]");
sails.log.error(error);
} else if (!comment && !noExistsCheck) {
error = new Error();
error.message = "Comment not found.";
error.status = 404;
}
next(error, comment);
});
};
/**
* Service to fetch comments for specified object. Note that service calls itself recursive to
* fetch all nested comments. Note that service fetches all users for performance reasons so we
* don't have to make n single user queries to database.
*
* @param {String} objectName Name of the object (Project, Sprint, Story, Task, etc.)
* @param {Number} objectId Id of the specified object
* @param {Number} commentId Possible parent comment id
* @param {Function} next Callback function which is called after comments are fetched
* @param {sails.model.user[]} [users] User objects or empty array
*/
exports.getComments = function(objectName, objectId, commentId, next, users) {
users = users || false;
async.parallel(
{
// Fetch comments
comments: function(callback) {
Comment
.find()
.where({
objectName: objectName,
objectId: objectId,
commentId: commentId
})
.sort("createdAt ASC")
.exec(function(error, /** sails.model.comment[] */ comments) {
callback(error, comments);
});
},
// Fetch users, note this is done only with first iteration
users: function(callback) {
if (users) {
callback(null, users);
} else {
DataService.getUsers({}, callback);
}
}
},
/**
* Main callback function which is called after all parallel jobs are done or
* an error has occurred within those.
*
* @param {null|Error} error
* @param {{
* comments: sails.model.comment[],
* users: sails.model.user[]
* }} data
*/
function(error, data) {
if (error) {
sails.log.error(__filename + ":" + __line + " [Failed to fetch comment data]");
sails.log.error(error);
next(error, null);
} else {
fetchSiblingsAndAttachUsers(data);
}
}
);
/**
* Private helper function to attach user object to each comment and fetch
* siblings by calling service method itself with current comment data.
*
* @param {{
* comments: sails.model.comment[],
* users: sails.model.user[]
* }} data
*/
function fetchSiblingsAndAttachUsers(data) {
async.map(
data.comments,
/**
* Iterator function which will attach user object to processed link object. Users
* are simply searched from user array which is fetched in main async parallel call.
*
* @param {sails.model.comment} comment Comment object
* @param {Function} callback Callback function to call after job is done.
*/
function(comment, callback) {
comment.author = _.find(data.users, function(user) {
return user.id === comment.createdUserId;
});
// Call service itself recursive, note that we pass the users to service
DataService.getComments(objectName, objectId, comment.id, function(error, comments) {
if (!error) {
comment.comments = comments;
}
callback(error, comment);
}, data.users);
},
/**
* Main callback function which is called after all links are mapped. In this
* comments contains author and siblings data.
*
* @param {null|Error} error Possible error
* @param {sails.model.comment[]} comment Processed comments
*/
function(error, comment) {
if (error) {
sails.log.error(__filename + ":" + __line + " [Failed to fetch comment sibling data]");
sails.log.error(error);
}
next(error, comment);
}
);
}
};
/**
* Service to fetch single task type data from database.
*
* @param {Number|{}} where Used query conditions
* @param {Function} next Callback function to call after query
* @param {Boolean} [noExistsCheck] If data is not found, skip error
*/
exports.getType = function(where, next, noExistsCheck) {
noExistsCheck = noExistsCheck || false;
Type
.findOne(where)
.exec(function(error, /** sails.model.type */ type) {
if (error) {
sails.log.error(__filename + ":" + __line + " [Failed to fetch type data]");
sails.log.error(error);
} else if (!type && !noExistsCheck) {
error = new Error();
error.message = "Task type not found.";
error.status = 404;
}
next(error, type);
});
};
/**
* Service to fetch type data from database.
*
* @param {{}} where Used query conditions
* @param {Function} next Callback function to call after query
*/
exports.getTypes = function(where, next) {
Type
.find()
.where(where)
.sort("order ASC")
.sort("title ASC")
.exec(function(error, /** sails.model.type[] */ types) {
if (error) {
sails.log.error(__filename + ":" + __line + " [Failed to fetch type data]");
sails.log.error(error);
}
next(error, types);
});
};
/**
* Service to fetch single link object from database.
*
* @param {Number|{}} where Used query conditions
* @param {Function} next Callback function to call after query
* @param {Boolean} [noExistsCheck] If data is not found, skip error
*/
exports.getLink = function(where, next, noExistsCheck) {
noExistsCheck = noExistsCheck || false;
Link
.findOne(where)
.exec(function(error, /** sails.model.link */ link) {
if (error) {
sails.log.error(__filename + ":" + __line + " [Failed to fetch link data]");
sails.log.error(error);
} else if (!link && !noExistsCheck) {
error = new Error();
error.message = "Link not found.";
error.status = 404;
}
next(error, link);
});
};
/**
* Service to fetch attached links for specified object. For performance reasons service
* will simply fetch all users from database and then map those to actual links. This will
* save a whole lot queries to database.
*
* @param {String} objectName Name of the object (Story, Task, etc.)
* @param {Number} objectId Id of the specified object
* @param {Function} next Callback function which is called after links are fetched
*/
exports.getLinks = function(objectName, objectId, next) {
async.parallel(
{
// Fetch links
links: function(callback) {
Link
.find()
.where({
objectName: objectName,
objectId: objectId
})
.sort("link ASC")
.sort("createdAt ASC")
.exec(function(error, /** sails.model.link[] */ links) {
callback(error, links);
});
},
// Fetch users
users: function(callback) {
DataService.getUsers({}, callback);
}
},
/**
* Main callback which is called after all parallel jobs are done or an
* error has occurred while processing those.
*
* @param {null|Error} error
* @param {{
* links: sails.model.link[],
* users: sails.model.user[]
* }} data
*/
function(error, data) {
if (error) {
sails.log.error(__filename + ":" + __line + " [Failed to fetch link data]");
sails.log.error(error);
next(error, null);
} else {
attachUsers(data);
}
}
);
/**
* Private helper function to attach user data to each link. This is much faster than
* fetching user data for each link separately.
*
* After we have added author (user) data to each link function will call callback
* function that has been provided to service.
*
* @param {{
* links: sails.model.link[],
* users: sails.model.user[]
* }} data
*/
function attachUsers(data) {
async.map(
data.links,
/**
* Iterator function which will attach user object to processed link object. Users
* are simply searched from user array which is fetched in main async parallel call.
*
* @param {sails.model.link} link Link object
* @param {Function} callback Callback function to call after job is done.
*/
function(link, callback) {
link.author = _.find(data.users, function(user) {
return user.id === link.createdUserId;
});
callback(null, link);
},
/**
* Main callback function which is called after all links are mapped.
*
* @param {null} error In this case error is always null
* @param {sails.model.link[]} links Processed links
*/
function(error, links) {
next(error, links);
}
)
}
};
/**
* Service to fetch single user data from database.
*
* @param {Number|{}} where Used query conditions
* @param {Function} next Callback function to call after query
* @param {Boolean} [noExistsCheck] If data is not found, skip error
*/
exports.getUser = function(where, next, noExistsCheck) {
noExistsCheck = noExistsCheck || false;
User
.findOne(where)
.exec(function(error, /** sails.model.user */ user) {
if (error) {
sails.log.error(__filename + ":" + __line + " [Failed to fetch user data]");
sails.log.error(error);
} else if (!user && !noExistsCheck) {
error = new Error();
error.message = "User not found.";
error.status = 404;
}
next(error, user);
});
};
/**
* Service to fetch specified user sign in data from database.
*
* @param {Number} userId User id
* @param {Function} next Callback function to call after query
*/
exports.getUserSignInData = function(userId, next) {
UserLogin
.find()
.where({userId: userId})
.sort("stamp DESC")
.exec(function(error, /** sails.model.userLogin[] */ userLogin) {
if (error) {
sails.log.error(__filename + ":" + __line + " [Failed to fetch user login data]");
sails.log.error(error);
}
next(error, userLogin);
});
};
/**
* Service to fetch user data from database.
*
* @param {{}} where Used query conditions
* @param {Function} next Callback function to call after query
*/
exports.getUsers = function(where, next) {
User
.find()
.where(where)
.sort("lastName ASC")
.sort("firstName ASC")
.sort("username ASC")
.exec(function(error, users) {
if (error) {
sails.log.error(__filename + ":" + __line + " [Failed to fetch user data]");
sails.log.error(error);
}