Employee self-service (`/api/me`)
Session-bound console APIs for an employee's profile, pay, documents, attendance, leave, expenses, and team approvals.
What this API area is for
A self-service operation belongs to the signed-in employee. The route should resolve that relationship within the host rather than asking a caller to choose an arbitrary employee record.
These routes expose employee self-service in the console: reads and actions tied to the signed-in person, not to arbitrary employee ids in the request. Every handler resolves the employee from the console session (console_user linked to app_user.employee_id). Payslips, tax worksheets, and documents are scoped so another person's id returns the same 404 as a missing record.
Staff users may call the same paths for their own records. The module docstring says these are the only /api paths an employee session may reach (auth_routes._EMPLOYEE_PATHS). With console sign-in off there is no person to bind to, so handlers answer 404.
Leave and expense flows use the same runtimes as the Slack bot and MCP tools (leave_runtime, expenses_runtime), with requested_via="console" where applicable. Previews are dry runs; submits recompute. Managers review direct reports' pending leave and expense claims under team request endpoints, using the same decide() paths as Slack, keyed by reviewer employee_id.
Routes
| Method | Path | Permission | Handler | What it does |
|---|---|---|---|---|
| GET | /api/me | self.use | my_profile | Returns profile fields, current role assignment, manager, work schedule, and count of direct reports (for showing team requests). |
| GET | /api/me/payslips | self.use | my_payslips | Lists issued payslips for the session employee, with parsed rows from rows_snapshot. |
| GET | /api/me/payslips/{payslip_id}/pdf | self.use | my_payslip_pdf | Serves the PDF file for an issued payslip owned by the employee via issuance plan payslip_filename. |
| GET | /api/me/tax | self.use | my_tax | Lists issued TDS worksheets for the employee. |
| GET | /api/me/tax/{worksheet_id}/pdf | self.use | my_tax_pdf | Serves the TDS PDF for an issued worksheet via issuance plan tds_filename. |
| GET | /api/me/documents | self.use | my_documents | Lists active documents that have a ready version and are filed for the employee or granted to the session principal (not revoked, not expired). |
| GET | /api/me/documents/{document_key} | self.use | my_document_content | Downloads the latest ready version; records access with event_type downloaded and data {"self_service": true}. |
| GET | /api/me/attendance | self.use | my_attendance | Returns attendance marks and check-in events since a computed start date (query days, default 90). |
| GET | /api/me/assets | self.use | my_assets | Lists non-returned asset assignments and active workstation assignments. |
| GET | /api/me/leave | self.use | my_leave | Returns leave balance (or unavailable message on ValueError) and the employee's leave requests. |
| POST | /api/me/leave/preview | self.use | preview_my_leave | Dry-run leave creation through leave_runtime.create_request (dry_run=True). |
| POST | /api/me/leave | self.use | request_my_leave | Submits leave; may notify reviewer on Slack when configured. |
| POST | /api/me/leave/{request_key}/cancel | self.use | cancel_my_leave | Cancels the employee's own leave request via leave_runtime.cancel. |
| GET | /api/me/expenses | self.use | my_expenses | Lists expense claims for the employee. |
| POST | /api/me/expenses/preview | self.use | preview_my_expense | Dry-run expense submit via expenses_runtime.submit (dry_run=True). |
| POST | /api/me/expenses | self.use | claim_my_expense | Submits an expense claim; may notify reviewer on Slack when configured. |
| GET | /api/me/team/requests | self.use | my_team_requests | Lists in-review leave and expense requests where the session employee is reviewer (excluding their own). |
| POST | /api/me/team/requests/{kind}/{key}/decide | self.use | decide_team_request | Forwards decision to leave_runtime.decide or expenses_runtime.decide for kind leave or expense; audits console decisions. |
Request and response models
Query parameters
| Route | Parameter | Type | Default | Behaviour in code |
|---|---|---|---|---|
GET /api/me/attendance | days | int | 90 | Window start is max(1, min(days, 366)) days before today. |
LeaveBody (POST leave and leave preview)
| Field | Type | Notes |
|---|---|---|
start_date | str | Required. |
end_date | str | Required. |
leave_type | str | Default "combined_paid". |
reason | str | None | Optional. |
half_day | bool | Default false. |
emergency | bool | Default false. |
Extra JSON fields are rejected (extra="forbid").
ExpenseBody (POST expenses and expenses preview)
| Field | Type | Notes |
|---|---|---|
amount | str | Required. |
category | str | Required. |
description | str | Required. |
expense_date | str | Required. |
receipt_note | str | None | Optional. |
Extra JSON fields are rejected.
DecisionBody (POST team decide)
| Field | Type | Notes |
|---|---|---|
decision | str | Required. |
note | str | None | Optional. |
Representative JSON responses
Handlers return dict unless noted. Shapes follow SQL row keys and runtime return values.
GET /api/me
{
"profile": { "...": "employee profile columns" },
"role": { "...": "role_assignment row or null" },
"manager": { "name", "email" } or null,
"work_schedule": { "...": "schedule row or null" },
"direct_reports": 0
}Profile columns include: name, email, phone, employee_code, date_of_joining, date_of_birth, personal_address, pan_number, tax_regime, bank_account_no, ifsc (unmasked for the owner).
GET /api/me/payslips: { "payslips": [ { id, pay_period, fiscal_year, pay_date, currency, days_in_month, paid_days, lop_days, gross_earnings, total_deductions, net_pay, rows } ] }.
GET /api/me/tax: { "worksheets": [ worksheet row dicts ] }.
GET /api/me/documents: { "documents": [ metadata without version_key ] }.
GET /api/me/attendance: { "marks": [...], "check_ins": [...] }.
GET /api/me/assets: { "assets": [...], "workstations": [...] }.
GET /api/me/leave: { "balance": { ... } or { "unavailable": "..." }, "requests": [...] }.
GET /api/me/expenses: { "claims": [...] }.
GET /api/me/team/requests: { "leave": [...], "expenses": [...] }.
PDF routes return FileResponse (application/pdf). Document download returns raw bytes with Content-Disposition: attachment and detected_mime or application/octet-stream.
Leave, expense, preview, cancel, and decide endpoints return the dict produced by leave_runtime or expenses_runtime (structure defined in those packages, not duplicated here).
Behaviour and errors
Session and scoping (_me)
- No
console_useron the request → 404not_found, message"Self-service needs console sign-in". - Console user not linked to an employee → 404,
"Your console account is not linked to an employee". - Look up payslip, tax, and leave keys with the session employee id; a document key must be filed for that employee or granted to the session principal. For team decide, match
reviewer_employee_idto that id (unknownkind, missing row, or own request is 404Not found).
File resolution (_file_in_docs)
PDF filenames must be a non-empty string without path separators, resolve inside the tenant docs_dir, and exist as a file; otherwise 404.
Validation and runtime refusals (_refused)
ValueErrorfrom leave/expense runtimes → 400 witherror.code"invalid", messagestr(exc),retryable: false.ApprovedStateChangedErrorfrom leave paths → 409 witherror.code"conflict".
Team decide:
- Unknown
kind(notleaveorexpense) → 404. - Row not found for reviewer, or reviewer is the requester → 404.
ValueErrorfromdecide→ 400invalid.- Result
outcomeinalready_decidedorbalance_blocked→ 409 witherror.codeset to the outcome string andresultincluded in the body. - Other non-
decidedoutcomes → 404. - Successful
decided→ writesagent_auditwith action{kind}.decided,actor_typeoperator, metadatadecisionandchannel: console, then returns the runtime result.
Console permission gate (before handler)
For signed-in users, route_roles.gate checks ROUTE_PERMISSIONS for the handler. Missing permission mapping → 403 denied. Policy denial → 403 with policy reason. public permissions skip the role check. These checks apply in addition to handler-level scoping.
Status code summary
| Code | Typical cause |
|---|---|
| 400 | Leave/expense/cancel/decide validation (invalid). |
| 403 | Role or policy denies route permission (denied). |
| 404 | No session employee, unscoped resource, bad document file, unknown team kind, or failed decide lookup. |
| 409 | Approved state changed on leave (conflict); team decide already_decided or balance_blocked. |
How the permission model applies
Each handler is registered in ROUTE_PERMISSIONS under the key self_service_routes.<handler_name> with permission self.use. That permission is a ceiling: the console refuses to start if any route lacks an entry (require_complete), and gate refuses signed-in users whose role does not hold the permission. The route still enforces employee-only data access in code; permissions do not widen scope beyond the signed-in person's records and reviewer responsibilities.
All eighteen handlers in this module map to self.use. The docstring says an employee session may reach only these /api paths (auth_routes._EMPLOYEE_PATHS). Staff with broader roles still hit the same permission name, but handlers bind the caller with _me and return that employee's rows, plus team-request rows for which that employee is the reviewer.
How is this page?
Last updated on