{
  "Comment": "GDA User Creation State machine",
  "StartAt": "MapInputFromKafka",
  "States": {
    "MapInputFromKafka": {
      "Type": "Pass",
      "Parameters": {
        "email.$": "$.Email",
        "first_name.$": "$.FirstName",
        "last_name.$": "$.LastName",
        "vendor_name.$": "$.ArtistName__c",
        "brand": "awal",
        "contract_template": "A",
        "country.$": "$.MailingCountry",
        "localization.$":"$.Language__c",
        "spotify_url.$": "$.SpotifyUrl__c",
        "voucher_code.$": "$.VoucherCode__c",
        "voucher_code_owner.$": "$.VoucherCodeOwner__c",
        "currency.$": "$.Currency__c",
        "source_id.$": "$.Orchard_Lead_Id__c",
        "correlation_id.$": "$.CorrelationId__c",
        "source": "event.gdaApproval",
        "service_tier_uuid": "5f2bd4fc-df94-4f35-97d3-ef23f8573279"
      },
      "Next": "CheckRequiredFields"
    },
    "CheckRequiredFields": {
      "Type": "Choice",
      "Choices": [
        {
          "And": [
            {
              "Variable": "$.brand",
              "IsPresent": true
            },
            {
              "Variable": "$.brand",
              "IsString": true
            },
            {
              "Variable": "$.service_tier_uuid",
              "IsPresent": true
            },
            {
              "Variable": "$.service_tier_uuid",
              "IsString": true
            },
            {
              "Variable": "$.email",
              "IsPresent": true
            },
            {
              "Variable": "$.email",
              "IsString": true
            },
            {
              "Variable": "$.first_name",
              "IsPresent": true
            },
            {
              "Variable": "$.first_name",
              "IsString": true
            },
            {
              "Variable": "$.last_name",
              "IsPresent": true
            },
            {
              "Variable": "$.last_name",
              "IsString": true
            },
            {
              "Variable": "$.vendor_name",
              "IsPresent": true
            },
            {
              "Variable": "$.vendor_name",
              "IsString": true
            },
            {
              "Variable": "$.contract_template",
              "IsPresent": true
            },
            {
              "Variable": "$.contract_template",
              "IsString": true
            },
            {
              "Variable": "$.correlation_id",
              "IsPresent": true
            },
            {
              "Variable": "$.country",
              "IsPresent": true
            },
            {
              "Variable": "$.country",
              "IsString": true
            },
            {
              "Variable": "$.source",
              "IsPresent": true
            },
            {
              "Variable": "$.source",
              "IsString": true
            }
          ],
          "Next": "CreateVendor"
        }
      ],
      "Default": "SendFailureToKafka"
    },
    "SendFailureToKafka": {
      "Type": "Task",
      "Resource": "${lambda_send_failure}",
      "HeartbeatSeconds": 60,
      "TimeoutSeconds": 3600,
      "ResultPath": "$.send_failure_result",
      "Retry": [
        {
          "ErrorEquals": [
            "RetryException",
            "States.Timeout",
            "Lambda.ServiceException",
            "Lambda.AWSLambdaException",
            "Lambda.SdkClientException",
            "Lambda.TooManyRequestsException"
          ],
          "IntervalSeconds": ${lambda_gda_account_creation_interval_seconds},
          "MaxAttempts": ${lambda_gda_account_creation_retry_attempts},
          "BackoffRate": 3
        }
      ],
      "Catch": [
        {
          "ErrorEquals": [
            "States.ALL"
          ],
          "Next": "FailState",
          "ResultPath": "$.send_failure_errors"
        }
      ],
      "Next": "FailState"
    },
    "FailState": {
      "Type": "Fail"
    },
    "CreateVendor": {
      "Type": "Task",
      "Resource": "${lambda_create_vendor}",
      "HeartbeatSeconds": 60,
      "TimeoutSeconds": 3600,
      "ResultPath": "$.create_vendor_result",
      "Retry": [
        {
          "ErrorEquals": [
            "RetryException",
            "States.Timeout",
            "Lambda.ServiceException",
            "Lambda.AWSLambdaException",
            "Lambda.SdkClientException",
            "Lambda.TooManyRequestsException"
          ],
          "IntervalSeconds": ${lambda_gda_account_creation_interval_seconds},
          "MaxAttempts": ${lambda_gda_account_creation_retry_attempts},
          "BackoffRate": 3
        }
      ],
      "Catch": [
        {
          "ErrorEquals": [
            "States.ALL"
          ],
          "Next": "SendFailureToKafka",
          "ResultPath": "$.create_vendor_errors"
        }
      ],
      "Next": "CreateArtist"
    },
    "CreateArtist": {
        "Type": "Task",
        "Resource": "${lambda_create_artist}",
        "Parameters": {
            "vendor_name.$": "$.create_vendor_result.name",
            "vendor_id.$": "$.create_vendor_result.vendor_id",
            "spotify_url.$": "$.spotify_url",
            "correlation_id.$": "$.correlation_id",
            "source.$": "$.source"
        },
        "HeartbeatSeconds": 60,
        "TimeoutSeconds": 3600,
        "ResultPath": "$.create_artist_result",
        "Retry": [
            {
                "ErrorEquals": [
                    "RetryException",
                    "States.Timeout",
                    "Lambda.ServiceException",
                    "Lambda.AWSLambdaException",
                    "Lambda.SdkClientException",
                    "Lambda.TooManyRequestsException"
                ],
                "IntervalSeconds": ${lambda_gda_account_creation_interval_seconds},
                "MaxAttempts": ${lambda_gda_account_creation_retry_attempts},
                "BackoffRate": 3
            }
        ],
        "Catch": [
            {
                "ErrorEquals": [
                    "States.ALL"
                ],
                "Next": "SendFailureToKafka",
                "ResultPath": "$.create_artist_errors"
            }
        ],
      "Next": "CreateUserAndContracts"
    },
    "CreateUserAndContracts": {
      "Type": "Parallel",
      "Branches": [
        {
          "StartAt": "CreateUser",
          "States": {
            "CreateUser": {
              "Type": "Task",
              "Resource": "${lambda_create_master_contact}",
              "Parameters": {
                  "first_name.$": "$.first_name",
                  "last_name.$": "$.last_name",
                  "email.$": "$.email",
                  "brand.$": "$.brand",
                  "vendor_id.$": "$.create_vendor_result.vendor_id",
                  "contract_template.$": "$.contract_template",
                  "localization.$": "$.localization",
                  "correlation_id.$": "$.correlation_id",
                  "source.$": "$.source"
              },
              "HeartbeatSeconds": 60,
              "TimeoutSeconds": 3600,
              "ResultPath": "$.create_master_contact_result",
              "Retry": [
                {
                  "ErrorEquals": [
                    "RetryException",
                    "States.Timeout",
                    "Lambda.ServiceException",
                    "Lambda.AWSLambdaException",
                    "Lambda.SdkClientException",
                    "Lambda.TooManyRequestsException"
                  ],
                  "IntervalSeconds": ${lambda_gda_account_creation_interval_seconds},
                  "MaxAttempts": ${lambda_gda_account_creation_retry_attempts},
                  "BackoffRate": 3
                }
              ],
              "Next": "CreateSubscriptions"
            },
            "CreateSubscriptions": {
              "Type": "Task",
              "Resource": "${lambda_create_subscription}",
              "Parameters": {
                "identity_id.$": "$.create_master_contact_result.identity_id",
                "vendor_uuid.$": "$.create_master_contact_result.vendor_uuid",
                "correlation_id.$": "$.correlation_id",
                "source.$": "$.source"
              },
              "HeartbeatSeconds": 60,
              "TimeoutSeconds": 3600,
              "ResultPath": "$.create_subscription_result",
              "Retry": [
                {
                  "ErrorEquals": [
                    "RetryException",
                    "States.Timeout",
                    "Lambda.ServiceException",
                    "Lambda.AWSLambdaException",
                    "Lambda.SdkClientException",
                    "Lambda.TooManyRequestsException"
                  ],
                  "IntervalSeconds": ${lambda_gda_account_creation_interval_seconds},
                  "MaxAttempts": ${lambda_gda_account_creation_retry_attempts},
                  "BackoffRate": 3
                }
              ],
              "End": true
            }
          }
        },
        {
          "StartAt": "CreateContracts",
          "States": {
            "CreateContracts": {
              "Type": "Task",
              "Resource": "${lambda_create_contract}",
              "Parameters": {
                "vendor_id.$": "$.create_vendor_result.vendor_id",
                "vendor_name.$": "$.create_vendor_result.name",
                "country.$": "$.country",
                "currency.$": "$.currency",
                "correlation_id.$": "$.correlation_id",
                "source.$": "$.source"
              },
              "HeartbeatSeconds": 60,
              "TimeoutSeconds": 3600,
              "ResultPath": "$.create_contract_result",
              "Retry": [
                {
                  "ErrorEquals": [
                    "RetryException",
                    "States.Timeout",
                    "Lambda.ServiceException",
                    "Lambda.AWSLambdaException",
                    "Lambda.SdkClientException",
                    "Lambda.TooManyRequestsException"
                  ],
                  "IntervalSeconds": ${lambda_gda_account_creation_interval_seconds},
                  "MaxAttempts": ${lambda_gda_account_creation_retry_attempts},
                  "BackoffRate": 3
                }
              ],
              "End": true
            }
          }
        }
      ],
      "Catch": [
        {
          "ErrorEquals": [
            "States.ALL"
          ],
          "Next": "SendFailureToKafka",
          "ResultPath": "$.create_user_and_contracts_errors"
        }
      ],
      "Next": "ArrayToProperties"
    },
    "ArrayToProperties": {
      "Type": "Pass",
      "Parameters": {
        "email.$": "$[0].email",
        "brand.$": "$[0].brand",
        "identity_id.$": "$[0].create_master_contact_result.identity_id",
        "label_profile_id.$": "$[0].create_master_contact_result.label_profile_id",
        "vendor_id.$": "$[0].create_vendor_result.vendor_id",
        "vendor_uuid.$": "$[0].create_master_contact_result.vendor_uuid",
        "voucher_code.$": "$[0].voucher_code",
        "voucher_code_owner.$": "$[0].voucher_code_owner",
        "source_id.$": "$[0].source_id",
        "correlation_id.$": "$[0].correlation_id",
        "source.$": "$[0].source",
        "service_tier_uuid.$": "$[0].service_tier_uuid"
      },
      "Next": "SendWelcomeEmail"

    },
    "SendWelcomeEmail": {
      "Type": "Task",
      "Resource": "${lambda_send_org_invite_email}",
      "HeartbeatSeconds": 60,
      "TimeoutSeconds": 3600,
      "ResultPath": "$.send_org_invite_email_result",
      "Retry": [
        {
          "ErrorEquals": [
            "RetryException",
            "States.Timeout",
            "Lambda.ServiceException",
            "Lambda.AWSLambdaException",
            "Lambda.SdkClientException",
            "Lambda.TooManyRequestsException"
          ],
          "IntervalSeconds": ${lambda_gda_account_creation_interval_seconds},
          "MaxAttempts": ${lambda_gda_account_creation_retry_attempts},
          "BackoffRate": 3
        }
      ],
      "Catch": [
        {
          "ErrorEquals": [
            "States.ALL"
          ],
          "Next": "SendFailureToKafka",
          "ResultPath": "$.send_org_invite_email_errors"
        }
      ],
      "Next": "CheckRequiredFieldsForRedeemVoucher"
    },
    "CheckRequiredFieldsForRedeemVoucher": {
      "Type": "Choice",
      "Choices": [
        {
          "And": [
            {
              "Variable": "$.voucher_code_owner",
              "IsPresent": true
            },
            {
              "Variable": "$.voucher_code_owner",
              "IsString": true
            },
            {
              "Variable": "$.voucher_code",
              "IsPresent": true
            },
            {
              "Variable": "$.voucher_code",
              "IsString": true
            },
            {
              "Variable": "$.source_id",
              "IsPresent": true
            },
            {
              "Variable": "$.source_id",
              "IsString": true
            },
            {
              "Variable": "$.email",
              "IsPresent": true
            },
            {
              "Variable": "$.email",
              "IsString": true
            },
            {
              "Variable": "$.correlation_id",
              "IsPresent": true
            },
            {
              "Variable": "$.source",
              "IsPresent": true
            },
            {
              "Variable": "$.source",
              "IsString": true
            }
          ],
          "Next": "RedeemVoucher"
        }
      ],
      "Default": "SendSuccessToKafka"
    },
    "RedeemVoucher": {
      "Type": "Task",
      "Resource": "${lambda_redeem_voucher}",
      "Parameters": {
        "voucher_code_owner.$": "$.voucher_code_owner",
        "voucher_code.$": "$.voucher_code",
        "source_id.$": "$.source_id",
        "email.$": "$.email",
        "correlation_id.$": "$.correlation_id",
        "source.$": "$.source"
      },
      "HeartbeatSeconds": 60,
      "TimeoutSeconds": 3600,
      "ResultPath": "$.redeem_voucher_result",
      "Retry": [
        {
          "ErrorEquals": [
            "RetryException",
            "States.Timeout",
            "Lambda.ServiceException",
            "Lambda.AWSLambdaException",
            "Lambda.SdkClientException",
            "Lambda.TooManyRequestsException"
          ],
          "IntervalSeconds": ${lambda_gda_account_creation_interval_seconds},
          "MaxAttempts": ${lambda_gda_account_creation_retry_attempts},
          "BackoffRate": 3
        }
      ],
      "Catch": [
        {
          "ErrorEquals": [
            "States.ALL"
          ],
          "Next": "SendSuccessToKafka",
          "ResultPath": "$.redeem_voucher_errors"
        }
      ],
      "Next": "SendSuccessToKafka"
    },
    "SendSuccessToKafka": {
      "Type": "Task",
      "Resource": "${lambda_send_success}",
      "HeartbeatSeconds": 60,
      "TimeoutSeconds": 3600,
      "ResultPath": "$.send_success_result",
      "Retry": [
        {
          "ErrorEquals": [
            "RetryException",
            "States.Timeout",
            "Lambda.ServiceException",
            "Lambda.AWSLambdaException",
            "Lambda.SdkClientException",
            "Lambda.TooManyRequestsException"
          ],
          "IntervalSeconds": ${lambda_gda_account_creation_interval_seconds},
          "MaxAttempts": ${lambda_gda_account_creation_retry_attempts},
          "BackoffRate": 3
        }
      ],
      "Catch": [
        {
          "ErrorEquals": [
            "States.ALL"
          ],
          "Next": "FailState",
          "ResultPath": "$.send_success_errors"
        }
      ],
      "End": true
    }
  }
}
