/
job.listings.controller.js
202 lines (159 loc) · 6.55 KB
/
job.listings.controller.js
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
'use strict';
var mongoose = require('mongoose'),
_ = require('lodash'),
JobListing = mongoose.model('JobListing'),
validationErrorHandler = require('../lib/validationErrorHandler.js'),
values = require('../config/values.js');
exports.index = function(req, res) {
req.assert('tableState.limit', 'Limit is not a valid param').optional().isInt().lte(10000);
req.assert('tableState.page', 'Page is not a valid param').optional().isInt().lte(10000);
req.assert('tableState.author', 'Author is not a valid param').optional().isMongoId();
var errors = req.validationErrors();
if (errors) return res.status(400).send(validationErrorHandler(errors));
var tableState = req.query.tableState || {};
var query = JobListing.pagination(tableState);
// We allow filter on author ID
if (tableState.author) query.where('author', tableState.author);
if (tableState.active) query.where('active', tableState.active);
if (tableState.email) query.where('email', tableState.email);
if (tableState.location) query.where('location.name', tableState.location);
if (tableState.jobType) query.where('jobType', {
"$in" : Array.isArray(tableState.jobType) ? tableState.jobType : [tableState.jobType]
});
if (tableState.vesselType) query.where('vesselType', tableState.vesselType);
if (tableState.position) query.where('position', tableState.position);
// Only admin and owner can see nonactive listings.
var isAdmin = req.user && req.user.isAdmin();
var isOwner = req.user && tableState.author && req.user._id.toString() === tableState.author.toString();
if (!(isAdmin || isOwner)) {
query.where('active', true);
}
// Only allow authenticated users to see protect fields email and phone number
query.select('-__v -kind');
if (!(req.user && req.user.isAuthenticated())) query.select('-email -phone');
// Location proximity search
// The proximity search sorts so we don't want to override it
if (tableState.longitude && tableState.latitude) {
query.where('location.coordinates', {
$near : {
$geometry: {
type: "Point",
coordinates: [tableState.longitude, tableState.latitude]
}
}
});
} else {
query.sort(tableState.order || '-updated');
}
query
.exec(function(err, result) {
if (err) return res.status(400).send(validationErrorHandler(err, true));
res.json(result);
});
};
exports.autocomplete = function(req, res) {
req.assert('field', 'Fields are limited to name and email').inArray(['name', 'email', 'location.name']);
req.assert('q', 'Search query string is required').notEmpty();
var errors = req.validationErrors();
if (errors) return res.status(400).send(validationErrorHandler(errors));
var field = req.params.field,
searchString = req.query.q.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
var match = new RegExp(searchString, 'i');
var rules = {};
rules[field] = match;
JobListing.aggregate([
{ $match: rules },
{ $limit: 10 },
{ $group: {
_id: '$' + field
}}
], function(err, results) {
if (err) return res.status(400).send(validationErrorHandler(err, true));
results = results.map(function(result) {
return result._id;
});
res.json(results);
});
};
exports.detail = function(req, res) {
res.json(req.app.locals.jobListing);
};
exports.create = function(req, res) {
var data = req.body;
data.author = req.user._id;
var jobListing = new JobListing(data);
jobListing.save(function(err) {
if (err) return res.status(400).send(validationErrorHandler(err, true));
res.json(jobListing);
});
};
exports.update = function(req, res) {
var jobListing = req.app.locals.jobListing;
// Protect information
delete req.body.author;
delete req.body.__v;
delete req.body._id;
delete jobListing.kind;
_.assignIn(jobListing, req.body);
jobListing.save({ runValidators: true }, function(err) {
if (err) return res.status(400).send(validationErrorHandler(err, true));
res.json(jobListing);
});
};
exports.remove = function(req, res) {
var jobListing = req.app.locals.jobListing;
jobListing.delete(function(err) {
if (err) return res.status(400).send(validationErrorHandler(err, true));
res.json(true);
})
};
exports.jobListingById = function(req, res, next, id) {
var query = JobListing.findById(id);
// Only allow authenticated users to see protect fields email and phone number
query.select('-__v -kind');
if (!(req.user && req.user.isAuthenticated())) query.select('-email -phone');
query
.exec(function(err, jobListing) {
if (err) return res.status(400).send(validationErrorHandler(err, true));
req.app.locals.jobListing = jobListing;
next();
});
};
exports.count = function(req, res) {
var userId = req.query.userId;
if (_.isEmpty(userId)) {
JobListing.count({active: true}, function(err, count) {
if (err) return res.status(400).send({message: 'An error occurred', errors: [err]});
return res.json({ total: count })
});
} else {
req.assert('userId', 'Not a valid User ID').isMongoId();
var errors = req.validationErrors();
if (errors) return res.status(400).send(validationErrorHandler(errors));
var isAdmin = req.user && req.user.roles.indexOf('administrator') !== -1;
var isOwner = req.user && userId && req.user._id.toString() === userId;
JobListing
.find()
.where('author', userId)
.select('active')
.exec(function(err, listings) {
if (err) return res.status(400).send({message: 'An error occurred', errors: [err]});
var activeCount = _.countBy(listings, function(item) {
return item.active ? 'active' : 'deactivated'
});
if (!isAdmin && !isOwner) {
var count = {
total: listings.length
};
return res.json(count);
} else {
var count = {
total: listings.length,
active: activeCount.active,
deactivated: activeCount.deactivated
};
return res.json(count);
}
})
}
};