@enonic/mock-xp
Version:
Mock Enonic XP API JavaScript Library
740 lines (739 loc) • 35.1 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Branch = void 0;
var tslib_1 = require("tslib");
var js_utils_1 = require("@enonic/js-utils");
var flatten_1 = require("@enonic/js-utils/array/flatten");
var forceArray_1 = require("@enonic/js-utils/array/forceArray");
var enonify_1 = require("@enonic/js-utils/storage/indexing/enonify");
var sortKeys_1 = require("@enonic/js-utils/object/sortKeys");
var isUuidV4String_1 = require("@enonic/js-utils/value/isUuidV4String");
var isString_1 = require("@enonic/js-utils/value/isString");
var uniqs_1 = tslib_1.__importDefault(require("uniqs"));
var intersect_1 = tslib_1.__importDefault(require("intersect"));
var NodeAlreadyExistAtPathException_1 = require("./node/NodeAlreadyExistAtPathException");
var NodeNotFoundException_1 = require("./node/NodeNotFoundException");
var deref_1 = tslib_1.__importDefault(require("../util/deref"));
var constants_1 = require("../constants");
var processNestedData_1 = require("./branch/processNestedData");
var DEFAULT_INDEX_CONFIG = {
default: {
decideByType: true,
enabled: true,
nGram: false,
fulltext: false,
includeInAllText: false,
path: false,
indexValueProcessors: [],
languages: []
},
configs: []
};
var IGNORED_ON_CREATE = [
'_path',
'_state',
'_ts',
];
var SEARCH_INDEX_BLACKLIST = [
'_childOrder',
'_id',
'_indexConfig',
'_inheritsPermissions',
'_permissions',
'_state',
'_ts',
'_versionKey'
];
function isPathString(key) {
return key.startsWith('/');
}
function supportedValueType(v) {
return (0, js_utils_1.isBoolean)(v) || (0, isString_1.isString)(v);
}
var Branch = (function () {
function Branch(_a) {
var _b;
var branchId = _a.branchId, repo = _a.repo;
this.binaryReferences = {};
this.nodes = (_b = {},
_b[constants_1.UUID_NIL] = {
_childOrder: '_ts DESC',
_id: constants_1.UUID_NIL,
_indexConfig: DEFAULT_INDEX_CONFIG,
_inheritsPermissions: false,
_name: '',
_nodeType: 'default',
_path: '/',
_permissions: [{
principal: 'role:system.admin',
allow: [
'READ',
'CREATE',
'MODIFY',
'DELETE',
'PUBLISH',
'READ_PERMISSIONS',
'WRITE_PERMISSIONS'
],
deny: []
}],
_state: 'DEFAULT',
_ts: Branch.generateInstantString(),
_versionKey: '00000000-0000-4000-8000-000000000001'
},
_b);
this.pathIndex = {
'/': constants_1.UUID_NIL
};
this.searchIndex = {
_name: {
'': [constants_1.UUID_NIL]
},
_nodeType: {
default: [constants_1.UUID_NIL]
},
_parentPath: {},
_path: {
'/': [constants_1.UUID_NIL]
}
};
this.id = branchId;
this.repo = repo;
this.log = this.repo.log;
}
Branch.generateInstantString = function () {
return new Date().toISOString();
};
Branch.prototype._createNodeInternal = function (_a) {
var e_1, _b, e_2, _c, e_3, _d;
var _e = _a._id, _id = _e === void 0 ? this.repo.generateId() : _e, _f = _a._indexConfig, _indexConfig = _f === void 0 ? DEFAULT_INDEX_CONFIG : _f, _name = _a._name, _g = _a._parentPath, _parentPath = _g === void 0 ? '/' : _g, _h = _a._ts, _ts = _h === void 0 ? Branch.generateInstantString() : _h, _j = _a._versionKey, _versionKey = _j === void 0 ? this.repo.generateId() : _j, rest = tslib_1.__rest(_a, ["_id", "_indexConfig", "_name", "_parentPath", "_ts", "_versionKey"]);
try {
for (var IGNORED_ON_CREATE_1 = tslib_1.__values(IGNORED_ON_CREATE), IGNORED_ON_CREATE_1_1 = IGNORED_ON_CREATE_1.next(); !IGNORED_ON_CREATE_1_1.done; IGNORED_ON_CREATE_1_1 = IGNORED_ON_CREATE_1.next()) {
var k = IGNORED_ON_CREATE_1_1.value;
if (rest.hasOwnProperty(k)) {
delete rest[k];
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (IGNORED_ON_CREATE_1_1 && !IGNORED_ON_CREATE_1_1.done && (_b = IGNORED_ON_CREATE_1.return)) _b.call(IGNORED_ON_CREATE_1);
}
finally { if (e_1) throw e_1.error; }
}
if (!_name) {
_name = _id;
}
if (!rest._nodeType) {
rest._nodeType = 'default';
}
if (!_parentPath.endsWith('/')) {
_parentPath += '/';
}
if (_parentPath !== '/' &&
!this.existsNode(_parentPath)) {
throw new NodeNotFoundException_1.NodeNotFoundException("Cannot create node with name ".concat(_name, ", parent '").concat(_parentPath, "' not found"));
}
if (this.nodes.hasOwnProperty(_id)) {
throw new Error("Node already exists with ".concat(_id, " repository: ").concat(this.repo.id, " branch: ").concat(this.id));
}
var _path = "".concat(_parentPath).concat(_name);
if (this.pathIndex.hasOwnProperty(_path)) {
throw new NodeAlreadyExistAtPathException_1.NodeAlreadyExistAtPathException(_path, this.repo.id, this);
}
var cleanedData = (0, processNestedData_1.processNestedData)({
branch: this,
data: rest,
nodeId: _id
});
var createdNode = tslib_1.__assign({ _id: _id, _indexConfig: _indexConfig, _name: _name, _path: _path, _state: 'DEFAULT', _ts: _ts, _versionKey: _versionKey }, (0, enonify_1.enonify)(cleanedData));
this.nodes[_id] = createdNode;
this.pathIndex[_path] = _id;
if (this.searchIndex['_name'][_name]) {
this.searchIndex['_name'][_name].push(_id);
}
else {
this.searchIndex['_name'][_name] = [_id];
}
var strippedParentPath = _parentPath === '/'
? '/'
: _parentPath.endsWith('/')
? _parentPath.substring(0, _parentPath.length - 1)
: _parentPath;
if (this.searchIndex['_parentPath'][strippedParentPath]) {
this.searchIndex['_parentPath'][strippedParentPath].push(_id);
}
else {
this.searchIndex['_parentPath'][strippedParentPath] = [_id];
}
if (this.searchIndex['_path'][_path]) {
this.searchIndex['_path'][_path].push(_id);
}
else {
this.searchIndex['_path'][_path] = [_id];
}
var restKeys = Object.keys(cleanedData).filter(function (k) { return !SEARCH_INDEX_BLACKLIST.includes(k); });
try {
RestKeys: for (var restKeys_1 = tslib_1.__values(restKeys), restKeys_1_1 = restKeys_1.next(); !restKeys_1_1.done; restKeys_1_1 = restKeys_1.next()) {
var rootProp = restKeys_1_1.value;
var rootPropValue = cleanedData[rootProp];
if (!(supportedValueType(rootPropValue)
|| (Array.isArray(rootPropValue)
&& rootPropValue.every(function (k) { return supportedValueType(k); })))) {
if (this.repo.server.indexWarnings) {
this.log.warning('mock-xp is only able to (index for quering) boolean and string properties, skipping rootProp:%s with value:%s', rootProp, (0, js_utils_1.toStr)(rootPropValue));
}
continue RestKeys;
}
var valueArr = (0, forceArray_1.forceArray)(rootPropValue);
try {
for (var valueArr_1 = (e_3 = void 0, tslib_1.__values(valueArr)), valueArr_1_1 = valueArr_1.next(); !valueArr_1_1.done; valueArr_1_1 = valueArr_1.next()) {
var valueArrItem = valueArr_1_1.value;
if (!this.searchIndex[rootProp]) {
this.searchIndex[rootProp] = {};
}
if (this.searchIndex[rootProp][valueArrItem]) {
this.searchIndex[rootProp][valueArrItem].push(_id);
}
else {
this.searchIndex[rootProp][valueArrItem] = [_id];
}
}
}
catch (e_3_1) { e_3 = { error: e_3_1 }; }
finally {
try {
if (valueArr_1_1 && !valueArr_1_1.done && (_d = valueArr_1.return)) _d.call(valueArr_1);
}
finally { if (e_3) throw e_3.error; }
}
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (restKeys_1_1 && !restKeys_1_1.done && (_c = restKeys_1.return)) _c.call(restKeys_1);
}
finally { if (e_2) throw e_2.error; }
}
return (0, deref_1.default)(createdNode);
};
Branch.prototype.createNode = function (params) {
return this._createNodeInternal(params);
};
Branch.prototype.keyToId = function (key) {
var maybeId = key;
if (isPathString(key)) {
var path = (key.length > 1 && key.endsWith('/')) ? key.substring(0, key.length - 1) : key;
maybeId = this.pathIndex[path];
if (!maybeId) {
return undefined;
}
}
if ((0, isUuidV4String_1.isUuidV4String)(maybeId) || maybeId === constants_1.UUID_NIL) {
return maybeId;
}
this.log.debug("key not an id! key:".concat(key));
return undefined;
};
Branch.prototype.existsNode = function (key) {
var id = this.keyToId(key);
if (!id) {
return false;
}
return this.nodes.hasOwnProperty(id);
};
Branch.prototype.deleteNode = function (keys) {
var e_4, _a, e_5, _b;
var _c;
var keysArray = (0, forceArray_1.forceArray)(keys);
var deletedKeys = [];
try {
NodeKeys: for (var keysArray_1 = tslib_1.__values(keysArray), keysArray_1_1 = keysArray_1.next(); !keysArray_1_1.done; keysArray_1_1 = keysArray_1.next()) {
var key = keysArray_1_1.value;
var maybeNode = void 0;
try {
maybeNode = this.getNode(key);
}
catch (e) {
}
if (!maybeNode) {
this.log.warning("Node with key:'".concat(key, "' doesn't exist. Skipping delete."));
continue NodeKeys;
}
try {
delete this.pathIndex[maybeNode._path];
var rootProps = Object.keys(maybeNode).filter(function (k) { return !SEARCH_INDEX_BLACKLIST.includes(k); });
try {
RootProps: for (var rootProps_1 = (e_5 = void 0, tslib_1.__values(rootProps)), rootProps_1_1 = rootProps_1.next(); !rootProps_1_1.done; rootProps_1_1 = rootProps_1.next()) {
var rootPropKey = rootProps_1_1.value;
var rootPropValue = maybeNode[rootPropKey];
if (!(0, isString_1.isString)(rootPropValue)) {
if (this.repo.server.indexWarnings) {
this.log.warning('mock-xp is not able to handle non-string properties yet, skipping rootProp:%s with value:%s', rootPropKey, (0, js_utils_1.toStr)(rootPropValue));
}
continue RootProps;
}
if ((_c = this.searchIndex[rootPropKey]) === null || _c === void 0 ? void 0 : _c[rootPropValue]) {
this.searchIndex[rootPropKey][rootPropValue].splice(this.searchIndex[rootPropKey][rootPropValue].indexOf(maybeNode._id), 1);
}
}
}
catch (e_5_1) { e_5 = { error: e_5_1 }; }
finally {
try {
if (rootProps_1_1 && !rootProps_1_1.done && (_b = rootProps_1.return)) _b.call(rootProps_1);
}
finally { if (e_5) throw e_5.error; }
}
delete this.nodes[maybeNode._id];
deletedKeys.push(key);
}
catch (e) {
this.log.error("Something went wrong when trying to delete node with key:'".concat(key, "'"));
}
}
}
catch (e_4_1) { e_4 = { error: e_4_1 }; }
finally {
try {
if (keysArray_1_1 && !keysArray_1_1.done && (_a = keysArray_1.return)) _a.call(keysArray_1);
}
finally { if (e_4) throw e_4.error; }
}
return deletedKeys;
};
Branch.prototype.getBranchId = function () {
return this.id;
};
Branch.prototype.getNode = function () {
var _this = this;
var keys = [];
for (var _i = 0; _i < arguments.length; _i++) {
keys[_i] = arguments[_i];
}
if (!keys.length) {
return [];
}
var flattenedKeys = (0, flatten_1.flatten)(keys);
var existingKeys = flattenedKeys
.map(function (k) { return _this.existsNode(k) ? k : undefined; })
.filter(function (k) { return k; });
var nodes = existingKeys.map(function (key) {
var id = _this.keyToId(key);
if (!id) {
throw new Error("Can't get id from key:".concat(key, ", even though exists???"));
}
return (0, deref_1.default)(_this.nodes[id]);
});
return nodes.length > 1
? nodes
: nodes[0];
};
Branch.prototype.getNodeActiveVersion = function (_a) {
var key = _a.key;
var node = this.getNode(key);
if (node) {
return {
versionId: node._versionKey,
nodeId: node._id,
nodePath: node._path,
timestamp: node._ts
};
}
this.log.error("No such node with key:'".concat(key));
return null;
};
Branch.prototype.getRepo = function () {
return this.repo;
};
Branch.prototype.modifyNode = function (_a) {
var _this = this;
var key = _a.key, editor = _a.editor;
var node = this.getNode(key);
if (!node) {
throw new Error("modify: Node with key:".concat(key, " not found!"));
}
var dereffedNode = (0, deref_1.default)(node);
var _id = dereffedNode._id;
var _name = dereffedNode._name;
var _path = dereffedNode._path;
var editedNode = editor(dereffedNode);
var foundBinaryReferenceNames = [];
var cleanedData = (0, processNestedData_1.processNestedData)({
branch: this,
data: editedNode,
foundBinaryReferenceNames: foundBinaryReferenceNames,
nodeId: _id
});
if (this.binaryReferences[_id]) {
Object.keys(this.binaryReferences[_id]).forEach(function (binaryReferenceName) {
if (!foundBinaryReferenceNames.includes(binaryReferenceName)) {
_this.log.debug('%s:%s:%s Binary reference: %s no longer used, removing sha512: %s', _this.repo.id, _this.id, _id, binaryReferenceName, _this.binaryReferences[_id][binaryReferenceName]);
delete _this.binaryReferences[_id][binaryReferenceName];
}
});
}
var modifiedNode = (0, sortKeys_1.sortKeys)(tslib_1.__assign(tslib_1.__assign({}, cleanedData), { _id: _id, _name: _name, _path: _path }));
modifiedNode._versionKey = this.repo.generateId();
this.nodes[_id] = modifiedNode;
return (0, deref_1.default)(this.nodes[_id]);
};
Branch.prototype.moveNode = function (_a) {
var source = _a.source, target = _a.target;
var node = this.getNode(source);
if (!node) {
this.log.error('move: Node with source:%s not found!', source);
throw new Error("move: Node with source:".concat(source, " not found!"));
}
var previousPath = node._path;
if (target.endsWith('/')) {
node._path = "".concat(target).concat(node._name);
}
else if (target.startsWith('/')) {
var targetParts = target.split('/');
var newName = targetParts.pop();
node._name = newName;
node._path = "".concat(targetParts.join('/'), "/").concat(newName);
}
else {
var pathParts = node._path.split('/');
pathParts.pop();
node._name = target;
node._path = "".concat(pathParts.join('/'), "/").concat(node._name);
}
if (node._path === previousPath) {
this.log.warning('move: Node with source:%s already at target:%s', source, target);
return null;
}
var newPathParts = node._path.split('/');
newPathParts.pop();
var newParentPath = newPathParts.join('/');
if (!newParentPath.endsWith('/')) {
newParentPath += '/';
}
if (newParentPath !== '/' &&
!this.existsNode(newParentPath)) {
throw new NodeNotFoundException_1.NodeNotFoundException("Cannot move node with source ".concat(source, " to target ").concat(target, ": Parent '").concat(newParentPath, "' not found!"));
}
if (this.pathIndex.hasOwnProperty(node._path)) {
throw new NodeAlreadyExistAtPathException_1.NodeAlreadyExistAtPathException("Cannot move node with source ".concat(source, " to target ").concat(target, ": Node already exists at ").concat(node._path, " repository: ").concat(this.repo.id, " branch: ").concat(this.id, "!"));
}
delete this.pathIndex[previousPath];
this.pathIndex[node._path] = node._id;
this.nodes[node._id] = node;
return (0, deref_1.default)(this.nodes[node._id]);
};
Branch.prototype._overwriteNode = function (_a) {
var node = _a.node;
var previousPath = this.nodes[node._id]._path;
delete this.pathIndex[previousPath];
this.pathIndex[node._path] = node._id;
this.nodes[node._id] = node;
return (0, deref_1.default)(this.nodes[node._id]);
};
Branch.prototype._handleHasValueFilter = function (hasValueFilter) {
var e_6, _a, e_7, _b;
var hasValueIds = [];
var _c = hasValueFilter.hasValue, field = _c.field, values = _c.values;
if (!SEARCH_INDEX_BLACKLIST.includes(field)
&& this.searchIndex[field]) {
try {
for (var values_1 = tslib_1.__values(values), values_1_1 = values_1.next(); !values_1_1.done; values_1_1 = values_1.next()) {
var value = values_1_1.value;
if (!supportedValueType(value)) {
this.log.error('query: Unsupported value type:%s', (0, js_utils_1.toStr)(value));
}
else {
if (this.searchIndex[field][value]
&& this.searchIndex[field][value].length) {
var ids = this.searchIndex[field][value];
try {
for (var ids_1 = (e_7 = void 0, tslib_1.__values(ids)), ids_1_1 = ids_1.next(); !ids_1_1.done; ids_1_1 = ids_1.next()) {
var id = ids_1_1.value;
if (!hasValueIds.includes(id)) {
hasValueIds.push(id);
}
}
}
catch (e_7_1) { e_7 = { error: e_7_1 }; }
finally {
try {
if (ids_1_1 && !ids_1_1.done && (_b = ids_1.return)) _b.call(ids_1);
}
finally { if (e_7) throw e_7.error; }
}
}
}
}
}
catch (e_6_1) { e_6 = { error: e_6_1 }; }
finally {
try {
if (values_1_1 && !values_1_1.done && (_a = values_1.return)) _a.call(values_1);
}
finally { if (e_6) throw e_6.error; }
}
}
return hasValueIds;
};
Branch.prototype.query = function (_a) {
var e_8, _b, e_9, _c, e_10, _d, e_11, _e, e_12, _f, e_13, _g, e_14, _h;
var _this = this;
var _j, _k;
var _l = _a.count, count = _l === void 0 ? 10 : _l, filters = _a.filters, _m = _a.query, query = _m === void 0 ? '' : _m, _o = _a.start, start = _o === void 0 ? 0 : _o;
var filtersMustSets = [];
var filtersMustNotSets = [];
var filtersMustIds = [];
var filtersMustNotIds = [];
if ((Array.isArray(filters) && (0, js_utils_1.isFilter)(filters))
|| (0, js_utils_1.isFilter)(filters)) {
var filtersArray = (0, forceArray_1.forceArray)(filters);
try {
for (var filtersArray_1 = tslib_1.__values(filtersArray), filtersArray_1_1 = filtersArray_1.next(); !filtersArray_1_1.done; filtersArray_1_1 = filtersArray_1.next()) {
var filter = filtersArray_1_1.value;
if ((0, js_utils_1.isBooleanFilter)(filter)) {
var must = (0, forceArray_1.forceArray)((_j = filter.boolean.must) !== null && _j !== void 0 ? _j : []);
var mustNot = (0, forceArray_1.forceArray)((_k = filter.boolean.mustNot) !== null && _k !== void 0 ? _k : []);
try {
for (var must_1 = (e_9 = void 0, tslib_1.__values(must)), must_1_1 = must_1.next(); !must_1_1.done; must_1_1 = must_1.next()) {
var mustFilter = must_1_1.value;
if ((0, js_utils_1.isHasValueFilter)(mustFilter)) {
filtersMustSets.push(this._handleHasValueFilter(mustFilter));
}
}
}
catch (e_9_1) { e_9 = { error: e_9_1 }; }
finally {
try {
if (must_1_1 && !must_1_1.done && (_c = must_1.return)) _c.call(must_1);
}
finally { if (e_9) throw e_9.error; }
}
try {
for (var mustNot_1 = (e_10 = void 0, tslib_1.__values(mustNot)), mustNot_1_1 = mustNot_1.next(); !mustNot_1_1.done; mustNot_1_1 = mustNot_1.next()) {
var mustNotFilter = mustNot_1_1.value;
if ((0, js_utils_1.isHasValueFilter)(mustNotFilter)) {
filtersMustNotSets.push(this._handleHasValueFilter(mustNotFilter));
}
}
}
catch (e_10_1) { e_10 = { error: e_10_1 }; }
finally {
try {
if (mustNot_1_1 && !mustNot_1_1.done && (_d = mustNot_1.return)) _d.call(mustNot_1);
}
finally { if (e_10) throw e_10.error; }
}
}
else if ((0, js_utils_1.isHasValueFilter)(filter)) {
filtersMustSets.push(this._handleHasValueFilter(filter));
}
}
}
catch (e_8_1) { e_8 = { error: e_8_1 }; }
finally {
try {
if (filtersArray_1_1 && !filtersArray_1_1.done && (_b = filtersArray_1.return)) _b.call(filtersArray_1);
}
finally { if (e_8) throw e_8.error; }
}
filtersMustIds = (0, intersect_1.default)(filtersMustSets);
filtersMustNotIds = uniqs_1.default.apply(void 0, tslib_1.__spreadArray([], tslib_1.__read(filtersMustNotSets), false));
var allMustIds = uniqs_1.default.apply(void 0, tslib_1.__spreadArray([], tslib_1.__read(filtersMustSets), false));
try {
for (var allMustIds_1 = tslib_1.__values(allMustIds), allMustIds_1_1 = allMustIds_1.next(); !allMustIds_1_1.done; allMustIds_1_1 = allMustIds_1.next()) {
var anMustId = allMustIds_1_1.value;
if (!filtersMustIds.includes(anMustId)
&& !filtersMustNotIds.includes(anMustId)) {
filtersMustNotIds.push(anMustId);
}
}
}
catch (e_11_1) { e_11 = { error: e_11_1 }; }
finally {
try {
if (allMustIds_1_1 && !allMustIds_1_1.done && (_e = allMustIds_1.return)) _e.call(allMustIds_1);
}
finally { if (e_11) throw e_11.error; }
}
}
var allIds = Object.keys(this.nodes);
if (query === '') {
var mustIds = filtersMustIds.length ? (0, intersect_1.default)([allIds, filtersMustIds]) : allIds;
var hitIds = [];
try {
for (var mustIds_1 = tslib_1.__values(mustIds), mustIds_1_1 = mustIds_1.next(); !mustIds_1_1.done; mustIds_1_1 = mustIds_1.next()) {
var matchingId = mustIds_1_1.value;
if (!filtersMustNotIds.includes(matchingId)) {
hitIds.push(matchingId);
}
}
}
catch (e_12_1) { e_12 = { error: e_12_1 }; }
finally {
try {
if (mustIds_1_1 && !mustIds_1_1.done && (_f = mustIds_1.return)) _f.call(mustIds_1);
}
finally { if (e_12) throw e_12.error; }
}
var total = hitIds.length;
if (count === -1) {
count = total;
}
return {
aggregations: {},
count: Math.min(count, total),
hits: hitIds.map(function (id) { return ({
id: id,
score: 1
}); }).slice(start, start + count),
total: total
};
}
if ((0, isString_1.isString)(query)) {
throw new Error("query: unhandeled query string: ".concat(query, "!"));
}
if ((0, js_utils_1.isQueryDsl)(query)) {
this.log.debug('query:%s Search Index: %s', query, this.searchIndex);
var mustSets_1 = [];
var mustNotSets_1 = [];
var boolean = query.boolean;
if (boolean) {
var _p = boolean, must = _p.must, mustNot = _p.mustNot;
if (must) {
(0, forceArray_1.forceArray)(must).forEach(function (mustDsl) {
var mustIds = [];
var inDslExpression = mustDsl.in, term = mustDsl.term;
if (inDslExpression) {
var _a = inDslExpression, field_1 = _a.field, values = _a.values;
if (!SEARCH_INDEX_BLACKLIST.includes(field_1)
&& _this.searchIndex[field_1]
&& values.every(function (v) { return supportedValueType(v); })) {
values.forEach(function (value) {
if (_this.searchIndex[field_1][value]) {
_this.searchIndex[field_1][value].forEach(function (id) {
if (!mustIds.includes(id)) {
mustIds.push(id);
}
});
}
});
}
}
else if (term) {
var _b = term, field = _b.field, value = _b.value;
if (!SEARCH_INDEX_BLACKLIST.includes(field)
&& _this.searchIndex[field]
&& supportedValueType(value)
&& _this.searchIndex[field][value]) {
_this.searchIndex[field][value].forEach(function (id) {
if (!mustIds.includes(id)) {
mustIds.push(id);
}
});
}
}
mustSets_1.push(mustIds);
});
}
if (mustNot) {
(0, forceArray_1.forceArray)(mustNot).forEach(function (mustNotDsl) {
var mustNotIds = [];
var inDslExpression = mustNotDsl.in, term = mustNotDsl.term;
if (inDslExpression) {
var _a = inDslExpression, field_2 = _a.field, values = _a.values;
if (!SEARCH_INDEX_BLACKLIST.includes(field_2)
&& _this.searchIndex[field_2]
&& values.every(function (v) { return supportedValueType(v); })) {
values.forEach(function (value) {
if (_this.searchIndex[field_2][value]) {
_this.searchIndex[field_2][value].forEach(function (id) {
if (!mustNotIds.includes(id)) {
mustNotIds.push(id);
}
});
}
});
}
}
else if (term) {
var _b = term, field = _b.field, value = _b.value;
if (!SEARCH_INDEX_BLACKLIST.includes(field)
&& _this.searchIndex[field]
&& supportedValueType(value)
&& _this.searchIndex[field][value]) {
_this.searchIndex[field][value].forEach(function (id) {
if (!mustNotIds.includes(id)) {
mustNotIds.push(id);
}
});
}
}
mustNotSets_1.push(mustNotIds);
});
}
}
var filterAndQueryMustSets = filtersMustSets.concat(mustSets_1);
var mustIds = (0, intersect_1.default)(filterAndQueryMustSets);
var partialMustIds = [];
var allMustIds = uniqs_1.default.apply(void 0, tslib_1.__spreadArray([], tslib_1.__read(filterAndQueryMustSets), false));
try {
for (var allMustIds_2 = tslib_1.__values(allMustIds), allMustIds_2_1 = allMustIds_2.next(); !allMustIds_2_1.done; allMustIds_2_1 = allMustIds_2.next()) {
var anMustId = allMustIds_2_1.value;
if (!mustIds.includes(anMustId)
&& !partialMustIds.includes(anMustId)) {
partialMustIds.push(anMustId);
}
}
}
catch (e_13_1) { e_13 = { error: e_13_1 }; }
finally {
try {
if (allMustIds_2_1 && !allMustIds_2_1.done && (_g = allMustIds_2.return)) _g.call(allMustIds_2);
}
finally { if (e_13) throw e_13.error; }
}
var filterAndQueryMustNotSets = filtersMustNotSets.concat(mustNotSets_1, partialMustIds);
var mustNotIds = uniqs_1.default.apply(void 0, tslib_1.__spreadArray([], tslib_1.__read(filterAndQueryMustNotSets), false));
var someorAllIds = (filtersMustSets.length || mustSets_1.length) ? (0, intersect_1.default)([allIds, mustIds]) : allIds;
var hitIds = [];
try {
for (var someorAllIds_1 = tslib_1.__values(someorAllIds), someorAllIds_1_1 = someorAllIds_1.next(); !someorAllIds_1_1.done; someorAllIds_1_1 = someorAllIds_1.next()) {
var matchingId = someorAllIds_1_1.value;
if (!mustNotIds.includes(matchingId)) {
hitIds.push(matchingId);
}
}
}
catch (e_14_1) { e_14 = { error: e_14_1 }; }
finally {
try {
if (someorAllIds_1_1 && !someorAllIds_1_1.done && (_h = someorAllIds_1.return)) _h.call(someorAllIds_1);
}
finally { if (e_14) throw e_14.error; }
}
if (count === -1) {
count = hitIds.length;
}
return {
aggregations: {},
count: Math.min(count, hitIds.length),
hits: hitIds.map(function (id) { return ({
id: id,
score: 1
}); }).slice(start, start + count),
total: hitIds.length
};
}
throw new Error("query: unhandeled query dsl: ".concat(query, "!"));
};
Branch.prototype.refresh = function (_a) {
var _b = _a === void 0 ? {} : _a, _c = _b.mode, mode = _c === void 0 ? 'ALL' : _c, _d = _b.repo, repo = _d === void 0 ? this.repo.id : _d, _e = _b.branch, branch = _e === void 0 ? this.id : _e;
this.log.debug("refresh({ mode:".concat(mode, " repo:").concat(repo, " branch:").concat(branch, " })"));
return;
};
return Branch;
}());
exports.Branch = Branch;