UNPKG

cspace-ui

Version:
133 lines (132 loc) 4.88 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.invoke = exports.default = void 0; var _get = _interopRequireDefault(require("lodash/get")); var _reactIntl = require("react-intl"); var _session = _interopRequireDefault(require("../helpers/session")); var _getErrorDescription = _interopRequireDefault(require("../helpers/getErrorDescription")); var _invocationHelpers = require("../helpers/invocationHelpers"); var _recordDataHelpers = require("../helpers/recordDataHelpers"); var _notificationHelpers = _interopRequireDefault(require("../helpers/notificationHelpers")); var _validationHelpers = require("../helpers/validationHelpers"); var _reducers = require("../reducers"); var _notification = require("./notification"); var _record = require("./record"); var _notificationStatusCodes = require("../constants/notificationStatusCodes"); var _actionCodes = require("../constants/actionCodes"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } const messages = (0, _reactIntl.defineMessages)({ running: { "id": "batch.running", "defaultMessage": "Running {name}\u2026" }, complete: { "id": "batch.complete", "defaultMessage": "Completed {name}: {numAffected, plural, =0 {No records} one {# record} other {# records}} affected. {userNote}" }, error: { "id": "batch.error", "defaultMessage": "Error running {name}: {error}" } }); const invoke = (config, batchMetadata, invocationDescriptor, onValidationSuccess) => (dispatch, getState) => { const batchCsid = (0, _recordDataHelpers.getCsid)(batchMetadata); const batchNameGetter = (0, _get.default)(config, ['recordTypes', 'batch', 'invocableName']); const batchName = batchNameGetter && batchNameGetter(batchMetadata); const paramRecordTypeConfig = (0, _get.default)(config, ['invocables', 'batch', batchName]); const paramRecordCsid = ''; let params; let validateParams; if (paramRecordTypeConfig) { validateParams = dispatch((0, _record.validateRecordData)(paramRecordTypeConfig, paramRecordCsid)).then(() => { if ((0, _validationHelpers.hasBlockingError)((0, _reducers.getRecordValidationErrors)(getState(), paramRecordCsid))) { return Promise.reject(); } const data = (0, _reducers.getNewRecordData)(getState()); params = data && data.first().toJS(); return Promise.resolve(); }); } else { validateParams = Promise.resolve(); } return validateParams.then(() => { if (onValidationSuccess) { onValidationSuccess(); } const notificationID = (0, _notificationHelpers.default)(); dispatch({ type: _actionCodes.BATCH_INVOKE_STARTED, meta: { csid: batchCsid } }); dispatch((0, _notification.showNotification)({ items: [{ message: messages.running, values: { name: batchMetadata.getIn(['document', 'ns2:batch_common', 'name']) } }], date: new Date(), status: _notificationStatusCodes.STATUS_PENDING }, notificationID)); const requestConfig = { data: (0, _invocationHelpers.createInvocationData)(config, invocationDescriptor, params) }; return (0, _session.default)().create(`batch/${batchCsid}/invoke`, requestConfig).then(response => { const { data } = response; const numAffected = (0, _get.default)(data, ['ns2:invocationResults', 'numAffected']); const userNote = (0, _get.default)(data, ['ns2:invocationResults', 'userNote']); let numAffectedInt; numAffectedInt = parseInt(numAffected, 10); if (Number.isNaN(numAffectedInt)) { numAffectedInt = undefined; } dispatch({ type: _actionCodes.BATCH_INVOKE_FULFILLED, meta: { csid: batchCsid, numAffected: numAffectedInt } }); dispatch((0, _notification.showNotification)({ items: [{ message: messages.complete, values: { numAffected, userNote, name: batchMetadata.getIn(['document', 'ns2:batch_common', 'name']) } }], date: new Date(), status: _notificationStatusCodes.STATUS_SUCCESS, autoClose: true }, notificationID)); return response; }).catch(error => { dispatch({ type: _actionCodes.BATCH_INVOKE_REJECTED, meta: { csid: batchCsid } }); dispatch((0, _notification.showNotification)({ items: [{ message: messages.error, values: { name: batchName, error: (0, _getErrorDescription.default)(error) } }], date: new Date(), status: _notificationStatusCodes.STATUS_ERROR }, notificationID)); }); }); }; exports.invoke = invoke; var _default = exports.default = {};