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

39.02% Statements 16/41
100% Branches 1/1
0% Functions 0/1
39.02% Lines 16/41

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 421x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 10x                                                   10x  
import {type IMeta} from '@feasibleone/blong';
 
import {handler} from './handlerGroup.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(({lib: {binHex, bufToBase64, capabilityActionRows, resourceNameFor}}) => ({
    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 = binHex(params.capabilityId ?? capability.capabilityId);
        if (hex) {
            result.capability = {
                ...capability,
                capabilityId: bufToBase64(capability.capabilityId),
                capabilityName:
                    (await resourceNameFor(qb, hex)) ?? capability.capabilityName ?? null,
            };
            const {action, otherAction} = await capabilityActionRows(qb, hex);
            result.action = action;
            result.otherAction = otherAction;
        }
        return result;
    },
}));