-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathremo.js
229 lines (211 loc) · 8.87 KB
/
remo.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
const { default: fetch } = require("node-fetch")
const fs = require('fs');
/**@class RemOnlineClient*/
class RemOnlineClient {
/**@constructor
* @param {String} apiKeyPath - path to file .JSON with apiKey in JSON ({"apiKey": "xxxxxx"})
* @param {String} apiTokenPath - path to file .JSON where will be stored apiToken (will be create automatically in specified adress)
*/
constructor(apiKeyPath, apiTokenPath) {
this._apiKeyPath = apiKeyPath;
this._apiTokenPath = apiTokenPath
this._apiKey = require(this._apiKeyPath).apiKey;
this._token = require(this._apiTokenPath).token;
this._baseUrl = 'https://api.remonline.ru/';
}
/**
* @method _request for send requests to remonline api, put apiToken in request and refreshs him if it need
* @param {Object} options object with options (method, methodsUrl, params, body, remArrValues) for request api
* @param {String} options.method ("GET" | "POST" | "PATCH" etc...)
* @param {String} options.methodsUrl url of api method ("orders/" | "branches/" etc...)
* @param {Object} [options.remArrValues] object with url params {key: [values]} make string "key=value1&key-value2..."
* @param {Object} [options.body] - object with post body
* @param {Object} [options.params] - object with url params, make string "key1=value1&key2=value2" and concat to url
* @returns {Object} object with response from remonline api
*/
async _request({ method, methodsUrl, params = "", body = "", remArrValues = "" }) {
let url = this._baseUrl + methodsUrl +
`?token=${this._token}`;
let paramsStr = "";
if (params) {
paramsStr = "&" + Object.entries(params)
.map(([key, val] = v) => `${key}=${val}`)
.join('&');
url += paramsStr;
}
let remArrParams = "";
if (remArrValues) {
let [key, arr] = Object.entries(remArrValues)[0];
remArrParams = "&" + arr.map(v => `${key}=${v}`).join('&');
url += remArrParams;
}
let options = {
method: method.toUpperCase(),
headers: { "Content-Type": "application/json" },
};
if (body) options.body = JSON.stringify(body);
let result = await fetch(encodeURI(url), options)
.then(response => {
return response.json()
})
if (result.success === false) {
result = await this._refreshToken()
.then(() => {
url = this._baseUrl + methodsUrl + `?token=${this._token}` + paramsStr + remArrParams;
return fetch(encodeURI(url), options)
})
.then(response => {
return response.json()
})
.catch(err => console.err(err))
}
return result
}
async _refreshToken() {
console.log("Получение нового токена");
await fetch(this._baseUrl + "token/new" + `?api_key=${this._apiKey}`, { method: "POST" })
.then(response => response.json())
.then(jsonResp => {
if (!jsonResp.success) {
throw new Error(jsonResp)
}
return jsonResp.token;
})
.then(token => {
try {
this._token = token;
fs.writeFileSync(this._apiTokenPath, JSON.stringify({ token: token }))
} catch (err) {
throw err
}
})
.catch(err => console.err(err))
return this
}
/**@method getBranchesList() for get list of branches from remonline api
* @returns {Array} array of branches in account
* @example //get list of branches in account
* const remo = new RemOnlineClient(__dirname + "//apiKey.json", __dirname + "//apiToken.json")
* .getBranchesList()
* .then(branchesList => console.log(branchesList))
* .catch(err => console.log(err))
*/
async getBranchesList() {
let response = await this._request({
method: "get",
methodsUrl: "branches/"
})
return response.data
}
/**@method getOrdersById(ids)
* @param {String|Number} orderIds may be Array of them
* @returns {Array} Array of orders (Promise)
* @example //get order from Remonline by id
* const Remo = new RemOnlineClient(__dirname + "//apiKey.json", __dirname + "//apiToken.json")
* .getOrdersById(7777777)
* .then(ArrayWithOrder => console.log(ArrayWithOrder))
* .catch(err => console.log(err))
*/
async getOrdersById(...ids) {
if (ids[0].length) ids = ids[0]
let response = await this._request({
method: "GET",
methodsUrl: "order/",
remArrValues: {
"ids[]": ids,
},
})
let orders = response.data;
return orders
}
/**@method getStatuses() for get list of statuses in your account
* @returns {Array} array with statuses (Promise)
* @example //get list of statuses in account
* const remo = new RemOnlineClient(__dirname + "//apiKey.json", __dirname + "//apiToken.json")
* .getStatuses()
* .then(arrayWithStatuses => console.log(arrayWithStatuses))
* .catch(err => console.log(err))
*/
async getStatuses() {
let response = await this._request({
method: "GET",
methodsUrl: "statuses/",
})
return response.data
}
/**@method getClientByPhone(...phone) for get client with specified phone(s) (only first in list)
* @param {String|Number} phone string or number, may be array of them for find client in remonline
* @returns {Object|undefined} client (object), first client of list with specified phone (Promise)
* if client not founded (or not exists) - returns undefined
* @example //get first client in founded list (find by phone number)
* const remo = new RemOnlineClient(__dirname + "//apiKey.json", __dirname + "//apiToken.json")
* .getClientByPhone(79157877757) // or "+79157877757" or other variants
* .then(client => console.log(client))
* .catch(err => console.log(err))
*/
async getClientByPhone(...phone) {
if (Array.isArray(phone[0])) phone = phone[0]
let contactData = await this._request({
method: "GET",
methodsUrl: "clients/",
remArrValues: { "phones[]": phone },
});
return contactData.data[0]
}
/**@method createClient(options) creating new client in remonline with specified name and phone
* @param {String} client.name name of new client
* @param {Number} client.phone phone number of new client
* @returns {Number} clientId (Promise)
* @example //create new client in remonline with specified name and phone
* const remo = new RemOnlineClient(__dirname + "//apiKey.json", __dirname + "//apiToken.json")
* .createClient({name: "Vasya", phone: 79157877757})
* .then(newClientId => console.log(newClientId))
* .catch(err => console.log(err))
*/
async createClient({ name, phone }) {
return await this._request({
method: "POST",
methodsUrl: "clients/",
body: {
name: name,
"phone[]": String(phone),
},
remArrValues: {
name: [name],
"phone[]": [String(phone)],
}
})
.then(response => response.data.id)
.catch(err => console.log(err))
}
/**@method createNewOrder(orderData) for create new order in remonline
* @param {Object} orderData - read documentation for build this object: https://remonline.ru/docs/api/#apisection11_22
* @returns {Number} newOrderId (Promise)
* @example //create new order in remonline in specified branch
* const remo = new RemOnlineClient(__dirname + "//apiKey.json", __dirname + "//apiToken.json")
*
* let orderData = {
*branch_id: 21464, //id of branch in remonline
*order_type: 36099, //id of order type
*kindof_good: 'iPhone', //type of good (String)
*model: 'test-test-test', // name of goods model (String)
*malfunction: 'Description', //description of malfunction (String)
*estimated_cost: '1500',
*manager_notes: 'notes from manager'
*}
* remo.createNewOrder({orderData})
* .then(newOrderId => console.log(newOrderId))
* .catch(err => console.log(err))
*/
async createNewOrder(orderData) {
let response = await this._request({
method: "POST",
methodsUrl: "order/",
params: orderData,
body: orderData
})
let order_id = response.data.id
return order_id
}
}
module.exports = RemOnlineClient