diff --git a/project/README.md b/project/README.md index fe7c1bb..3b38796 100644 --- a/project/README.md +++ b/project/README.md @@ -1,35 +1,90 @@ # BiteTrack Backend v2 -A production-ready, high-performance REST API built with FastAPI and PostgreSQL, serving as the modernized, rebooted backend for the BiteTrack inventory and order system. +BiteTrack Backend v2 is a REST API designed for small food businesses that manage perishable inventory. -## Features -- **User Authentication & RBAC:** Secure user registration, password hashing (bcrypt), token-based login (OAuth2 + JWT), refresh token rotation via HttpOnly cookies, and strict Role-Based Access Control (Admin, Staff, Customer). -- **Relational Inventory Schema:** Modular schemas handling Categories, Products, and Orders with ACID transactional safety during stock decrements. -- **Search, Filtering, and Pagination:** Limit-offset list query parameters, category filtering, and text-based SKU/name search. -- **Robust Integration Testing:** Over 80% test coverage using pytest with a containerized test database. -- **Deployment-Ready:** Multi-stage production Docker image configuration and automated CI actions via GitHub Actions. +The system centralizes customer management, inventory, sales, outstanding payments, inventory losses, and operational reporting into a unified platform, replacing fragmented spreadsheet-based workflows. + +Its goal is to simplify daily operations while preserving historical business data that helps owners make better production and purchasing decisions. + +--- + +## Project Docs Index + +[README](./README.md) \[this documment\] → What is BiteTrack? + +[SRS](./SRS.md) → Software Requirement Specification: What must BiteTrack do? + +[Database](./database.md) → How is the business represented? + +[API Design](./api-design.md) → How is the functionality exposed? + +[Architecture](architecture.md) → How is the software organized internally? + + +--- + +## Core Features + +### Authentication & Authorization +- Secure user registration and authentication +- Role-Based Access Control (Administrator, Staff) + +### Customer Management +- Customer directory +- Outstanding balance tracking +- Payment history + +### Inventory Management +- Product catalog +- Categories +- Stock level management +- Inventory adjustments +- Inventory loss registration + +### Sales +- Sales registration +- Payment status +- Customer association +- Automatic inventory updates + +### Reporting +- Sales history +- Inventory loss history +- Customer balances +- Product performance +- Operational analytics + +--- ## Tech Stack -- **Language:** Python -- **Framework:** FastAPI -- **Database:** PostgreSQL -- **ORM:** SQLAlchemy (Async) -- **Migrations:** Alembic -- **Testing:** pytest -- **Containerization:** Docker & Docker Compose -- **Linting & Formatting:** Ruff + +- Python +- FastAPI +- SQLAlchemy (Async) +- PostgreSQL +- Alembic +- pytest +- Docker +- Ruff + +--- ## Getting Started ### Prerequisites -- Docker & Docker Compose -- Python 3.12+ (if running locally without Docker) -- `uv` (Fast Python package installer and resolver) -### Installation -1. Clone the repository. -2. Spin up the application stack: - ```bash - docker compose up --build - ``` -3. Access the interactive OpenAPI docs at `http://localhost:8000/docs`. +- Docker +- Docker Compose + +### Run + +```bash +docker compose up --build +``` + +Interactive documentation: + +``` +http://localhost:8000/docs +``` + diff --git a/project/SRS.md b/project/SRS.md new file mode 100644 index 0000000..007ddfc --- /dev/null +++ b/project/SRS.md @@ -0,0 +1,253 @@ +# BiteTrack v2 — Software Requirements Specification (SRS) + +Version: 0.1 (Draft) + +--- + +# 1. Purpose + +BiteTrack is an operational management system designed for small food businesses producing and selling perishable products. + +The system centralizes inventory management, customer management, sales, payment tracking, inventory loss registration, and operational reporting into a unified workflow. + +Its objective is to reduce operational overhead while preserving historical business information that supports better production and inventory decisions. + +--- + +# 2. Scope + +The system shall provide functionality for: + +- User authentication +- User authorization +- Customer management +- Product catalog management +- Category management +- Inventory management +- Sales registration +- Payment tracking +- Inventory loss registration +- Business reporting + +The current version does not attempt to replace accounting or ERP software. + +--- + +# 3. Stakeholders + +- Business Owner +- Administrator +- Staff Member + +Future versions may include customer-facing functionality. + +--- + +# 4. Business Problem + +Small food businesses frequently rely on multiple spreadsheets to independently manage: + +- Customers +- Inventory +- Sales +- Outstanding payments +- Inventory waste +- Business reports + +This fragmentation makes it difficult to answer questions such as: + +- Which customers still owe money? +- Which products generate the most waste? +- How much inventory expires every week? +- Which products are most profitable? +- How much inventory should be produced tomorrow? + +BiteTrack consolidates these operational workflows into a single system. + +--- + +# 5. Functional Requirements + +## 5.1 Identity & Access + +FR-001 +The system shall allow administrators to register users. + +FR-002 +The system shall authenticate registered users. + +FR-003 +The system shall enforce role-based authorization. + +FR-004 +The system shall allow administrators to activate or deactivate user accounts. + +--- + +## 5.2 Customer Management + +FR-010 +The system shall maintain a customer directory. + +FR-011 +A customer record shall contain: + +- Full name +- Phone number +- Optional email address + +FR-012 +The system shall associate sales with customers. + +FR-013 +The system shall determine each customer's outstanding balance from recorded sales and payments. + +--- + +## 5.3 Product Management + +FR-020 +The system shall manage product categories. + +FR-021 +The system shall create, update and archive products. + +FR-022 +Products shall define a default shelf life. + +--- + +## 5.4 Inventory + +FR-030 +The system shall maintain current inventory levels. + +FR-031 +The system shall automatically decrease inventory when sales are recorded. + +FR-032 +The system shall allow manual registration of inventory losses. + +FR-033 +Inventory losses shall record a reason. + +Examples include: + +- Expired +- Damaged +- Quality issue +- Manual adjustment + +--- + +## 5.5 Sales + +FR-040 +The system shall register product sales. + +FR-041 +A sale shall contain one or more sale items. + +FR-042 +The system shall calculate sale totals automatically. + +FR-043 +The system shall associate every sale with the staff member who created it. + +--- + +## 5.6 Payments + +FR-050 +The system shall record payments associated with a sale. + +FR-051 +A sale may receive multiple payments. + +FR-052 +The system shall calculate outstanding balances from recorded payments. + +--- + +## 5.7 Reporting + +FR-060 +Display current inventory. + +FR-061 +Display inventory loss history. + +FR-062 +Display customer balances. + +FR-063 +Display sales history. + +FR-064 +Display product performance. + +FR-065 +Provide historical operational reports. + +--- + +# 6. Non-functional Requirements + +## Performance + +The system shall provide responsive interaction during normal business operations. + +## Security + +Passwords shall be stored using an adaptive cryptographic password hashing algorithm. + +The system shall authenticate users before granting access to protected resources. + +Role-based authorization shall restrict access to administrative functionality. + +## Reliability + +Operational data shall remain persistent and recoverable after unexpected failures. + +## Maintainability + +The software architecture shall support incremental feature additions with minimal impact on existing functionality. + +--- + +# 7. Constraints + +- Backend-first development. +- Single business deployment. +- Lightweight deployment on commodity hardware. +- Designed primarily for small food businesses. + +--- + +# 8. Assumptions + +- One deployment corresponds to one business. +- Inventory quantities are entered manually. +- Products belong to one category. +- Staff members authenticate before using the system. + +--- + +# 9. Out of Scope + +The current version does not include: + +- Accounting +- Supplier management +- Payment gateway integration +- Online ordering +- Multi-tenant deployments + +--- + +# 10. Planned Future Features + +- Bootstrap initialization using a one-time deployment key. +- QR-based administrator recovery. +- Automatic inventory expiration. +- Production recommendations based on historical sales and inventory losses. \ No newline at end of file diff --git a/project/TODO.md b/project/TODO.md index 83f73f2..fce34a3 100644 --- a/project/TODO.md +++ b/project/TODO.md @@ -3,8 +3,8 @@ This is the operational TODO list for your flagship backend project. ## Currently Doing -- [ ] Initialize repository with `uv` -- [ ] Set up Docker Compose for local development (PostgreSQL & FastAPI) +- [x] Initialize repository with `uv` +- [x] Set up Docker Compose for local development (PostgreSQL & FastAPI) ## To Do (Backlog) - [ ] Implement Users CRUD diff --git a/project/api-design.md b/project/api-design.md index 24c71e3..a8a2a5d 100644 --- a/project/api-design.md +++ b/project/api-design.md @@ -1,33 +1,78 @@ # BiteTrack API Design -## Endpoints Specification +## Endpoint Specification -### Authentication -- `POST /auth/register` - Create user. -- `POST /auth/token` - Authenticate user & issue JWT Access & Refresh Tokens. -- `POST /auth/refresh` - Rotate access tokens using HttpOnly Refresh Cookie. +--- -### Users & Profiles -- `GET /users/me` - Fetch own profile. -- `PATCH /users/me` - Update own profile. -- `GET /users` - List all users (Admin only). +## Authentication + +- `POST /auth/register` - Register a new user. +- `POST /auth/token` - Authenticate a user and issue access & refresh tokens. +- `POST /auth/refresh` - Rotate an access token using a refresh token. + +--- + +## Users + +- `GET /users/me` - Retrieve the authenticated user's profile. +- `PATCH /users/me` - Update the authenticated user's profile. +- `GET /users` - List users (Administrator only). +- `POST /users` - Create a new staff or administrator account (Administrator only). + +--- + +## Customers + +- `POST /customers` - Create customer. +- `GET /customers` - List customers. +- `GET /customers/{id}` - Retrieve customer information. +- `PATCH /customers/{id}` - Update customer information. +- `DELETE /customers/{id}` - Archive customer. +- `GET /customers/{id}/balance` - Retrieve current outstanding balance. + +--- + +## Categories -### Categories - `POST /categories` - Create category. -- `GET /categories` - List categories (with pagination). -- `GET /categories/{id}` - Retrieve category detail. +- `GET /categories` - List categories. +- `GET /categories/{id}` - Retrieve category details. - `PUT /categories/{id}` - Update category. - `DELETE /categories/{id}` - Delete category. -### Products +--- + +## Products + - `POST /products` - Create product. -- `GET /products` - List products (with text search, category filters, and pagination). +- `GET /products` - List products. - `GET /products/{id}` - Retrieve product details. -- `PUT /products/{id}` - Update product attributes. +- `PUT /products/{id}` - Update product. - `DELETE /products/{id}` - Delete product. -### Orders -- `POST /orders` - Place order (adjusts stock level inside database transaction). -- `GET /orders` - List orders. -- `GET /orders/{id}` - Retrieve order details. -- `PATCH /orders/{id}/status` - Cancel or process order (Admin/Worker only). +--- + +## Sales + +- `POST /sales` - Register a sale and update inventory. +- `GET /sales` - List sales. +- `GET /sales/{id}` - Retrieve sale details. +- `POST /sales/{id}/payments` - Register a payment for an existing sale. +- `GET /sales/{id}/payments` - List payments associated with a sale. + +--- + +## Inventory + +- `POST /inventory/losses` - Register inventory loss. +- `GET /inventory/losses` - List inventory losses. + +--- + +## Reports + +- `GET /reports/sales` +- `GET /reports/customer-balances` +- `GET /reports/inventory` +- `GET /reports/product-performance` +- `GET /reports/waste` \ No newline at end of file diff --git a/project/architecture.md b/project/architecture.md index f3ab9b2..8db8d06 100644 --- a/project/architecture.md +++ b/project/architecture.md @@ -1,7 +1,6 @@ -# Flagship Project Architecture +# BiteTrack Backend v2 Architecture -## Name: BiteTrack Backend v2 -A production-grade reboot of the BiteTrack system focusing on inventory, order management, and core transactional processes using FastAPI. +BiteTrack follows a layered architecture that separates HTTP concerns, data validation, business rules, and persistence. ## Core Architectural Layout The system follows a layered architecture to decouple routing, validation, business rules, and database access. @@ -15,8 +14,43 @@ graph TD DB -->|Read/Write| Postgres[(PostgreSQL Database)] ``` -## Layers: -1. **Routers (`routers/`)**: Handles HTTP routing, status codes, and security dependency injections. -2. **Schemas (`schemas/`)**: JSON serialization, deserialization, and strict input/output verification. -3. **Services (`services/`)**: Orchestrates business actions, processes transactions, and wraps DB actions. -4. **Models (`models/`)**: Represents relational tables mapped to PostgreSQL. +## Layers + +### Routers (`routers/`) + +Responsible for: + +- HTTP routing +- Status codes +- Dependency injection +- Authentication + +--- + +### Schemas (`schemas/`) + +Responsible for: + +- Request validation +- Response serialization +- API contracts + +--- + +### Services (`services/`) + +Responsible for: + +- Business rules +- Workflow orchestration +- Database transactions +- Coordination between repositories and domain models + +--- + +### Models (`models/`) + +Responsible for: + +- Relational database mapping +- Persistence diff --git a/project/database.md b/project/database.md index 0073583..6751d4a 100644 --- a/project/database.md +++ b/project/database.md @@ -1,48 +1,94 @@ -# Database Schema Design +# Database Design -## ERD Layout (Entity-Relationship) +## Planned Domain Model ```mermaid erDiagram - USER { - uuid id PK - string email UK - string hashed_password - string role "Admin | Staff | Customer" - boolean is_active - datetime created_at - } - CATEGORY { - int id PK - string name UK - string description - } - PRODUCT { - uuid id PK - string name - string sku UK - string description - decimal price - int stock_level - int category_id FK - } - ORDER { - uuid id PK - uuid user_id FK - string status "Pending | Completed | Cancelled" - decimal total_amount - datetime created_at - } - ORDER_ITEM { - uuid id PK - uuid order_id FK - uuid product_id FK - int quantity - decimal unit_price - } - CATEGORY ||--o{ PRODUCT : contains - USER ||--o{ ORDER : places - ORDER ||--|{ ORDER_ITEM : has - PRODUCT ||--o{ ORDER_ITEM : referenced_by +USER { + uuid id PK + string email UK + string password_hash + string role + boolean is_active + datetime created_at +} + +CUSTOMER { + uuid id PK + string name + string phone + string email + datetime created_at +} + +CATEGORY { + uuid id PK + string name + string description +} + +PRODUCT { + uuid id PK + string name + string sku UK + string description + decimal unit_price + int stock_level + int shelf_life_days +} + +SALE { + uuid id PK + uuid customer_id FK + uuid created_by FK + decimal total_amount + datetime created_at +} + +SALE_ITEM { + uuid id PK + uuid sale_id FK + uuid product_id FK + int quantity + decimal unit_price +} + +PAYMENT { + uuid id PK + uuid sale_id FK + decimal amount + string payment_method + datetime paid_at +} + +INVENTORY_LOSS { + uuid id PK + uuid product_id FK + int quantity + string reason + datetime created_at +} + +CATEGORY ||--o{ PRODUCT : contains + +CUSTOMER ||--o{ SALE : places + +USER ||--o{ SALE : records + +SALE ||--|{ SALE_ITEM : contains + +SALE ||--o{ PAYMENT : receives + +PRODUCT ||--o{ SALE_ITEM : sold_as + +PRODUCT ||--o{ INVENTORY_LOSS : discarded ``` + +## Notes + +- Outstanding customer balances are **derived** from sales and payments rather than stored directly. +- A sale may receive multiple payments. +- Product stock is maintained as a `stock_level` field during the initial project milestones. +- Inventory losses represent products removed from inventory without completing a sale. +- Payment status is derived (calculated field) from recorded payments rather than stored explicitly. \ No newline at end of file