Created
August 1, 2017 02:33
-
-
Save cameri/8c5bb1ddc5c864ca0211bd218ff6ee09 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define([ | |
'angular', | |
'moment', | |
'private/tickers', | |
'private/services/wip', | |
'shared/services/post', | |
'shared/services/dialog', | |
], function(angular, moment) { | |
'use strict'; | |
function PostsService ($q, Post, WipService, DialogService) { | |
var modelName = 'Post'; | |
var service = Post; | |
function getAll (params) { | |
var wips = WipService.getByModelName(modelName); | |
var deferred = $q.defer(); | |
service.get(params, | |
function (data, headers) { | |
if (data.success && !data.error) { | |
var results = data.result.map(function(item) { | |
var id = item[modelName].id; | |
var wip = wips.filter(function(j) { return j.id === id; }) | |
.map(function(k) { return WipService.get(k.key); }) | |
.shift(); | |
return { | |
wipKey: (wip) ? wip.key : null, | |
yours: (wip) ? wip.model : null, | |
theirs: item, | |
}; | |
}); | |
results = wips.filter(function(j) {return !j.id; }) | |
.map(function(k) { return WipService.get(k.key); }) | |
//.sort(function(a, b) { return a.model[modelName].name.localeCompare(b.model[modelName].name); }) | |
.map(function(v){ | |
return { | |
theirs: null, | |
yours: v.model, | |
wipKey: v.key, | |
}; | |
}) | |
.concat(results); | |
deferred.resolve(results); | |
} else { | |
deferred.reject(data); | |
} | |
}, | |
function (httpResponse) { | |
deferred.reject(httpResponse.data); | |
}); | |
return deferred.promise; | |
} | |
function getWip(id) { | |
var wip = WipService.getByModelNameAndId(modelName, id); | |
if (!wip) { | |
wip = WipService.get(id); // key | |
} | |
return wip; | |
} | |
function get (params) { | |
var wip = getWip(params.id); | |
var deferred = $q.defer(); | |
service.get(params, | |
function (data, headers) { | |
if (data.success && !data.error) { | |
if (!wip) { | |
deferred.resolve({ | |
wip: null, | |
theirs: data.result, | |
yours: null, | |
}); | |
} else { | |
var last_modified = moment(data.result[modelName].modified, 'YYYY-MM-DD H:m:s'); | |
if (last_modified.isAfter(wip.modified)) { | |
DialogService | |
.confirm({ | |
title: 'Conflicto', | |
message: 'Alguien ha realizado cambios mientras editabas. Para continuar debes deshacer tus cambios.', | |
primary: 'Deshacer', | |
secondary: 'Cancelar', | |
backdrop: 'static', | |
size: 'md', | |
keyboard: true, | |
}) | |
.result | |
.then(function() { | |
discard(wip); // not working | |
//WipService.remove(wip.key); | |
deferred.resolve({ | |
wip: null, | |
theirs: data.result, | |
yours: null, | |
}); | |
}, function() { | |
deferred.reject(); | |
}); | |
} else { | |
deferred.resolve({ | |
wip: wip, | |
theirs: data.result, | |
yours: wip.model, | |
}); | |
} | |
} | |
} else { | |
deferred.reject(data); | |
} | |
}, | |
function (httpResponse) { | |
if (httpResponse.status === 404 && wip && wip.key) { | |
deferred.resolve({ | |
wip: wip, | |
theirs: null, | |
yours: wip.model | |
}); | |
} else if (httpResponse.status === 410 && wip && wip.key) { | |
WipService.remove(wip.key); | |
} | |
deferred.reject(httpResponse.data); | |
}); | |
return deferred.promise; | |
} | |
function save(model) { | |
var id = model[modelName].id; | |
var deferred = $q.defer(); | |
service.save({ id : id }, model, | |
function (data, headers) { | |
if (data.success && !data.error) { | |
deferred.resolve(data.result); | |
} else { | |
deferred.reject(data); | |
} | |
}, | |
function (httpResponse) { | |
deferred.reject(httpResponse.data); | |
}); | |
return deferred.promise; | |
} | |
function remove(model) { | |
return DialogService | |
.confirm({ | |
title: 'Confirmar', | |
message: '¿Está seguro de que desea eliminar este registro?', | |
primary: 'Eliminar', | |
secondary: 'Cancelar', | |
backdrop: 'static', | |
size: 'md', | |
keyboard: true, | |
}) | |
.result | |
.then(function() { | |
if (model.yours) { | |
discard(model.wipKey); | |
} | |
}) | |
.then(function(){ | |
if (model.theirs) { | |
return service | |
.delete({ id: model.theirs[modelName].id }) | |
.$promise; | |
} | |
}); | |
} | |
function discard(wipKey) { | |
if (wipKey) { | |
WipService.remove(wipKey); | |
} | |
} | |
function stash(wipKey, model) { | |
return WipService.save(modelName, wipKey, model, 'saved'); | |
} | |
return { | |
getAll: getAll, | |
get: get, | |
save: save, | |
stash: stash, | |
discard: discard, | |
remove: remove, | |
}; | |
} | |
angular | |
.module('app.posts') | |
.lazy | |
.factory('PostsService', [ | |
'$q', | |
'Post', | |
'WipService', | |
'DialogService', | |
PostsService | |
]); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment