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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<#
    .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
            }
        }
    }
}