1 | import fetch from 'node-fetch';
|
2 |
|
3 | import MatchingWorkflow from '../../Workflows/MatchingWorkflow';
|
4 | import { formatUsersLocations } from '../../Mapper/Geolocation';
|
5 |
|
6 | const BASE_URL = 'http://geolocations-dev.madbean.ovh';
|
7 |
|
8 |
|
9 | const 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 |
|
24 | try {
|
25 | const updated = await fetch(`${BASE_URL}/${location.collectionID}`, options).then(data => data.json());
|
26 | if (location.fetchCloseUsers && updated.data.success) {
|
27 |
|
28 | return MatchingWorkflow().execMatchingWorkflow(location, getCloseUsersLocations);
|
29 | }
|
30 | } catch (err) {
|
31 | console.error(err);
|
32 | }
|
33 |
|
34 | return [];
|
35 | };
|
36 |
|
37 |
|
38 | const getJSONFromRelativeURL = relativeURL => fetch(`${BASE_URL}${relativeURL}`)
|
39 | .then(res => res.json());
|
40 |
|
41 | const getLocationByURL = relativeURL => getJSONFromRelativeURL(relativeURL)
|
42 | .then(json => json);
|
43 |
|
44 | const getLocation = ({ id, key }) => getLocationByURL(`/${id}/${key}`);
|
45 |
|
46 | const getDistanceLocation = ({ id, key, foreignKey }) => getLocationByURL(`/${id}/${key}/${foreignKey}`);
|
47 |
|
48 | const getCloseUsersLocations = ({
|
49 | id, key, distance, unit,
|
50 | }) =>
|
51 | getLocationByURL(`/${id}/${key}/${distance}/${unit}`)
|
52 | .then(json => formatUsersLocations(json.data));
|
53 |
|
54 | export default {
|
55 | getCloseUsersLocations,
|
56 | getDistanceLocation,
|
57 | getLocation,
|
58 | updateGeolocation,
|
59 | };
|