Overview
The Leave Management module handles the end-to-end lifecycle of employee leave requests, from submission to approval and balance tracking. It integrates with the Better Auth identity system to enforce organization-level permissions and roles.Core Features
- Leave Requests: Create, view, and track leave requests (Annual, Sick, Emergency, etc.).
- Leave Balance Tracking: Real-time balance calculations and usage tracking.
- Approval Workflow: Multi-tier approval system based on organization roles and management relationships.
- Team Impact Analysis: Visual indicators for team coverage when reviewing requests.
- Audit Logging: Traceability for all status changes and approvals.
π Approval Workflow
The approval workflow is the core of the leave module, ensuring requests are reviewed by authorized personnel.Workflow States
PENDING: Request submitted and awaiting review.APPROVED: Request granted; leave balance is automatically deducted.REJECTED: Request denied; reason provided to the employee.
Authorization Rules
Authorization is handled via thePermissionService, which queries Better Authβs members table for roles and permissions.
| Role | Permissions | Approval Capabilities |
|---|---|---|
| Owner | leave:approve, leave:* | Can approve any request in the organization. |
| Admin | leave:approve, leave:* | Can approve any request in the organization. |
| Staff | leave:request | Cannot approve requests (unless they are a manager). |
| Member | leave:request | Cannot approve requests. |
Approval Logic (CanApproveLeaveFor)
To approve or reject a request, the following conditions must be met:
- Org Membership: Both the approver and the requestor must be active members of the same organization.
- Permission Check: The approver must possess the
leave:approvepermission string. - Role Hierarchy:
- Owners and Admins can approve requests for any member of the organization.
- Non-Admin Approvers (if granted
leave:approve) can only approve requests for employees where they are explicitly set as themanager_id.
Balance Deduction
Leave balance deduction happens atomically upon approval. If the deduction fails (e.g., due to insufficient remaining days during concurrent approvals), the request status is rolled back toPENDING.
π Team Impact & Coverage
When an approver reviews a request, the system provides a Team Impact assessment:- Overlapping Leaves: Number of other team members (same department) on leave during the requested dates.
- Coverage Percentage: Percentage of active team members remaining.
- Low Coverage Alert: Triggered if coverage falls below 70%.
π‘ Security & Audit
- Read-Only Identity: User roles are fetched directly from Better Auth tables to ensure consistency with the Bun backend.
- Transactional Safety: Balance updates use database transactions to prevent double-spending of leave days.
- Audit Logs: Every approval action records the
approver_id,timestamp, andcomment.
Related Files
internal/modules/leave/application/usecase/approval_service.goβ Approval logic implementationinternal/modules/organizations/application/usecase/permission_service.goβ Authorization enginedocs/backend/go/guides/permission-service-integration.mdβ Integration guide