Public/New-Patient.ps1

<#
    .SYNOPSIS
    Creates an new patient in eCC

    .DESCRIPTION
    Create a new patient in Salesforce and PDS/CDR.

    .INPUTS
    None. You cannot pipe objects to New-Patient.

    .OUTPUTS
    The new PSCustomObject that represents the patient

    .PARAMETER FamilyName
    The family name of the patient

    .PARAMETER GivenName
    The given name of the patient

    .PARAMETER BirthDate
    The birthdate of the patient

    .PARAMETER Gender
    The gender of the patient

    .PARAMETER EMRPID
    The EMR ID of the patient

    .PARAMETER MRN
    The MRN of the patient

    .PARAMETER TACProgram
    The TAC Program for the patient. Defaults to 'eTrAC'

    .PARAMETER DeliveryChannel
    The delivery channel for the patient. Defaults to 'Managed Tablet'

    .PARAMETER TierOfService
    The Tie of service for the patient. Defaults to 'Standard'

    .LINK
#>

function New-Patient {

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

        [Parameter(Mandatory = $true, Position = 1)]
        [ValidateNotNullOrEmpty()]
        [String]
        $GivenName,

        [Parameter(Mandatory = $true, Position = 2)]
        [ValidateNotNullOrEmpty()]
        [DateTime]
        $BirthDate,

        [Parameter(Mandatory = $true, Position = 3)]
        [ValidateNotNullOrEmpty()]
        [ValidateSet("male", "female", "undifferentiated")]
        $Gender,

        [Parameter(Mandatory = $false, Position = 4)]
        [String]
        $EMRPID,

        [Parameter(Mandatory = $false, Position = 5)]
        [String]
        $MRN,

        [Parameter(Mandatory = $false, Position = 6)]
        [String]
        $TACProgram = "eTrAC",

        [Parameter(Mandatory = $false, Position = 7)]
        [String]
        $DeliveryChannel = "Managed Tablet",

        [Parameter(Mandatory = $false, Position = 8)]
        [String]
        $TierOfService = "Standard"

    )

    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)"
        $sfConfig = Get-SfConfig
        $orgId = $sfConfig.phecc__HSDP_Managing_Org_Id__c
        $fhirUrl = "$($sfConfig.phecc__FHIRURL__c)/Patient"

        $cdrPost = [PSCustomObject]@{
            name                 = @(
                @{
                    use    = "usual"
                    family = @($FamilyName)
                    given  = @($GivenName)
                    prefix = @()
                },
                @{
                    use    = "nickname"
                    family = @()
                    given  = @()
                    prefix = @()
                }
            )
            telecom              = @(
                @{
                    system = "phone"
                    use    = "home"
                    value  = ""
                    rank   = 1
                },
                @{
                    system = "phone"
                    use    = "mobile"
                    value  = ""
                    rank   = 2
                },
                @{
                    system = "email"
                    value  = ""
                    rank   = 1
                },
                @{
                    system = "email"
                    value  = ""
                    rank   = 2
                }
            )
            birthDate            = $BirthDate.ToString("yyyy-MM-dd")
            gender               = $Gender.ToLower()
            address              = @(
                @{
                    city       = ""
                    district   = ""
                    state      = ""
                    postalCode = ""
                    country    = ""
                    line       = @()
                }
            )
            identifier           = @(
                @{
                    value  = $MRN
                    system = "MRN"
                    type   = @{
                        coding = @(
                            @{
                                system = "http://hl7.org/fhir/v2/0203"
                                code   = "MR"
                            }
                        )
                    }
                },
                @{
                    value  = ""
                    system = "Last4SSN"
                    type   = @{
                        coding = @(
                            @{
                                system = "http://hl7.org/fhir/identifier-type"
                                code   = "SNO"
                            }
                        )
                    }
                },
                @{
                    value  = $EMRPID
                    system = "EMRPID"
                    type   = @{
                        coding = @(
                            @{
                                system = "http://hl7.org/fhir/identifier-type"
                                code   = "PLAC"
                            }
                        )
                    }
                }
            )
            managingOrganization = @{
                type      = "Organization"
                id        = $orgId
                reference = "Organization/$($orgId)"
            }
            extension            = @()
            resourceType         = "Patient"
        }
        $json = ConvertTo-Json $cdrPost -Depth 100

        $headers = @{
            "Connection"    = "keep-alive"
            "api-version"   = "1"
            "Authorization" = "Bearer $($script:__iamAuth.access_token)"
            "Accept"        = "application/json"
            "Content-Type"  = "application/json"
        }
        $cdrPatient = Invoke-RestMethod -Uri $fhirUrl -Method Post -Headers $headers -Body $json

        $sfPost = [PSCustomObject]@{
            phecc__Status__c           = "Pending - Activation"
            phecc__Language__c         = "English"
            phecc__Locale__c           = "English (United States)"
            phecc__Time_Zone__c        = "Eastern Time (America/New_York)"
            phecc__Start_of_Day__c     = "09:00 AM"
            phecc__Tier_of_Service__c  = $TierOfService
            phecc__Delivery_Channel__c = $DeliveryChannel
            phecc__TAC_Program__c      = $TACProgram
            phecc__CDRPID__c           = $cdrPatient.Id
            phecc__Site__c             = "a1O3t0000077O6HEAU"
        }

        $json = ConvertTo-Json $sfPost -Depth 100
        $sfPatient = Invoke-SfApi -Path "/sobjects/phecc__Patient__c" -Method Post -Body $json

        [PSCustomObject](Get-Patients -sfid $sfPatient.Id | Select-Object -First 1)
    }
}