{
    "Comment": "Workflow for NR Ownership Ingestion",
    "StartAt": "ContextAdder",
    "States": {
        "ContextAdder": {
            "Type": "Pass",
            "Parameters": {
                "ExecutionName.$": "$$.Execution.Name"
            },
            "ResultPath": "$.detail.executionContext",
            "Next": "ParticipantDeduplicator"
        },
        "ParticipantDeduplicator": {
            "Type": "Task",
            "Resource": "arn:aws:states:::lambda:invoke",
            "Catch": [
                {
                  "ErrorEquals": [
                    "States.ALL"
                  ],
                  "ResultPath": "$.originalFileError",
                  "Next": "Finalizer"
                }
            ],
            "Parameters": {
                "Payload.$": "States.JsonMerge($$, $, false)",
                "FunctionName": "${participant_deduplicator_arn}"
            },
            "Retry": [
                {
                    "ErrorEquals": [
                        "Lambda.ServiceException",
                        "Lambda.AWSLambdaException",
                        "Lambda.SdkClientException",
                        "Lambda.TooManyRequestsException"
                    ],
                    "IntervalSeconds": 2,
                    "MaxAttempts": 3,
                    "BackoffRate": 2
                }
            ],
            "Next": "ParticipantWriter",
            "OutputPath": "$.Payload"
        },
        "ParticipantWriter": {
            "Type": "Task",
            "Resource": "arn:aws:states:::lambda:invoke",
            "Catch": [
                {
                  "ErrorEquals": [
                    "States.ALL"
                  ],
                  "ResultPath": "$.originalFileError",
                  "Next": "Finalizer"
                }
            ],
            "Parameters": {
                "Payload.$": "States.JsonMerge($$, $, false)",
                "FunctionName": "${participant_writer_arn}"
            },
            "Retry": [
                {
                    "ErrorEquals": [
                        "Lambda.ServiceException",
                        "Lambda.AWSLambdaException",
                        "Lambda.SdkClientException",
                        "Lambda.TooManyRequestsException"
                    ],
                    "IntervalSeconds": 2,
                    "MaxAttempts": 6,
                    "BackoffRate": 2
                }
            ],
            "Next": "ProductDeduplicator",
            "OutputPath": "$.Payload"
        },
        "ProductDeduplicator": {
            "Type": "Task",
            "Resource": "arn:aws:states:::lambda:invoke",
            "Catch": [
                {
                  "ErrorEquals": [
                    "States.ALL"
                  ],
                  "ResultPath": "$.originalFileError",
                  "Next": "Finalizer"
                }
            ],
            "Parameters": {
                "Payload.$": "States.JsonMerge($$, $, false)",
                "FunctionName": "${product_deduplicator_arn}"
            },
            "Retry": [
                {
                    "ErrorEquals": [
                        "Lambda.ServiceException",
                        "Lambda.AWSLambdaException",
                        "Lambda.SdkClientException",
                        "Lambda.TooManyRequestsException",
                        "Runtime.ExitError",
                        "Runtime.UnhandledPromiseRejection"
                    ],
                    "IntervalSeconds": 2,
                    "MaxAttempts": 3,
                    "BackoffRate": 2
                }
            ],
            "Next": "MapProducts",
            "OutputPath": "$.Payload"
        },
        "MapProducts": {
            "Type": "Map",
            "Catch": [
                {
                  "ErrorEquals": [
                    "States.ALL"
                  ],
                  "ResultPath": "$.originalFileError",
                  "Next": "Finalizer"
                }
            ],
            "MaxConcurrency": 10,
            "ItemReader": {
                "Resource": "arn:aws:states:::s3:getObject",
                "ReaderConfig": {
                    "InputType": "JSON"
                },
                "Parameters": {
                    "Bucket": "${bucket_name}",
                    "Key.$": "$.dedupedFile"
                }
            },
            "ItemSelector": {
                "ParentContext.$": "$$",
                "Item.$": "$$.Map.Item.Value"
            },
            "ItemProcessor": {
                "ProcessorConfig": {
                    "Mode": "DISTRIBUTED",
                    "ExecutionType": "STANDARD"
                },
                "StartAt": "ProductWriter",
                "States": {
                    "ProductWriter": {
                        "Type": "Task",
                        "Resource": "arn:aws:states:::lambda:invoke",
                        "Parameters": {
                            "Payload.$": "States.JsonMerge($.ParentContext, $.Item, false)",
                            "FunctionName": "${product_writer_arn}"
                        },
                        "Retry": [
                            {
                                "ErrorEquals": [
                                    "Lambda.ServiceException",
                                    "Lambda.AWSLambdaException",
                                    "Lambda.SdkClientException",
                                    "Lambda.TooManyRequestsException"
                                ],
                                "IntervalSeconds": 2,
                                "MaxAttempts": 6,
                                "BackoffRate": 2
                            }
                        ],
                        "OutputPath": "$.Payload",
                        "End": true
                    }
                }
            },
            "Next": "SoundRecordingDeduplicator",
            "ResultPath": null,
            "ToleratedFailurePercentage": 10,
            "Label": "MapProducts"
        },
        "SoundRecordingDeduplicator": {
            "Type": "Task",
            "Resource": "arn:aws:states:::lambda:invoke",
            "Parameters": {
                "Payload.$": "States.JsonMerge($$, $, false)",
                "FunctionName": "${sr_deduplicator_arn}"
            },
            "Retry": [
                {
                    "ErrorEquals": [
                        "Lambda.ServiceException",
                        "Lambda.AWSLambdaException",
                        "Lambda.SdkClientException",
                        "Lambda.TooManyRequestsException"
                    ],
                    "IntervalSeconds": 2,
                    "MaxAttempts": 3,
                    "BackoffRate": 2
                }
            ],
            "Catch": [
                {
                  "ErrorEquals": [
                    "States.ALL"
                  ],
                  "ResultPath": "$.originalFileError",
                  "Next": "Finalizer"
                }
            ],
            "Next": "MapSoundRecordings",
            "OutputPath": "$.Payload"
        },
        "MapSoundRecordings": {
            "Type": "Map",
            "MaxConcurrency": 5,
            "ItemReader": {
                "Resource": "arn:aws:states:::s3:getObject",
                "ReaderConfig": {
                    "InputType": "JSON"
                },
                "Parameters": {
                    "Bucket": "${bucket_name}",
                    "Key.$": "$.dedupedFile"
                }
            },
            "ItemSelector": {
                "ParentContext.$": "$$",
                "Item.$": "$$.Map.Item.Value"
            },
            "ItemProcessor": {
                "ProcessorConfig": {
                    "Mode": "DISTRIBUTED",
                    "ExecutionType": "STANDARD"
                },
                "StartAt": "SoundRecordingWriter",
                "States": {
                    "SoundRecordingWriter": {
                        "Type": "Task",
                        "Resource": "arn:aws:states:::lambda:invoke",
                        "Parameters": {
                            "Payload.$": "States.JsonMerge($.ParentContext, $.Item, false)",
                            "FunctionName": "${sr_writer_arn}"
                        },
                        "Retry": [
                            {
                                "ErrorEquals": [
                                    "Lambda.ServiceException",
                                    "Lambda.AWSLambdaException",
                                    "Lambda.SdkClientException",
                                    "Lambda.TooManyRequestsException"
                                ],
                                "IntervalSeconds": 2,
                                "MaxAttempts": 6,
                                "BackoffRate": 2
                            }
                        ],
                        "OutputPath": "$.Payload",
                        "Next": "RulesWriter"
                    },
                    "RulesWriter": {
                        "Type": "Task",
                        "Resource": "arn:aws:states:::lambda:invoke",
                        "Parameters": {
                            "Payload.$": "States.JsonMerge($$.Execution.Input.ParentContext, $, false)",
                            "FunctionName": "${rules_writer_arn}"
                        },
                        "Retry": [
                            {
                                "ErrorEquals": [
                                    "Lambda.ServiceException",
                                    "Lambda.AWSLambdaException",
                                    "Lambda.SdkClientException",
                                    "Lambda.TooManyRequestsException"
                                ],
                                "IntervalSeconds": 2,
                                "MaxAttempts": 6,
                                "BackoffRate": 2
                            }
                        ],
                        "OutputPath": "$.Payload",
                        "End": true
                    }
                }
            },
            "Catch": [
                {
                  "ErrorEquals": [
                    "States.ALL"
                  ],
                  "ResultPath": "$.originalFileError",
                  "Next": "Finalizer"
                }
            ],
            "Next": "ContributorDeduplicator",
            "ResultPath": null,
            "ToleratedFailurePercentage": 10,
            "Label": "MapSoundRecordings"
        },
        "ContributorDeduplicator": {
            "Type": "Task",
            "Resource": "arn:aws:states:::lambda:invoke",
            "Catch": [
                {
                  "ErrorEquals": [
                    "States.ALL"
                  ],
                  "ResultPath": "$.originalFileError",
                  "Next": "Finalizer"
                }
            ],
            "Parameters": {
                "Payload.$": "States.JsonMerge($$, $, false)",
                "FunctionName": "${contributor_deduplicator_arn}"
            },
            "Retry": [
                {
                    "ErrorEquals": [
                        "Lambda.ServiceException",
                        "Lambda.AWSLambdaException",
                        "Lambda.SdkClientException",
                        "Lambda.TooManyRequestsException"
                    ],
                    "IntervalSeconds": 2,
                    "MaxAttempts": 3,
                    "BackoffRate": 2
                }
            ],
            "Next": "MapContributors",
            "OutputPath": "$.Payload"
        },
        "MapContributors": {
            "Type": "Map",
            "MaxConcurrency": 10,
            "ItemReader": {
                "Resource": "arn:aws:states:::s3:listObjectsV2",
                "Parameters": {
                    "Bucket": "${bucket_name}",
                    "Prefix.$": "$.dedupedFilePath"
                }
            },
            "ItemSelector": {
                "ParentContext.$": "$$",
                "Item.$": "$$.Map.Item.Value"
            },
            "ItemProcessor": {
                "ProcessorConfig": {
                    "Mode": "DISTRIBUTED",
                    "ExecutionType": "STANDARD"
                },
                "StartAt": "ContributorWriter",
                "States": {
                    "ContributorWriter": {
                        "Type": "Task",
                        "Resource": "arn:aws:states:::lambda:invoke",
                        "Parameters": {
                            "Payload.$": "States.JsonMerge($.ParentContext, $.Item, false)",
                            "FunctionName": "${contributor_writer_arn}"
                        },
                        "Retry": [
                            {
                                "ErrorEquals": [
                                    "Lambda.ServiceException",
                                    "Lambda.AWSLambdaException",
                                    "Lambda.SdkClientException",
                                    "Lambda.TooManyRequestsException"
                                ],
                                "IntervalSeconds": 2,
                                "MaxAttempts": 6,
                                "BackoffRate": 2
                            }
                        ],
                        "OutputPath": "$.Payload",
                        "End": true
                    }
                }
            },
            "Catch": [
                {
                  "ErrorEquals": [
                    "States.ALL"
                  ],
                  "ResultPath": "$.originalFileError",
                  "Next": "Finalizer"
                }
            ],
            "Next": "Finalizer",
            "ResultPath": null,
            "ToleratedFailurePercentage": 10,
            "Label": "MapContributors"
        },
        "Finalizer": {
            "Type": "Task",
            "Resource": "arn:aws:states:::lambda:invoke",
            "Parameters": {
                "Payload.$": "States.JsonMerge($$, $, false)",
                "FunctionName": "${finalizer_arn}"
            },
            "Retry": [
                {
                    "ErrorEquals": [
                        "Lambda.ServiceException",
                        "Lambda.AWSLambdaException",
                        "Lambda.SdkClientException",
                        "Lambda.TooManyRequestsException"
                    ],
                    "IntervalSeconds": 2,
                    "MaxAttempts": 3,
                    "BackoffRate": 2
                }
            ],
            "End": true,
            "ResultPath": null
        }
    }
}
