## Commit Royalties

## Requirements

- Python 3.11

## OWS Dependencies

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

## Overview

This lambda creates ledger entries from the results of an accounting run by:

1. Using the `target_id` of the received `abacus_event` to determine which accounting run's results are to be committed.
2. Getting a list of account_ids that have accounting run results in snowflake.
3. Getting each account's accounting run results from snowflake (using the list of account_ids).
4. Combining accounting run results to create rounded ledger_account entries.
5. Creating ledger_deposit entries to track remaining balances after rounding.

## Running

### Set up the environment
Copy the `.env.shadow` to a local `.env`:
```shell
cp .env.shadow .env
```

#### AWS Creds
Generate your AWS credentials using [awsume](https://www.notion.so/AWS-Access-f841b9dd815d4443a80e96a86c92cd2f?pvs=4#f11f08d09f16493a8a3e9dbf38d87164): `awsume prod`.

Get your `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`, and `AWS_SESSION_TOKEN` by running `awsume prod -s`.

Copy the values into your `.env` and/or export them in your CLI.

Example export:
```shell
export AWS_ACCESS_KEY_ID=<secret>
export AWS_SECRET_ACCESS_KEY=<secret>
export AWS_SESSION_TOKEN=<secret>
```


#### 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
```

### Start local lambda
The lambda function can be run locally as a python function or as a container.

As a python function:
```shell
make run
```

As a container:
```shell
make docker_local_up
```

Note: This brings up the local code, if you want to pull and run the most recently deployed image run `make start_ows`, it may take a while either way 

This container will act similar to a web server by listening on a port for the event data to be POSTed.

### Execute Function

Use the HTTP client of your choice. The body of the request is the `abacus_event` passed into the function.

```shell
curl --request POST \
  --url http://localhost:9000/2015-03-31/functions/function/invocations \
  --header 'Content-Type: application/json' \
  --data $(cat tests/sample_event.json)
```

## Deployment

There is one shared [jenkins pipeline](https://pipeline.theorchard.io/job/lambda-abacus-pipeline/) for all abacus lambdas.
A webhook will trigger the deploy for the appropriate sub-directory in this repo.

## Integration testing

OWS services can be added to the `docker-compose.yml` file in order to test integrations.
