Commit 46b74188 authored by Aurélien Vermylen's avatar Aurélien Vermylen

Major rehaul of automaticapi storage: list by device and not user.

parent b7a612ea
...@@ -14498,12 +14498,12 @@ return new Parser; ...@@ -14498,12 +14498,12 @@ return new Parser;
var result = [], var result = [],
type = endpoint.split('/')[1], type = endpoint.split('/')[1],
promises, promises,
usr = filters.user || 'all', dev = filters.device || 'all',
isId = checkEndpointAsId(endpoint), isId = checkEndpointAsId(endpoint),
user_dict = {}, device_dict = {},
i; i;
// Remove 'user' filter which should not be put in Automatic request. // Remove 'device' filter which should not be put in Automatic request.
delete filters.user; delete filters.device;
// Check endpoint validity. // Check endpoint validity.
if (!checkEndpoint(endpoint)) { if (!checkEndpoint(endpoint)) {
throw new jIO.util.jIOError('Wrong Automatic API query. (usually ' + throw new jIO.util.jIOError('Wrong Automatic API query. (usually ' +
...@@ -14512,7 +14512,7 @@ return new Parser; ...@@ -14512,7 +14512,7 @@ return new Parser;
// Promise chain to handle multi-part response ("_metadata"->"next" parts). // Promise chain to handle multi-part response ("_metadata"->"next" parts).
function treatNext(returned) { function treatNext(returned) {
var data, var data,
user_id, device_id,
next; next;
// If the returned value was an error, return it (-> quite nasty design // If the returned value was an error, return it (-> quite nasty design
// to have to return errors, but this is in order for all RSVP.all() to // to have to return errors, but this is in order for all RSVP.all() to
...@@ -14521,7 +14521,7 @@ return new Parser; ...@@ -14521,7 +14521,7 @@ return new Parser;
return returned; return returned;
} }
data = returned[0]; data = returned[0];
user_id = user_dict[returned[1]]; device_id = device_dict[returned[1]];
data = [data]; data = [data];
if (!isId) { if (!isId) {
if (data[0]._metadata === undefined) { if (data[0]._metadata === undefined) {
...@@ -14536,16 +14536,16 @@ return new Parser; ...@@ -14536,16 +14536,16 @@ return new Parser;
path = URI(dat.url).path(); path = URI(dat.url).path();
temp = { temp = {
'automatic_path': path, 'automatic_path': path,
'reference': '/' + user_id + path, 'reference': '/' + device_id + path,
'id': '/' + user_id + path, 'id': '/' + device_id + path,
'type': type, 'type': type,
'start_date': dat.started_at || null, 'start_date': dat.started_at || null,
'stop_date': dat.ended_at || null, 'stop_date': dat.ended_at || null,
'automatic_user': user_id 'automatic_device': device_id
}; };
result.push(temp); result.push(temp);
return jio._cache.put('/' + user_id + path, temp).push(function () { return jio._cache.put('/' + device_id + path, temp).push(function () {
return jio._cache.putAttachment('/' + user_id + path, 'data', return jio._cache.putAttachment('/' + device_id + path, 'data',
new Blob([JSON.stringify(dat)], {type: new Blob([JSON.stringify(dat)], {type:
'text/plain'})); 'text/plain'}));
}); });
...@@ -14578,13 +14578,15 @@ return new Parser; ...@@ -14578,13 +14578,15 @@ return new Parser;
return new RSVP.Queue().push(function () { return new RSVP.Queue().push(function () {
return jIO.util.ajax({ return jIO.util.ajax({
'type': 'GET', 'type': 'GET',
'url': automatic_template.expand({endpoint: ['user', 'me', '']}), 'url': automatic_template.expand({endpoint: ['user', 'me', 'device',
'']}),
'headers': {'Authorization': 'Bearer ' + token} 'headers': {'Authorization': 'Bearer ' + token}
//'xhrFields': {withCredentials: true} //'xhrFields': {withCredentials: true}
}); });
}).push(function (respusr) { }).push(function (respdev) {
user_dict[token] = JSON.parse(respusr.target.responseText).id; device_dict[token] =
if (usr === 'all' || usr === user_dict[token]) { JSON.parse(respdev.target.responseText).results[0].id;
if (dev === 'all' || dev === device_dict[token]) {
return jIO.util.ajax({ return jIO.util.ajax({
'type': 'GET', 'type': 'GET',
'url': URI(automatic_template.expand({endpoint: 'url': URI(automatic_template.expand({endpoint:
...@@ -14610,7 +14612,7 @@ return new Parser; ...@@ -14610,7 +14612,7 @@ return new Parser;
return RSVP.all(promises); return RSVP.all(promises);
}).push(function (trueOrErrorArray) { }).push(function (trueOrErrorArray) {
// If we queried an id, return results should be length 1 // If we queried an id, return results should be length 1
if (isId && (usr !== 'all')) { if (isId && (dev !== 'all')) {
if (result.length === 1) { if (result.length === 1) {
return result[0]; return result[0];
} }
...@@ -14620,7 +14622,7 @@ return new Parser; ...@@ -14620,7 +14622,7 @@ return new Parser;
} }
// Result is empty, so we throw the correct token error. // Result is empty, so we throw the correct token error.
i = jio._access_tokens.map(function (token) { i = jio._access_tokens.map(function (token) {
if (user_dict[token] === usr) { if (device_dict[token] === dev) {
return true; return true;
} }
return false; return false;
...@@ -14630,8 +14632,8 @@ return new Parser; ...@@ -14630,8 +14632,8 @@ return new Parser;
throw trueOrErrorArray[i]; throw trueOrErrorArray[i];
} }
// If we didn't find the error in the promise returns, we don't have // If we didn't find the error in the promise returns, we don't have
// a token for user usr. // a token for device dev.
throw new jIO.util.jIOError('No valid token for user: ' + usr, 400); throw new jIO.util.jIOError('No valid token for device: ' + dev, 400);
} }
// Otherwise return results and errors and let caller handle. // Otherwise return results and errors and let caller handle.
return result; return result;
...@@ -14662,8 +14664,8 @@ return new Parser; ...@@ -14662,8 +14664,8 @@ return new Parser;
AutomaticAPIStorage.prototype.get = function (id) { AutomaticAPIStorage.prototype.get = function (id) {
var self = this, var self = this,
endpoint = id.split('/'), endpoint = id.split('/'),
usr; dev;
usr = endpoint.splice(1, 1)[0]; dev = endpoint.splice(1, 1)[0];
endpoint = endpoint.join('/'); endpoint = endpoint.join('/');
if (id.indexOf('/') !== 0) { if (id.indexOf('/') !== 0) {
throw new jIO.util.jIOError('id ' + id + throw new jIO.util.jIOError('id ' + id +
...@@ -14676,13 +14678,13 @@ return new Parser; ...@@ -14676,13 +14678,13 @@ return new Parser;
if (!checkEndpointAsId(endpoint)) { if (!checkEndpointAsId(endpoint)) {
throw new jIO.util.jIOError('Invalid id.', 400); throw new jIO.util.jIOError('Invalid id.', 400);
} }
if (usr === 'all') { if (dev === 'all') {
throw new jIO.util.jIOError('Invalid id.', 400); throw new jIO.util.jIOError('Invalid id.', 400);
} }
return this._cache.get(id).push(function (res) { return this._cache.get(id).push(function (res) {
return res; return res;
}, function () { }, function () {
return _queryAutomaticAPI(endpoint, {'user': usr}, self) return _queryAutomaticAPI(endpoint, {'device': dev}, self)
.push(function (res) { .push(function (res) {
return res; return res;
}, function (err) { }, function (err) {
...@@ -14715,8 +14717,8 @@ return new Parser; ...@@ -14715,8 +14717,8 @@ return new Parser;
AutomaticAPIStorage.prototype.getAttachment = function (id, name, options) { AutomaticAPIStorage.prototype.getAttachment = function (id, name, options) {
var self = this, var self = this,
endpoint = id.split('/'), endpoint = id.split('/'),
usr; dev;
usr = endpoint.splice(1, 1)[0]; dev = endpoint.splice(1, 1)[0];
endpoint = endpoint.join('/'); endpoint = endpoint.join('/');
if (id.indexOf('/') !== 0) { if (id.indexOf('/') !== 0) {
throw new jIO.util.jIOError('id ' + id + throw new jIO.util.jIOError('id ' + id +
...@@ -14729,7 +14731,7 @@ return new Parser; ...@@ -14729,7 +14731,7 @@ return new Parser;
if (!checkEndpointAsId(endpoint)) { if (!checkEndpointAsId(endpoint)) {
throw new jIO.util.jIOError('Invalid id.', 400); throw new jIO.util.jIOError('Invalid id.', 400);
} }
if (usr === 'all') { if (dev === 'all') {
throw new jIO.util.jIOError('Invalid id.', 400); throw new jIO.util.jIOError('Invalid id.', 400);
} }
return this._cache.get(id).push(function () { return this._cache.get(id).push(function () {
...@@ -14759,8 +14761,8 @@ return new Parser; ...@@ -14759,8 +14761,8 @@ return new Parser;
AutomaticAPIStorage.prototype.allAttachments = function (id) { AutomaticAPIStorage.prototype.allAttachments = function (id) {
var endpoint = id.split('/'), var endpoint = id.split('/'),
usr; dev;
usr = endpoint.splice(1, 1)[0]; dev = endpoint.splice(1, 1)[0];
endpoint = endpoint.join('/'); endpoint = endpoint.join('/');
if (id.indexOf('/') !== 0) { if (id.indexOf('/') !== 0) {
throw new jIO.util.jIOError('id ' + id + throw new jIO.util.jIOError('id ' + id +
...@@ -14773,7 +14775,7 @@ return new Parser; ...@@ -14773,7 +14775,7 @@ return new Parser;
if (!checkEndpointAsId(endpoint)) { if (!checkEndpointAsId(endpoint)) {
throw new jIO.util.jIOError('Invalid id.', 400); throw new jIO.util.jIOError('Invalid id.', 400);
} }
if (usr === 'all') { if (dev === 'all') {
throw new jIO.util.jIOError('Invalid id.', 400); throw new jIO.util.jIOError('Invalid id.', 400);
} }
return {data: null}; return {data: null};
...@@ -14793,9 +14795,9 @@ return new Parser; ...@@ -14793,9 +14795,9 @@ return new Parser;
key_list, key_list,
automatic_filters = {}, automatic_filters = {},
simplequery_type_value, simplequery_type_value,
intercept_keys = ['start_date', 'stop_date', 'automatic_user', intercept_keys = ['start_date', 'stop_date', 'automatic_device',
'vehicle'], 'vehicle'],
intercept_keys_automatic_name = ['started_at', 'ended_at', 'user', intercept_keys_automatic_name = ['started_at', 'ended_at', 'device',
'vehicle'], 'vehicle'],
intercept_accepted_operators = [['>', '>=', '<', '<='], intercept_accepted_operators = [['>', '>=', '<', '<='],
['>', '>=', '<', '<='], ['='], ['=']], ['>', '>=', '<', '<='], ['='], ['=']],
......
This source diff could not be displayed because it is too large. You can view the blob instead.
...@@ -56,12 +56,12 @@ ...@@ -56,12 +56,12 @@
var result = [], var result = [],
type = endpoint.split('/')[1], type = endpoint.split('/')[1],
promises, promises,
usr = filters.user || 'all', dev = filters.device || 'all',
isId = checkEndpointAsId(endpoint), isId = checkEndpointAsId(endpoint),
user_dict = {}, device_dict = {},
i; i;
// Remove 'user' filter which should not be put in Automatic request. // Remove 'device' filter which should not be put in Automatic request.
delete filters.user; delete filters.device;
// Check endpoint validity. // Check endpoint validity.
if (!checkEndpoint(endpoint)) { if (!checkEndpoint(endpoint)) {
throw new jIO.util.jIOError('Wrong Automatic API query. (usually ' + throw new jIO.util.jIOError('Wrong Automatic API query. (usually ' +
...@@ -70,7 +70,7 @@ ...@@ -70,7 +70,7 @@
// Promise chain to handle multi-part response ("_metadata"->"next" parts). // Promise chain to handle multi-part response ("_metadata"->"next" parts).
function treatNext(returned) { function treatNext(returned) {
var data, var data,
user_id, device_id,
next; next;
// If the returned value was an error, return it (-> quite nasty design // If the returned value was an error, return it (-> quite nasty design
// to have to return errors, but this is in order for all RSVP.all() to // to have to return errors, but this is in order for all RSVP.all() to
...@@ -79,7 +79,7 @@ ...@@ -79,7 +79,7 @@
return returned; return returned;
} }
data = returned[0]; data = returned[0];
user_id = user_dict[returned[1]]; device_id = device_dict[returned[1]];
data = [data]; data = [data];
if (!isId) { if (!isId) {
if (data[0]._metadata === undefined) { if (data[0]._metadata === undefined) {
...@@ -94,16 +94,16 @@ ...@@ -94,16 +94,16 @@
path = URI(dat.url).path(); path = URI(dat.url).path();
temp = { temp = {
'automatic_path': path, 'automatic_path': path,
'reference': '/' + user_id + path, 'reference': '/' + device_id + path,
'id': '/' + user_id + path, 'id': '/' + device_id + path,
'type': type, 'type': type,
'start_date': dat.started_at || null, 'start_date': dat.started_at || null,
'stop_date': dat.ended_at || null, 'stop_date': dat.ended_at || null,
'automatic_user': user_id 'automatic_device': device_id
}; };
result.push(temp); result.push(temp);
return jio._cache.put('/' + user_id + path, temp).push(function () { return jio._cache.put('/' + device_id + path, temp).push(function () {
return jio._cache.putAttachment('/' + user_id + path, 'data', return jio._cache.putAttachment('/' + device_id + path, 'data',
new Blob([JSON.stringify(dat)], {type: new Blob([JSON.stringify(dat)], {type:
'text/plain'})); 'text/plain'}));
}); });
...@@ -136,13 +136,15 @@ ...@@ -136,13 +136,15 @@
return new RSVP.Queue().push(function () { return new RSVP.Queue().push(function () {
return jIO.util.ajax({ return jIO.util.ajax({
'type': 'GET', 'type': 'GET',
'url': automatic_template.expand({endpoint: ['user', 'me', '']}), 'url': automatic_template.expand({endpoint: ['user', 'me', 'device',
'']}),
'headers': {'Authorization': 'Bearer ' + token} 'headers': {'Authorization': 'Bearer ' + token}
//'xhrFields': {withCredentials: true} //'xhrFields': {withCredentials: true}
}); });
}).push(function (respusr) { }).push(function (respdev) {
user_dict[token] = JSON.parse(respusr.target.responseText).id; device_dict[token] =
if (usr === 'all' || usr === user_dict[token]) { JSON.parse(respdev.target.responseText).results[0].id;
if (dev === 'all' || dev === device_dict[token]) {
return jIO.util.ajax({ return jIO.util.ajax({
'type': 'GET', 'type': 'GET',
'url': URI(automatic_template.expand({endpoint: 'url': URI(automatic_template.expand({endpoint:
...@@ -168,7 +170,7 @@ ...@@ -168,7 +170,7 @@
return RSVP.all(promises); return RSVP.all(promises);
}).push(function (trueOrErrorArray) { }).push(function (trueOrErrorArray) {
// If we queried an id, return results should be length 1 // If we queried an id, return results should be length 1
if (isId && (usr !== 'all')) { if (isId && (dev !== 'all')) {
if (result.length === 1) { if (result.length === 1) {
return result[0]; return result[0];
} }
...@@ -178,7 +180,7 @@ ...@@ -178,7 +180,7 @@
} }
// Result is empty, so we throw the correct token error. // Result is empty, so we throw the correct token error.
i = jio._access_tokens.map(function (token) { i = jio._access_tokens.map(function (token) {
if (user_dict[token] === usr) { if (device_dict[token] === dev) {
return true; return true;
} }
return false; return false;
...@@ -188,8 +190,8 @@ ...@@ -188,8 +190,8 @@
throw trueOrErrorArray[i]; throw trueOrErrorArray[i];
} }
// If we didn't find the error in the promise returns, we don't have // If we didn't find the error in the promise returns, we don't have
// a token for user usr. // a token for device dev.
throw new jIO.util.jIOError('No valid token for user: ' + usr, 400); throw new jIO.util.jIOError('No valid token for device: ' + dev, 400);
} }
// Otherwise return results and errors and let caller handle. // Otherwise return results and errors and let caller handle.
return result; return result;
...@@ -220,8 +222,8 @@ ...@@ -220,8 +222,8 @@
AutomaticAPIStorage.prototype.get = function (id) { AutomaticAPIStorage.prototype.get = function (id) {
var self = this, var self = this,
endpoint = id.split('/'), endpoint = id.split('/'),
usr; dev;
usr = endpoint.splice(1, 1)[0]; dev = endpoint.splice(1, 1)[0];
endpoint = endpoint.join('/'); endpoint = endpoint.join('/');
if (id.indexOf('/') !== 0) { if (id.indexOf('/') !== 0) {
throw new jIO.util.jIOError('id ' + id + throw new jIO.util.jIOError('id ' + id +
...@@ -234,13 +236,13 @@ ...@@ -234,13 +236,13 @@
if (!checkEndpointAsId(endpoint)) { if (!checkEndpointAsId(endpoint)) {
throw new jIO.util.jIOError('Invalid id.', 400); throw new jIO.util.jIOError('Invalid id.', 400);
} }
if (usr === 'all') { if (dev === 'all') {
throw new jIO.util.jIOError('Invalid id.', 400); throw new jIO.util.jIOError('Invalid id.', 400);
} }
return this._cache.get(id).push(function (res) { return this._cache.get(id).push(function (res) {
return res; return res;
}, function () { }, function () {
return _queryAutomaticAPI(endpoint, {'user': usr}, self) return _queryAutomaticAPI(endpoint, {'device': dev}, self)
.push(function (res) { .push(function (res) {
return res; return res;
}, function (err) { }, function (err) {
...@@ -273,8 +275,8 @@ ...@@ -273,8 +275,8 @@
AutomaticAPIStorage.prototype.getAttachment = function (id, name, options) { AutomaticAPIStorage.prototype.getAttachment = function (id, name, options) {
var self = this, var self = this,
endpoint = id.split('/'), endpoint = id.split('/'),
usr; dev;
usr = endpoint.splice(1, 1)[0]; dev = endpoint.splice(1, 1)[0];
endpoint = endpoint.join('/'); endpoint = endpoint.join('/');
if (id.indexOf('/') !== 0) { if (id.indexOf('/') !== 0) {
throw new jIO.util.jIOError('id ' + id + throw new jIO.util.jIOError('id ' + id +
...@@ -287,7 +289,7 @@ ...@@ -287,7 +289,7 @@
if (!checkEndpointAsId(endpoint)) { if (!checkEndpointAsId(endpoint)) {
throw new jIO.util.jIOError('Invalid id.', 400); throw new jIO.util.jIOError('Invalid id.', 400);
} }
if (usr === 'all') { if (dev === 'all') {
throw new jIO.util.jIOError('Invalid id.', 400); throw new jIO.util.jIOError('Invalid id.', 400);
} }
return this._cache.get(id).push(function () { return this._cache.get(id).push(function () {
...@@ -317,8 +319,8 @@ ...@@ -317,8 +319,8 @@
AutomaticAPIStorage.prototype.allAttachments = function (id) { AutomaticAPIStorage.prototype.allAttachments = function (id) {
var endpoint = id.split('/'), var endpoint = id.split('/'),
usr; dev;
usr = endpoint.splice(1, 1)[0]; dev = endpoint.splice(1, 1)[0];
endpoint = endpoint.join('/'); endpoint = endpoint.join('/');
if (id.indexOf('/') !== 0) { if (id.indexOf('/') !== 0) {
throw new jIO.util.jIOError('id ' + id + throw new jIO.util.jIOError('id ' + id +
...@@ -331,7 +333,7 @@ ...@@ -331,7 +333,7 @@
if (!checkEndpointAsId(endpoint)) { if (!checkEndpointAsId(endpoint)) {
throw new jIO.util.jIOError('Invalid id.', 400); throw new jIO.util.jIOError('Invalid id.', 400);
} }
if (usr === 'all') { if (dev === 'all') {
throw new jIO.util.jIOError('Invalid id.', 400); throw new jIO.util.jIOError('Invalid id.', 400);
} }
return {data: null}; return {data: null};
...@@ -351,9 +353,9 @@ ...@@ -351,9 +353,9 @@
key_list, key_list,
automatic_filters = {}, automatic_filters = {},
simplequery_type_value, simplequery_type_value,
intercept_keys = ['start_date', 'stop_date', 'automatic_user', intercept_keys = ['start_date', 'stop_date', 'automatic_device',
'vehicle'], 'vehicle'],
intercept_keys_automatic_name = ['started_at', 'ended_at', 'user', intercept_keys_automatic_name = ['started_at', 'ended_at', 'device',
'vehicle'], 'vehicle'],
intercept_accepted_operators = [['>', '>=', '<', '<='], intercept_accepted_operators = [['>', '>=', '<', '<='],
['>', '>=', '<', '<='], ['='], ['=']], ['>', '>=', '<', '<='], ['='], ['=']],
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment