All files / src/services/translators NearbyDriversTranslator.js

97.37% Statements 37/38
95.45% Branches 21/22
100% Functions 3/3
97.37% Lines 37/38
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    1x   1x 1x 1x   1x       5x 1x     4x   4x 1x     3x 8x         16x 1x     15x 1x     14x   14x 1x     13x   13x       13x         13x 46x 1x     45x   45x 1x     44x   102x 1x     101x 1x     100x         100x 25x           9x            
'use es6';
 
import {List} from 'immutable';
 
import Distance from '../../data/Distance';
import DistanceUnit from '../../data/DistanceUnit';
import NearbyDriver from '../../data/NearbyDriver';
 
import DistanceCalculator from '../calculators/DistanceCalculator';
 
export default class NearbyDriversTranslator {
  static translate(response, location) {
    if (!('nearby_drivers' in response)) {
      throw new ReferenceError('expected nearby_drivers field');
    }
 
    const nearbyDrivers = response.nearby_drivers;
 
    if (!Array.isArray(nearbyDrivers)) {
      throw new TypeError('expected nearby_drivers to be an array');
    }
 
    return List(nearbyDrivers.map((nearbyDriver) => {
      return NearbyDriversTranslator.translateNearbyDriver(nearbyDriver, location.coordinate);
    }));
  }
 
  static translateNearbyDriver(nearbyDriver, coordinate) {
    if (!('ride_type' in nearbyDriver)) {
      throw new ReferenceError('expected ride_type field');
    }
 
    if (!('drivers' in nearbyDriver)) {
      throw new ReferenceError('expected drivers field');
    }
 
    const rideType = nearbyDriver.ride_type;
 
    if (typeof rideType !== 'string') {
      throw new TypeError('expected ride_type to be a string');
    }
 
    const drivers = nearbyDriver.drivers;
 
    Iif (!Array.isArray(drivers)) {
      throw new TypeError('expected drivers to be an array');
    }
 
    let closestDistance = new Distance({
      value: Infinity,
      unit: DistanceUnit.MILE,
    });
 
    drivers.forEach((driver) => {
      if (!('locations' in driver)) {
        throw new ReferenceError('expected locations field');
      }
 
      const locations = driver.locations;
 
      if (!Array.isArray(locations)) {
        throw new TypeError('expected locations to be an array');
      }
 
      locations.forEach((location) => {
 
        if (!('lat' in location)) {
          throw new ReferenceError('expected lat field');
        }
 
        if (!('lng' in location)) {
          throw new ReferenceError('expected lng field');
        }
 
        const distance = DistanceCalculator.calculateDistance(coordinate, {
          latitude: location.lat,
          longitude: location.lng,
        });
 
        if (distance.value < closestDistance.value) {
          closestDistance = distance;
        }
      });
 
    });
 
    return new NearbyDriver({
      rideType: rideType,
      distance: closestDistance,
    });
  }
}