lml-main
Version:
This is now a mono repository published into many standalone packages.
133 lines • 6.18 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const lodash_1 = require("lodash");
const redux_observable_1 = require("redux-observable");
const moment = require("moment");
const selectors_1 = require("../../jobs/selectors");
const selectors_2 = require("../../frequencies/selectors");
const actions_1 = require("../../jobs/actions");
const actions_2 = require("../../frequencies/actions");
const cosmo_redux_api_1 = require("@lml/cosmo-redux-api");
const JOB_WINDOW_IN_HOURS = 24;
const MINIMUM_POLLING_INTERVAL = 5000;
/**
* A list of actions which trigger the jobs to be reloaded
*
* These are:
*
* - when the job sort order is changed
* - when new frequencies are added to the list
* - when selected frequencies are toggled on or off
* - when the auto/manual frequency tab is switched
* - when the job list is paged forwards or backwards
* -
*
*
* Please note that frequencies has its own epic called handle-frequencies-change
* which also triggers the loadAllJobs actions when the frequencies are changed
*
* @param action$
* @param store
*/
exports.reloadJobsByStatus = (action$, store) => action$.ofType(actions_2.SET_SELECTED_FREQUENCIES, actions_2.SET_SELECTED_FREQUENCY_TYPE, actions_2.SET_SELECTED_FREQUENCY_IS_ACTIVE, actions_2.SET_FREQUENCIES_DATA, actions_1.SET_JOB_SORT_ORDER, actions_1.PAGE_FORWARDS, actions_1.PAGE_BACKWARDS, cosmo_redux_api_1.POST_COURIER_ACTION.SUCCESS, cosmo_redux_api_1.POST_ALLOCATION.FINALLY)
.mapTo(actions_1.reloadJobs());
/**
*
* @param action$
* @param store
*/
exports.mkReloadJobsByStatusAction = (action$, store) => action$.ofType(actions_1.RELOAD_JOBS)
.map((action) => {
const state = store.getState();
const status = selectors_1.getJobPage(state);
if (!status) {
return { type: 'NO_STATUS_SELECTED' };
}
//Currently selected day start & end
const multiplier = selectors_1.getJobFilterDay(state);
const start = moment().add(multiplier, 'days').startOf('day').toISOString();
const end = moment().add(multiplier, 'days').endOf('day').toISOString();
// just using the status as the search key for now
const searchKey = status;
const search = cosmo_redux_api_1.getJobSearch(state, searchKey);
if (search && search.lastUpdate && !diffUpdate(search.lastUpdate)) {
//Check last search start & end against current start & end.
const sameDay = (search.params.start === start && search.params.end === end);
if (sameDay) {
return { type: 'JOB_STATUS_POLLING_NOOP' };
}
}
const labels = selectors_2.getSelectedFrequencyLabels(state);
// console.log('PREPARING TO RELOAD JOBS', status, labels)
if (!labels.length) {
return { type: 'NO_FREQUENCIES_SELECTED' };
}
const order = selectors_1.getJobSortOrder(state);
const lastKeys = selectors_1.getLastKeysForPage(state);
// fuck! we have to poll for job data as well until
// we separate out the allocation object
const showData = action.showData || 'allocations,scrMatches,jobDelays';
const args = Object.assign({}, action, { status, labels, order, lastKeys });
const params = {
statuses: [status],
labels,
start,
end,
lastKeys,
order,
};
return cosmo_redux_api_1.fetchJobsByStatus(status, params, showData, args);
});
exports.reloadJobsByStatusSuccess = (action$, store) => action$.ofType(cosmo_redux_api_1.FETCH_JOBS_BY_STATUS.SUCCESS)
.filter(a => a.args.type === actions_1.RELOAD_JOBS)
.mergeMap((action) => {
// console.log('LISTENED TO FETCH JOBS BY STATUS ACTION', action)
const res = [];
const state = store.getState();
const { lastEvaluatedKeys, jobStatuses } = action.result;
const jobsLength = jobStatuses
? action.result.jobStatuses.length
: 0;
const lastKeys = [...state.jobs.pagination.lastKeys];
const pagePointer = state.jobs.pagination.pagePointer;
// set the current last keys in the
lastKeys[pagePointer] = lastEvaluatedKeys;
const canPageBack = pagePointer > 0;
const canPageForwards = lastEvaluatedKeys && Object.keys(lastEvaluatedKeys).length ? true : false;
const jobsNotInState = selectors_1.getJobsNotInState(state, action.result.jobStatuses);
// console.log('LIST OF JOBS NOT IN STATE', jobsNotInState)
if (jobsNotInState.length) {
res.push(cosmo_redux_api_1.fetchJobsByIdBatch(jobsNotInState, 'jobs'));
}
res.push(actions_1.setJobsPagination(lastKeys, canPageForwards, canPageBack));
// todo - set courier callsigns as well?
return res;
});
// HACK WARNING!!!! todo - depracate
// This is here because in order to allocate/deallocate a courier from/to a job
// it's necessary to make sure the full courier object is already in the state.
// For instance, we check if the courier's frequency matches the job's frequency
// and we issue a warning if it doesn't. I hope that in the future we can
// think of a way round this
exports.ensureCouriersAreInState = (action$, store) => action$.ofType(cosmo_redux_api_1.FETCH_JOBS_BY_ID_BATCH.SUCCESS)
.map(action => {
const { jobs } = action.result;
if (jobs) {
const couriersInJobs = Object.values(jobs).reduce((acc, j) => {
const refId = lodash_1.get(j, 'currentAllocation.courier.refId', null);
if (refId)
acc.push(refId);
return acc;
}, []);
if (couriersInJobs.length) {
const state = store.getState();
const couriers = Object.keys(cosmo_redux_api_1.getCouriersData(state));
const couriersNotInState = lodash_1.difference(couriers, couriersInJobs);
return cosmo_redux_api_1.fetchCouriersByIdBatch(couriersNotInState);
}
}
return { type: 'ALL_COURIERS_ALREADY_IN_STATE' };
});
const diffUpdate = (lastUpdate) => (Date.now() - lastUpdate) > MINIMUM_POLLING_INTERVAL;
exports.reloadJobsEpics = redux_observable_1.combineEpics(exports.reloadJobsByStatus, exports.mkReloadJobsByStatusAction, exports.reloadJobsByStatusSuccess, exports.ensureCouriersAreInState);
//# sourceMappingURL=reload-jobs.js.map