{"size": 1341, "ext": "js", "max_stars_repo_path": "back/controllers/business/viewOwnBusinessRating.js", "max_stars_repo_name": "hnlopez89/proyecto", "max_stars_repo_stars_event_min_datetime": "2020-05-09T17:31:24.000Z", "max_stars_repo_stars_event_max_datetime": "2020-05-09T17:31:24.000Z", "max_issues_repo_path": "back/controllers/business/viewOwnBusinessRating.js", "max_issues_repo_name": "hnlopez89/proyecto", "max_issues_repo_issues_event_min_datetime": "2020-08-13T22:42:24.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-27T19:00:12.000Z", "max_forks_repo_path": "back/controllers/business/viewOwnBusinessRating.js", "max_forks_repo_name": "hnlopez89/proyecto", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": 7, "max_forks_count": null, "avg_line_length": 24.3818181818, "max_line_length": 80, "alphanum_fraction": 0.5316927666}
const { getConnection } = require("../../db");

async function viewOwnBusinessRating(req, res, next) {
  let connection;
  try {
    connection = await getConnection();
    const { id } = req.params;
    const [summary] = await connection.query(
      `
    SELECT  AVG(rating) AS vote_average,
            COUNT(rating) AS count,
            BU.name
            FROM booking B
            INNER JOIN business BU on BU.id = B.id_business 
            WHERE id_business=?
    `,
      [id]
    );
    const [result] = await connection.query(
      `
            SELECT U.name,
            id_business,
            B.id,
            check_in_time,
            rating,
            rating_description,
            rating_answer
            FROM booking B LEFT OUTER JOIN users U
            ON U.id = B.id_user
            WHERE B.id_business=?
            AND B.rating >= 0
            ORDER BY B.check_in_time DESC
            `,
      [id]
    );
    if (result.length === 0) {
      const error = new Error(`Este negocio ${id} no tuvo ningún voto todavía`);
      error.httpStatus = 404;
      throw error;
    }

    res.send({
      status: "ok",
      summary: summary,
      data: result,
    });
  } catch (error) {
    next(error);
  } finally {
    if (connection) connection.release();
  }
}

module.exports = viewOwnBusinessRating;
