Public/New-PatientCalendar.ps1

<#
    .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
    }
}