Public/Get-PatientMeasurementRequirements.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
<#
    .SYNOPSIS
    Gets the measurement requirements from the eCC salesforce org for patients.

    .DESCRIPTION
    Measurement requirements are determine by examining the planned entries on a patient calendar
    and the current devices assign.

    Any measurements required that do not have devices that support those measurement types are
    the needs.

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

    .OUTPUTS
    An array of PSObjects that contain the following members:
        - Patient - the patient object
        - has - array of measurement id that the patient has
        - needs - array of measurement ids that the patient needs
        - preferences - the array of all the preferences

    .PARAMETER Patients
    The patients to retrieve needs. See Get-Patients.

    .PARAMETER Def
    The metric metadata definitions from Get-MetricDefinitions. If not supplied then it will be called.

    .EXAMPLE
    PS> $requirements = Get-PatientMeasurementRequirements -Patients (Get-Patients -SelectCdrIds @("195a2e2a-0d78-4558-935d-1a1c793d8626"))

    .LINK

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

function Get-PatientMeasurementRequirements {
    param($Patients, $Def)

    if (-not $PSBoundParameters.ContainsKey('Def')) {
        $Def = Get-MetricDefinitions
    }

    # For performance reasons these query retrieve for all patient and not per patient
    $Devices = Get-SfDevices
    $PlannedEntries = Get-SfPlannedEntries
    $Preferences = Get-SfPatientPreferences

    foreach($Patient in $Patients) {
        $has = @()
        $Devices | Where-Object { $Patient.sfPatient.Id -eq $_.phecc__Patient__c } | ForEach-Object {
            $model = $_.phecc__Device_Model__c
            $has += $Def.capabilities | Where-Object { $_.phecc__Device_Model__c -eq $model } | Select-Object phecc__Measurement_Type__c -Expand phecc__Measurement_Type__c
        }
        $needs = $PlannedEntries | Where-Object { $Patient.sfPatient.phecc__Permission__c -eq $_.phecc__Permission__c } | Select-Object pecc__Measurement_Type__c -ExpandProperty phecc__Measurement_Type__c | Sort-Object | Get-Unique | Where-Object {$has -notcontains $_ }
        [PSobject]@{
            Patient = $Patient
            has = $Def.measurements | Where-Object { $has -contains $_.Id }
            needs = $Def.measurements | Where-Object { $needs -contains $_.Id }
            preferences = $Preferences | Where-Object { $_.phecc__Patient__c -eq $Patient.sfPatient.Id }
        }
    }
}