2016-08-06 19:56:32 -07:00
|
|
|
|
define(['localassetmanager'], function (localAssetManager) {
|
2015-09-09 20:22:52 -07:00
|
|
|
|
|
2016-08-06 19:56:32 -07:00
|
|
|
|
function syncNext(users, index, resolve, reject, apiClient, server) {
|
2015-09-09 20:22:52 -07:00
|
|
|
|
|
2016-08-06 19:56:32 -07:00
|
|
|
|
var length = users.length;
|
2015-09-09 20:22:52 -07:00
|
|
|
|
|
2016-08-06 19:56:32 -07:00
|
|
|
|
if (index >= length) {
|
2015-09-09 20:22:52 -07:00
|
|
|
|
|
2016-08-06 19:56:32 -07:00
|
|
|
|
resolve();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2015-09-09 20:22:52 -07:00
|
|
|
|
|
2016-08-06 19:56:32 -07:00
|
|
|
|
var onFinish = function () {
|
|
|
|
|
syncNext(users, index + 1, resolve, reject, apiClient, server);
|
2015-09-09 20:22:52 -07:00
|
|
|
|
};
|
2015-09-10 11:28:22 -07:00
|
|
|
|
|
2016-08-06 19:56:32 -07:00
|
|
|
|
syncUser(users[index], apiClient).then(onFinish, onFinish);
|
|
|
|
|
}
|
2015-09-10 11:28:22 -07:00
|
|
|
|
|
2016-08-06 19:56:32 -07:00
|
|
|
|
function syncUser(user, apiClient) {
|
2015-09-10 11:28:22 -07:00
|
|
|
|
|
2016-08-06 19:56:32 -07:00
|
|
|
|
return apiClient.getOfflineUser(user.Id).then(function (result) {
|
2015-09-10 11:28:22 -07:00
|
|
|
|
|
2016-08-06 19:56:32 -07:00
|
|
|
|
return localAssetManager.saveOfflineUser(result);
|
2015-09-10 11:28:22 -07:00
|
|
|
|
|
2016-08-06 19:56:32 -07:00
|
|
|
|
}, function () {
|
2015-09-10 11:28:22 -07:00
|
|
|
|
|
2016-08-06 19:56:32 -07:00
|
|
|
|
// TODO: We should only delete if there's a 401 response
|
|
|
|
|
return localAssetManager.deleteOfflineUser(user.Id).catch(function () {
|
|
|
|
|
return Promise.resolve();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
2015-09-10 11:28:22 -07:00
|
|
|
|
|
2016-08-06 19:56:32 -07:00
|
|
|
|
return function offlineUserSync() {
|
2015-09-10 11:28:22 -07:00
|
|
|
|
|
2016-08-06 19:56:32 -07:00
|
|
|
|
var self = this;
|
2015-09-15 20:55:26 -07:00
|
|
|
|
|
2016-08-06 19:56:32 -07:00
|
|
|
|
self.sync = function (apiClient, server) {
|
2015-09-15 20:55:26 -07:00
|
|
|
|
|
2016-08-06 19:56:32 -07:00
|
|
|
|
return new Promise(function (resolve, reject) {
|
2015-09-15 20:55:26 -07:00
|
|
|
|
|
2016-08-06 19:56:32 -07:00
|
|
|
|
var users = server.Users || [];
|
|
|
|
|
syncNext(users, 0, resolve, reject, apiClient, server);
|
2015-09-10 11:28:22 -07:00
|
|
|
|
});
|
2016-08-06 19:56:32 -07:00
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
});
|