# parse_ddex

This lambda is responsible for taking in a DDEX XML file and converting it into a JSON context for use within a state machine. 

## Installation

Use Docker to setup and run this lambda. A Makefile is provided with the commands to get started. 

### Running locally

Use the following make command to start up a local instance of this lambda:

```bash
make run
```

This will be running by default on port `9000`

You can then invoke the lambda through a POST request. 

Example curl request:
```bash
curl --location --request POST 'http://localhost:9000/2015-03-31/functions/function/invocations' \
--header 'Content-Type: application/json' \
--data-raw '{
    "bucket": "qa-ddex-ingester",
    "key": "sme_ddex/DB_Scratch_Folder/A10301A00035480592_single_sc8.xml",
    "execution_name": "test-123"
}'
```

### Running tests

Use the following make command to lint and unit test this lambda

```bash
make test
```

Other more granular test and lint commands are provided under the `dev_` prefixed commands.

```bash
make dev_lint
make dev_test
```

## Input

This lambda takes the following JSON as an input:
```json
{
    "bucket": "qa-ddex-ingester",
    "key": "sme_ddex/DB_Scratch_Folder/A10301A00035480592_single_sc8.xml",
    "execution_name": "test-123"
}
```

### Fields

- `bucket` 
    - The S3 bucket that the lambda needs to act in.
- `key`
    - Path that points to the XML file we want to parse.
- `execution_name`
    - State machine execution name that the lambda is running within.


## Output

This lambda is responsible for outputting both a state machine context as well as writing out JSON back to the S3 location where the ingestion of the XML is occuring. 

### Typical context output

A typical state machine context that would be produced by this lambda looks like:

```json
{
    "error_correction": null,
    "tracks": [
        {
            "release_reference": "R1",
            "asset": {
                "filename": "A10301A00035480592_T-110238693789_SoundRecording_001-001.flac",
                "bucket": "qa-ddex-ingester",
                "ows_assets_filename": null,
                "key": "sme_ddex/DB_Scratch_Folder/resources/A10301A00035480592_T-110238693789_SoundRecording_001-001.flac",
                "filepath": "resources/"
            },
            "isrc": "GBHMU1600031",
            "resource_reference": "A1",
            "tuid": null
        }
    ],
    "execution_name": "test-123",
    "is_purged_release": false,
    "maintenance_owner": null,
    "message_id": "204100967",
    "orchard_label": null,
    "message_thread_id": "G0100035480592_TOC",
    "product": {
        "artwork": {
            "filename": "A10301A00035480592_T-120378754488_Image.jpg",
            "bucket": "qa-ddex-ingester",
            "ows_assets_filename": null,
            "key": "sme_ddex/DB_Scratch_Folder/resources/A10301A00035480592_T-120378754488_Image.jpg",
            "filepath": "resources/"
        },
        "product_id": null,
        "sale_start_date": null,
        "not_for_distribution": null,
        "product_name": "Millionaire",
        "subaccount_id": null,
        "original_release_date": "2016-06-03",
        "release_reference": "R0",
        "display_artist_name": "Digital Farm Animals & Cash Cash feat. Nelly",
        "original_values": null,
        "grid": "A10301A00035480592",
        "upc": "886445897142",
        "vendor_id": null,
        "status": null,
        "catalog_number": "G0100035480592",
        "release_type": "Single"
    },
    "state_machine_name": null,
    "bucket": "qa-ddex-ingester",
    "errors": null,
    "video": null,
    "execution_start_time": null,
    "key": "sme_ddex/DB_Scratch_Folder/",
    "ddex_provider": null,
    "update_indicator": "OriginalMessage"
}
```

See the schema for this JSON output located within the [`lambda-ddex-ingester-common` repo here](https://github.com/theorchard/lambda-ddex-ingester-common/blob/master/ddex_ingester_common/schemas/state_machine_schema.py)
