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

100% Statements 38/38
50% Branches 2/4
100% Functions 1/1
100% Lines 38/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 1x 1x 1x 7x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x  
import {type IMeta, handler} from '@feasibleone/blong';
 
import * as model from './accessModel.ts';
 
type KnexQb = any;
 
/**
 * `access.user.remove` — delete a user and everything that references it.
 *
 * Removes the `hasRole` graph edges (refreshing the materialized path), the
 * credential rows, any session rows, the `access_user` row and finally the
 * `core_resource` row.
 */
export default handler(() => ({
    async accessUserRemove(
        params: {userId?: 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.userId);
        if (!hex) return {success: true};
        const buf = Buffer.from(hex, 'hex');
        const roleIds = await model.listEdgeObjectIds(qb, hex, 'hasRole');
        if (roleIds.length) {
            await qb('core_triple')
                .where('subjectId', buf)
                .where('predicateName', 'hasRole')
                .del();
            await qb.raw('CALL access_pathRefresh()');
        }
        await qb('access_credential').where('userId', buf).del();
        await qb('access_session').where('userId', buf).del();
        await qb('access_user').where('userId', buf).del();
        await qb('core_resource').where('resourceId', buf).del();
        return {success: true};
    },
}));