forked from resource-watch/resource-watch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubscriptions.js
165 lines (154 loc) · 5.1 KB
/
subscriptions.js
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import WRISerializer from 'wri-json-api-serializer';
// utils
import { WRIAPI } from 'utils/axios';
import { logger } from 'utils/logs';
/**
* Get Subscriptions
* Check out the API docs for this endpoint {@link https://resource-watch.github.io/doc-api/index-rw.html#obtain-the-subscriptions-for-a-user|here}
* @param {String} token User's token
* @param {Object} params request paremeters to API.
*/
export const fetchSubscriptions = (token, params) => {
logger.info('Fetch subscriptions');
return WRIAPI.get('subscriptions', {
headers: {
...WRIAPI.defaults.headers,
Authorization: token
},
params: {
application: process.env.APPLICATIONS,
env: process.env.API_ENV,
...params
}
})
.then((response) => {
const { status, statusText, data } = response;
if (status >= 300) {
logger.error('Error fetching subscriptions:', `${status}: ${statusText}`);
throw new Error(statusText);
}
return WRISerializer(data);
})
.catch(({ response = {} }) => {
const { status, statusText } = response;
logger.error(`Error fetching subscriptions: ${status}: ${statusText}`);
throw new Error(`Error fetching subscriptions: ${status}: ${statusText}`);
});
};
/**
* Creates a subscription for an area
* Check out the API docs for this endpoint {@link https://resource-watch.github.io/doc-api/index-rw.html#with-an-area|here}
* @param {Object} options
*/
export const createSubscriptionToArea = ({
areaId,
datasets,
datasetsQuery,
user,
language,
name = ''
}) => {
logger.info(`Create subscription to area: ${areaId}`);
const bodyObj = {
name,
application: process.env.APPLICATIONS,
env: process.env.API_ENV,
language: language || 'en',
datasets,
datasetsQuery,
resource: {
type: 'EMAIL',
content: user.email
},
params: { area: areaId }
};
return WRIAPI.post('subscriptions',
bodyObj,
{ headers: { Authorization: user.token } })
.catch(({ response }) => {
const { status, statusText } = response;
logger.error(`Error creating subscription to area ${areaId}: ${status}: ${statusText}`);
throw new Error(`Error creating subscription to area ${areaId}: ${statusText}`);
});
};
/**
* Update Subscription
* Check out the API docs for this endpoint {@link https://resource-watch.github.io/doc-api/index-rw.html#modify-subscription|here}
* @param {String} subscriptionId
* @param {*} datasets
* @param {*} datasetsQuery
* @param {Object} user
* @param {String} language
* @param {String} areaId
*/
export const updateSubscriptionToArea = (
subscriptionId,
datasets,
datasetsQuery,
user,
language,
areaId
) => {
logger.info(`Update subscription: ${subscriptionId}`);
const bodyObj = {
application: process.env.APPLICATIONS,
env: process.env.API_ENV,
language: language || 'en',
datasets,
datasetsQuery,
resource: {
type: 'EMAIL',
content: user.email
},
params: { area: areaId }
};
return WRIAPI.patch(`subscriptions/${subscriptionId}`,
bodyObj,
{ headers: { Authorization: user.token } })
.catch(({ response }) => {
const { status, statusText } = response;
logger.error(`Error updating subscription ${subscriptionId}: ${status}: ${statusText}`);
throw new Error(`Error updating subscription ${subscriptionId}: ${statusText}`);
});
};
/**
* Get Subscription
* Check out the API docs for this endpoint {@link https://resource-watch.github.io/doc-api/index-rw.html#get-a-subscription-by-its-id|here}
* @param {String} subscriptionId
* @param {String} token User's token
*/
export const fetchSubscription = (subscriptionId, token) => {
logger.info(`Fetch subscription: ${subscriptionId}`);
return WRIAPI.get(`subscriptions/${subscriptionId}?application=${process.env.APPLICATIONS}&env=${process.env.API_ENV}`,
{ headers: { Authorization: token } })
.then(response => response.data)
.catch(({ response }) => {
const { status, statusText } = response;
logger.error(`Error fetching subscription ${subscriptionId}: ${status}: ${statusText}`);
throw new Error(`Error fetching subscription ${subscriptionId}: ${statusText}`);
});
};
/**
* Deletes a subscription
* Check out the API docs for this endpoint {@link https://resource-watch.github.io/doc-api/index-rw.html#delete-subscription|here}
* @param {String} subscriptionId of the subscription that will be deleted
* @param {Strign} token User's token
* @returns {Promise}
*/
export const deleteSubscription = (subscriptionId, token) => {
logger.info(`Delete subscription: ${subscriptionId}`);
return WRIAPI.delete(`subscriptions/${subscriptionId}`,
{ headers: { Authorization: token } })
.catch(({ response }) => {
const { status, statusText } = response;
logger.error(`Error deleting subscription ${subscriptionId}: ${status}: ${statusText}`);
throw new Error(`Error deleting subscription ${subscriptionId}: ${statusText}`);
});
};
export default {
fetchSubscriptions,
createSubscriptionToArea,
updateSubscriptionToArea,
fetchSubscription,
deleteSubscription
};