<commit_before>'use strict';

const DEFAULT_RADIUS_IN_KILOMETERS = 5;
const DEFAULT_PAGE_SIZE = 10;
let TouristSite = require('mongoose').model('TouristSite');
let geopositionHelper = require('./../infrastructure/geoposition-helper.js').defaultInstance;
let constants = require('./../common/constants');

class TouristSitesServices {
  getAll() {
    let promise = new Promise(function(resolve, reject) {
      TouristSite.find({})
        .then(resolve, reject);
    });

    return promise;
  }

  getForPage(page, type, pageSize) {
    pageSize = pageSize || DEFAULT_PAGE_SIZE;

    let promise = new Promise(function(resolve, reject) {
      let query;

      if (type === constants.ALL_TOURIST_SITES_TYPE) {
        query = TouristSite.find({});
      } else if (type === constants.OFFICIAL_TOURIST_SITES_TYPE) {
        query = TouristSite.find({
          isOfficial: true,
          isApprovedForVisiting: true
        });
      } else if (type === constants.UNOFFICIAL_TOURIST_SITES_TYPE) {
        query = TouristSite.find({
          isOfficial: false,
          isApprovedForVisiting: true
        });
      }

      query = query.sort({
        title: 1
      })
        .skip(page * pageSize)
        .limit(pageSize);

      query.exec()
        .then(resolve, reject);
    });

    return promise;
  }

  addTouristSite(touristSite) {
    let promise = new Promise(function(resolve, reject) {
      touristSite.ratings = [];
      touristSite.comments = [];
      touristSite.status = constants.TOURIST_SITE_STATUS_WAITING_FOR_APPROVAL;

      TouristSite.create(touristSite)
        .then(resolve, reject);
    });

    return promise;
  }

  getTouristSitesNearMe(latitude, longitude, radius) {
    radius = radius || DEFAULT_RADIUS_IN_KILOMETERS;

    let deltaInLatitudeDegrees = geopositionHelper.calculateLatitudeDegreesFromRadiusInKilometers(radius);
    let deltaInLongitudeDegrees = geopositionHelper.calculateLongitudeDegreesFromRadiusInKilometers(latitude, radius);

    let minLatitude = latitude - deltaInLatitudeDegrees;
    let maxLatitude = latitude + deltaInLatitudeDegrees;
    let minLongitude = longitude - deltaInLongitudeDegrees;
    let maxLongitude = longitude + deltaInLongitudeDegrees;

    // Use lean() to be able to attach temporary property to mongoose object.
    let promise = new Promise(function(resolve, reject) {
      TouristSite.find()
        .where('latitude')
        .lt(maxLatitude)
        .gt(minLatitude)
        .where('longitude')
        .lt(maxLongitude)
        .gt(minLongitude)
        .lean()
        .exec(function(err, data) {
          if (err || !data) {
            reject(err);
            return;
          }

          let myPosition = {<commit_msg>Added filter for approved for visiting tourist sites for near me route.<commit_after>'use strict';

const DEFAULT_RADIUS_IN_KILOMETERS = 5;
const DEFAULT_PAGE_SIZE = 10;
let TouristSite = require('mongoose').model('TouristSite');
let geopositionHelper = require('./../infrastructure/geoposition-helper.js').defaultInstance;
let constants = require('./../common/constants');

class TouristSitesServices {
  getAll() {
    let promise = new Promise(function(resolve, reject) {
      TouristSite.find({})
        .then(resolve, reject);
    });

    return promise;
  }

  getForPage(page, type, pageSize) {
    pageSize = pageSize || DEFAULT_PAGE_SIZE;

    let promise = new Promise(function(resolve, reject) {
      let query;

      if (type === constants.ALL_TOURIST_SITES_TYPE) {
        query = TouristSite.find({});
      } else if (type === constants.OFFICIAL_TOURIST_SITES_TYPE) {
        query = TouristSite.find({
          isOfficial: true,
          isApprovedForVisiting: true
        });
      } else if (type === constants.UNOFFICIAL_TOURIST_SITES_TYPE) {
        query = TouristSite.find({
          isOfficial: false,
          isApprovedForVisiting: true
        });
      }

      query = query.sort({
        title: 1
      })
        .skip(page * pageSize)
        .limit(pageSize);

      query.exec()
        .then(resolve, reject);
    });

    return promise;
  }

  addTouristSite(touristSite) {
    let promise = new Promise(function(resolve, reject) {
      touristSite.ratings = [];
      touristSite.comments = [];
      touristSite.status = constants.TOURIST_SITE_STATUS_WAITING_FOR_APPROVAL;

      TouristSite.create(touristSite)
        .then(resolve, reject);
    });

    return promise;
  }

  getTouristSitesNearMe(latitude, longitude, radius) {
    radius = radius || DEFAULT_RADIUS_IN_KILOMETERS;

    let deltaInLatitudeDegrees = geopositionHelper.calculateLatitudeDegreesFromRadiusInKilometers(radius);
    let deltaInLongitudeDegrees = geopositionHelper.calculateLongitudeDegreesFromRadiusInKilometers(latitude, radius);

    let minLatitude = latitude - deltaInLatitudeDegrees;
    let maxLatitude = latitude + deltaInLatitudeDegrees;
    let minLongitude = longitude - deltaInLongitudeDegrees;
    let maxLongitude = longitude + deltaInLongitudeDegrees;

    // Use lean() to be able to attach temporary property to mongoose object.
    let promise = new Promise(function(resolve, reject) {
      TouristSite.find({
        isApprovedForVisiting: true
      })
        .where('latitude')
        .lt(maxLatitude)
        .gt(minLatitude)
        .where('longitude')
        .lt(maxLongitude)
        .gt(minLongitude)
        .lean()
        .exec(function(err, data) {
          if (err || !data) {
            reject(err);
            return;
          }

          let myPosition = {
{'fraction_non_alphanumeric': 0.10653230160158358, 'fraction_numerical': 0.001439625697318697, 'mean_word_length': 2.8225584594222832, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '660129', 'n_tokens_mistral': 1751, 'n_tokens_neox': 1674, 'n_words': 329}