## ADDED Requirements ### Requirement: Document storage and metadata The system SHALL store uploaded documents (receipts, invoices, supporting files) on the filesystem with metadata in the database. Metadata includes: document type, foreign object reference (expense claim, invoice, transaction), original filename, server filename, file size, uploader, and upload timestamp. #### Scenario: Upload document - **WHEN** a user uploads a file attached to an expense claim - **THEN** the file is stored under the configured storage root in a `yyyy/mm/dd` directory structure and metadata is recorded in the database #### Scenario: Retrieve document - **WHEN** a user requests a document by its ID - **THEN** the system serves the file from the storage path recorded in metadata ### Requirement: Document type association Each document SHALL be typed (expense receipt, inbound invoice, outbound invoice, financial transaction support, etc.) and linked to a foreign object by type and ID. A single foreign object can have multiple documents attached. #### Scenario: List documents for expense claim - **WHEN** a user views an expense claim - **THEN** all documents of type "expense receipt" linked to that claim are listed #### Scenario: Reassign document - **WHEN** an authorized user reassigns a document from one foreign object to another - **THEN** the document's foreign reference is updated ### Requirement: Storage path abstraction The system SHALL access document storage through an `IDocumentStorage` interface with a default local filesystem implementation. The storage root path SHALL be configurable. This enables a future swap to S3/MinIO (Bucket 2) without changing the application layer. #### Scenario: Configurable storage root - **WHEN** the storage root is configured (defaulting to `/var/lib/usingcash/upload/` for fresh installs, or pointing at the legacy `/var/lib/swarmops/upload/` path for migrations) - **THEN** all document writes and reads use the configured path as the base directory #### Scenario: Storage interface isolation - **WHEN** a document is stored or retrieved - **THEN** the operation goes through `IDocumentStorage` and does not directly reference filesystem APIs in the business logic