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

34.21% Statements 13/38
100% Branches 1/1
0% Functions 0/1
34.21% Lines 13/38

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 391x 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.remove` — delete a capability, its graph edges and its
 * resource row.
 */
export default handler(() => ({
    async accessCapabilityRemove(
        params: {capabilityId?: string},
        $meta: IMeta,
    ): Promise<{success: boolean}> {
        const qb: KnexQb = this.config?.context?.queryBuilder;
        if (!qb) throw new Error('Database not available');
        const hex = model.binHex(params.capabilityId);
        if (!hex) return {success: true};
        const buf = Buffer.from(hex, 'hex');
        const actionIds = await model.listEdgeObjectIds(qb, hex, 'hasAction');
        if (actionIds.length) {
            await qb('core_triple')
                .where('subjectId', buf)
                .where('predicateName', 'hasAction')
                .del();
            await qb.raw('CALL access_pathRefresh()');
        }
        // A capability can be granted to roles — remove those edges too.
        await qb('core_triple')
            .where('objectId', buf)
            .where('predicateName', 'hasCapability')
            .del();
        await qb('access_capability').where('capabilityId', buf).del();
        await qb('core_resource').where('resourceId', buf).del();
        return {success: true};
    },
}));