All files / global/manifest/services menu-builder.mjs

94.44% Statements 34/36
100% Branches 18/18
60% Functions 3/5
100% Lines 31/31

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92                                    3x 3x   3x   3x               3x               3x                 3x       10x 8x 8x 10x 9x 9x 9x 8x 1x 1x   7x 4x 12x 12x     3x 3x     1x 1x       11x 8x 8x       15x       15x 15x    
/*
  Copyright (C) 2023 Sber
 
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at
 
          http://www.apache.org/licenses/LICENSE-2.0
 
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/
 
import {getLoggerWithTag} from '@global/logger/v2/logger.mjs';
 
const LOG_TAG = 'menu-builder';
const logger = getLoggerWithTag(LOG_TAG);
 
const DEF_ORDER = 10000;
// Служебные пункты ядра помечены флагом service: интерфейс закрепляет их отдельно от дерева метамодели
const JSONATA = {
    'title': 'JSONata',
    'route': '/devtool',
    'service': true,
    'icon': 'mdi-card-text-outline',
    'location': 'devtool',
    'order': DEF_ORDER
};
const SEARCH = {
    'title': 'Поиск',
    'route': '/search',
    'service': true,
    'icon': 'mdi-magnify',
    'location': 'Поиск',
    'order': DEF_ORDER - 1
};
const PROBLEMS = {
    'title': 'Проблемы',
    'location': 'Проблемы',
    'route': '/problems',
    'service': true,
    'icon': 'mdi-alert-circle-outline',
    'order': DEF_ORDER
};
 
const isUrl = new RegExp('^[a-zA-Z]*\\:.*$', 'i');
 
 
export async function buildUserMenu(manifest, driver) {
    if (!manifest?.entities) return [];
    const output = [];
    for (const [key, entity] of Object.entries(manifest.entities)) {
        if (!entity.menu) continue;
        const query = `($manifest := $; entities."${key}".(($exists(menu.source) ? $parsesource(menu) : $eval(menu, $manifest)).{"route": link, "location": location, "icon": icon, "order": order}))`;
        try {
            const ele = await driver.expression(query).evaluate(manifest);
            if (!ele) {
                logger.info(() => `Menu query for entity ${key} didn't return anything`);
                continue;
            }
            if (Array.isArray(ele)) {
                for (const item of ele) {
                    prepareElement(item);
                    output.push(item);
                }
            } else {
                prepareElement(ele);
                output.push(ele);
            }
        } catch (e) {
            logger.error(() => `Failed to build user menu for entity ${key}: ${JSON.stringify(e)}`);
            continue;
        }
    }
 
    output.sort((a, b) => a.order - b.order || a.location.localeCompare(b.location));
    output.push(SEARCH, PROBLEMS, JSONATA);
    return output;
}
 
function prepareElement(ele) {
    ele.route = ele.route ? (
        isUrl.test(ele.route) ? ele.route
        : (ele.route.startsWith('/') ? ele.route : '/' + ele.route )
    ) : undefined;
    ele.title = ele.location?.slice(ele.location.lastIndexOf('/') + 1);
    ele.order = ele.order ?? DEF_ORDER;
}