// Jenkinsfile for the `ows-abacus-event-create-ingest-sales-event` job on scheduler.theorchard.io

pipeline {
    agent any

    options {
        ansiColor('xterm')
        disableConcurrentBuilds()
        timestamps()
    }

    parameters {
        choice(name: 'ENVIRONMENT', choices: ['qa', 'prod'], description: 'The environment to create the event for')
        choice(name: 'SALES_TYPE', choices: ['distro', 'nr'], description: 'The type of sales to ingest')
        string(name: 'BATCH_ID', description: 'The ID of the batch of sales to ingest')
    }

    stages {
        stage('Create Abacus Event') {
            steps {
                sh '''
                    if [ -z ${ENVIRONMENT} ]; then
                        echo 'Missing `ENVIRONMENT` parameter'
                        exit 1
                    fi

                    if [ -z ${SALES_TYPE} ]; then
                        echo 'Missing `SALES_TYPE` parameter'
                        exit 1
                    fi

                    if [ -z ${BATCH_ID} ]; then
                        echo 'Missing `BATCH_ID` parameter'
                        exit 1
                    fi

                    curl https://${ENVIRONMENT}-ows-abacus-event.theorchard.io/abacus-event \
                        --request POST \
                        --header 'Content-Type: application/json' \
                        --data '{
                            "event_name": "'ingest_sales_${SALES_TYPE}'",
                            "target_type": "batch",
                            "target_id": '${BATCH_ID}'
                        }' \
                        --fail-with-body
                '''
            }
        }
    }
}
