All files / src/security/helpers convertPermsToModelFields.ts

42.85% Statements 9/21
100% Branches 0/0
0% Functions 0/2
42.85% Lines 9/21

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 211x 1x 1x 1x 1x 1x 1x 1x 1x                        
 
 
import { _ } from 'good-cop'
import { capitalize1st } from 'topkat-utils'
 
 
const defVal = () => _.boolean().default(false)
 
/** This one helps to convert a list of roles into boolean values */
export function convertRoleToPermsToModelFields<R extends readonly string[]>(
  roles: R
): Record<`is${Capitalize<R[number]>}`, ReturnType<typeof defVal>> {

  type AddedUserModelPermissionFields = Record<`is${Capitalize<R[number]>}`, ReturnType<typeof defVal>>
  const addedPerms = {} as AddedUserModelPermissionFields
  for (const role of roles) {
    addedPerms['is' + capitalize1st(role)] = defVal()
  }

  return addedPerms
}