{"size": 1307, "ext": "js", "max_stars_repo_path": "src/middlewares/feedback.middleware.js", "max_stars_repo_name": "Elvis-rugamba/fan7-bn-backend", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/middlewares/feedback.middleware.js", "max_issues_repo_name": "Elvis-rugamba/fan7-bn-backend", "max_issues_repo_issues_event_min_datetime": "2020-01-21T08:49:44.000Z", "max_issues_repo_issues_event_max_datetime": "2020-02-12T14:36:18.000Z", "max_forks_repo_path": "src/middlewares/feedback.middleware.js", "max_forks_repo_name": "Elvis-rugamba/fan7-bn-backend", "max_forks_repo_forks_event_min_datetime": "2020-12-14T18:59:07.000Z", "max_forks_repo_forks_event_max_datetime": "2021-03-24T15:36:13.000Z"}
{"max_stars_count": null, "max_issues_count": 12, "max_forks_count": 3, "avg_line_length": 32.675, "max_line_length": 113, "alphanum_fraction": 0.6771231829}
import 'dotenv';
import Response from '../utils/response';
import FeedbackSchema from '../modules/feedbackSchema';
import RequestRepository from '../repositories/requestRepository';
/**
 * @description FeedbackMiddleware checks if the param id is valid
 */
class FeedbackMiddleware {
  /**
   * @param {req} req object
   * @param {res} res object
   * @param {next} next forwards request to the next middleware function
   * @returns {obj} returns a response object
  */
  static async FeedbackValidate(req, res, next) {
    const { userData } = req;
    const { error } = FeedbackSchema.createSchema(req.body);
    try {
      const accommodationId = Number(req.params.id);
      const owner = await RequestRepository
        .checkIfUserCheckedInAccommodation(userData.id, accommodationId);

      if (!owner) {
        const response = new Response(res, 403, 'You are not allowed to provide feedback on this accommodation');
        return response.sendErrorMessage();
      }

      if (error) {
        const response = new Response(res, 400, error.message);
        return response.sendErrorMessage();
      }
      return next();
    } catch (err) {
      const response = new Response(res, 500, err.message);
      return response.sendErrorMessage();
    }
  }
}
export default FeedbackMiddleware;
