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 | import "server-only"; import { SESSION_ID_BY_ROLE } from "@/constants/auth"; import type { EmployeeRecord, EmployeeRow, RoleProfileRow } from "@/lib/db/schema"; import { listEmployeeDirectory, listEmployeeRows, listRoleProfiles, } from "@/services/company-system/employees"; import { getRoleLabel, type AppRole, type MockAuthSession, } from "@/lib/auth/session"; function buildSession( role: AppRole, employeeDirectory: EmployeeRecord[], employeeRows: EmployeeRow[], roleProfiles: RoleProfileRow[], ): MockAuthSession { const employeeById = new Map( employeeDirectory.map((employee) => [employee.employeeId, employee]), ); const roleEmployeeIdMap = new Map( roleProfiles.map((profile) => [profile.role, profile.employeeId]), ); const managerFallbackEmployeeId = employeeRows.find((employee) => employeeRows.some( (candidate) => candidate.managerEmployeeId === employee.employeeId, ), )?.employeeId; const fallbackEmployeeId = role === "manager" ? managerFallbackEmployeeId : employeeRows[0]?.employeeId; const roleEmployeeId = roleEmployeeIdMap.get(role) ?? fallbackEmployeeId ?? employeeRows[0]?.employeeId; if (!roleEmployeeId) { throw new Error("Company system has no employees configured."); } const employee = employeeById.get(roleEmployeeId); if (!employee) { throw new Error(`Employee ${roleEmployeeId} not found in employee directory.`); } const managedEmployeeIds = employeeRows .filter((candidate) => candidate.managerEmployeeId === roleEmployeeId) .map((candidate) => candidate.employeeId); const managedEmployees = managedEmployeeIds .map((employeeId) => employeeById.get(employeeId)) .filter((candidate): candidate is NonNullable<typeof candidate> => Boolean(candidate), ); return { ...employee, sessionId: SESSION_ID_BY_ROLE[role], role, employeeId: roleEmployeeId, managedEmployeeIds, roleLabel: getRoleLabel(role), managedEmployees, }; } async function loadSessionData() { const [employeeDirectory, employeeRows, roleProfiles] = await Promise.all([ listEmployeeDirectory(), listEmployeeRows(), listRoleProfiles(), ]); return { employeeDirectory, employeeRows, roleProfiles }; } export async function getMockAuthSession( role: AppRole = "user", ): Promise<MockAuthSession> { const { employeeDirectory, employeeRows, roleProfiles } = await loadSessionData(); return buildSession(role, employeeDirectory, employeeRows, roleProfiles); } export async function getMockAuthSessionsByRole(): Promise< Record<AppRole, MockAuthSession> > { const { employeeDirectory, employeeRows, roleProfiles } = await loadSessionData(); return { user: buildSession("user", employeeDirectory, employeeRows, roleProfiles), manager: buildSession("manager", employeeDirectory, employeeRows, roleProfiles), }; } |