Public/Get-CdrPatients.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
<#
    .SYNOPSIS
    Fetchs patient FHIR resources from the CDR/PDS for a supplied list of CDR ids.

    .DESCRIPTION

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

    .OUTPUTS
    The response from the POST to /Patient/_search to the CDR/PDS endpoint configured in the salesforce org

    .PARAMETER Ids
    An array of patient resource ids

    .EXAMPLE
    C:\PS> $result = Get-CdrPatients -Ids @("5b135c52-f603-4044-a49f-2f56967c4449", "5555c808-c186-48ab-a6c0-a8aedbfad852")

    .LINK
    Set-Config

    .NOTES
    Assumes config is initialized for org access.
    Returns a maximum of 5000 patient resources
#>

function Get-CdrPatients {

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

    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)"

        $fhirUrl = "$((Get-SfConfig).phecc__FHIRURL__c)/Patient/_search"
        $headers = @{
            "Connection"    = "keep-alive"
            "api-version"   = "2"
            "Authorization" = "Bearer $($script:__iamAuth.access_token)"
            "Content-Type"  = "application/x-www-form-urlencoded; charset=UTF-8"
        }
        $idList = $Ids -join ","
        $body = "_id=$($idList)&_count=5000"
        Invoke-RestMethod -Uri $fhirUrl -Method Post -Headers $headers -Body $body
    }
}