Instantly share code, notes, and snippets.

Embed
What would you like to do?
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