All files / realm/blong-access/adapter/db accessCapabilityGet.ts

37.2% Statements 16/43
100% Branches 1/1
0% Functions 0/1
37.2% Lines 16/43

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 441x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x                                                       7x  
import {type IMeta, handler} from '@feasibleone/blong';
 
import * as model from './accessModel.ts';
 
type KnexQb = any;
 
/**
 * `access.capability.get` — a capability plus its assigned actions.
 *
 * The standard row comes from `super.exec`; the display name is joined from
 * `core_resource` and the `hasAction` edges are returned as the `action` pivot
 * rows (standard CRUD booleans) and `otherAction` rows (non-CRUD actions).
 */
export default handler(() => ({
    async accessCapabilityGet(
        params: {capabilityId?: string},
        $meta: IMeta,
    ): Promise<Record<string, unknown>> {
        const qb: KnexQb = this.config?.context?.queryBuilder;
        if (!qb) throw new Error('Database not available');
        const result = (await super.exec(params, $meta)) as Record<string, unknown>;
        const capability = (result.capability ?? {}) as {
            capabilityId?: string;
            capabilityName?: string;
            description?: string;
        };
        const hex = model.binHex(params.capabilityId ?? capability.capabilityId);
        if (hex) {
            result.capability = {
                ...capability,
                capabilityId: model.bufToBase64(capability.capabilityId),
                capabilityName:
                    (await model.resourceNameFor(qb, hex)) ??
                    capability.capabilityName ??
                    null,
            };
            const {action, otherAction} = await model.capabilityActionRows(qb, hex);
            result.action = action;
            result.otherAction = otherAction;
        }
        return result;
    },
}));