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-Config

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

function Add-PatientScenario {

    [CmdletBinding()]
    [OutputType([System.Void])]
    param(
        [Parameter(Mandatory = $false, Position = 0, ValueFromPipeline)]
        [PSCustomObject[]]
        $Patients,

        [Parameter(Mandatory = $false, Position = 1)]
        [System.DateTime]
        $StartDate,

        [Parameter(Mandatory = $false, Position = 2)]
        [Switch]
        $ClearMeasurements = $false,

        [Parameter(Mandatory = $false, Position = 3)]
        [String]
        $CsvFile = "data.csv",

        [Parameter(Mandatory = $false, Position = 4)]
        [String[]]
        $CdrIds,

        [Parameter(Mandatory = $false, Position = 5)]
        [Switch]
        $ClearFlags = $false
    )

    begin {
        Write-Verbose "[$($MyInvocation.MyCommand.Name)] Function started"
    }

    end {
        Write-Verbose "[$($MyInvocation.MyCommand.Name)] Complete"
    }

    process {
        Write-Debug "[$($MyInvocation.MyCommand.Name)] PSBoundParameters: $($PSBoundParameters | Out-String)"

        $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 -SelectCdrIds $CdrIds
            }
            else {
                $Patients = Get-Patients -Status "Active"
            }
        }
        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
            }
        }
    }
}