# alternera_resources_finance

[![CI](https://github.com/10e-ab/alternera-finance/actions/workflows/ci.yml/badge.svg)](https://github.com/10e-ab/alternera-finance/actions/workflows/ci.yml)

Finance reference data for [Alternera](https://github.com/10e-ab/alternera):
currencies, exchange rates, units of measure and VAT codes, as a resource
package the proxy hosts. Records are per tenant and versioned; the proxy
serves them on its Resources page, over its JSON:API and MCP tools, and to
pipeline steps. The package adds its own UI: an overview page, the rates
and their history, conversions between units, and a unit converter in the
Workbench.

It builds on the resource SDK and the resource UI contract
([alternera-sdk](https://github.com/10e-ab/alternera-sdk)) and calls
nothing of the proxy. Its tables live in the Postgres schema
`resources_finance`; the proxy runs the migrations in
`priv/resource_repo/migrations` after its own.

## Using it in Alternera

The package is opt-in. Alternera depends on it, and a build hosts it when
`config/plugins.exs` lists the domain:

```elixir
config :alternera, :resource_domains, [Alternera.Resources.Finance]
```

`docs/user.md` is its page in the proxy's user guide.

## Tests

`test/` checks the contract, the actions and the UI with the SDK's kits,
with no proxy around, against a Postgres (user and password `postgres`,
host `DATABASE_HOST`, default `localhost`):

```bash
mix deps.get
mix test
```

To test against an unreleased SDK, point `ALTERNERA_SDK_PATH` at an
absolute path to a checkout of alternera-sdk.

## Migrations

A change to a resource's attributes needs a migration. Codegen is told where
to write with flags (see `config/config.exs`):

```bash
mix ash.codegen NAME --migration-path priv/resource_repo/migrations \
  --snapshot-path priv/resource_snapshots
```

## Example rates

`priv/examples/exchange_rates.csv` is a year of made-up weekday rates of EUR
against USD, SEK, GBP, NOK and JPY, for the Resources page's import.
`elixir scripts/generate_example_exchange_rates.exs [last day]` writes it
again.

## Working on it with Alternera

Alternera takes this package from Hex. To change it and the proxy together,
check this repository out and point `ALTERNERA_FINANCE_PATH` at that
checkout (an absolute path) when building Alternera; it then takes the
package from there. Release this package first, then the Alternera change
that requires the new version.

## Licence

Licensed under the Apache License, Version 2.0 (`LICENSE`, `NOTICE`).
Alternera itself, the proxy this package plugs into, is under the Elastic
License 2.0.
