Public/Add-PatientScenario.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<#
    .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
        }
    }
}