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

100% Statements 51/51
92.15% Branches 47/51
100% Functions 12/12
100% Lines 51/51

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 151 152 153 154 155 156 157 158 159                                        2x         23x     2x       71x   61x 60x           71x     2x                 47x 40x     7x 7x     7x   7x 20x 20x   20x 20x   20x     7x   3x 3x 3x 3x   3x 2x     1x     9x                       2x 8x 8x   8x 10x 9x 17x 14x         10x 9x 14x 11x         10x 3x       8x     2x                     9x 5x         5x           4x   4x 4x 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) {
    if (item.to && Array.isArray(item.to)) {
      for (const email of item.to) {
        if (to.length < 3 && !to.includes(email)) {
          to.push(email);
        }
      }
    }
 
    if (item.cc && Array.isArray(item.cc)) {
      for (const email of item.cc) {
        if (cc.length < 3 && !cc.includes(email)) {
          cc.push(email);
        }
      }
    }
 
    if (to.length >= 3 && cc.length >= 3) {
      break;
    }
  }
 
  return { to, cc };
};
 
export const resolveDefaultParticipantIds = ({
  isFirstRequest,
  relationshipSuggestedIds,
  items,
  meEmail,
}: {
  isFirstRequest: boolean;
  relationshipSuggestedIds: string[];
  items: Item[];
  meEmail?: string;
}) => {
  if (isFirstRequest) {
    const suggestedIds = Array.from(new Set(relationshipSuggestedIds)).slice(
      0,
      4,
    );
 
    return {
      defaultApprovers: suggestedIds,
      defaultObservers: suggestedIds,
    };
  }
 
  const { to, cc } = extractEmail(items);
 
  return {
    defaultApprovers: to.filter(email => email !== meEmail).slice(0, 4),
    defaultObservers: cc.filter(email => email !== meEmail).slice(0, 4),
  };
};