All files / apps / api/src / server / permission service.ts

100% Statements 3/3
100% Branches 4/4
100% Functions 1/1
100% Lines 2/2

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                                          62x   43x    
import type { ChannelKind, MemberCreate, UserRole } from '@chattra/shared'
 
// PermissionService (design 2.2, 2.5): every entry point asks here, never a role check in a route.
// B5 brings the skeleton: who the caller is. Each rule of the 2.5 table lands with the first route
// that needs it, and reading a conversation always needs membership, whatever the role.
 
/** The signed-in caller, set by the token middleware from one D1 read (design 4.2). */
export interface Actor {
  userId: string
  workspaceId: string
  role: UserRole
  sessionId: string
  firebaseUid: string
}
 
/** Owner and Admin always may; a Member follows the workspace's member_create. */
export function canCreateChannel(
  actor: Actor,
  kind: ChannelKind,
  memberCreate: MemberCreate,
): boolean {
  if (actor.role !== 'member') return true
 
  return memberCreate === 'both' || memberCreate === kind
}