Last active
February 10, 2016 15:15
-
-
Save benhoIIand/2f1b9e6797befdbf39b5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import cloneDeep from 'lodash/cloneDeep'; | |
import PayloadService from './payload.svc'; | |
import StatsResource from '../resources/stats.rsc'; | |
import DataTransformationService from '../../core-data-transformation/services/core-data-transformation.svc'; | |
let $q, $timeout, statsResource, dataTransformationService; | |
const SUMMARY_RETRIEVAL_SERVICE_NAME = 'socialgrids.coreData.summaryRetrievalService'; | |
class SummaryRetrievalService extends PayloadService { | |
constructor(_$q, _$timeout, _statsResource, _dataTransformationService) { | |
super(); | |
$q = _$q; | |
$timeout = _$timeout; | |
statsResource = _statsResource; | |
dataTransformationService = _dataTransformationService; | |
this.currentState = null; | |
} | |
getSummaryData(state, requestId) { | |
if (!(hasStateChanged(state, currentState))) { | |
return $q.when(); | |
} | |
const payload = this.constructSummariesPayload(state, requestId); | |
this.currentState = filterState(state); | |
return loadSummaryAsync(state, requestId) | |
.then((summaryDataResponse) => { | |
if (summaryDataResponse.requestId !== requestId) { | |
return false; | |
} | |
const summaryData = summaryDataResponse.response.data; | |
return dataTransformationService.transformData({ summaryData }, state.columns); | |
}); | |
} | |
} | |
function filterState(state) { | |
const clonedState = cloneDeep(state); | |
delete clonedState.paging; | |
delete clonedState.sorting; | |
clonedState.columns = clonedState.columns.map(column => column.id); | |
return clonedState; | |
} | |
function hasStateChanged(state, currentState) { | |
return !(_.isEqual(filterState(state), currentState)); | |
} | |
function loadSummaryAsync(payload) { | |
return statsResource.search(null, payload).$promise; | |
} | |
export default { | |
name: SUMMARY_RETRIEVAL_SERVICE_NAME, | |
main: [ | |
'$q', | |
'$timeout', | |
StatsResource.name, | |
DataTransformationService.name, | |
SummaryRetrievalService | |
] | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment