Public/Get-Patients.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
<#
    .SYNOPSIS
    Returns a selected subset of patients in a Salesforce org.

    .DESCRIPTION
    Queries for Salesforce patients and queires the associated CDR resources for those patients.

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

    .OUTPUTS
    An array of PSCustomObjects with each object contains the following keys:
        - sfPatient - the salesforce patient object
        - cdrPatient - the FHIR resource from PDS or CDR

    .PARAMETER SelectCdrIds
    An array of CDR ids to retrieve.

    .PARAMETER Status
    Only return patients with this status. Defaults to "Active"
    Can pass $null for all patients regardless of status.

    .PARAMETER Prompt
    If specified will display an interactive UI to select one or more patients. Default is not not prompt.

    .EXAMPLE
    PS> $patients = Get-Patients -Prompt -Status $null

    .LINK
    Set-Config

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

function Get-Patients {

    [CmdletBinding()]
    [OutputType([PSCustomObject[]])]
    param(
        [Parameter(Mandatory = $false, Position = 0, ValueFromPipeline)]
        [String[]]
        $SelectCdrIds,

        [Parameter(Mandatory = $false, Position = 1)]
        [ValidateSet("Pending - Activation", "Active", "Suspended", "Pending - Removal", "Removed", "All", "Deleted")]
        [String]
        $Status = "All",

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

        [Parameter(Mandatory = $false, Position = 3)]
        [String]
        $SfId
    )

    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)"
        $params = @{ Status = $Status }
        if ($PSBoundParameters.ContainsKey('SfId')) {
            $params.Add("Id", $SfId)
        }

        $sfPatients = Get-SfPatients @params
        if ($sfPatients.Count -gt 0) {
            $cdrIds = @($sfPatients | Select-Object phecc__CDRPID__c -ExpandProperty phecc__CDRPID__c)
            $bundle = Get-CdrPatients -Ids $cdrIds
            $cdrPatients = $bundle.entry | Select-Object resource

            if ($PSBoundParameters.ContainsKey('SelectCdrIds')) {
                $selected = $cdrPatients | Where-Object { $SelectCdrIds.Contains($_.resource.Id) } | Select-Object -ExpandProperty resource | Select-Object -Expandproperty id
                @($selected | ForEach-Object {
                        $cdrId = $_
                        @{
                            sfPatient  = ($sfPatients | Where-Object { $_.phecc__CDRPID__c -eq $cdrId })
                            cdrPatient = ($cdrPatients | Where-Object { $_.resource.id -eq $cdrId })
                        }
                    })
            }
            elseif ($PSBoundParameters.ContainsKey('Prompt')) {
                $selected = ($cdrPatients | ForEach-Object { $_.resource } | Select-Object id, @{Name = 'family'; expression = { ($_.name | Where-Object { $_.use -eq 'usual' }).family } }, @{Name = 'given'; expression = { ($_.name | Where-Object { $_.use -eq 'usual' }).given } }, birthDate) | Out-GridView -PassThru | Select-Object Id -ExpandProperty Id
                @($selected | ForEach-Object {
                        $cdrId = $_
                        @{
                            sfPatient  = ($sfPatients | Where-Object { $_.phecc__CDRPID__c -eq $cdrId })
                            cdrPatient = ($cdrPatients | Where-Object { $_.resource.id -eq $cdrId })
                        }
                    })
            }
            else {
                $map = @{}
                $cdrPatients | ForEach-Object { $map.Add($_.resource.id, $_) }
                @($sfPatients | ForEach-Object {
                        @{
                            sfPatient  = $_
                            cdrPatient = $map."$($_.phecc__CDRPID__c)"
                        }
                    }
                )

            }

        }
    }
}