All files / services/company-system employees.ts

0% Statements 0/41
0% Branches 0/49
0% Functions 0/11
0% Lines 0/37

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                                                                                                                                                                                                                                     
import "server-only";
 
import { COMPANY_SYSTEM_ENDPOINT_PATH } from "@/constants/db";
import type {
  EmployeeRecord,
  EmployeeRow,
  LeaveEntitlementRow,
  RoleProfileRow,
  TeamRow,
} from "@/lib/db/schema";
import { cloneRows, request } from "@/services/company-system/client";
import { getAvatarUrl } from "@/utils/avatar";
 
function isTeamRow(value: unknown): value is TeamRow {
  if (!value || typeof value !== "object") return false;
  const candidate = value as Partial<TeamRow>;
  return typeof candidate.teamId === "string" && typeof candidate.name === "string";
}
 
function isEmployeeRow(value: unknown): value is EmployeeRow {
  if (!value || typeof value !== "object") return false;
  const candidate = value as Partial<EmployeeRow>;
 
  return (
    typeof candidate.employeeId === "string" &&
    typeof candidate.name === "string" &&
    typeof candidate.email === "string" &&
    (typeof candidate.avatar === "string" ||
      typeof candidate.avatar === "undefined") &&
    typeof candidate.teamId === "string" &&
    typeof candidate.timeZone === "string" &&
    (typeof candidate.managerEmployeeId === "string" ||
      typeof candidate.managerEmployeeId === "undefined" ||
      candidate.managerEmployeeId === null)
  );
}
 
function isLeaveEntitlementRow(value: unknown): value is LeaveEntitlementRow {
  if (!value || typeof value !== "object") return false;
  const candidate = value as Partial<LeaveEntitlementRow>;
 
  return (
    typeof candidate.employeeId === "string" &&
    typeof candidate.annual === "number" &&
    typeof candidate.sick === "number" &&
    typeof candidate.personal === "number"
  );
}
 
function isRoleProfileRow(value: unknown): value is RoleProfileRow {
  if (!value || typeof value !== "object") return false;
  const candidate = value as Partial<RoleProfileRow>;
 
  return (
    (candidate.role === "user" || candidate.role === "manager") &&
    typeof candidate.employeeId === "string"
  );
}
 
export async function listEmployeeRows(): Promise<EmployeeRow[]> {
  const rows = await request<unknown[]>(COMPANY_SYSTEM_ENDPOINT_PATH.employees);
  return cloneRows(rows.filter(isEmployeeRow));
}
 
export async function listRoleProfiles(): Promise<RoleProfileRow[]> {
  const rows = await request<unknown[]>(COMPANY_SYSTEM_ENDPOINT_PATH.roleProfiles);
  return cloneRows(rows.filter(isRoleProfileRow));
}
 
export async function listEmployeeDirectory(): Promise<EmployeeRecord[]> {
  const [teams, employees, leaveEntitlements] = await Promise.all([
    request<unknown[]>(COMPANY_SYSTEM_ENDPOINT_PATH.teams),
    request<unknown[]>(COMPANY_SYSTEM_ENDPOINT_PATH.employees),
    request<unknown[]>(
      COMPANY_SYSTEM_ENDPOINT_PATH.leaveEntitlements,
    ),
  ]);
 
  const validTeams = teams.filter(isTeamRow);
  const validEmployees = employees.filter(isEmployeeRow);
  const validLeaveEntitlements = leaveEntitlements.filter(isLeaveEntitlementRow);
 
  const teamNameByTeamId = new Map(
    validTeams.map((team) => [team.teamId, team.name]),
  );
  const employeeByEmployeeId = new Map(
    validEmployees.map((employee) => [employee.employeeId, employee]),
  );
  const entitlementByEmployeeId = new Map(
    validLeaveEntitlements.map((entitlement) => [entitlement.employeeId, entitlement]),
  );
 
  return validEmployees.map((employee) => {
    const managerName =
      employee.managerEmployeeId &&
      employeeByEmployeeId.get(employee.managerEmployeeId)?.name;
    const entitlement = entitlementByEmployeeId.get(employee.employeeId);
 
    return {
      employeeId: employee.employeeId,
      name: employee.name,
      email: employee.email,
      avatar: employee.avatar?.trim() || getAvatarUrl(employee.name),
      team: teamNameByTeamId.get(employee.teamId) ?? employee.teamId,
      manager: managerName ?? "Not assigned",
      timeZone: employee.timeZone,
      entitlements: {
        annual: entitlement?.annual ?? 0,
        sick: entitlement?.sick ?? 0,
        personal: entitlement?.personal ?? 0,
      },
    };
  });
}