Public/Add-PatientScenario.ps1

<#
    .SYNOPSIS
    Will insert a scenario for patient measurements and surveys into a Salesforce org.

    .DESCRIPTION

    .INPUTS
    None. You cannot pipe objects to Get-Patients.

    .OUTPUTS
    None.

    .PARAMETER Patients
    An array of patient object. See the Get-Patients function. Use this parameter or the CdrIds.

    .PARAMETER StartDate
    The start date to insert values relative to the offset of each row in the scenario CSV. If not provided then
    the current date/time is used.

    .PARAMETER ClearMeasurements
    A switch that indicates if the measurements should be cleared for each patient. The default is $false

    .PARAMETER CsvFile
    The input CSV file containing the scenario. Defaults to "data.csv"

    .PARAMETER CdrIds
    An array of CDR identifiers. If this is passed instead of patients then the patient objects for these identifiers are used.

    .PARAMETER ClearFlags
    A switch that indicates if the flags should be cleared for each patient. The default is $false

    .EXAMPLE
    PS> Add-PatientScenario -CdrIds @("101273bc-3314-4dde-9252-70f79dc8503b") -Csv "mydata.csv" -ClearMeasurements -ClearFlags

    .EXAMPLE
    PS> Add-PatientScenario -CdrIds @("101273bc-3314-4dde-9252-70f79dc8503b") -Csv "mydata.csv" -ClearMeasurements -ClearFlags

    .LINK
    Get-Patients
    Set-FileConfig

    .NOTES
    Assumes config is initialized for org access.
#>

function Add-PatientScenario {
    param($Patients, [System.DateTime]$StartDate, [Switch]$ClearMeasurements = $false, [String]$CsvFile = "data.csv", $CdrIds, [Switch]$ClearFlags = $false)

    $limits = Get-SFLimits
    Write-Debug "Salesforce API limit exceeded. Request API limit extension. Max: $($limits.DailyApiRequests.Max) Remaining: $($limits.DailyApiRequests.Remaining)"

    if ($limits.DailyApiRequests.Remaining -lt 0) {
        throw "Salesforce API limit exceeded. Request API limit extension. Max: $($limits.DailyApiRequests.Max) Remaining: $($limits.DailyApiRequests.Remaining)"
    }

    if ($limits.DailyApiRequests.Remaining -lt 1000) {
        Write-Warning "Low Salesforce API remaining Max: $($limits.DailyApiRequests.Max) Remaining: $($limits.DailyApiRequests.Remaining)"
    }

    if (-not $PSBoundParameters.ContainsKey('StartDate')) {
        $StartDate = Get-Date
    }
    if (-not $PSBoundParameters.ContainsKey('Patients')) {
        if ($PSBoundParameters.ContainsKey('CdrIds')) {
            $Patients = Get-Patients $CdrIds
        } else {
            $Patients = Get-Patients
        }
    }
    if ($Patients) {
        $Patients | ForEach-Object {
            if ($ClearMeasurements) {
                Invoke-SfExecuteApex "phecc.TriggerHandlers.setEnabled(false, new List<String> {'ObservationBD' });"
                Remove-Measurements -Patient $_
            }
            if ($ClearFlags) {
                Invoke-SfExecuteApex "phecc.TriggerHandlers.setEnabled(false, new List<String> {'FlagBD' });"
                Remove-Flags -Patient $_
            }
            Write-Debug "Adding data for SFID: $($_.sfPatient.Id) CDR: $($_.cdrPatient.resource.id)"
            $messages = Convert-CsvToMessages -CsvFile $CsvFile -Patient $_
            Add-MessagesToOrg -Messages $messages
        }
    }
}