# Ensure artists

Resolves destination artist identities for a product transfer job. For each product in the job that is missing a destination artist, this Lambda:

1. Calls `ows-artist` to bulk-ensure all source artists exist on the destination vendor.
2. Writes the resulting destination artist IDs back to each product via `ows-project-manager`.
3. Updates the transfer job's `destination_artist_id` to the originating artist's destination counterpart.

If all destination artists are already resolved, the step is skipped and returns early.

## Event payload

### Input

| Field | Type | Description |
|-------|------|-------------|
| `job_id` | `int` | ID of the transfer job to process. |

Additional fields from the Step Function state are passed through unchanged.

### Output

Returns the input event merged with `ensure_artists_result`:

| Field | Type | Description |
|-------|------|-------------|
| `ensure_artists_result.requested_source_artist_ids` | `list[int]` | Source artist IDs passed to `bulk-ensure`. |
| `ensure_artists_result.artist_mapping` | `dict[str, int]` | Map of `source_artist_id` → `destination_artist_id`. |
| `ensure_artists_result.updated_count` | `int` | Number of products updated. |
| `ensure_artists_result.destination_artist_id` | `int` | Destination artist ID for the originating artist (omitted when skipped). |
| `ensure_artists_result.skipped` | `bool` | `true` if all artists were already resolved. |

## Setup

This Lambda uses M2M token authentication, which requires AWS access to retrieve credentials at runtime. See [AWS Access](https://www.notion.so/AWS-Access-f841b9dd815d4443a80e96a86c92cd2f) for setup instructions.

Run locally — exposes the Lambda invocation endpoint on port 9000:
```sh
docker compose up dev
```

Invoke the function in a second terminal:
```sh
curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{"job_id": 123}'
```

## Development

Run linting and tests:
```sh
docker compose run --rm --build unit-lint
```

Format code:
```sh
docker compose run --rm --build format
```

Update the lock file (required after changing `pyproject.toml`):
```sh
docker compose run --rm --build update-lockfile
```
