lml-main
Version:
This is now a mono repository published into many standalone packages.
64 lines • 2.67 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const redux_observable_1 = require("redux-observable");
const actions_1 = require("../../allocation/actions");
const cosmoui_1 = require("cosmoui");
const cosmo_ui_auth_1 = require("@lml/cosmo-ui-auth");
const selectors_1 = require("../../jobs/selectors");
const selectors_2 = require("../../couriers/selectors");
const cosmo_redux_api_1 = require("@lml/cosmo-redux-api");
/**
* Post an allocation to the api
*
* @param jobRefId
* @param courierRefId
* @param user
*/
exports.postAllocation = (action$, store) => action$.ofType(actions_1.ALLOCATE)
.mergeMap((action) => {
const state = store.getState();
const { allocationType, courierRefId, jobRefId } = action;
// get the job or use the active job
const job = jobRefId
? cosmo_redux_api_1.getJobById(state, jobRefId)
: selectors_1.getActiveJob(state);
if (!job) {
return [
cosmoui_1.notificationError('Please select a job before attempting to allocate')
];
}
const jobStatus = cosmo_redux_api_1.getJobStatusById(state, job.refId);
if (!jobStatus) {
return [
cosmoui_1.notificationError(`Could not find a valid status for job ${job.jobNumber}`)
];
}
// get the courier or use the active courier
const courier = courierRefId
? cosmo_redux_api_1.getCourierById(state, courierRefId)
: selectors_2.getActiveCourier(state);
if (!courier) {
return [cosmoui_1.notificationError('Please select a courier before attempting to allocate')];
}
const username = cosmo_ui_auth_1.getUsername(state);
const res = [
cosmo_redux_api_1.postAllocation(job, courier, courier.assignment.callsign, allocationType, username, action),
];
// optimistic update of the job status
if (action.allocationType === 'ACTUAL') {
res.push(cosmo_redux_api_1.changeJobStatus(jobStatus, 'live', 'allocated'));
}
else if (action.allocationType === 'DEALLOCATION') {
res.push(cosmo_redux_api_1.changeJobStatus(jobStatus, 'allocated', 'live'));
}
return res;
});
exports.postDeallocationSuccess = (action$, store) => action$.ofType(cosmo_redux_api_1.POST_DEALLOCATION.SUCCESS)
.map((action) => {
const state = store.getState();
// console.log('POST DEALLOCATION SUCCESS', action)
const { job } = action.args;
return cosmo_redux_api_1.changeJobStatus(job, 'allocated', 'live');
});
exports.postAllocationEpics = redux_observable_1.combineEpics(exports.postAllocation, exports.postDeallocationSuccess);
//# sourceMappingURL=post-allocation.js.map