Metadata-Version: 2.4
Name: eam_doc_helper
Version: 0.0.7
Summary: A package to interact with documents
Author-email: Dave Gunn <daveg@8amsolutions.com>
License-File: LICENSE
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Requires-Python: >=3.8
Description-Content-Type: text/markdown

# eam_doc_helper

This package provides helper classes for working with user documents and form distributions in the 8am system. It is used to fetch user documents, format OCR output for reporting, and build/export form distribution reports from Cosmos DB.

## Installation

```bash
pip install eam_doc_helper
```

## Usage

All classes take a `DatabaseHelper` (from `eam_db_helper`) in their constructor.

### Helper (user_doc.py)

The `Helper` class fetches user documents from the database.

- `Helper(database)` - constructs the helper
- `get_by_user_ids(user_ids)` - fetches `userDoc` items for a list of user IDs of any size. IDs are queried in batches of 2000 (`MAX_BATCH_SIZE`) and the results merged.

```python
from eam_doc_helper.user_doc import Helper

user_docs = helper.get_by_user_ids(['id1', 'id2'])
```

### Formatter (ocr_output.py)

The `Formatter` class normalizes OCR output and user document questions into a consistent shape for reporting.

- `Formatter(database)` - constructs the formatter
- `get_vals_from_ocr_output(user_docs)` - processes a list of `userDoc` items and returns each with a normalized `output` list:
  - If a document's `ocrOutput` contains usable values, they are normalized from `ocrOutput['values']` (using `updatedValue` when present, otherwise `ocrValue`).
  - If `ocrOutput` is empty, values are sourced from the corresponding `userDocQuestion` records' `answer` field.
  - The original `ocrOutput` key is replaced by `output` on each returned item.

Each normalized value has the shape:

```python
{
    'userDocId': '<doc id>',
    'key': '<field key>',
    'reportValue': '<value>',
    'source': 'ocrOutput' | 'userDocQuestion'
}
```

### Get (form_distribution.py)

The `Get` class fetches and groups form distribution details for a template/company.

- `Get(db, template_id, company_id=None)` - constructs the fetcher
- `get_form_template()` - fetches a single `formTemplate` by id, optionally filtered by company id
- `get_distribution_details()` - returns the distribution detail for a template owned by a company, in the shape:

```python
{
    "template": <template dict>,
    "distribution": [
        {
            "id": "<company id>",
            "name": "<company name>",
            "users": [<user row>, ...],
            "dateSent": "YYYY-MM-DD",
            "completedOn": "YYYY-MM-DD",
            "incomplete": <int>,
            "completed": <int>,
            "total": <int>
        },
        ...
    ]
}
```

Each user row contains `id`, `name`, `email`, `createdOn`, `expiryDate`, `reminders`, `completedOn`, `distributionId`, `certificateId`, and optionally `canRetake`.

### ReportBuilder (form_distribution.py)

The `ReportBuilder` class exports form distribution details as flat report rows.

- `ReportBuilder(db, template_id, company_id=None)` - constructs the builder (fetches distribution details on construction)
- `export_distribution_details()` - returns a list of report rows. For each user, archived (`formArchive`) completions are included first with status `"Expired"`, followed by the current distribution record with status `"Completed"` (if a certificate exists) or `"Incomplete"`.

Each row contains `id`, `templateName`, `companyName`, `userName`, `email`, `createdOn`, `completedOn`, and `status`.
