refactor(skydive): replace MongoDB User with MySQL UserService
All skydive routes now fetch users via UserService (MySQL) instead of
mongoose.model('User'). Key changes:
- user._id.toString() → user.id (same UUID, different source)
- User.findOne({ username }) → UserService.getUserByUsername()
- /feed route migrated to async/await using UserService.getUserFollowed()
to resolve the following list from the MySQL Follower table
- jump/file/application author set as user.id (UUID string) instead of
the full Mongoose document
- jump.toJSONFor(null) — following field in responses is now always false
until Jump model is migrated away from MongoDB User dependency
- user.controller: licence/poids/bg_image routed to SkydiverProfileService
fixing the silent Sequelize save bug for those fields
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
const { UserService } = require('../services');
|
||||
const { UserService, SkydiverProfileService } = require('../services');
|
||||
const asyncHandler = require("../middlewares/asyncHandler");
|
||||
const ErrorResponse = require("../utils/errorResponse");
|
||||
var crypto = require('crypto');
|
||||
@@ -129,33 +129,26 @@ module.exports.updateUser = asyncHandler(async (req, res, next) => {
|
||||
if (typeof req.body.user.phone !== 'undefined') {
|
||||
user.phone = req.body.user.phone;
|
||||
}
|
||||
if (typeof req.body.user.licence !== 'undefined') {
|
||||
user.licence = req.body.user.licence;
|
||||
}
|
||||
if (typeof req.body.user.poids !== 'undefined') {
|
||||
user.poids = req.body.user.poids;
|
||||
}
|
||||
if (typeof req.body.user.image !== 'undefined') {
|
||||
user.image = req.body.user.image;
|
||||
}
|
||||
if (typeof req.body.user.bg_image !== 'undefined') {
|
||||
user.bg_image = req.body.user.bg_image;
|
||||
}
|
||||
if (typeof req.body.user.role !== 'undefined') {
|
||||
return next(new ErrorResponse("Forbidden", 403, "A suspected attempt to usurp rights has been detected. The suspicious activity has been reported and xill be investigated."));
|
||||
}
|
||||
|
||||
// Persist changes using the service (service will save instance or update+fetch)
|
||||
const updatedUser = await UserService.updateUserById(user, req.payload.id);
|
||||
|
||||
const skydiverFields = {};
|
||||
if (typeof req.body.user.licence !== 'undefined') skydiverFields.licence = req.body.user.licence;
|
||||
if (typeof req.body.user.poids !== 'undefined') skydiverFields.poids = req.body.user.poids;
|
||||
if (typeof req.body.user.bg_image !== 'undefined') skydiverFields.bg_image = req.body.user.bg_image;
|
||||
if (Object.keys(skydiverFields).length > 0) {
|
||||
await SkydiverProfileService.upsert(req.payload.id, skydiverFields);
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
user: updatedUser.toAuthJSON()
|
||||
});
|
||||
/*return user.save().then(function () {
|
||||
return res.json({
|
||||
message: 'User updated successfully.',
|
||||
user: user.toAuthJSON()
|
||||
});
|
||||
});*/
|
||||
} catch (err) {
|
||||
return next(new ErrorResponse('Something went wrong while updating user.', 500, err.message));
|
||||
}
|
||||
|
||||
@@ -1,135 +0,0 @@
|
||||
var mongoose = require('mongoose');
|
||||
var uniqueValidator = require('mongoose-unique-validator');
|
||||
var crypto = require('crypto');
|
||||
const { v4: uuidv4 } = require('uuid');
|
||||
var jwt = require('jsonwebtoken'),
|
||||
config = require('../../../config');
|
||||
|
||||
var UserSchema = new mongoose.Schema({
|
||||
//_id: { type: String, default: uuidv4() },
|
||||
_id: { type: mongoose.Schema.Types.UUID },
|
||||
email: { type: String, lowercase: true, unique: true, required: [true, "email can't be blank"], match: [/\S+@\S+\.\S+/, 'is invalid'], index: true },
|
||||
username: { type: String, lowercase: true, unique: true, required: [true, "username can't be blank"], match: [/^[a-zA-Z0-9_]+$/, 'is invalid'], index: true },
|
||||
role: String,
|
||||
firstname: { type: String, required: [true, "firstname can't be blank"] },
|
||||
lastname: { type: String, required: [true, "lastname can't be blank"] },
|
||||
phone: String,
|
||||
licence: Number,
|
||||
poids: Number,
|
||||
image: String,
|
||||
bg_image: String,
|
||||
favorites: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Product' }],
|
||||
following: [{ type: mongoose.Schema.Types.UUID, ref: 'User' }],
|
||||
hash: String,
|
||||
salt: String
|
||||
}, { timestamps: true, toJSON: { virtuals: true } });
|
||||
|
||||
UserSchema.plugin(uniqueValidator, { message: 'is already taken.' });
|
||||
|
||||
UserSchema.methods.validPassword = function (password) {
|
||||
let hash = crypto.pbkdf2Sync(password, this.salt, 10000, 512, 'sha512').toString('hex');
|
||||
return this.hash === hash;
|
||||
};
|
||||
|
||||
UserSchema.methods.setPassword = function (password) {
|
||||
this.salt = crypto.randomBytes(16).toString('hex');
|
||||
this.hash = crypto.pbkdf2Sync(password, this.salt, 10000, 512, 'sha512').toString('hex');
|
||||
};
|
||||
|
||||
UserSchema.methods.generateJWT = function () {
|
||||
var today = new Date();
|
||||
var exp = new Date(today.getTime() + (config.session_lifetime * 1000));
|
||||
|
||||
return jwt.sign({
|
||||
id: this._id,
|
||||
username: this.username,
|
||||
exp: parseInt(exp.getTime() / 1000)
|
||||
}, config.secret, { algorithm: 'HS256' });
|
||||
};
|
||||
|
||||
UserSchema.methods.toAuthJSON = function () {
|
||||
return {
|
||||
id: this._id,
|
||||
email: this.email,
|
||||
role: this.role,
|
||||
token: this.generateJWT(),
|
||||
firstname: this.firstname,
|
||||
lastname: this.lastname,
|
||||
phone: this.phone,
|
||||
licence: this.licence,
|
||||
poids: this.poids,
|
||||
username: this.username,
|
||||
image: this.image || '/assets/images/avatars/default.jpg',
|
||||
bg_image: this.bg_image || '/assets/images/users/bg_user.jpg'
|
||||
};
|
||||
};
|
||||
|
||||
UserSchema.methods.toJSONFor = function () {
|
||||
return {
|
||||
email: this.email,
|
||||
role: this.role,
|
||||
firstname: this.firstname,
|
||||
lastname: this.lastname,
|
||||
phone: this.phone,
|
||||
licence: this.licence,
|
||||
poids: this.poids,
|
||||
username: this.username,
|
||||
image: this.image || '/assets/images/avatars/default.jpg',
|
||||
bg_image: this.bg_image || '/assets/images/users/bg_user.jpg'
|
||||
};
|
||||
};
|
||||
|
||||
UserSchema.methods.toProfileJSONFor = function (user) {
|
||||
return {
|
||||
email: this.email,
|
||||
firstname: this.firstname,
|
||||
lastname: this.lastname,
|
||||
phone: this.phone,
|
||||
licence: this.licence,
|
||||
poids: this.poids,
|
||||
username: this.username,
|
||||
image: this.image || '/assets/images/avatars/default.jpg',
|
||||
bg_image: this.bg_image || '/assets/images/users/bg_user.jpg',
|
||||
following: user ? user.isFollowing(this._id) : false
|
||||
};
|
||||
};
|
||||
|
||||
UserSchema.methods.favorite = function (id) {
|
||||
if (this.favorites.indexOf(id) === -1) {
|
||||
this.favorites.push(id);
|
||||
}
|
||||
|
||||
return this.save();
|
||||
};
|
||||
|
||||
UserSchema.methods.unfavorite = function (id) {
|
||||
this.favorites.remove(id);
|
||||
return this.save();
|
||||
};
|
||||
|
||||
UserSchema.methods.isFavorite = function (id) {
|
||||
return this.favorites.some(function (favoriteId) {
|
||||
return favoriteId.toString() === id.toString();
|
||||
});
|
||||
};
|
||||
|
||||
UserSchema.methods.follow = function (id) {
|
||||
if (this.following.indexOf(id) === -1) {
|
||||
this.following.push(id);
|
||||
}
|
||||
|
||||
return this.save();
|
||||
};
|
||||
|
||||
UserSchema.methods.unfollow = function (id) {
|
||||
this.following.remove(id);
|
||||
return this.save();
|
||||
};
|
||||
|
||||
UserSchema.methods.isFollowing = function (id) {
|
||||
return this.following.some(function (followId) {
|
||||
return followId.toString() === id.toString();
|
||||
});
|
||||
};
|
||||
|
||||
mongoose.model('User', UserSchema);
|
||||
@@ -1,9 +1,9 @@
|
||||
var router = require('express').Router(),
|
||||
mongoose = require('mongoose'),
|
||||
Jump = mongoose.model('Jump'),
|
||||
User = mongoose.model('User');
|
||||
Jump = mongoose.model('Jump');
|
||||
const auth = require('../../../middlewares/auth'),
|
||||
queries = require('../../queries');
|
||||
const { UserService } = require('../../../services');
|
||||
|
||||
|
||||
router.get('/allByImat', auth.required, function (req, res, next) {
|
||||
@@ -15,41 +15,31 @@ router.get('/allByImat', auth.required, function (req, res, next) {
|
||||
if (typeof req.query.offset !== 'undefined') {
|
||||
offset = req.query.offset;
|
||||
}
|
||||
|
||||
User.findById(req.payload.id).then(function (user) {
|
||||
|
||||
UserService.getUserById(req.payload.id).then(function (user) {
|
||||
if (!user) {
|
||||
return res.status(401).json({ errors: { "Unauthorized": "autentification requise" } });
|
||||
}
|
||||
let query = queries.getAeronefsByImatQuery(user._id.toString());
|
||||
let query = queries.getAeronefsByImatQuery(user.id);
|
||||
Promise.all([
|
||||
Jump.aggregate(query)
|
||||
.skip(Number(offset))
|
||||
.limit(Number(limit))
|
||||
.exec()
|
||||
//Aeronef.count(query)
|
||||
]).then(function (results) {
|
||||
let aeronefs = results[0];
|
||||
//let aeronefsCount = results[1];
|
||||
return res.json({
|
||||
aeronefs: aeronefs.map(function (data) {
|
||||
return { aeronef: data._id.aeronef, imat: data._id.imat, count: data.count };
|
||||
}),
|
||||
aeronefsCount: aeronefs.length
|
||||
});
|
||||
/*
|
||||
return res.json({
|
||||
aeronefs: aeronefs.map(function (aeronef) {
|
||||
return aeronef.toJSONFor(user);
|
||||
}),
|
||||
aeronefsCount: aeronefsCount
|
||||
});
|
||||
*/
|
||||
}).catch(next);
|
||||
}).catch(next);
|
||||
});
|
||||
|
||||
router.get('/allByImatByYear', auth.required, function (req, res, next) {
|
||||
User.findById(req.payload.id).then(function (user) {
|
||||
UserService.getUserById(req.payload.id).then(function (user) {
|
||||
if (!user) {
|
||||
return res.status(401).json({ errors: { "Unauthorized": "autentification requise" } });
|
||||
}
|
||||
@@ -61,7 +51,7 @@ router.get('/allByImatByYear', auth.required, function (req, res, next) {
|
||||
if (typeof req.query.offset !== 'undefined') {
|
||||
offset = req.query.offset;
|
||||
}
|
||||
let query = queries.getAeronefsByImatByYearQuery(user._id.toString());
|
||||
let query = queries.getAeronefsByImatByYearQuery(user.id);
|
||||
|
||||
Jump.aggregate(query)
|
||||
.skip(Number(offset))
|
||||
@@ -77,67 +67,6 @@ router.get('/allByImatByYear', auth.required, function (req, res, next) {
|
||||
});
|
||||
}).catch(next);
|
||||
}).catch(next);
|
||||
/*
|
||||
let limit = 50;
|
||||
let offset = 0;
|
||||
if (typeof req.query.limit !== 'undefined') {
|
||||
limit = req.query.limit;
|
||||
}
|
||||
if (typeof req.query.offset !== 'undefined') {
|
||||
offset = req.query.offset;
|
||||
}
|
||||
let query = [
|
||||
{
|
||||
'$match': {
|
||||
'$expr': {
|
||||
'$eq': [ '$author' , { '$toObjectId': user._id.toString() } ]
|
||||
}
|
||||
}
|
||||
}, {
|
||||
'$project': {
|
||||
'_id': 0,
|
||||
'aeronef': 1,
|
||||
'imat': 1,
|
||||
'year': {
|
||||
'$year': '$date'
|
||||
}
|
||||
}
|
||||
}, {
|
||||
'$group': {
|
||||
'_id': {
|
||||
'aeronef': '$aeronef',
|
||||
'imat': '$imat',
|
||||
'year': '$year'
|
||||
},
|
||||
'count': {
|
||||
'$sum': 1
|
||||
}
|
||||
}
|
||||
}, {
|
||||
'$sort': {
|
||||
'_id': 1
|
||||
}
|
||||
}
|
||||
];
|
||||
Promise.all([
|
||||
req.payload ? User.findById(req.payload.id) : null,
|
||||
Jump.aggregate(query)
|
||||
.skip(Number(offset))
|
||||
.limit(Number(limit))
|
||||
.exec()
|
||||
]).then(function (results) {
|
||||
let user = results[0];
|
||||
let aeronefs = results[1];
|
||||
if (!user) {
|
||||
return res.status(401).json({ errors: { "Unauthorized": "autentification requise" } });
|
||||
}
|
||||
return res.json({
|
||||
aeronefs: aeronefs,
|
||||
aeronefsCount: aeronefs.length
|
||||
});
|
||||
}).catch(next);
|
||||
*/
|
||||
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
var router = require('express').Router(),
|
||||
mongoose = require('mongoose'),
|
||||
Application = mongoose.model('Application'),
|
||||
User = mongoose.model('User');
|
||||
Application = mongoose.model('Application');
|
||||
const auth = require('../../../middlewares/auth'),
|
||||
mailer = require('@sendgrid/mail');
|
||||
const { UserService } = require('../../../services');
|
||||
|
||||
mailer.setApiKey(process.env.SENDGRID_API_KEY);
|
||||
|
||||
@@ -42,10 +42,10 @@ router.get('/', auth.required, function (req, res, next) {
|
||||
}
|
||||
|
||||
Promise.all([
|
||||
req.query.author ? User.findOne({ username: req.query.author }) : null
|
||||
req.query.author ? UserService.getUserByUsername(req.query.author) : null
|
||||
]).then(function (author) {
|
||||
if (author[0]) {
|
||||
query.author = author[0]._id;
|
||||
query.author = author[0].id;
|
||||
}
|
||||
return Promise.all([
|
||||
Application.find(query)
|
||||
@@ -55,7 +55,7 @@ router.get('/', auth.required, function (req, res, next) {
|
||||
.populate('author')
|
||||
.exec(),
|
||||
Application.count(query).exec(),
|
||||
req.payload ? User.findById(req.payload.id) : null
|
||||
req.payload ? UserService.getUserById(req.payload.id) : null
|
||||
]).then(function (results) {
|
||||
let applications = results[0];
|
||||
let applicationsCount = results[1];
|
||||
@@ -76,7 +76,7 @@ router.get('/', auth.required, function (req, res, next) {
|
||||
*/
|
||||
router.post('/', auth.required, function (req, res, next) {
|
||||
Promise.all([
|
||||
User.findById(req.payload.id),
|
||||
UserService.getUserById(req.payload.id),
|
||||
auth.generateAPIKey()
|
||||
]).then(function (results) {
|
||||
let user = results[0];
|
||||
@@ -85,7 +85,7 @@ router.post('/', auth.required, function (req, res, next) {
|
||||
return res.sendStatus(401).json({message: "Unauthorized - You are not allowed to access this resource."});
|
||||
}
|
||||
let application = new Application(req.body.application);
|
||||
application.author = user;
|
||||
application.author = user.id;
|
||||
application.apikey = keys.encrypted;
|
||||
application.maskedkey = keys.masked;
|
||||
return application.save().then(function () {
|
||||
@@ -117,7 +117,7 @@ router.post('/', auth.required, function (req, res, next) {
|
||||
*/
|
||||
router.get('/:application', auth.required, function (req, res, next) {
|
||||
Promise.all([
|
||||
req.payload ? User.findById(req.payload.id) : null,
|
||||
req.payload ? UserService.getUserById(req.payload.id) : null,
|
||||
req.application.populate('author')
|
||||
]).then(function (results) {
|
||||
let user = results[0];
|
||||
@@ -129,8 +129,9 @@ router.get('/:application', auth.required, function (req, res, next) {
|
||||
* update an application
|
||||
*/
|
||||
router.put('/:application', auth.required, function (req, res, next) {
|
||||
User.findById(req.payload.id).then(function (user) {
|
||||
if (req.application.author._id.toString() === req.payload.id.toString()) {
|
||||
UserService.getUserById(req.payload.id).then(function (user) {
|
||||
const authorId = req.application.author?.id ?? req.application.author?.toString();
|
||||
if (authorId === req.payload.id) {
|
||||
if (typeof req.body.application.title !== 'undefined') {
|
||||
req.application.title = req.body.application.title;
|
||||
}
|
||||
@@ -156,11 +157,12 @@ router.put('/:application', auth.required, function (req, res, next) {
|
||||
* delete an application
|
||||
*/
|
||||
router.delete('/:application', auth.required, function (req, res, next) {
|
||||
User.findById(req.payload.id).then(function (user) {
|
||||
UserService.getUserById(req.payload.id).then(function (user) {
|
||||
if (!user) {
|
||||
return res.status(401).json({ errors: { "Unauthorized": "autentification requise" } });
|
||||
}
|
||||
if (user.role == 'Admin' || req.application.author._id.toString() === req.payload.id.toString()) {
|
||||
const authorId = req.application.author?.id ?? req.application.author?.toString();
|
||||
if (user.role == 'Admin' || authorId === req.payload.id) {
|
||||
return req.application.remove().then(function () {
|
||||
return res.json({ deleted: true });
|
||||
});
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
var router = require('express').Router(),
|
||||
mongoose = require('mongoose'),
|
||||
Jump = mongoose.model('Jump'),
|
||||
User = mongoose.model('User');
|
||||
Jump = mongoose.model('Jump');
|
||||
const auth = require('../../../middlewares/auth'),
|
||||
queries = require('../../queries');
|
||||
const { UserService } = require('../../../services');
|
||||
|
||||
router.get('/', auth.required, function (req, res, next) {
|
||||
let query = {};
|
||||
@@ -19,7 +19,7 @@ router.get('/', auth.required, function (req, res, next) {
|
||||
query.slug = req.query.slug;
|
||||
}
|
||||
|
||||
User.findById(req.payload.id).then(function (user) {
|
||||
UserService.getUserById(req.payload.id).then(function (user) {
|
||||
if (!user) {
|
||||
return res.status(401).json({ errors: { "Unauthorized": "autentification requise" } });
|
||||
}
|
||||
@@ -50,11 +50,11 @@ router.get('/allBySize', auth.required, function (req, res, next) {
|
||||
offset = req.query.offset;
|
||||
}
|
||||
|
||||
User.findById(req.payload.id).then(function (user) {
|
||||
UserService.getUserById(req.payload.id).then(function (user) {
|
||||
if (!user) {
|
||||
return res.status(401).json({ errors: { "Unauthorized": "autentification requise" } });
|
||||
}
|
||||
let query = queries.getCanopiesBySizeQuery(user._id.toString());
|
||||
let query = queries.getCanopiesBySizeQuery(user.id);
|
||||
Jump.aggregate(query)
|
||||
.skip(Number(offset))
|
||||
.limit(Number(limit))
|
||||
@@ -72,7 +72,7 @@ router.get('/allBySize', auth.required, function (req, res, next) {
|
||||
});
|
||||
|
||||
router.get('/allBySizeByYear', auth.required, function (req, res, next) {
|
||||
User.findById(req.payload.id).then(function (user) {
|
||||
UserService.getUserById(req.payload.id).then(function (user) {
|
||||
if (!user) {
|
||||
return res.status(401).json({ errors: { "Unauthorized": "autentification requise" } });
|
||||
}
|
||||
@@ -84,7 +84,7 @@ router.get('/allBySizeByYear', auth.required, function (req, res, next) {
|
||||
if (typeof req.query.offset !== 'undefined') {
|
||||
offset = req.query.offset;
|
||||
}
|
||||
let query = queries.getCanopiesBySizeByYearQuery(user._id.toString());
|
||||
let query = queries.getCanopiesBySizeByYearQuery(user.id);
|
||||
Jump.aggregate(query)
|
||||
.skip(Number(offset))
|
||||
.limit(Number(limit))
|
||||
@@ -93,7 +93,6 @@ router.get('/allBySizeByYear', auth.required, function (req, res, next) {
|
||||
let canopies = result.map(function (data) {
|
||||
return { taille: data._id.taille, year: data._id.year, count: data.count };
|
||||
});
|
||||
//console.log(dropzones);
|
||||
return res.json({
|
||||
canopies: canopies,
|
||||
canopiesCount: canopies.length
|
||||
@@ -112,11 +111,11 @@ router.get('/allBySizeByModel', auth.required, function (req, res, next) {
|
||||
offset = req.query.offset;
|
||||
}
|
||||
|
||||
User.findById(req.payload.id).then(function (user) {
|
||||
UserService.getUserById(req.payload.id).then(function (user) {
|
||||
if (!user) {
|
||||
return res.status(401).json({ errors: { "Unauthorized": "autentification requise" } });
|
||||
}
|
||||
let query = queries.getCanopiesBySizeByModelQuery(user._id.toString());
|
||||
let query = queries.getCanopiesBySizeByModelQuery(user.id);
|
||||
Jump.aggregate(query)
|
||||
.skip(Number(offset))
|
||||
.limit(Number(limit))
|
||||
@@ -134,7 +133,7 @@ router.get('/allBySizeByModel', auth.required, function (req, res, next) {
|
||||
});
|
||||
|
||||
router.get('/allBySizeByModelByYear', auth.required, function (req, res, next) {
|
||||
User.findById(req.payload.id).then(function (user) {
|
||||
UserService.getUserById(req.payload.id).then(function (user) {
|
||||
if (!user) {
|
||||
return res.status(401).json({ errors: { "Unauthorized": "autentification requise" } });
|
||||
}
|
||||
@@ -146,7 +145,7 @@ router.get('/allBySizeByModelByYear', auth.required, function (req, res, next) {
|
||||
if (typeof req.query.offset !== 'undefined') {
|
||||
offset = req.query.offset;
|
||||
}
|
||||
let query = queries.getCanopiesBySizeByModelByYearQuery(user._id.toString());
|
||||
let query = queries.getCanopiesBySizeByModelByYearQuery(user.id);
|
||||
Jump.aggregate(query)
|
||||
.skip(Number(offset))
|
||||
.limit(Number(limit))
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
var router = require('express').Router(),
|
||||
mongoose = require('mongoose'),
|
||||
Jump = mongoose.model('Jump'),
|
||||
User = mongoose.model('User');
|
||||
Jump = mongoose.model('Jump');
|
||||
const auth = require('../../../middlewares/auth'),
|
||||
queries = require('../../queries');
|
||||
const { UserService } = require('../../../services');
|
||||
|
||||
|
||||
router.get('/allByOaci', auth.required, function (req, res, next) {
|
||||
User.findById(req.payload.id).then(function (user) {
|
||||
UserService.getUserById(req.payload.id).then(function (user) {
|
||||
if (!user) {
|
||||
return res.status(401).json({ errors: { "Unauthorized": "autentification requise" } });
|
||||
}
|
||||
@@ -19,7 +19,7 @@ router.get('/allByOaci', auth.required, function (req, res, next) {
|
||||
if (typeof req.query.offset !== 'undefined') {
|
||||
offset = req.query.offset;
|
||||
}
|
||||
let query = queries.getDropZonesByOaciQuery(user._id.toString());
|
||||
let query = queries.getDropZonesByOaciQuery(user.id);
|
||||
Jump.aggregate(query)
|
||||
.skip(Number(offset))
|
||||
.limit(Number(limit))
|
||||
@@ -37,7 +37,7 @@ router.get('/allByOaci', auth.required, function (req, res, next) {
|
||||
});
|
||||
|
||||
router.get('/allByOaciByYear', auth.required, function (req, res, next) {
|
||||
User.findById(req.payload.id).then(function (user) {
|
||||
UserService.getUserById(req.payload.id).then(function (user) {
|
||||
if (!user) {
|
||||
return res.status(401).json({ errors: { "Unauthorized": "autentification requise" } });
|
||||
}
|
||||
@@ -49,7 +49,7 @@ router.get('/allByOaciByYear', auth.required, function (req, res, next) {
|
||||
if (typeof req.query.offset !== 'undefined') {
|
||||
offset = req.query.offset;
|
||||
}
|
||||
let query = queries.getDropZonesByOaciByYearQuery(user._id.toString());
|
||||
let query = queries.getDropZonesByOaciByYearQuery(user.id);
|
||||
Jump.aggregate(query)
|
||||
.skip(Number(offset))
|
||||
.limit(Number(limit))
|
||||
|
||||
@@ -2,13 +2,13 @@ var router = require('express').Router(),
|
||||
mongoose = require('mongoose'),
|
||||
Jump = mongoose.model('Jump'),
|
||||
JumpFile = mongoose.model('JumpFile'),
|
||||
User = mongoose.model('User'),
|
||||
X2DataLog = mongoose.model('X2DataLog');
|
||||
const auth = require('../../../middlewares/auth'),
|
||||
chalk = require('chalk'),
|
||||
queries = require('../../queries');
|
||||
const DateUtilities = require('../../../utils/dateUtilities');
|
||||
const utils = require('../../../utils');
|
||||
const { UserService } = require('../../../services');
|
||||
|
||||
// Preload jump objects on routes with ':jump'
|
||||
router.param('jump', function (req, res, next, slug) {
|
||||
@@ -85,13 +85,13 @@ router.get('/', auth.required, function (req, res, next) {
|
||||
}
|
||||
|
||||
Promise.all([
|
||||
req.query.author ? User.findOne({ username: req.query.author }) : null,
|
||||
req.query.favorited ? User.findOne({ username: req.query.favorited }) : null
|
||||
req.query.author ? UserService.getUserByUsername(req.query.author) : null,
|
||||
req.query.favorited ? UserService.getUserByUsername(req.query.favorited) : null
|
||||
]).then(function (users) {
|
||||
let author = users[0];
|
||||
|
||||
if (author) {
|
||||
query.author = author._id;
|
||||
query.author = author.id;
|
||||
}
|
||||
|
||||
Promise.all([
|
||||
@@ -104,33 +104,13 @@ router.get('/', auth.required, function (req, res, next) {
|
||||
.populate('x2data')
|
||||
.exec(),
|
||||
Jump.count(query).exec(),
|
||||
req.payload ? User.findById(req.payload.id) : null
|
||||
req.payload ? UserService.getUserById(req.payload.id) : null
|
||||
]).then(function (results) {
|
||||
let jumps = results[0];
|
||||
let jumpsCount = results[1];
|
||||
let user = results[2];
|
||||
/*
|
||||
Promise.all(
|
||||
jumps.map(jump => {
|
||||
if (jump.files[0] !== undefined && jump.files[0].type !== 'kml') {
|
||||
console.log( chalk.green(`Fichier ${jump.files[0].type}`))
|
||||
//console.log( chalk.green(`Suppression du fichier ${jump.files[2]._id}`))
|
||||
//return jump.removeFile(jump.files[2]._id);
|
||||
}
|
||||
return;
|
||||
})
|
||||
).then(function () {
|
||||
return res.json({
|
||||
jumps: jumps.map(function (jump) {
|
||||
return jump.toJSONFor(user);
|
||||
}),
|
||||
jumpsCount: jumpsCount
|
||||
});
|
||||
});
|
||||
*/
|
||||
return res.json({
|
||||
jumps: jumps.map(function (jump) {
|
||||
return jump.toJSONFor(user);
|
||||
return jump.toJSONFor(null);
|
||||
}),
|
||||
jumpsCount: jumpsCount
|
||||
});
|
||||
@@ -139,7 +119,7 @@ router.get('/', auth.required, function (req, res, next) {
|
||||
});
|
||||
|
||||
router.get('/allByCategorie', auth.required, function (req, res, next) {
|
||||
User.findById(req.payload.id).then(function (user) {
|
||||
UserService.getUserById(req.payload.id).then(function (user) {
|
||||
if (!user) {
|
||||
return res.status(401).json({ errors: { "Unauthorized": "autentification requise" } });
|
||||
}
|
||||
@@ -151,7 +131,7 @@ router.get('/allByCategorie', auth.required, function (req, res, next) {
|
||||
if (typeof req.query.offset !== 'undefined') {
|
||||
offset = req.query.offset;
|
||||
}
|
||||
let query = queries.getJumpsByCategoryQuery(user._id.toString());
|
||||
let query = queries.getJumpsByCategoryQuery(user.id);
|
||||
|
||||
Jump.aggregate(query)
|
||||
.skip(Number(offset))
|
||||
@@ -170,7 +150,7 @@ router.get('/allByCategorie', auth.required, function (req, res, next) {
|
||||
});
|
||||
|
||||
router.get('/allByDate', auth.required, function (req, res, next) {
|
||||
User.findById(req.payload.id).then(function (user) {
|
||||
UserService.getUserById(req.payload.id).then(function (user) {
|
||||
if (!user) {
|
||||
return res.status(401).json({ errors: { "Unauthorized": "autentification requise" } });
|
||||
}
|
||||
@@ -182,7 +162,7 @@ router.get('/allByDate', auth.required, function (req, res, next) {
|
||||
if (typeof req.query.offset !== 'undefined') {
|
||||
offset = req.query.offset;
|
||||
}
|
||||
let query = queries.getJumpsByDateQuery(user._id.toString());
|
||||
let query = queries.getJumpsByDateQuery(user.id);
|
||||
|
||||
Jump.aggregate(query)
|
||||
.skip(Number(offset))
|
||||
@@ -201,7 +181,7 @@ router.get('/allByDate', auth.required, function (req, res, next) {
|
||||
});
|
||||
|
||||
router.get('/allByDay', auth.required, function (req, res, next) {
|
||||
User.findById(req.payload.id).then(function (user) {
|
||||
UserService.getUserById(req.payload.id).then(function (user) {
|
||||
if (!user) {
|
||||
return res.status(401).json({ errors: { "Unauthorized": "autentification requise" } });
|
||||
}
|
||||
@@ -216,12 +196,12 @@ router.get('/allByDay', auth.required, function (req, res, next) {
|
||||
}
|
||||
if (typeof req.query.dateRangeStart !== 'undefined' && typeof req.query.dateRangeEnd !== 'undefined') {
|
||||
query = queries.getJumpsByDayQuery(
|
||||
user._id.toString(),
|
||||
user.id,
|
||||
req.query.dateRangeStart,
|
||||
req.query.dateRangeEnd
|
||||
);
|
||||
} else {
|
||||
query = queries.getJumpsByDayQuery(user._id.toString());
|
||||
query = queries.getJumpsByDayQuery(user.id);
|
||||
}
|
||||
|
||||
Jump.aggregate(query)
|
||||
@@ -241,7 +221,7 @@ router.get('/allByDay', auth.required, function (req, res, next) {
|
||||
});
|
||||
|
||||
router.get('/allByModule', auth.required, function (req, res, next) {
|
||||
User.findById(req.payload.id).then(function (user) {
|
||||
UserService.getUserById(req.payload.id).then(function (user) {
|
||||
if (!user) {
|
||||
return res.status(401).json({ errors: { "Unauthorized": "autentification requise" } });
|
||||
}
|
||||
@@ -253,7 +233,7 @@ router.get('/allByModule', auth.required, function (req, res, next) {
|
||||
if (typeof req.query.offset !== 'undefined') {
|
||||
offset = req.query.offset;
|
||||
}
|
||||
let query = queries.getJumpsByModuleQuery(user._id.toString());
|
||||
let query = queries.getJumpsByModuleQuery(user.id);
|
||||
|
||||
Jump.aggregate(query)
|
||||
.skip(Number(offset))
|
||||
@@ -275,7 +255,7 @@ router.get('/allByModule', auth.required, function (req, res, next) {
|
||||
* return a jump from SkydiverId API
|
||||
*/
|
||||
router.get('/allFromSkydiverIdApi', auth.required, function (req, res, next) {
|
||||
User.findById(req.payload.id).then(function (user) {
|
||||
UserService.getUserById(req.payload.id).then(function (user) {
|
||||
if (!user) {
|
||||
return res.status(401).json({ errors: { "Unauthorized": "autentification requise" } });
|
||||
}
|
||||
@@ -293,7 +273,7 @@ router.get('/allFromSkydiverIdApi', auth.required, function (req, res, next) {
|
||||
}).catch(next);
|
||||
});
|
||||
|
||||
router.get('/feed', auth.required, function (req, res, next) {
|
||||
router.get('/feed', auth.required, async function (req, res, next) {
|
||||
let limit = 25;
|
||||
let offset = 0;
|
||||
if (typeof req.query.limit !== 'undefined') {
|
||||
@@ -303,12 +283,15 @@ router.get('/feed', auth.required, function (req, res, next) {
|
||||
offset = req.query.offset;
|
||||
}
|
||||
|
||||
User.findById(req.payload.id).then(function (user) {
|
||||
try {
|
||||
const user = await UserService.getUserById(req.payload.id);
|
||||
if (!user) {
|
||||
return res.status(401).json({ errors: { "Unauthorized": "autentification requise" } });
|
||||
}
|
||||
let query = { author: { $in: user.following } };
|
||||
Promise.all([
|
||||
const following = await UserService.getUserFollowed(user);
|
||||
const followingIds = following.map(u => u.id);
|
||||
let query = { author: { $in: followingIds } };
|
||||
const [jumps, jumpsCount] = await Promise.all([
|
||||
Jump.find(query)
|
||||
.skip(Number(offset))
|
||||
.limit(Number(limit))
|
||||
@@ -317,37 +300,27 @@ router.get('/feed', auth.required, function (req, res, next) {
|
||||
.populate('x2data')
|
||||
.exec(),
|
||||
Jump.count(query)
|
||||
]).then(function (results) {
|
||||
let jumps = results[0];
|
||||
let jumpsCount = results[1];
|
||||
return res.json({
|
||||
jumps: jumps.map(function (jump) {
|
||||
return jump.toJSONFor(user);
|
||||
}),
|
||||
jumpsCount: jumpsCount
|
||||
});
|
||||
}).catch(next);
|
||||
});
|
||||
]);
|
||||
return res.json({
|
||||
jumps: jumps.map(function (jump) {
|
||||
return jump.toJSONFor(null);
|
||||
}),
|
||||
jumpsCount: jumpsCount
|
||||
});
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* return last jump
|
||||
*/
|
||||
router.get('/last', auth.required, function (req, res, next) {
|
||||
User.findById(req.payload.id).then(function (user) {
|
||||
UserService.getUserById(req.payload.id).then(function (user) {
|
||||
if (!user) {
|
||||
return res.status(401).json({ errors: { "Unauthorized": "autentification requise" } });
|
||||
}
|
||||
/*Jump.findOne({ slug: slug })
|
||||
.populate('author')
|
||||
.then(function (jump) {
|
||||
if (!jump) {
|
||||
return res.sendStatus(404);
|
||||
}
|
||||
req.jump = jump;
|
||||
return next();
|
||||
}).catch(next);*/
|
||||
Jump.find({author: user._id})
|
||||
Jump.find({author: user.id})
|
||||
.limit(1)
|
||||
.sort({ numero: 'desc' })
|
||||
.populate('author')
|
||||
@@ -362,7 +335,7 @@ router.get('/last', auth.required, function (req, res, next) {
|
||||
if (req.lastjump.author.username !== req.payload.username) {
|
||||
return res.status(403).json({ errors: { "Forbidden": "Vous ne disposez pas des autorisations suffisantes" } });
|
||||
}
|
||||
return res.json({ jump: req.lastjump.toJSONFor(user) });
|
||||
return res.json({ jump: req.lastjump.toJSONFor(null) });
|
||||
}).catch(next);
|
||||
}).catch(next);
|
||||
});
|
||||
@@ -371,11 +344,12 @@ router.get('/last', auth.required, function (req, res, next) {
|
||||
* return a jump
|
||||
*/
|
||||
router.get('/:jump', auth.required, function (req, res, next) {
|
||||
User.findById(req.payload.id).then(function (user) {
|
||||
UserService.getUserById(req.payload.id).then(function (user) {
|
||||
if (!user) {
|
||||
return res.status(401).json({ errors: { "Unauthorized": "autentification requise" } });
|
||||
}
|
||||
if (req.jump.author._id.toString() !== req.payload.id.toString()) {
|
||||
const authorId = req.jump.author?.id ?? req.jump.author?.toString();
|
||||
if (authorId !== req.payload.id) {
|
||||
return res.status(403).json({ errors: { "Forbidden": "Vous ne disposez pas des autorisations suffisantes" } });
|
||||
}
|
||||
Promise.all([
|
||||
@@ -383,7 +357,7 @@ router.get('/:jump', auth.required, function (req, res, next) {
|
||||
Jump.findOne({numero: (req.jump.numero + 1)}).exec(),
|
||||
]).then(function (results) {
|
||||
return res.json({
|
||||
jump: req.jump.toJSONFor(user),
|
||||
jump: req.jump.toJSONFor(null),
|
||||
prevJump: results[0],
|
||||
nextJump: results[1]
|
||||
});
|
||||
@@ -395,49 +369,44 @@ router.get('/:jump', auth.required, function (req, res, next) {
|
||||
* save a jump
|
||||
*/
|
||||
router.post('/', auth.required, function (req, res, next) {
|
||||
User.findById(req.payload.id).then(function (user) {
|
||||
//let numero = (results[1][0].numero + 1);
|
||||
UserService.getUserById(req.payload.id).then(function (user) {
|
||||
if (!user) {
|
||||
return res.status(401).json({ errors: { "Unauthorized": "autentification requise" } });
|
||||
}
|
||||
let jump = new Jump(req.body.jump);
|
||||
jump.author = user;
|
||||
//jump.numero = numero;
|
||||
jump.author = user.id;
|
||||
return jump.save().then(function () {
|
||||
return res.json({ jump: jump.toJSONFor(user) });
|
||||
return res.json({ jump: jump.toJSONFor(null) });
|
||||
});
|
||||
}).catch(next);
|
||||
});
|
||||
|
||||
/**
|
||||
* save a jump file
|
||||
* save jump data file
|
||||
*/
|
||||
router.post('/data/:jump', auth.required, function (req, res, next) {
|
||||
User.findById(req.payload.id).then(function (user) {
|
||||
UserService.getUserById(req.payload.id).then(function (user) {
|
||||
if (!user) {
|
||||
return res.status(401).json({ errors: { "Unauthorized": "autentification requise" } });
|
||||
}
|
||||
if (req.jump.author._id.toString() !== req.payload.id.toString()) {
|
||||
const authorId = req.jump.author?.id ?? req.jump.author?.toString();
|
||||
if (authorId !== req.payload.id) {
|
||||
return res.status(403).json({ errors: { "Forbidden": "Vous ne disposez pas des autorisations suffisantes" } });
|
||||
}
|
||||
/*
|
||||
console.log(req.jump.files);
|
||||
return res.json({ jump: req.jump.toJSONFor(user) });
|
||||
*/
|
||||
Object.assign(req.body.file, {_id: new mongoose.Types.ObjectId()});
|
||||
let file = new JumpFile(req.body.file);
|
||||
let files = req.jump.files ? req.jump.files : [];
|
||||
file.author = user;
|
||||
file.author = user.id;
|
||||
files.push(file);
|
||||
return file.save().then(function () {
|
||||
Object.assign(req.body.data, {_id: new mongoose.Types.ObjectId()});
|
||||
let x2data = new X2DataLog(req.body.data);
|
||||
x2data.author = user;
|
||||
x2data.author = user.id;
|
||||
return x2data.save().then(function () {
|
||||
req.jump.files = files;
|
||||
req.jump.x2data = x2data;
|
||||
Jump.updateOne({_id: req.jump._id}, {files: files, x2data: x2data._id}).then(function () {
|
||||
return res.json({ jump: req.jump.toJSONFor(user) });
|
||||
return res.json({ jump: req.jump.toJSONFor(null) });
|
||||
}).catch(next);
|
||||
}).catch(next);
|
||||
}).catch(next);
|
||||
@@ -448,17 +417,18 @@ router.post('/data/:jump', auth.required, function (req, res, next) {
|
||||
* save a jump file
|
||||
*/
|
||||
router.post('/file/:jump', auth.required, function (req, res, next) {
|
||||
User.findById(req.payload.id).then(function (user) {
|
||||
UserService.getUserById(req.payload.id).then(function (user) {
|
||||
if (!user) {
|
||||
return res.status(401).json({ errors: { "Unauthorized": "autentification requise" } });
|
||||
}
|
||||
if (req.jump.author._id.toString() !== req.payload.id.toString()) {
|
||||
const authorId = req.jump.author?.id ?? req.jump.author?.toString();
|
||||
if (authorId !== req.payload.id) {
|
||||
return res.status(403).json({ errors: { "Forbidden": "Vous ne disposez pas des autorisations suffisantes" } });
|
||||
}
|
||||
Object.assign(req.body.file, {_id: new mongoose.Types.ObjectId()});
|
||||
let file = new JumpFile(req.body.file);
|
||||
let files = req.jump.files ? req.jump.files : [];
|
||||
file.author = user;
|
||||
file.author = user.id;
|
||||
files.push(file);
|
||||
return file.save().then(function () {
|
||||
Jump.updateOne({_id: req.jump._id}, {files: files}).then(function () {
|
||||
@@ -469,14 +439,15 @@ router.post('/file/:jump', auth.required, function (req, res, next) {
|
||||
});
|
||||
|
||||
/**
|
||||
* save a jump file
|
||||
* save a jump kml file
|
||||
*/
|
||||
router.post('/kml/:jump', auth.required, function (req, res, next) {
|
||||
User.findById(req.payload.id).then(function (user) {
|
||||
UserService.getUserById(req.payload.id).then(function (user) {
|
||||
if (!user) {
|
||||
return res.status(401).json({ errors: { "Unauthorized": "autentification requise" } });
|
||||
}
|
||||
if (req.jump.author._id.toString() !== req.payload.id.toString()) {
|
||||
const authorId = req.jump.author?.id ?? req.jump.author?.toString();
|
||||
if (authorId !== req.payload.id) {
|
||||
return res.status(403).json({ errors: { "Forbidden": "Vous ne disposez pas des autorisations suffisantes" } });
|
||||
}
|
||||
Object.assign(req.body.file, {
|
||||
@@ -487,45 +458,9 @@ router.post('/kml/:jump', auth.required, function (req, res, next) {
|
||||
});
|
||||
let file = new JumpFile(req.body.file);
|
||||
let files = req.jump.files ? req.jump.files : [];
|
||||
file.author = user;
|
||||
file.author = user.id;
|
||||
files.push(file);
|
||||
//console.log(chalk.yellow(`jump/${req.jump.x2data.id}/kml`));
|
||||
return res.json({ file: file.toJSONForJump() });
|
||||
/*
|
||||
DateUtilities.fetchSkydiverIdApi(`jump/${req.jump.x2data.id}/kml`, {}).then(result => {
|
||||
if (result.errors === undefined) {
|
||||
console.log(`${DateUtilities.getDateTimeISOString()}`, chalk.green(`Réception de l'url du fichier kml SkydiverId`));
|
||||
} else {
|
||||
console.log(`${DateUtilities.getDateTimeISOString()}`, chalk.red(`Une erreur ${result.errors.code} '${result.errors.type}' est survenue lors de la réception de l'url du fichier kml SkydiverId : ${result.errors.name} ${result.errors.message}`));
|
||||
}
|
||||
if (result.url !== undefined) {
|
||||
console.log(chalk.green(result.url));
|
||||
} else {
|
||||
console.log(chalk.red('result.url is undefined'));
|
||||
}
|
||||
return res.json({ file: file.toJSONForJump() });
|
||||
}).catch(next);
|
||||
|
||||
|
||||
|
||||
return file.save().then(function () {
|
||||
Jump.updateOne({_id: req.jump._id}, {files: files}).then(function () {
|
||||
DateUtilities.fetchSkydiverIdApi(`jump/${req.jump.x2data.id}/kml`, {}).then(result => {
|
||||
if (result.errors === undefined) {
|
||||
console.log(`${DateUtilities.getDateTimeISOString()}`, chalk.green(`Réception de l'url du fichier kml SkydiverId`));
|
||||
} else {
|
||||
console.log(`${DateUtilities.getDateTimeISOString()}`, chalk.red(`Une erreur ${result.errors.code} '${result.errors.type}' est survenue lors de la réception de l'url du fichier kml SkydiverId : ${result.errors.name} ${result.errors.message}`));
|
||||
}
|
||||
if (result.url !== undefined) {
|
||||
console.log(result.url);
|
||||
} else {
|
||||
console.log('result.url is undefined');
|
||||
}
|
||||
return res.json({ file: file.toJSONForJump() });
|
||||
}).catch(next);
|
||||
}).catch(next);
|
||||
}).catch(next);
|
||||
*/
|
||||
}).catch(next);
|
||||
});
|
||||
|
||||
@@ -533,11 +468,12 @@ router.post('/kml/:jump', auth.required, function (req, res, next) {
|
||||
* update a jump
|
||||
*/
|
||||
router.put('/:jump', auth.required, function (req, res, next) {
|
||||
User.findById(req.payload.id).then(function (user) {
|
||||
UserService.getUserById(req.payload.id).then(function (user) {
|
||||
if (!user) {
|
||||
return res.status(401).json({ errors: { "Unauthorized": "autentification requise" } });
|
||||
}
|
||||
if (req.jump.author._id.toString() !== req.payload.id.toString()) {
|
||||
const authorId = req.jump.author?.id ?? req.jump.author?.toString();
|
||||
if (authorId !== req.payload.id) {
|
||||
return res.status(403).json({ errors: { "Forbidden": "Vous ne disposez pas des autorisations suffisantes" } });
|
||||
}
|
||||
if (typeof req.body.jump.date !== 'undefined') {
|
||||
@@ -596,7 +532,7 @@ router.put('/:jump', auth.required, function (req, res, next) {
|
||||
}
|
||||
|
||||
return req.jump.save().then(function (jump) {
|
||||
return res.json({ jump: jump.toJSONFor(user) });
|
||||
return res.json({ jump: jump.toJSONFor(null) });
|
||||
});
|
||||
}).catch(next);
|
||||
});
|
||||
@@ -605,11 +541,12 @@ router.put('/:jump', auth.required, function (req, res, next) {
|
||||
* delete a jump
|
||||
*/
|
||||
router.delete('/:jump', auth.required, function (req, res, next) {
|
||||
User.findById(req.payload.id).then(function (user) {
|
||||
UserService.getUserById(req.payload.id).then(function (user) {
|
||||
if (!user) {
|
||||
return res.status(401).json({ errors: { "Unauthorized": "autentification requise" } });
|
||||
}
|
||||
if (req.jump.author._id.toString() === req.payload.id.toString()) {
|
||||
const authorId = req.jump.author?.id ?? req.jump.author?.toString();
|
||||
if (authorId === req.payload.id) {
|
||||
return req.jump.remove().then(function () {
|
||||
return res.json({ deleted: true });
|
||||
});
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
var router = require('express').Router(),
|
||||
mongoose = require('mongoose'),
|
||||
Jump = mongoose.model('Jump'),
|
||||
User = mongoose.model('User');
|
||||
Jump = mongoose.model('Jump');
|
||||
const auth = require('../../../middlewares/auth'),
|
||||
queries = require('../../queries');
|
||||
const { UserService } = require('../../../services');
|
||||
|
||||
router.get('/aeronefs', auth.required, function (req, res, next) {
|
||||
User.findById(req.payload.id).then(function (user) {
|
||||
UserService.getUserById(req.payload.id).then(function (user) {
|
||||
if (!user) {
|
||||
return res.status(401).json({ errors: { "Unauthorized": "autentification requise" } });
|
||||
}
|
||||
@@ -27,17 +27,17 @@ router.get('/aeronefs', auth.required, function (req, res, next) {
|
||||
.populate('files')
|
||||
.populate('x2data')
|
||||
.exec(),
|
||||
Jump.aggregate(queries.getAeronefsByImatQuery(user._id.toString()))
|
||||
Jump.aggregate(queries.getAeronefsByImatQuery(user.id))
|
||||
.skip(Number(offset))
|
||||
.limit(Number(limit))
|
||||
.exec(),
|
||||
Jump.aggregate(queries.getAeronefsByImatByYearQuery(user._id.toString()))
|
||||
Jump.aggregate(queries.getAeronefsByImatByYearQuery(user.id))
|
||||
.skip(Number(offset))
|
||||
.limit(Number(limit))
|
||||
.exec()
|
||||
]).then(function (results) {
|
||||
if (results[0].length) {
|
||||
let lastjump = results[0][0].toJSONFor(user);
|
||||
let lastjump = results[0][0].toJSONFor(null);
|
||||
let aeronefsByImat = results[1].map(function (data) {
|
||||
return { aeronef: data._id.aeronef, imat: data._id.imat, count: data.count };
|
||||
});
|
||||
@@ -62,7 +62,7 @@ router.get('/aeronefs', auth.required, function (req, res, next) {
|
||||
});
|
||||
|
||||
router.get('/canopies', auth.required, function (req, res, next) {
|
||||
User.findById(req.payload.id).then(function (user) {
|
||||
UserService.getUserById(req.payload.id).then(function (user) {
|
||||
if (!user) {
|
||||
return res.status(401).json({ errors: { "Unauthorized": "autentification requise" } });
|
||||
}
|
||||
@@ -83,49 +83,36 @@ router.get('/canopies', auth.required, function (req, res, next) {
|
||||
.populate('files')
|
||||
.populate('x2data')
|
||||
.exec(),
|
||||
Jump.aggregate(queries.getCanopiesBySizeQuery(user._id.toString()))
|
||||
Jump.aggregate(queries.getCanopiesBySizeQuery(user.id))
|
||||
.skip(Number(offset))
|
||||
.limit(Number(limit))
|
||||
.exec(),
|
||||
Jump.aggregate(queries.getCanopiesBySizeByYearQuery(user._id.toString()))
|
||||
Jump.aggregate(queries.getCanopiesBySizeByYearQuery(user.id))
|
||||
.skip(Number(offset))
|
||||
.limit(Number(limit))
|
||||
.exec(),
|
||||
Jump.aggregate(queries.getCanopiesBySizeByModelQuery(user._id.toString()))
|
||||
Jump.aggregate(queries.getCanopiesBySizeByModelQuery(user.id))
|
||||
.skip(Number(offset))
|
||||
.limit(Number(limit))
|
||||
.exec(),
|
||||
Jump.aggregate(queries.getCanopiesBySizeByModelByYearQuery(user._id.toString()))
|
||||
Jump.aggregate(queries.getCanopiesBySizeByModelByYearQuery(user.id))
|
||||
.skip(Number(offset))
|
||||
.limit(Number(limit))
|
||||
.exec()
|
||||
]).then(function (results) {
|
||||
if (results[0].length) {
|
||||
let lastjump = results[0][0].toJSONFor(user);
|
||||
let lastjump = results[0][0].toJSONFor(null);
|
||||
let canopiesBySize = results[1].map(function (data) {
|
||||
return { taille: data._id.taille, count: data.count };
|
||||
});
|
||||
let canopiesBySizeByYear = results[2].map(function (data) {
|
||||
return {
|
||||
taille: data._id.taille,
|
||||
year: data._id.year,
|
||||
count: data.count
|
||||
};
|
||||
return { taille: data._id.taille, year: data._id.year, count: data.count };
|
||||
});
|
||||
let canopiesBySizeByModel = results[3].map(function (data) {
|
||||
return {
|
||||
taille: data._id.taille,
|
||||
voile: data._id.voile,
|
||||
count: data.count
|
||||
};
|
||||
return { taille: data._id.taille, voile: data._id.voile, count: data.count };
|
||||
});
|
||||
let canopiesBySizeByModelByYear = results[4].map(function (data) {
|
||||
return {
|
||||
taille: data._id.taille,
|
||||
voile: data._id.voile,
|
||||
year: data._id.year,
|
||||
count: data.count
|
||||
};
|
||||
return { taille: data._id.taille, voile: data._id.voile, year: data._id.year, count: data.count };
|
||||
});
|
||||
return res.json({
|
||||
canopiesBySize: canopiesBySize,
|
||||
@@ -142,7 +129,7 @@ router.get('/canopies', auth.required, function (req, res, next) {
|
||||
});
|
||||
|
||||
router.get('/dropzones', auth.required, function (req, res, next) {
|
||||
User.findById(req.payload.id).then(function (user) {
|
||||
UserService.getUserById(req.payload.id).then(function (user) {
|
||||
if (!user) {
|
||||
return res.status(401).json({ errors: { "Unauthorized": "autentification requise" } });
|
||||
}
|
||||
@@ -163,27 +150,22 @@ router.get('/dropzones', auth.required, function (req, res, next) {
|
||||
.populate('files')
|
||||
.populate('x2data')
|
||||
.exec(),
|
||||
Jump.aggregate(queries.getDropZonesByOaciQuery(user._id.toString()))
|
||||
Jump.aggregate(queries.getDropZonesByOaciQuery(user.id))
|
||||
.skip(Number(offset))
|
||||
.limit(Number(limit))
|
||||
.exec(),
|
||||
Jump.aggregate(queries.getDropZonesByOaciByYearQuery(user._id.toString()))
|
||||
Jump.aggregate(queries.getDropZonesByOaciByYearQuery(user.id))
|
||||
.skip(Number(offset))
|
||||
.limit(Number(limit))
|
||||
.exec()
|
||||
]).then(function (results) {
|
||||
if (results[0].length) {
|
||||
let lastjump = results[0][0].toJSONFor(user);
|
||||
let lastjump = results[0][0].toJSONFor(null);
|
||||
let dropZonesByOaci = results[1].map(function (data) {
|
||||
return { lieu: data._id.lieu, oaci: data._id.oaci, count: data.count };
|
||||
});
|
||||
let dropZonesByOaciByYear = results[2].map(function (data) {
|
||||
return {
|
||||
lieu: data._id.lieu,
|
||||
oaci: data._id.oaci,
|
||||
year: data._id.year,
|
||||
count: data.count
|
||||
};
|
||||
return { lieu: data._id.lieu, oaci: data._id.oaci, year: data._id.year, count: data.count };
|
||||
});
|
||||
return res.json({
|
||||
dropZonesByOaci: dropZonesByOaci,
|
||||
@@ -198,7 +180,7 @@ router.get('/dropzones', auth.required, function (req, res, next) {
|
||||
});
|
||||
|
||||
router.get('/jumps', auth.required, function (req, res, next) {
|
||||
User.findById(req.payload.id).then(function (user) {
|
||||
UserService.getUserById(req.payload.id).then(function (user) {
|
||||
if (!user) {
|
||||
return res.status(401).json({ errors: { "Unauthorized": "autentification requise" } });
|
||||
}
|
||||
@@ -219,30 +201,29 @@ router.get('/jumps', auth.required, function (req, res, next) {
|
||||
.populate('files')
|
||||
.populate('x2data')
|
||||
.exec(),
|
||||
Jump.aggregate(queries.getJumpByYearsQuery(user._id.toString()))
|
||||
Jump.aggregate(queries.getJumpByYearsQuery(user.id))
|
||||
.skip(Number(offset))
|
||||
.limit(Number(limit))
|
||||
.exec(),
|
||||
Jump.aggregate(queries.getJumpsByCategoryQuery(user._id.toString()))
|
||||
Jump.aggregate(queries.getJumpsByCategoryQuery(user.id))
|
||||
.skip(Number(offset))
|
||||
.limit(Number(limit))
|
||||
.exec(),
|
||||
Jump.aggregate(queries.getJumpsByModuleQuery(user._id.toString()))
|
||||
Jump.aggregate(queries.getJumpsByModuleQuery(user.id))
|
||||
.skip(Number(offset))
|
||||
.limit(Number(limit))
|
||||
.exec(),
|
||||
Jump.aggregate(queries.getJumpsByDateQuery(user._id.toString()))
|
||||
Jump.aggregate(queries.getJumpsByDateQuery(user.id))
|
||||
.skip(Number(offset))
|
||||
.limit(Number(limit))
|
||||
.exec(),
|
||||
Jump.aggregate(queries.getJumpsByModuleByDateQuery(user._id.toString()))
|
||||
Jump.aggregate(queries.getJumpsByModuleByDateQuery(user.id))
|
||||
.skip(Number(offset))
|
||||
.limit(Number(limit))
|
||||
.exec()
|
||||
//utils.fetchSkydiverIdApi('jumps')
|
||||
]).then(function (results) {
|
||||
if (results[0].length) {
|
||||
let lastjump = results[0][0].toJSONFor(user);
|
||||
let lastjump = results[0][0].toJSONFor(null);
|
||||
let jumpsByYears = results[1].map(function (data) {
|
||||
return { year: data._id.year, count: data.count };
|
||||
});
|
||||
@@ -264,14 +245,12 @@ router.get('/jumps', auth.required, function (req, res, next) {
|
||||
count: data.count
|
||||
};
|
||||
});
|
||||
//let jumpsFromSkydiverIdApi = results[6];
|
||||
return res.json({
|
||||
jumpsByCategory: jumpsByCategory,
|
||||
jumpsByDate: jumpsByDate,
|
||||
jumpsByDateByModule: jumpsByDateByModule,
|
||||
jumpsByModule: jumpsByModule,
|
||||
jumpsByYears: jumpsByYears,
|
||||
//jumpsFromSkydiverIdApi: jumpsFromSkydiverIdApi,
|
||||
lastjump: lastjump
|
||||
});
|
||||
} else {
|
||||
|
||||
@@ -3,9 +3,9 @@ var router = require('express').Router(),
|
||||
Qcm = mongoose.model('Qcm'),
|
||||
QcmCategory = mongoose.model('QcmCategory'),
|
||||
QcmQuestion = mongoose.model('QcmQuestion'),
|
||||
QcmChoice = mongoose.model('QcmChoice'),
|
||||
User = mongoose.model('User');
|
||||
QcmChoice = mongoose.model('QcmChoice');
|
||||
const auth = require('../../../middlewares/auth');
|
||||
const { UserService } = require('../../../services');
|
||||
|
||||
// Preload qcm objects on routes with ':type'
|
||||
router.param('type', function (req, res, next, type) {
|
||||
@@ -37,7 +37,7 @@ router.param('type', function (req, res, next, type) {
|
||||
*/
|
||||
router.get('/', auth.required, function (req, res, next) {
|
||||
Promise.all([
|
||||
req.payload ? User.findById(req.payload.id) : null
|
||||
req.payload ? UserService.getUserById(req.payload.id) : null
|
||||
]).then(function (results) {
|
||||
let user = results[0];
|
||||
if (!user) {
|
||||
@@ -52,7 +52,7 @@ router.get('/', auth.required, function (req, res, next) {
|
||||
*/
|
||||
router.get('/type/:type', auth.required, function (req, res, next) {
|
||||
Promise.all([
|
||||
req.payload ? User.findById(req.payload.id) : null
|
||||
req.payload ? UserService.getUserById(req.payload.id) : null
|
||||
]).then(function (results) {
|
||||
let user = results[0];
|
||||
if (!user) {
|
||||
@@ -67,14 +67,13 @@ router.get('/type/:type', auth.required, function (req, res, next) {
|
||||
*/
|
||||
router.post('/choices/', auth.required, function (req, res, next) {
|
||||
Promise.all([
|
||||
req.payload ? User.findById(req.payload.id) : null,
|
||||
req.payload ? UserService.getUserById(req.payload.id) : null,
|
||||
QcmQuestion
|
||||
.findOne({num: req.body.question.num})
|
||||
.populate('choices')
|
||||
.exec()
|
||||
]).then(async function (results) {
|
||||
const user = results[0];
|
||||
//let numero = (results[1][0].numero + 1);
|
||||
if (!user) {
|
||||
return res.status(401).json({ errors: { "Unauthorized": "autentification requise" } });
|
||||
}
|
||||
@@ -113,14 +112,13 @@ router.post('/choices/', auth.required, function (req, res, next) {
|
||||
*/
|
||||
router.post('/questions/', auth.required, function (req, res, next) {
|
||||
Promise.all([
|
||||
req.payload ? User.findById(req.payload.id) : null,
|
||||
req.payload ? UserService.getUserById(req.payload.id) : null,
|
||||
QcmCategory
|
||||
.findOne({num: req.body.category.num})
|
||||
.populate('questions')
|
||||
.exec()
|
||||
]).then(async function (results) {
|
||||
const user = results[0];
|
||||
//let numero = (results[1][0].numero + 1);
|
||||
if (!user) {
|
||||
return res.status(401).json({ errors: { "Unauthorized": "autentification requise" } });
|
||||
}
|
||||
@@ -154,4 +152,4 @@ router.post('/questions/', auth.required, function (req, res, next) {
|
||||
}).catch(next);
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
module.exports = router;
|
||||
|
||||
Reference in New Issue
Block a user