var jSendResponder = function(res, process) {
	try {
		var data = process();
		let body =
			{ status: "success"
			, data: process()
			};
		res.statusCode = 200;
		res.setHeader('Content-Type', 'application/json; charset=utf-8');
		res.end(JSON.stringify(body, null, "\t"));
	} catch(e) {
		let body = {};
		if (e instanceof FilteringReadError) {
			res.statusCode = 400;
			body.status = "fail";
			body.data = {};
			if (e.name) {
				body.data[e.name] = e.message;
			} else {
				body.data.error = e.message;
			}
		} else {
			res.statusCode = 500;
			body.status = "error";
			body.message = "Server error";
		}
		res.end(JSON.stringify(body, null, "\t"));
	}
}

Router.map(function () {
	this.route('api.0.json.groups', {
		path: '/api/0/json/groups',
		where: 'server',
		action: function () {
			jSendResponder(this.response, () => {
				var groupQuery = Filtering(GroupPredicates).readAndValidate(this.params.query).done().toQuery();
				var groups = GroupLib.find(groupQuery).fetch();

				_.each(groups, function(group) {
					group.link = Router.url('groupDetails', group);
				});
				return groups;
			});
		}
	});
});

Router.map(function () {
    this.route('api.0.json.venues', {
        path: '/api/0/json/venues',
        where: 'server',
        action: function () {
			jSendResponder(this.response, () => {
	            var venueQuery = Filtering(VenuePredicates).readAndValidate(this.params.query).done().toQuery();
	            var venues = Venues.find(venueQuery).fetch();
				_.each(venues, function(venue) {
					venue.link = Router.url('venueDetails', venue);
				});
				return venues;
			});
        }
    });
});

Router.map(function () {
    this.route('api.0.json.events', {
        path: '/api/0/json/events',
        where: 'server',
        action: function () {
			jSendResponder(this.response, () => {
	            var eventQuery = Filtering(EventPredicates).readAndValidate(this.params.query).done().toQuery();
	            var events = eventsFind(eventQuery).fetch();

				var result = _.map(events, function(ev) {
					var evr =
						{ id: ev._id
						, title: ev.title
						, description: ev.description
						, startLocal: ev.startLocal
						, endLocal: ev.endLocal
						, start: ev.start
						, end: ev.end
						, duration: moment(ev.end).diff(ev.start) / 60000 // Minutes
						, link: Router.url('showEvent', ev)
						, internal: ev.internal
						, room: ev.room
						};

					if (ev.venue) {
						evr.venue =
							{ id: ev.venue._id
							, name: ev.venue.name
							, loc: ev.venue.loc
							, link: Router.url('venueDetails', ev.venue)
							};
					}

					if (ev.courseId) {
						let course = Courses.findOne(ev.courseId);
						if (course) {
							evr.course =
								{ id: ev.courseId
								, name: course.name
								, link: Router.url('showCourse', course)
								};
						}
					}

					evr.groups = [];
					var groups = ev.groups || [];
					for(var groupId of groups) {
						let group = Groups.findOne(groupId);
						if (group) {
							evr.groups.push(
								{ id: group._id
								, name: group.name
								, short: group.short
								, link: Router.url('groupDetails', group)
								}
							);
						}
					};

					return evr;
				});
				return result;
			});
        }
    });
});

@@ -943,28 +943,22 @@
 ();%0A%09%09%09%09
-var groups =
+return
  GroupLi
@@ -980,52 +980,20 @@
 ry).
-fetch();%0A%0A%09%09%09%09_.each(groups, function(group)
+map(group =%3E
  %7B%0A%09
@@ -1056,27 +1056,8 @@
 %7D);%0A
-%09%09%09%09return groups;%0A
 %09%09%09%7D
@@ -1265,36 +1265,27 @@
 e, () =%3E %7B%0A%09
-            
+%09%09%09
 var venueQue
@@ -1374,32 +1374,17 @@
 );%0A%09
-            var venues =
+%09%09%09return
  Ven
@@ -1408,51 +1408,20 @@
 ry).
-fetch();%0A%09%09%09%09_.each(venues, function(venue)
+map(venue =%3E
  %7B%0A%09
@@ -1473,23 +1473,16 @@
 enue);%0A%09
-%09%09%09%7D);%0A
 %09%09%09%09retu
@@ -1489,17 +1489,24 @@
 rn venue
-s
+;%0A%09%09%09%09%7D)
 ;%0A%09%09%09%7D);
@@ -1851,20 +1851,14 @@
     
-var events =
+return
  eve
@@ -1881,61 +1881,17 @@
 ry).
-fetch();%0A%0A%09%09%09%09var result = _.map(events, function(ev)
+map(ev =%3E
  %7B%0A%09
@@ -3069,27 +3069,8 @@
 %7D);%0A
-%09%09%09%09return result;%0A
 %09%09%09%7D

simplify with map()