All files / apps/timeOff/src/screens/ChooseApproversAndObservers utils.ts

100% Statements 47/47
82.05% Branches 32/39
100% Functions 10/10
100% Lines 47/47

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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150                                        2x         11x     2x       26x   29x 28x           26x     2x                 16x 11x     5x 5x     5x   5x 15x 15x   15x 15x   15x     5x   3x 3x 3x 3x   3x 2x     1x     7x                       2x 3x 3x   3x 4x 9x 8x       4x 8x 7x       4x 2x       3x     2x                 6x 5x   5x           1x   1x          
import { AvatarItem } from '@repo/ui/components/AvatarList';
 
import { EmailOption } from '@repo/types/form';
 
import { mapSelectedIdsToAvatarItems } from '@/utils/avatar';
 
import { ROLE } from '@/constants/role';
 
import { ChooseRole } from './types';
 
interface BuildSearchEmployeesParams {
  employees: EmailOption[];
  searchValue: string;
  role: ChooseRole | null;
  approversSelected: string[];
  observersSelected: string[];
  searchSelectedIds: string[];
  limit?: number;
}
 
export const resolveSearchSelectedIds = (
  role: ChooseRole | null,
  approversSelected: string[],
  observersSelected: string[],
) => {
  return role === ROLE.APPROVER ? approversSelected : observersSelected;
};
 
export const mapSelectedEmployees = (
  selectedIds: string[],
  employees: EmailOption[],
): AvatarItem[] => {
  const employeeById = new Map(
    employees
      .filter(employee => !!employee.email)
      .map(employee => [
        employee.email as string,
        { name: employee.name, avatar: employee.avatar },
      ]),
  );
 
  return mapSelectedIdsToAvatarItems(selectedIds, employeeById);
};
 
export const buildSearchEmployees = ({
  employees,
  searchValue,
  role,
  approversSelected,
  observersSelected,
  searchSelectedIds,
  limit = 8,
}: BuildSearchEmployeesParams): AvatarItem[] => {
  if (!searchValue) {
    return [];
  }
 
  const lowerSearchValue = searchValue.toLowerCase();
  const excludedIds = new Set(
    role === ROLE.APPROVER ? observersSelected : approversSelected,
  );
  const selectedSet = new Set(searchSelectedIds);
 
  const filteredEmployees = employees.filter(employee => {
    const email = employee.email ?? '';
    const nameLower = employee.name?.toLowerCase() ?? '';
 
    const matchSearch = nameLower.includes(lowerSearchValue);
    const shouldExclude = excludedIds.has(email) && !selectedSet.has(email);
 
    return matchSearch && !shouldExclude;
  });
 
  return filteredEmployees
    .sort((left, right) => {
      const leftName = left.name?.toLowerCase() ?? '';
      const rightName = right.name?.toLowerCase() ?? '';
      const leftStarts = leftName.startsWith(lowerSearchValue);
      const rightStarts = rightName.startsWith(lowerSearchValue);
 
      if (leftStarts !== rightStarts) {
        return leftStarts ? -1 : 1;
      }
 
      return leftName.localeCompare(rightName);
    })
    .slice(0, limit)
    .map(employee => ({
      id: employee.email || '',
      name: employee.name || '',
      uri: employee.avatar || '',
    }));
};
 
type Item = {
  to: string[];
  cc: string[];
};
 
export const extractEmail = (items: Item[]) => {
  const to: string[] = [];
  const cc: string[] = [];
 
  for (const item of items) {
    for (const email of item.to) {
      if (to.length < 3) {
        to.push(email);
      }
    }
 
    for (const email of item.cc) {
      if (cc.length < 3) {
        cc.push(email);
      }
    }
 
    if (to.length >= 3 && cc.length >= 3) {
      break;
    }
  }
 
  return { to, cc };
};
 
export const resolveDefaultParticipantIds = ({
  isFirstRequest,
  relationshipSuggestedIds,
  items,
}: {
  isFirstRequest: boolean;
  relationshipSuggestedIds: string[];
  items: Item[];
}) => {
  if (isFirstRequest) {
    const suggestedIds = Array.from(new Set(relationshipSuggestedIds));
 
    return {
      defaultApprovers: suggestedIds,
      defaultObservers: suggestedIds,
    };
  }
 
  const { to, cc } = extractEmail(items);
 
  return {
    defaultApprovers: to,
    defaultObservers: cc,
  };
};