Public/Get-SfPatients.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
<#
    .SYNOPSIS
    Queries salesforce for patients

    .DESCRIPTION
    Used to query for patients in the salesforce org by status

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

    .OUTPUTS
    An array of PSCustomObject with the properties:
        Id
        phecc__CDRPID__c
        phecc__Delivery_Channel__c
        phecc__Permission__c
        phecc__Status__c
        phecc__Tier_of_Service__c

    .PARAMETER Status
    The status to query patients. If not supplid then the default is "Active". Use $null for all patients.

    .EXAMPLE
    PS> $allPatients = Get-Patients -Status $null
    PS> $activePatients = Get-Patients
    PS> $pending = Get-Patients -Status "Pending - Activation"

    .LINK
    Set-Config

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

function Get-SfPatients {

    [CmdletBinding()]
    [OutputType([PSCustomObject[]])]
    param(
        [Parameter(Mandatory = $false, Position = 0, ValueFromPipeline)]
        [ValidateSet("Pending - Activation", "Active", "Suspended", "Pending - Removal", "Removed", "All", "Deleted")]
        [String]
        $Status = "All",

        [Parameter(Mandatory = $false, Position = 1)]
        [String]
        $Id
    )

    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)"
        $query = "SELECT Id,phecc__CDRPID__c,phecc__Permission__c,phecc__Status__c,phecc__Tier_of_Service__c,phecc__Delivery_Channel__c,phecc__Site__c,phecc__Deleted__c FROM phecc__Patient__c "
        if ($PSBoundParameters.ContainsKey('Status') -and ($Status -cne "All") -and ($Status -cne "Deleted")) {
            $query += "WHERE phecc__Deleted__c=False AND phecc__Status__c='$($Status)'"
        }
        elseif ($PSBoundParameters.ContainsKey('Status') -and ($Status -ceq "Deleted")) {
            $query += "WHERE phecc__Deleted__c=True"
        }
        else {
            $query += "WHERE phecc__Deleted__c=False"
        }
        if ($PSBoundParameters.ContainsKey('Id')) {
            $query += " AND Id='$($Id)'"
        }
        Invoke-SfQuery $query
    }
}