UNPKG

1.62 kBJavaScriptView Raw
1import fetch from 'node-fetch';
2
3import MatchingWorkflow from '../../Workflows/MatchingWorkflow';
4import { formatUsersLocations } from '../../Mapper/Geolocation';
5
6const BASE_URL = 'http://geolocations-dev.madbean.ovh';
7
8// resolvers setters
9const updateGeolocation = async (location) => {
10 const options = {
11 method: 'POST',
12 headers: {
13 'Content-Type': 'application/json',
14 Accept: 'application/json',
15 },
16 body: JSON.stringify([{
17 latitude: location.lat,
18 longitude: location.lon,
19 key: location.key,
20 }]),
21 };
22
23 // update location
24 try {
25 const updated = await fetch(`${BASE_URL}/${location.collectionID}`, options).then(data => data.json());
26 if (location.fetchCloseUsers && updated.data.success) {
27 // find close users
28 return MatchingWorkflow().execMatchingWorkflow(location, getCloseUsersLocations);
29 }
30 } catch (err) {
31 console.error(err);
32 }
33
34 return [];
35};
36
37// resolvers
38const getJSONFromRelativeURL = relativeURL => fetch(`${BASE_URL}${relativeURL}`)
39 .then(res => res.json());
40
41const getLocationByURL = relativeURL => getJSONFromRelativeURL(relativeURL)
42 .then(json => json);
43
44const getLocation = ({ id, key }) => getLocationByURL(`/${id}/${key}`);
45
46const getDistanceLocation = ({ id, key, foreignKey }) => getLocationByURL(`/${id}/${key}/${foreignKey}`);
47
48const getCloseUsersLocations = ({
49 id, key, distance, unit,
50}) =>
51 getLocationByURL(`/${id}/${key}/${distance}/${unit}`)
52 .then(json => formatUsersLocations(json.data));
53
54export default {
55 getCloseUsersLocations,
56 getDistanceLocation,
57 getLocation,
58 updateGeolocation,
59};