Public/New-Patient.ps1

function New-Patient {
    [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 {
    }

    process {
        $sfConfig = Get-SfConfig
        $orgId = $sfConfig.phecc__HSDP_Managing_Org_Id__c
        $fhirUrl = "$($sfConfig.phecc__FHIRURL__c)/Patient"

        $cdrPost = [PSObject]@{
            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)
    }
}