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

100% Statements 24/24
75% Branches 3/4
100% Functions 1/1
100% Lines 24/24

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 251x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x 2x 2x 2x 2x 2x 2x 2x 2x 7x  
import {type IMeta, handler} from '@feasibleone/blong';
 
/** Fields joined for display by `access.acl.find` / `.get` — never persisted. */
const VIRTUAL_FIELDS = ['principalName', 'actionName', 'targetName'];
 
/**
 * `access.acl.add` — create an ACL rule.
 *
 * The write itself is the generic CRUD (`super.exec`); this handler only removes
 * the virtual display fields the editor round-trips and supplies the `ulid`
 * marker for `aclId`, whose value the adapter generates — an ACL rule is
 * identified by its own ULID rather than by a `core.resource` row.
 */
export default handler(() => ({
    async accessAclAdd(
        params: {acl?: Record<string, unknown>} & Record<string, unknown>,
        $meta: IMeta,
    ): Promise<unknown> {
        const acl = {...(params.acl ?? {})};
        for (const field of VIRTUAL_FIELDS) delete acl[field];
        if (!acl.aclId) acl.aclId = 'ulid';
        return super.exec({...params, acl}, $meta);
    },
}));