-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jobcontroller.js
246 lines (239 loc) · 11.7 KB
/
Jobcontroller.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
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
var express = require('express');
var router = express.Router();
var bodyParser = require('body-parser');
var Job = require('./Job');
var jwt = require('jsonwebtoken');
var config = require('./config');
const time = new Date();
router.use(bodyParser.urlencoded({ extended: true }));
router.use(bodyParser.json());
//+++++++++++++++++++++++++++++++++++++++++++++++
///////////////////////////////////////////////////////////////////////////////
//////////Whole process to upload image(s) from client/////////////////////////
//declarations
//cloudinary CDN images
var cloudinary = require('cloudinary').v2;
//config
cloudinary.config({
cloud_name: config.cloud_name,
api_key: config.api_key,
api_secret: config.api_secret,
});
/////////////
var multer = require('multer');
var storage = multer.diskStorage({
destination: function (req, file, callback) {
if(config.testingData){
(file) ? console.log('Destination:::::::File::::::',file) : console.log('No file from client');
}
callback(null, __dirname + '/uploads')
},
filename: function (req, file, callback) {
if(config.testingData){
(file) ? console.log('Filename:::::::File::::::',file) : console.log('No file from client');
}
callback(null, file.fieldname + '_' + Date.now() + "_" + file.originalname);
}
});
const upload = multer({ storage: storage }).array("file");
const uploadSingle = multer({ storage: storage }).single("file");
//////to delete the file after sending it to cloud
const fs = require('fs');
let resultHandler = function (err) {
if (err) {
console.log("unlink failed", err);
} else {
console.log("file deleted");
}
}
////////////////////////////////////////////////////////////////////
// const cloudinaryUpload = file => cloudinary.uploader.upload(file);
router.post('/createJob', async function(req,res){
const time = new Date();
const token = req.headers['x-access-token'];
if(!token) return res.status(404).send({auth: false, message: 'No token provided!'});
jwt.verify(token, config.secret, function(err, decoded){
if(err) return res.status(500).send({auth: false,message: 'Failed to authenticate token.'});
if(!decoded) res.status(500).send({auth: false, message: 'Invalid token.'}); //invalid token
else if(decoded){
// then search for this user jobs by the title comming. req.headers['title']
const title = req.headers['title'];
console.log(`trying to find:${title}`);
//so we search to be sure than this user has no other job with same title.
Job.findOne({ title: title, username: decoded.usernameHive },function(err,found){
if(err){ //return res.status(500).send({ 'status': 'failed request -job Search', 'error': err });
console.log('Error on search.',err);
}
if(!found){
//cloudinary loop depending if at least one image to upload to server.
upload(req, res, function (err) {
if (err) {console.log('Err',err);}
let res_promises = req.files.map(file => new Promise((resolve,reject) => {
cloudinary.uploader.upload(file.path,{ tags: 'testMultiple'}, function(err, image){
if(err) reject(err)
else {
resolve(image.secure_url);
fs.unlink(file.path, resultHandler);
}
})
})
)
Promise.all(res_promises)
.then(result => { //result is the array holding the images as we need them.
var data = {};
data = req.body;
data.images = [...result]; //we should have the array if any now we create.
if(config.testingData){
console.log(result);
console.log('To save:');
console.log(data);
}
Job.create(data,function(err,job){
if(err) return res.status(500).send({ 'status': 'failed', 'message': err });
if(job){
//job was created...al went well plug&pray
if(config.testingData){ console.log(`Job created.\nDateTime:${time}.\nTitle:${job.title}\nId:${job.id}.`)};
console.log('Sending to client:');
console.log(job);
return res.status(200).send({ 'status': 'success', 'data': job});
}
});
})
.catch((error) => { res.status(400).send({'status': 'failed', 'message': error})});
});
//END cloudinary loop
}else if(found){
if(config.testingData){ console.log('Job found on that title.')}
return res.status(200).send({'status': 'failed', 'message': 'job already exists with that title.'});
}
});
}
});
});
///update/edit a job
router.post('/updateJob', function(req,res){
const token = req.headers['x-access-token'];
const job_id = req.headers['job_id'];
const operation = req.headers['operation']; //as 'edit' || 'delete'
if(!job_id) return res.status(404).send({status: 'failed', message: 'No Job_ID provided!'});
if(!operation) return res.status(404).send({status: 'failed', message: 'No operation provided!'});
if(!token) return res.status(404).send({auth: false, message: 'No token provided!'});
jwt.verify(token, config.secret, function(err, decoded){
if(err) return res.status(500).send({auth: false,message: 'Failed to authenticate token.'});
if(!decoded) res.status(500).send({auth: false, message: 'Invalid token.'}); //invalid token
else if(decoded){
uploadSingle(req, res, function (err) {
//TODO later as an update when project approved and released add the image editor so the user can modify the images on the job as they want.
if(err){
if(config.testingData){ console.log('Error Multer.', err )};
return res.status(500).send({ status: 'failed', message: err });
}
const data = req.body;
if(config.testingData){
console.log(`About to ${operation} On Job_id:`, job_id);
console.log('About to update data:', data);
};
//TODO send this to Oplogger.
if(operation === 'edit'){
Job.findByIdAndUpdate(job_id, data, { new: true }, function(err, updated){
if(err){
if(config.testingData){ console.log('Error updating Job.', err)};
return res.status(500).send({ status: 'failed', message: err });
}
return res.status(200).send({ status: 'sucess', message: `Job id:${updated._id} Updated Successfully. You can make more great Gigs and services.`, result: updated });
});
}else if(operation === 'delete'){
Job.findByIdAndDelete(job_id, function(err, response){
if(err){
if(config.testingData){ console.log('Error deleting Job.', err)};
return res.status(500).send({ status: 'failed', message: err });
}
return res.status(200).send({ status: 'sucess', message: `Job id:${job_id} was deleted.`, result: response} );
});
}
});
}
});
})
///END update/edit a job
/////get job title on a user - just that.
router.get('/myjoblist',function(req,res){
const time = new Date();
const token = req.headers['x-access-token'];
if(!token) return res.status(404).send({auth: false, message: 'No token provided!'});
jwt.verify(token, config.secret, function(err, decoded){
if(err) return res.status(500).send({auth: false,message: 'Failed to authenticate token.'});
if(!decoded) res.status(500).send({auth: false, message: 'Invalid token.'}); //invalid token
else if(decoded){
//bring all jobs of this user.
Job.find({ username: decoded.usernameHive }, function(err, jobs){
if(err) {console.log('Error finding jobs',err)}
if(!jobs) return res.status(200).send({ result: []});
if(jobs) return res.status(200).send({ result: jobs});
});
}
});
});
// TODO: set this a a public router just to handle all the OPEN queries.
////PUBLIC ROUTES....
//get all job PUBLIC query
// TODO add the sort option as asc = 1 || desc = -1 + field to sortBy.
router.get('/publicAllJobs',function(req,res){
const time = new Date();
Job.find({ active: true }, function(err, jobs){
if(err) {console.log('Error finding jobs',err)}
if(!jobs) return res.status(200).send({ result: []});
if(jobs) return res.status(200).send({ result: jobs});
}).sort( { createdAt: -1 })
if(config.testingData){
console.log('Public request made asking for ALL JOBS');
console.log(time);
}
});
router.get('/publicAllJobsQuery',function(req,res){
const time = new Date();
const query = JSON.parse(req.headers['query']);
const limit = Number(req.headers['limit']) || 0; //just in case no number comes from request.
if(config.testingData){
console.log('query to find on all jobs:');
console.log(query);
}
Job.find({ ...query }, function(err, jobs){
if(err) {console.log('Error finding jobs',err)}
if(!jobs) return res.status(200).send({ result: []});
if(jobs) return res.status(200).send({ result: jobs});
}).sort( { createdAt: -1 }).limit(limit)
if(config.testingData){
console.log('Public request made asking for ALL JOBS');
console.log(time);
}
});
////END PUBLIC ROUTES....
/////////////////////////
//Route for Job
//Get jobs by username
router.get('/:username', function(req, res){
//check for a valid token
var token = req.headers['x-access-token'];
if(!token) return res.status(404).send({ auth: false, message: 'No token provided!' });
jwt.verify(token, config.secret, function(err, decoded){
if(err) return res.status(500).send({ auth: false, message: 'Failed to authenticate token.' });
if(decoded){
// console.log(decoded);
//search jobs under this username
Job.find({ username: decoded.usernameHive }, function (err, docs) {
if(err) return res.status(500).send("There was a problem finding the user's Jobs." + "\n" + err);
if(!docs || docs.length <= 0) return res.status(404).send({ message: "No jobs for this user" });
res.status(200).send(docs);
if(config.testingData){
console.log('Token', token);
console.log(`Searched Jobs on DB. \n name:${decoded.usernameHive} \n Time:${time}`);
console.log('Jobs Found:',docs);
}
});
}else{
return res.status(500).send({ auth: false, message: 'Error authenticating token GET Jobs.' });
}
});
});
module.exports = router;