All files / libs/tenants/teams/shared/tree/src/lib get-team-ancestors.ts

100% Statements 25/25
100% Branches 6/6
100% Functions 4/4
100% Lines 20/20

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 14x 14x 14x 14x 9343x 9351x 9351x 9351x 14x 9357x 14x 1x 1x 6x 6x 6x 2827x 6x  
import { type TeamContract, type TeamTreeNode } from '@amalia/tenants/teams/types';
 
function getTeamAncestorsRecursive<
  TTeam extends Pick<TeamContract, 'id' | 'parentTeamId'>,
  TTeamTreeNode extends TeamTreeNode<TTeam>,
>(teamNode: TTeamTreeNode, acc: TTeam[] = []): TTeam[] {
  if (teamNode.parent) {
    acc.push(teamNode.parent.team);
    getTeamAncestorsRecursive(teamNode.parent, acc);
  }
 
  return acc;
}
 
export function getTeamAncestors<
  TTeam extends Pick<TeamContract, 'id' | 'parentTeamId'>,
  TTeamTreeNode extends TeamTreeNode<TTeam> = TeamTreeNode<TTeam>,
>(teamNode: TTeamTreeNode): TTeam[] {
  return getTeamAncestorsRecursive(teamNode);
}