const Middleware = require('libflitter/middleware/Middleware')

/*
 * UserRoute Middleware
 * -------------------------------------------------------------
 * Put some description here!
 */
class UserRoute extends Middleware {
    static get services() {
        return [...super.services, 'models']
    }

    /*
     * Run the middleware test.
     * This method is required by all Flitter middleware.
     * It should either call the next function in the stack,
     * or it should handle the response accordingly.
     */
    async test(req, res, next, args = {}){
        const User = this.models.get('auth:User')
        const user_id = req.form.user_id ? req.form.user_id : req.params.user_id

        if ( !user_id ) return res.status(400).message('Midding user_id.').api({})

        const user = await User.findById(user_id)
        if ( !user ) return res.status(404).message('Unable to find user with that ID.').api({})

        if ( !req.form ) req.form = {}
        req.form.user = user

        next()
    }
}

module.exports = exports = UserRoute
