# Troubleshooting Adjustment File Validation

Validating an adjustment file is done through the `adjustment_file_validation` Lambda.

The Lambda is triggered by the `adjustment_file_upload` DAG when a user uploads an adjustment file from the Adjustments page in Abacus.

Here are some steps to follow when the Lambda errors out unexpectedly.

## Getting the `statement_period_adjustment_file_id`

You can get the `statement_period_adjustment_file_id` for a particular execution from the [logs](https://sonymusic-pde.datadoghq.com/logs?query=service%3Alambda-abacus-adjustment-file-validation%20environment%3Aprod&agg_m=count&agg_m_source=base&agg_t=count&cols=host%2Cservice&context_event=AZsm7zBkAACQLL6Lr-MKCAAE&event=&messageDisplay=inline&refresh_mode=sliding&storage=hot&stream_sort=time%2Cdesc&viz=&from_ts=1765716708544&to_ts=1765889508544&live=true).

Look for the line with `Get statement period adjustment file for {id}`

![Logs](./assets/adjustment_file_validation_logs.png)

## Getting the `valid_file_location`

You can get the `valid_file_location` for the `statement_period_adjustment_file_id` by querying the Royalty Accounting database:

```sql
SELECT *
FROM statement_period_adjustment_file spaf
WHERE spaf.statement_period_adjustment_file_id = {id}
```

## Downloading and inspecting the file

Once you have the `valid_file_location` you can download the file from S3 using the AWS Console or the CLI:

```sh
aws s3 cp {valid_file_location} .
```

Once you have downloaded the file, inspect it to make sure the format corresponds to the template and that the columns are not merged.

NOTE: These format errors should already be handled by the Lambda but we never know.

If you find something odd with the file, you should contact the person that uploaded it in the first place.
You can get the user's Identity ID by looking at the `created_by` column of the `statement_period_adjustment_file` record, and you can then get the user's details by calling `ows-users`:

```sh
curl https://qa-ows-users.theorchard.io/users/identity/{identity_id}/application/frontend-royalties/profiles
```

## Running the Lambda locally

If you didn't find anything odd with the file format, you might have to run the Lambda locally using the same file.

To do so, first follow the steps in the Lambda's [README](https://github.com/theorchard/lambda-abacus/tree/master/lambda/adjustment_file_validation) to setup the Lambda.

Then, make sure that the `S3_BUCKET_NAME` in the `.env` file corresponds to the bucket from the `valid_file_location`.

You'll need to update the content of the `tests/sample_event.json` file to set the `target_id` to the `statement_period_adjustment_file_id` and to set the `statement_period_id` to the one from the `statement_period_adjustment_file` record.

You should then be able to run `make run` to run the Lambda locally with the correct file.

OPTIONAL: If you don't want the Lambda to update the `satement_period_adjustment_file` record and to upload the error file in the S3 bucket, you should comment out the content of these two functions:
- [https://github.com/theorchard/lambda-abacus/blob/master/lambda/adjustment_file_validation/adjustment_file_validation/ows_royalties.py#L68](https://github.com/theorchard/lambda-abacus/blob/master/lambda/adjustment_file_validation/adjustment_file_validation/ows_royalties.py#L68)
- [https://github.com/theorchard/lambda-abacus/blob/master/lambda/adjustment_file_validation/adjustment_file_validation/connectors/s3.py#L27](https://github.com/theorchard/lambda-abacus/blob/master/lambda/adjustment_file_validation/adjustment_file_validation/connectors/s3.py#L27)

NOTE: If you want to run the Lambda inside a Docker container using the `make docker_local` command, you should copy the content of the `tests/sample_event.json` file in the `make local_event` command.
