@reactivehub/cli
Version:
243 lines (190 loc) • 7.8 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.run = exports.checkAll = exports.checkEvent = undefined;
var _promise = require('babel-runtime/core-js/promise');
var _promise2 = _interopRequireDefault(_promise);
var _extends2 = require('babel-runtime/helpers/extends');
var _extends3 = _interopRequireDefault(_extends2);
var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');
var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);
var _event = require('../event');
var _event2 = _interopRequireDefault(_event);
var _query = require('../../libs/query');
var _query2 = _interopRequireDefault(_query);
var _objects = require('../../libs/objects');
var _objects2 = _interopRequireDefault(_objects);
var _serviceAccounts = require('../../serviceAccounts');
var _serviceAccounts2 = _interopRequireDefault(_serviceAccounts);
var _checkDeploy = require('../../messages/checkDeploy');
var _checkDeploy2 = _interopRequireDefault(_checkDeploy);
var _errorLog = require('./errorLog');
var _errorLog2 = _interopRequireDefault(_errorLog);
var _actions = require('../../actions');
var _actions2 = _interopRequireDefault(_actions);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const actionExists = ({ type, actionType, operation }) => {
const map = _actions2.default.getActionConfig(type, actionType);
if (!map) return { status: false, messages: [`Action "${type}/${actionType}" does not exists. Check the ${type} documentation.`] };
if (operation) {
const { actions } = map;
const hasOperation = actions.filter(act => act.name === operation).length > 0;
if (!hasOperation) {
return {
status: false,
messages: [`Operation "${operation}" of action "${type}/${actionType}" does not exists. Check the ${type} documentation.`]
};
}
}
return { status: true };
};
const checkAction = (() => {
var _ref = (0, _asyncToGenerator3.default)(function* ({ action, filter }) {
const { id, type, action: actionType, serviceAccountId, operation } = action;
const prefix = `Action: ${filter}:${id} `;
const required = _objects2.default.checkRequired(action, ['id', 'type', 'action', 'serviceAccountId'], prefix);
let status = true;
let messages = [];
if (!required.status) {
status = false;
messages = messages.concat(required.messages);
}
const checkActionExists = actionExists({ type, actionType, operation });
if (!checkActionExists.status) {
status = false;
messages = messages.concat(checkActionExists.messages);
}
const checkServiceAccount = yield _serviceAccounts2.default.isServiceAccountOfType({ type, id: serviceAccountId });
if (!checkServiceAccount) {
status = false;
messages.push(`Service Account "${serviceAccountId}" is not a valid "${type}" service type.`);
}
return { status, filter, action: id, messages };
});
return function checkAction(_x) {
return _ref.apply(this, arguments);
};
})();
const checkConditionFilter = ({ id, condition }) => {
if (!condition) return { status: true, messages: [] };
return (0, _extends3.default)({ filter: id }, _query2.default.checkQuery(condition));
};
const checkFilters = (() => {
var _ref2 = (0, _asyncToGenerator3.default)(function* (filter) {
const { id, condition, actions = [] } = filter;
const checkCondition = checkConditionFilter({ id, condition });
let { status } = checkCondition;
let actionErrors = [];
if (actions) {
actionErrors = yield _promise2.default.all(actions.map(function (action) {
return checkAction({ filter: id, action });
})).then(function (actionVerify) {
return actionVerify.filter(function ({ status: verifyStatus }) {
return !verifyStatus;
});
});
}
if (actionErrors.length > 0) status = false;
let errors = [];
const requiredFields = _objects2.default.checkRequired(filter, ['id'], 'Filters: ');
if (!requiredFields.status) {
status = false;
errors.push((0, _extends3.default)({ filter: id }, requiredFields));
}
if (!checkCondition.status) errors.push(checkCondition);
if (actionErrors.length > 0) errors = errors.concat(actionErrors);
return { id, status, errors };
});
return function checkFilters(_x2) {
return _ref2.apply(this, arguments);
};
})();
const checkModel = (id, model) => (0, _extends3.default)({ id }, _query2.default.checkPropertyTypes(model));
const checkEvent = exports.checkEvent = (() => {
var _ref3 = (0, _asyncToGenerator3.default)(function* (id) {
const eventPayload = _event2.default.loadEventData(id);
const { model = {}, filters = [] } = eventPayload;
const requiredVerify = _objects2.default.checkRequired(eventPayload, ['id', 'model', 'filters']);
const modelVerify = checkModel(id, model);
let { status } = requiredVerify;
const { status: modelStatus } = modelVerify;
if (!modelStatus) status = modelStatus;
const filterErrors = filters && (yield _promise2.default.all(filters.map(function (filter) {
return checkFilters(filter);
})).then(function (verify) {
return verify.filter(function ({ status: verifyStatus }) {
return !verifyStatus;
});
})) || [];
let errors = [];
let totalErrors = 0;
if (!requiredVerify.status) {
errors = errors.concat(requiredVerify);
const { messages = [] } = requiredVerify;
totalErrors += messages.length;
}
if (!modelVerify.status) {
errors = errors.concat(modelVerify);
const { messages = [] } = requiredVerify;
totalErrors += messages.length;
}
if (filterErrors.length > 0) {
filterErrors.forEach(function (error) {
errors = errors.concat(error.errors);
totalErrors += errors.map(function (err) {
return err.messages.length;
}).reduce(function (a, b) {
return a + b;
});
});
status = false;
}
return { id, totalErrors, status, errors };
});
return function checkEvent(_x3) {
return _ref3.apply(this, arguments);
};
})();
const checkAll = exports.checkAll = (() => {
var _ref4 = (0, _asyncToGenerator3.default)(function* ({ outputLogFile = true } = {}) {
const events = _event2.default.getEventsInFolder();
_checkDeploy2.default.totalBeignProcessed(events.length);
const errors = yield _promise2.default.all(events.map(function (item) {
return checkEvent(item);
})).then(function (verify) {
return verify.filter(function ({ status: verifyStatus }) {
return !verifyStatus;
});
});
const totalErrors = errors.length;
const status = totalErrors === 0;
let logFileLink = null;
if (outputLogFile) logFileLink = yield _errorLog2.default.create(totalErrors, errors);
return { status, logFileLink, errors, totalErrors };
});
return function checkAll() {
return _ref4.apply(this, arguments);
};
})();
const run = exports.run = (() => {
var _ref5 = (0, _asyncToGenerator3.default)(function* ({ isDeploy = false } = {}) {
_checkDeploy2.default.testing();
const { status, errors, totalErrors, logFileLink } = yield checkAll();
if (!status) {
console.log(status);
errors.forEach(function (error) {
const { id: eventId, totalErrors: total } = error;
_checkDeploy2.default.eventErrors(eventId, total);
});
_checkDeploy2.default.hasErrors(totalErrors, logFileLink, isDeploy);
return false;
}
_checkDeploy2.default.testPassed();
return true;
});
return function run() {
return _ref5.apply(this, arguments);
};
})();
//# sourceMappingURL=check.js.map