Public/New-PatientCalendar.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<#
    .SYNOPSIS
    Creates an new calendar for a patient

    .DESCRIPTION
    Creates an new calendar for a patient

    .INPUTS
    A PSCustomObject that is the patient.

    .OUTPUTS
    None.

    .PARAMETER Patient
    The Patient PSCustomObject.

    .NOTES
    This only handles adding a basic weight measurement. Should be expaneded for more detailed protocol saves.

    .LINK
    Get-Patients
#>

function New-PatientCalendar {

    [CmdletBinding()]
    [OutputType([System.Void])]
    param(
        [Parameter(Mandatory, ValueFromPipeline)]
        [ValidateNotNullOrEmpty()]
        [PSCustomObject]$Patient
    )

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

        $metricDefs = Get-MetricDefinitions
        $measurement = $metricDefs.measurements | Where-Object { $_.Name -eq "Weight" }
        $metric = $metricDefs.metrics | Where-Object { $_.Name -eq "Body Weight" }
        $unit = $metricDefs.units | Where-Object { $_.Name -eq "lb" }

        $cdrId = $Patient.cdrPatient.resource.id
        $sfId = $Patient.sfPatient.id

        $StartDate = Get-Date
        $StartDateString = $StartDate.ToString('yyyy-MM-dd')
        $EndDate = $StartDate.AddDays(90)
        $EndDateSTring = $EndDate.ToString('yyyy-MM-dd')

        $message = @{
            patient                  = @{
                attributes                = @{
                    type = "Patient__c"
                    url  = "/services/data/v49.0/sobjects/Patient__c/$($sfId)"
                }
                Id                        = $sfId
                CDRPID__c                 = $cdrId
                Name                      = $cdrId
                Status__c                 = "Pending - Activation"
                Unexpected_Observation__c = $false
                Time_Zone__c              = "Eastern Time (America/New_York)"
                Interventions_Expired__c  = $true
                Language__c               = "English"
            }
            protocolSubs             = @(
                @{
                    eId           = "1"
                    Patient__c    = $sfId
                    Is_Adhoc__c   = $true
                    Start_Date__c = $StartDateString
                }
            )
            plannedEntries           = @(
                @{
                    eId                        = "2"
                    pEId                       = "1"
                    Protocol_Subscription__c   = $null
                    title                      = $measurement.Name
                    Is_Template__c             = $false
                    Type__c                    = "Measurement"
                    Measurement_Type__c        = $measurement.Id
                    Recurrence_Pattern_Type__c = "Repeat Daily"
                    Until_Date__c              = $EndDateString
                    Delay__c                   = 0
                    modified                   = "true"
                    Hour__c                    = 6
                    Minute__c                  = 0
                }
            )
            interventionRules        = @(
                @{
                    Is_Template__c                    = $false
                    eId                               = "3"
                    pEId                              = "2"
                    psEId                             = "1"
                    markedDeleted                     = $false
                    allowSuppression                  = $false
                    suppressionIntervalInMinute       = 0
                    allowMultipleIRConditions         = $true
                    seqNum                            = 1
                    isSuppressIntervalInMinuteInValid = $false
                    Condition_Type__c                 = "Simple comparison"
                    Sequence_Number__c                = 1
                    Allow_Suppression__c              = $false
                    Suppression_Interval_In_Minute__c = 0
                    Rule_Text__c                      = "If : ( Body Weight: > 200.0 lb ) Then : ( Flag : Low )"
                }
            )
            stickyNotes              = @()
            appointments             = @()
            clinicianTasks           = @()
            ruleConditions           = @(
                @{
                    eId                      = "4"
                    Intervention_Rule__c     = $null
                    Is_Template__c           = $false
                    pEId                     = "3"
                    seqNum                   = 1
                    trendThresholdError      = $false
                    trendWindowError         = $false
                    xValueError              = $false
                    yValueError              = $false
                    changeOverTimeError      = $false
                    movingAverageError       = $false
                    baselinePercentageError  = $false
                    Sequence_Number__c       = 1
                    Survey_Answer__c         = $null
                    Change_Over_Time_Days__c = $null
                    Moving_Average_Days__c   = $null
                    Baseline_Percentage__c   = $null
                    Trend_Window__c          = $null
                    Trend_Threshold__c       = $null
                    Metric__c                = $metric.Id
                    Unit__c                  = $unit.Id
                    Low_Value__c             = "200"
                    Normalized_Low_Value__c  = 90.718474
                    High_Value__c            = $null
                })
            ruleActions              = @(@{
                    eId                    = "5"
                    Intervention_Rule__c   = $null
                    Is_Template__c         = $false
                    pEId                   = "3"
                    seqNum                 = 1
                    cTaskDescriptionError  = $false
                    cTaskDueDaysError      = $false
                    Action__c              = "Set a flag"
                    Sequence_Number__c     = 1
                    Flag_Severity_LT__c    = "Low"
                    PT_Survey__c           = $null
                    PT_Measurement_Type__c = $null
                    CT_Description__c      = $null
                    CT_Detail__c           = $null
                    CT_AssignedTo__c       = $null
                    CT_Due_Days__c         = $null
                    CT_Due_Minutes__c      = $null
                }
            )
            deletedProtocolSubs      = @()
            deletedPlannedEntries    = @()
            deletedStickyNotes       = @()
            deletedAppointments      = @()
            skipDates                = @()
            deletedInterventionRules = @()
            deletedRuleConditions    = @()
            deletedRuleActions       = @()
            deletedClinicianTasks    = @()
        }

        $calendarJson = ($message | ConvertTo-Json -Depth 100 -Compress)
        $calendarJson = $calendarJson -Replace """", "\"""
        Invoke-SfExecuteApex "phecc.DataManager.updateCalendarService('$($calendarJson)');" | Out-Null
    }
}