-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathbackend.js
251 lines (205 loc) · 7.69 KB
/
backend.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
var Driver = require("./models/driver").Driver,
Client = require("./models/client").Client,
driverRepository = require('./models/driver').repository,
clientRepository = require('./models/client').repository,
request = require("request"),
util = require("util"),
MessageFactory = require("./messageFactory"),
config = require('konfig')(),
_ = require('underscore');
function Backend() {
}
var backendUrl = 'http://' + config.app.BackendApiHost + ':' + config.app.BackendApiPort;
var backendApiUrl = backendUrl + '/api/v1';
function login(url, email, password, deviceId, constructor, repository, callback) {
request.post(url, { form: {email: email, password: password} }, function (error, response, body) {
// network error
if (error) return callback(error);
try {
var properties = JSON.parse(body);
util.inspect(properties, {colors: true});
} catch (e) {
console.log(e.message);
return callback(new Error("Техническая ошибка входа. Уже работаем над ней."));
}
// authentication error
if (response.statusCode !== 200) return callback(new Error(properties['error'] || body));
// set user properties
repository.get(properties.id, function(err, user) {
if (err) {
user = new constructor();
}
// case of a 2nd login with same credentials
else if (user.connected && user.deviceId !== deviceId) {
return callback(new Error("Повторный вход с указанными параметрами запрещен."));
}
_.extend(user, properties);
callback(null, user);
});
});
}
Backend.prototype.loginDriver = function(email, password, deviceId, callback) {
login(backendUrl + '/api/v1/drivers/sign_in', email, password, deviceId, Driver, driverRepository, callback);
}
Backend.prototype.loginClient = function(email, password, deviceId, callback) {
login(backendUrl + '/api/v1/sign_in', email, password, deviceId, Client, clientRepository, callback);
}
// TODO: Сделать через AMQP
Backend.prototype.signupClient = function(signupInfo, callback) {
request.post(backendUrl + '/api/v1/sign_up', { form: signupInfo }, function (error, response, body) {
// network error
if (error) return callback(error);
console.log(body);
try {
var data = JSON.parse(body);
} catch (e) {
console.log(e.message);
return callback(new Error("Техническая ошибка входа. Уже работаем над ней."));
}
console.log('Response statusCode = ' + response.statusCode);
// if response not HTTP 201 Created
if (response.statusCode !== 201) {
var apiResponse = {
error: { statusCode: response.statusCode },
data: data.errors
}
// Generate API response as expected by client app
return callback(null, null, { messageType: 'Error', apiResponse: apiResponse });
}
// set user properties
var client = new Client();
// TODO: Передать данные в конструктор и там их присвоить
_.extend(client, data.client);
callback(null, client, null);
});
}
function tripToJson(trip) {
var tripData = {};
trip.getSchema().forEach(function(prop) {
if (trip[prop]) {
tripData[prop] = trip[prop];
}
});
return tripData;
}
// TODO: Сделать через AMQP
Backend.prototype.addTrip = function(trip, callback) {
request.post(backendUrl + '/api/v1/trips', { json: {trip: tripToJson(trip)} }, function (error, response, body) {
callback(error);
});
}
// TODO: Сделать через AMQP
Backend.prototype.billTrip = function(trip, callback) {
request.post(backendUrl + '/api/v1/trips/bill', { json: {trip: tripToJson(trip)} }, function (error, response, body) {
// network error
if (error) return callback(error);
callback(null, body['fare_billed_to_card'], body['fare'], body['paid_by_card']);
});
}
// TODO: Сделать через AMQP
Backend.prototype.rateDriver = function(tripId, rating, feedback, callback) {
var payload = {
trip: { rating: rating, feedback: feedback }
};
request.put(backendUrl + '/api/v1/trips/' + tripId + '/rate_driver', { json: payload }, function (error, response, body) {
callback();
});
}
// TODO: Сделать через AMQP
Backend.prototype.rateClient = function(tripId, rating, callback) {
var payload = {
trip: { rating: rating }
};
request.put(backendUrl + '/api/v1/trips/' + tripId + '/rate_client', { json: payload }, function (error, response, body) {
callback();
});
}
// TODO: Сделать через AMQP
// apiParameters:
// { password: 'fwfweewfwe',
// mobile: '+7 (920) 213-30-56',
// email: '[email protected]' },
// apiUrl: '/clients/validate',
// apiMethod: 'POST'
Backend.prototype.apiCommand = function(client, message, callback) {
request(
{ method: message.apiMethod,
uri: backendApiUrl + message.apiUrl,
form: message.apiParameters
},
function(error, response, body) {
var apiResponse = {
error: {
statusCode: response.statusCode
}
};
if (error) {
apiResponse.error.message = error.message;
}
else if (body) {
try {
apiResponse.data = JSON.parse(body);
}
catch(e) { /* ignore */ }
}
callback(null, MessageFactory.createClientOK(client, { apiResponse: apiResponse }));
}
);
}
// TODO: Сделать через AMQP
Backend.prototype.smsTripStatusToClient = function(trip, client) {
var payload = {
driver_name: trip.driver.firstName,
driver_rating: trip.driver.rating,
trip_state: trip.state.toLowerCase(),
eta_minutes: trip.eta,
vehicle_view_id: trip.vehicleViewId
};
request.post(backendUrl + '/api/v1/clients/' + client.id + '/sms', { json: payload }, function (error, response, body) {
if (error) console.log(error);
});
}
Backend.prototype.listVehicles = function(driver, callback) {
request.get(backendUrl + '/api/v1/drivers/' + driver.id + '/vehicles', function (error, response, body) {
if (error) console.log(error);
try {
var response = JSON.parse(body);
} catch (e) {
console.log(e.message);
return callback(new Error("Техническая ошибка. Уже работаем над ней."));
}
callback(null, response.vehicles);
});
}
Backend.prototype.selectVehicle = function(driver, vehicleId, callback) {
request.put(backendUrl + '/api/v1/drivers/' + driver.id + '/select_vehicle', { json: { vehicle_id: vehicleId } }, function (error, response, body) {
// network error
if (error) return callback(error);
callback(null, body.vehicle);
});
}
Backend.prototype.getActiveFare = function(callback) {
request.get(backendUrl + '/api/v1/fares', function (error, response, body) {
if (error) console.log(error);
try {
var response = JSON.parse(body);
} catch (e) {
console.log(e.message);
return callback(new Error("Техническая ошибка."));
}
callback(null, response.fare);
});
}
Backend.prototype.requestMobileConfirmation = function(clientId) {
request.put(backendUrl + '/api/v1/clients/' + clientId + '/request_mobile_confirmation', function(error, response, body) {
});
}
Backend.prototype.clientOpenApp = function(clientId) {
request.get(backendUrl + '/api/v1/clients/' + clientId + '/open_app', function(error, response, body) {
});
}
Backend.prototype.clientRequestPickup = function(clientId, params) {
request.put(backendUrl + '/api/v1/clients/' + clientId + '/request_pickup', { json: params }, function(error, response, body) {
});
}
module.exports = new Backend();