All files / src/utils workflowTransitionPermission.ts

100% Statements 21/21
100% Branches 20/20
100% Functions 9/9
100% Lines 18/18

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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84                                                5x                         19x             10x 10x 3x 3x             9x 13x 13x 7x 13x 8x                               5x 14x 10x   5x 5x 5x    
// Client-side mirror of flows-api/infrastructure/lambda/items/workflow-transitions.js
// — keep the permission string shape in sync (UC-WORKFLOW-005.1).
import type { WorkflowDefinition, WorkflowTransition } from '@/schemas/entity-schema'
 
type Translatable = string | Record<string, string>
type Translate = (value: Translatable | undefined) => string
 
interface WorkflowFieldLike {
  fieldId?: string
  name: Translatable
  type?: string
  workflowDefinition?: unknown
}
 
/** A transition flagged "requires permission", with what's needed to name it. */
export interface ProtectedTransition {
  fieldId: string
  from: string
  to: string
  field: WorkflowFieldLike
  definition: WorkflowDefinition
  transition: WorkflowTransition
}
 
const TRANSITION_PERMISSION_REGEX = /^entities-([^:]+):items:transition:([^:]+):([^:]+):([^:]+)$/
 
/**
 * The permission string guarding one transition. Transitions have no id: the
 * workflow editor forbids two transitions between the same pair of states, so
 * (fieldId, from, to) identifies one.
 */
export function transitionPermission(
  entityId: string,
  fieldId: string,
  from: string,
  to: string
): string {
  return `entities-${entityId}:items:transition:${fieldId}:${from}:${to}`
}
 
/** Splits a transition permission into its parts, or null for any other permission. */
export function parseTransitionPermission(
  permission: string
): { entityId: string; fieldId: string; from: string; to: string } | null {
  const match = TRANSITION_PERMISSION_REGEX.exec(permission)
  if (!match) return null
  const [, entityId, fieldId, from, to] = match
  return { entityId: entityId!, fieldId: fieldId!, from: from!, to: to! }
}
 
/** Every protected transition of every workflow field of an entity. */
export function listProtectedTransitions(
  fields: WorkflowFieldLike[] | undefined
): ProtectedTransition[] {
  return (fields ?? []).flatMap((field) => {
    const definition = field.workflowDefinition as WorkflowDefinition | undefined
    if (field.type !== 'workflow' || !field.fieldId || !definition) return []
    return (definition.transitions ?? [])
      .filter((transition) => !!transition.requiresPermission)
      .map((transition) => ({
        fieldId: field.fieldId!,
        from: transition.from,
        to: transition.to,
        field,
        definition,
        transition
      }))
  })
}
 
/**
 * Human-readable name of a protected transition: "Status: Draft → Published",
 * followed by the transition's own label when it has one.
 */
export function describeTransition(entry: ProtectedTransition, translate: Translate): string {
  const stateName = (stateId: string) => {
    const state = entry.definition.states?.find((s) => s.id === stateId)
    return (state && translate(state.label)) || stateId
  }
  const base = `${translate(entry.field.name) || entry.fieldId}: ${stateName(entry.from)} → ${stateName(entry.to)}`
  const label = entry.transition.label ? translate(entry.transition.label) : ''
  return label ? `${base} (${label})` : base
}