# Commit Ledger Account Contract

## Overview

Lambda for committing to `ledger_account_contract` (AKA "the general ledger").

## Getting Started

### Requirements

- Python 3.11
- Docker

### OWS Dependencies

- [ows-ledger](https://github.com/theorchard/ows-ledger)
- [ows-payment](https://github.com/theorchard/ows-payment)
- [ows-moneyhub](https://github.com/theorchard/ows-moneyhub)

#### Authorization

This lambda connects to `ows-moneyhub`, which enforces Authorization in both QA and PROD environments.
If the code being run is not routing to `ows-moneyhub`, skip this `Authorization` section entirely.

By default, the lambda client connects to `ows-moneyhub` in QA.
Connecting to `ows-moneyhub` in the `Dev` environment does not enforce authorization.

If connecting to QA, an authorization token is needed, which can either be an OSP Client Token or be the M2M token from the secrets manager.


##### Using the M2M token

Using an M2M Token requires access to the following secret in Secrets
Manager:
* `qa/lambda-abacus-commit-ledger-account-contract/M2M_JWT_ACCESS_TOKEN`


Before running the lambda, assume the **prod** AWS profile:
```bash
awsume prod
<enter prod mfa token>
```

If everything is configured correctly the following should appear when running the lambda:
```bash
Found credentials in environment variables.
```

##### Connecting to Locally Running ows-moneyhub
Run ows-moneyhub locally and then add the following line to the `.env` file:
```python
OWSREQUEST_SERVICE_MAP='{"ows-moneyhub": "http://localhost:5000"}'
```

### Installation

Create a `.env` file based on the `.env.shadow` and fill in the values:

```bash
$ cp .env.shadow .env
```

Install [uv](https://docs.astral.sh/uv/getting-started/installation/) then install dependencies:

```bash
make env_dev
```

### Running

You can run the function locally with: 

```
awsume prod

make run
```


Or you can run the function inside Docker with: `make docker_local_up` and trigger an event with `make local_event`

### Testing

You can run the linter and the unit tests locally with: `make lint` and `make test`

Or you can run the linter and the unit tests inside Docker with: `make docker_test`

The integration tests can only be run inside Docker with: `make docker_test_integration`

**Note:**
You might need to login to Docker with the shared AWS account in order to run the integration tests:

```bash
$ awsume <shared_profile>
$ make docker_login
```

### Adding new packages

- Regular packages: `uv add <pkg>`
- Dev packages: `uv add <pkg> --dev`
- Integration packages: `uv add <pkg> --group integration`

### Upgrading packages

- All packages: `uv sync --upgrade`
- A specific package: `uv sync --upgrade-package <pkg>`
