# Ledger Accounting Run Balance

## Overview

This lambda sends sums up accounting run totals by contract and sends this data to ows-ledger by:

1. Using the `target_id` of the received `abacus_event` to determine which `accounting_run`'s accounting run totals are to be sent.
2. Using the `target_id` to get the parent `accounting_period`.
3. Getting the `contract_type` from the parent `accounting_period.
4. Connecting to snowflake account and querying required tables for the respective `contract_type`.
5. Summing accounting run totals by `contract_id`.
6. Sending the contract's accounting run totals to ows-ledger service to be inserted into `ledger_accounting_run_balance`.

More on the Accounting Run calculation, particularly this step of creating a "run summary" can be found [here](https://www.notion.so/Accounting-Run-c9ecc13c18cb4362962ce54853992580?pvs=4#7f710b21e834467bb320547d9f3a4c9d).

## Getting Started

### Requirements

- Python 3.11
- Docker

### OWS Dependencies

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

### Installation

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

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

Create a virtual env and install the requirements:

```bash
$ make dev_env
```

#### Snowflake Creds
You will need to have a Snowflake SSH key pair to run this lambda -- using a username and password is no longer valid.

If you do not have a Snowflake SSH key pair set up, follow these instructions:
1. create a snowflake subdirectory: `mkdir ~/.ssh/snowflake`
2. navigate to the new subdirectory: `cd ~/.ssh/snowflake`
3. run the following command to create a public and private key:
```shell
openssl genrsa 2048 | openssl pkcs8 -topk8 -inform PEM -out rsa_key.p8
openssl rsa -in rsa_key.p8 -pubout -out rsa_key.pub
```
4. you will be prompted to create a **passphrase** -- **use something you will remember and/or jot it down, you will need this to decrypt your private key**
5. copy your `rsa_key.pub` (without the header/footer) and add it to [terraform](https://github.com/theorchard/terraform-infra/blob/master/prod/snowflake/orchard/users/users.auto.tfvars) ([example PR](https://github.com/theorchard/terraform-infra/pull/15503))


Make sure to fill in all the necessary snowflake environment variables in your `.env`:
* `SNOWFLAKE_USER` is your email (i.e. `dczinsky@sonymusic-pde.com`)
* `SNOWFLAKE_KEY_PASSPHRASE` is the key passphrase you used when creating your key-pair
* `SNOWFLAKE_PRIVATE_KEY_PATH` is the path to your private key on your machine -- if you followed the instructions above it should be `~/.ssh/snowflake/rsa_key.p8`


You may need to export these variables:
```shell
export SNOWFLAKE_USER=dczinsky@sonymusic-pde.com
export SNOWFLAKE_PRIVATE_KEY_PATH=~/.ssh/snowflake/rsa_key.p8
export SNOWFLAKE_KEY_PASSPHRASE=<secret>
```

or source all your environment variables from your `.env`:
```shell
source .env
```

### Running

You can run the function locally with: `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

- For dev packages, add them normally to `requirements-dev.txt`
- For regular packages, add the new top level package name
  and optionally version constraints to `requirements-to-freeze.txt`
- Run `make pip_lock_versions`

### Upgrading packages

Regularly running `make pip_lock_versions` will update and freeze all non-dev dependencies in `requirements.txt`
