All files / src/services GeocodeService.js

67.57% Statements 25/37
40% Branches 4/10
58.33% Functions 7/12
67.57% Lines 25/37
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101    1x 1x 1x   1x 1x 1x   1x     2x 2x                                 1x 1x 1x     1x       1x         12x 12x 12x     12x       12x           11x                                       11x   11x     11x               11x       11x      
'use es6';
 
import GoogleMapsClient from '@google/maps';
import CurrentLocation from 'current-location';
import dotenv from 'dotenv';
 
import Coordinate from '../data/Coordinate';
import GeocodeTranslator from './translators/GeocodeTranslator';
import CurrentLocationTranslator from './translators/CurrentLocationTranslator';
 
dotenv.load();
 
export default class GeocodeService {
  constructor() {
    this.googleMapsClient = GoogleMapsClient.createClient({
      key: process.env.GOOGLE_GEOCODE_API_KEY,
    });
  }
 
  getCurrentLocationData() {
    return new Promise((resolve, reject) => {
      CurrentLocation((error, data) => {
        if (error !== null) {
          return reject(error);
        }
 
        resolve(data);
      });
    });
  }
 
  getReverseGeocodeAddressData(coordinate) {
    return new Promise((resolve, reject) => {
      this.googleMapsClient.reverseGeocode({
        latlng: [coordinate.latitude, coordinate.longitude],
      }, (error, data) => {
        Iif (error !== null) {
          return reject(error);
        }
 
        resolve(data.json);
      });
    });
  }
 
  getGeocodeCoordinatesData(address) {
    return new Promise((resolve, reject) => {
      this.googleMapsClient.geocode({
        address: address,
      }, (error, data) => {
        Iif (error !== null) {
          return reject(error);
        }
 
        resolve(data.json);
      });
    });
  }
 
  getLocation(address) {
    Iif (!address) {
      return this.getCurrentLocationData()
        .then((response) => {
          return CurrentLocationTranslator.translate(response);
        })
        // Uncomment below for showing current location address
        // .then((coordinate) => {
        //   return this.getReverseGeocodeAddressData(coordinate)
        // })
        // .then((response) => {
        //   return GeocodeTranslator.translate(response);
        // })
        // .then((locations) => {
        //   return GeocodeService.getFirstLocation(locations);
        // })
        .catch((error) => {
          throw error;
        });
    }
 
    return this.getGeocodeCoordinatesData(address)
      .then((response) => {
        return GeocodeTranslator.translate(response);
      })
      .then((locations) => {
        return GeocodeService.getFirstLocation(locations);
      })
      .catch((error) => {
        throw error;
      });
  }
 
  static getFirstLocation(locations) {
    Iif (locations.isEmpty()) {
      throw new RangeError('no locations for address');
    }
 
    return locations.first();
  }
}