All files / blong-browser/src/model/component subjectObjectComponent.ts

100% Statements 63/63
66.66% Branches 4/6
100% Functions 2/2
100% Lines 63/63

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 641x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
import type {IAdapter, IComponent, IHandlerProxy, IModelSpec} from '@feasibleone/blong';
import {withDefaults} from '@feasibleone/blong-mock';
import type {IPortalConfig} from '../../index.js';
import {subjectObjectBrowse} from './subjectObjectBrowse.js';
import {subjectObjectNew} from './subjectObjectNew.js';
import {subjectObjectOpen} from './subjectObjectOpen.js';
import {subjectObjectReport} from './subjectObjectReport.js';
 
export default async function component(
    this: IAdapter<
        {
            portal?: IPortalConfig;
            context?: {
                menus?: Record<string, unknown[]>;
            };
        },
        object
    >,
    models: IModelSpec[],
    blong: IHandlerProxy<unknown>,
) {
    const components: Record<string, () => Promise<IComponent | IPortalConfig>> = {};
    this.config!.context ||= {menus: {}};
    this.config!.context.menus ||= {};
    for (const rawModel of models) {
        const model = withDefaults(rawModel);
        const {subject, object, subjectTitle} = model;
        components[`${subject}.${object}.browse`] = await subjectObjectBrowse(model, blong);
        components[`${subject}.${object}.new`] = await subjectObjectNew(model, blong);
        components[`${subject}.${object}.open`] = await subjectObjectOpen(model, blong);
        if (model.report?.permission) {
            // Default report — always registered when report permission is set
            components[`${subject}.${object}.report`] = await subjectObjectReport(model, blong);
            // Named reports — one component per entry in model.reports
            for (const reportId of Object.keys(model.reports)) {
                components[`${subject}.${object}.report.${reportId}`] = await subjectObjectReport(
                    model,
                    blong,
                    reportId,
                );
            }
        }
        this.config!.context.menus[`${subjectTitle}`] ||= [];
        this.config!.context.menus[`${subjectTitle}`].push(`${subject}.${object}.browse`);
    }
 
    components.portalConfigGet = async () => {
        return blong.lib.merge(
            {},
            {
                menu: Object.entries(this.config!.context?.menus || {}).map(([subject, items]) => ({
                    title: subject,
                    items,
                })),
                title: 'Blong',
                name: 'blong-portal',
            },
            this.config!.portal,
        );
    };
 
    return components;
}