I document my attendence-api-assignment GitHub repository as a Python backend for a small HR attendance system. Its application code is concentrated in app/main.py; the API contract, stored-data layout, design decisions, and starter-code review are documented separately. The system combines employee punch records, manager corrections with an audit trail, and attendance analytics.
My attendence-api-assignment repository is on @Mousam098's GitHub.
The repository is written in Python and was started 2026-10, last updated 2026-10.
I built the backend service for a small HR attendance system with FastAPI and MongoDB.
All application code lives in the single module app/main.py.
The technology stack requires Python 3.11+, FastAPI and Uvicorn, MongoDB 6.0+ via PyMongo, Pydantic v2, pytest, and httpx.
Employees punch in and out, and managers can regularize records with every correction appended to an immutable history.
Attendance capture calculates lateness, overtime, work hours, and half days, including overnight shifts that cross midnight.
MongoDB aggregation pipelines power the monthly employee summary, department summary, late-comers leaderboard with competition ranking, and daily department trend with gap filling and a 7-day moving average.
An admin endpoint returns MongoDB explain output for each endpoint's main query.
Duplicate punch-ins, duplicate punch-outs and concurrent corrections are resolved by the database, not by application-level checks.
Employees are identified everywhere by emp_code; MongoDB's _id never appears in the API.
Instants are epoch milliseconds in the API and BSON dates in MongoDB; calendar dates (YYYY-MM-DD), months (YYYY-MM), and shift times (HH:MM) are IST strings.
Late, overtime, work hours and half day follow the rules R1 to R10 in openapi.yaml; derived values are stored with each record, and analytics read the stored values.
The readiness probe returns HTTP 200 only when MongoDB answers, and employee creation returns 409 on duplicate emp_code.
- My API exposes these routes:
GET /healthfor readiness;POST /employeesto create an employee;GET /employeesto list, filter, and paginate employees;POST /attendance/punch-inandPOST /attendance/punch-outto open and close a daily record;GET /attendanceto list filtered, paginated records;PATCH /attendance/{emp_code}/{date}to regularize a record and append history;GET /analytics/employees/{emp_code}/monthlyfor an employee's monthly summary;GET /analytics/departments/summaryfor a monthly department summary;GET /analytics/leaderboard/latefor monthly lateness ranking;GET /analytics/departments/{department}/trendfor a daily series with moving average; andGET /admin/explain/{endpoint}for the main query's MongoDB explain output.
MongoDB enforces uniqueness for employee identity and one attendance record per employee per day, providing duplicate protection and a punch-in race guard.
Indexes are created idempotently at startup, so no manual index setup is needed.
- My indexes and their purposes are:
employeesuniqueemp_codefor identity and duplicate protection;employeesondepartment, emp_codefor filtered, ordered listing;employeesondepartment, joined_onandjoined_onfor headcount and trend queries;attendance_logsuniqueemp_code, datefor one record per day and the punch-in race guard;attendance_logsondate desc, emp_codefor default listing order and month scans;attendance_logsonstatus, date desc, emp_codefor status-filtered listing; andattendance_logsonemp_code, punch_in descto locate the open record at punch-out.
Configuration uses environment variables, with a local .env file as fallback; real environment variables always take priority.
The documented local MongoDB connection-string example is mongodb://localhost:27017, and the example database name is attendance_db.
The documented local MongoDB container command is docker run -p 27017:27017 mongo:7.
git clone https://github.com/Mousam098/attendence-api-assignment.git
cd attendence-api-assignment
python -m venv .venv
source .venv/bin/activate
# Windows (cmd)
.venv\Scripts\activate
pip install -r requirements.txt
cp .env.example .env # Windows: copy .env.example .env
uvicorn app.main:app --port 8000
After startup, readiness is at http://localhost:8000/health and interactive API documentation is at http://localhost:8000/docs.
My documented prerequisites are Python 3.11 or newer and MongoDB 6.0+; MongoDB can run locally, in Docker, or on a free MongoDB Atlas (M0) cluster.
I run the test suite with pip install -r requirements-dev.txt followed by pytest.
tests/test_rules.py covers lateness, overtime, rounding, overnight shifts, and working days and requires no database.
tests/test_api.py covers analytics edge cases including ties, employees without logs, weekend records, and mid-month joiners, as well as concurrency races and index usage in explain output.
The API integration tests use the database in MONGO_URI, drop a separate attendance_test database afterwards, and are skipped when MongoDB is unreachable.
Continuous integration runs the full suite against MongoDB 7 on every push using .github/workflows/ci.yml.
- My repository includes
app/main.pyfor configuration, rules, models, pipelines, and endpoints;tests/test_rules.pyandtests/test_api.py;.github/workflows/ci.yml;openapi.yamlfor the API contract and business rules;DATA_MODEL.mdfor stored-data layout;DECISIONS.mdfor design decisions;REVIEW.mdfor review of the original starter code;requirements.txt;requirements-dev.txt; and.env.example.
The full API contract is in openapi.yaml, and interactive documentation is served at /docs when the app is running.