All projects
Employee Management
hardBuild an HR system with employee records, departments, role-based permissions, and a leave-approval workflow. This project exercises advanced typing: branded id types so an EmployeeId can never be passed where a DepartmentId is expected, mapped types for permission matrices, and state machines for leave requests.
Next.jsTypeScriptTailwind CSSPrismaPostgreSQLNextAuth.js
Architecture
- UI layer: employee directory, org-chart view, and a leave-request inbox, each gated by typed permission checks
- Auth layer: NextAuth sessions enriched with a typed role, exposed to components through a typed session helper
- Domain layer: the leave-request state machine as a transition map, so illegal transitions are compile-time errors
- Service layer: permission checks as pure functions over a mapped Role-to-Permission matrix
- Persistence layer: Prisma repositories that convert raw ids into branded EmployeeId and DepartmentId values at the boundary
- Type layer: branded ids, the Role and Permission unions, and the LeaveRequest state types
Folder structure
src/
app/
page.tsx
employees/
[id]/
page.tsx
departments/
page.tsx
leave/
page.tsx
api/
leave/
route.ts
components/
EmployeeDirectory.tsx
EmployeeCard.tsx
OrgChart.tsx
LeaveInbox.tsx
LeaveRequestForm.tsx
lib/
permissions.ts
leaveMachine.ts
repository.ts
session.ts
types/
hr.tsDomain type definitions
Start every project by defining its types — they become the contract for every component, service, and API call that follows.
declare const employeeIdBrand: unique symbol;
declare const departmentIdBrand: unique symbol;
export type EmployeeId = string & { readonly [employeeIdBrand]: true };
export type DepartmentId = string & { readonly [departmentIdBrand]: true };
export type Role = "admin" | "hr" | "manager" | "employee";
export type Permission =
| "view-directory"
| "edit-employee"
| "approve-leave"
| "manage-departments";
export type PermissionMatrix = Record<Role, readonly Permission[]>;
export interface Employee {
id: EmployeeId;
name: string;
email: string;
role: Role;
departmentId: DepartmentId;
managerId: EmployeeId | null;
hiredAt: string;
}
export type LeaveStatus = "draft" | "submitted" | "approved" | "rejected" | "cancelled";
export interface LeaveRequest {
id: string;
employeeId: EmployeeId;
startDate: string;
endDate: string;
status: LeaveStatus;
reviewerId: EmployeeId | null;
}Build steps
- 1Define the type layer — branded EmployeeId and DepartmentId, the Role and Permission unions, and the LeaveRequest model.
- 2Write permissions.ts: a const PermissionMatrix and a hasPermission(role, permission) helper, verified exhaustive by the mapped type.
- 3Write leaveMachine.ts: a transition map from LeaveStatus to its allowed next statuses, plus a canTransition guard used by both UI and API.
- 4Set up Prisma and repository.ts, casting raw string ids to branded ids only inside the repository boundary.
- 5Configure NextAuth with a session callback that attaches the typed Role, and write a requirePermission helper for server code.
- 6Build EmployeeDirectory with search and department filters, hiding edit actions unless hasPermission allows them.
- 7Build OrgChart by walking managerId links, handling cycles defensively and typing the tree node structure.
- 8Build LeaveRequestForm and the leave API route: every status change goes through canTransition, and reviewers must hold approve-leave.
- 9Build LeaveInbox for managers, showing pending requests for their reports only.
- 10Add an audit log entry for every leave transition, typed as a discriminated union of audit events.