Accounting Flask EB Service
=====================================

A microservice for offering Accounting functionality.

## Getting Started

### (Future) Endpoints

- GET /reports
- GET /report
- POST /report
- GET /transaction_types

### Installation

Before starting make sure you have python3.13 installed.
An easy way to manage python versions is with pyenv.
```bash
$ brew update
$ brew install pyenv
$ pyenv install 3.13
$ pyenv local 3.13
```

```bash
$ cd ows-accounting
$ make pip_dev
$ pre-commit install
```

### Dependency Handling
When adjusting dependencies, make changes to `requirements-to-freeze.txt` and
then run `make pip_lock_versions` to lock dependency versions for deployment.

### Running

Copy `.env.shadow` to `.env` and set environment variables.

When all dependencies have been installed, you can run the flask application
on your local instance by running:

```bash
(env) $ dotenv run python dev.py
```

or

```bash
$ dotenv run make dev
```
All make commands will automatically install all dependencies in an virtual environment folder called env.
You can activate a virtual environment by running `. env/bin/activate` or `source env/bin/activate`.

By using the development server, you will have access to specific features that
are not necessarily available in production, such as the exception tracer.

#### Running with Docker

##### with Hot Reload

To run:

```bash
$ make up_dev
```

You'll be able to see stuff on http://localhost:8888 (per docker-compose.yml)

You can bring down this service with:

```bash
$ make down_dev
```

NOTE: The `up_dev` Makefile target does not force rebuild. If you need to do this, for whatever reason, you can locally run:

```
docker compose build \
    --no-cache
    ows-accounting-dev
```

##### with a production/deploy-ready image

To build and run:

```bash
$ make up_deploy
```

You'll be able to see stuff on http://localhost:8889 (per docker-compose.yml)

You can bring down this service with:

```bash
$ make down_deploy
```

NOTE: This does not support hot-reloading code, and will re-build (without cache) the image every time.

### Testing

To run the linting and unit tests:

```bash
$ make lint
$ make test_unit
```

To run with Docker:

```bash
$ make docker_unit_lint
```

To run the integration tests, first copy `tests/integration/.env.shadow` to `tests/integration/.env` and set values accordingly. Then run

```bash
$ dotenv -f tests/integration/.env run make test_integration 
```

Or to run with Docker, run:

```bash
$ make docker_test_integration
```

To run integration tests against your local Docker environment rather than QA, set `BASE_QA_OWS_ACCOUNTING_URL` to `http://ows-accounting-integration:8080`. Then:

```bash
$ make up_deploy
$ make docker_test_integration
```

## Snowflake authentication using key pair

To authenticate using SSH use following documentation:
https://docs.snowflake.net/manuals/user-guide/snowsql-start.html#using-key-pair-authentication
TL;DR:
```bash
mkdir ~/.ssh/snowflake && cd ~/.ssh/snowflake # recommended
openssl genrsa 2048 | openssl pkcs8 -topk8 -inform PEM -out rsa_key.p8
openssl rsa -in rsa_key.p8 -pubout -out rsa_key.pub
```
Then send newly generated public key to systems@theorchard.com and create SYS ticket.
Note: if you put key not in ~/.ssh/snowflake, then enter location in SNOWFLAKE_PRIVATE_KEY_PATH variable.
