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,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 });
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user