Public/New-PatientDeviceNeedsCsv.ps1

<#
    .SYNOPSIS
    Create a new CSV file that contains new patient device needs.

    .DESCRIPTION
    The patient device needs

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

    .OUTPUTS
    The file name if there are new needs or $null.

    If created, the CSV file create will contain the following columns:
        FamilyName
        GivenName
        Needs
        DeliveryChannel
        TierOfService
        Preferences
        MRN
        City
        State
        Address
        PostalCode
        District
        Country
        HomePhone
        MobilePhone
        CdrId

    .PARAMETER Folder
    The folder to place the needs file.

    .LINK

    .NOTES
    The file name will be created in yyyy-MM-dd-hhmmss format
#>

function New-PatientDeviceNeedsCsv {

    [CmdletBinding()]
    [OutputType([String])]
    param(
        [Parameter(Mandatory = $false, Position = 0, ValueFromPipeline)]
        [ValidateNotNullOrEmpty()]
        [String]
        $Folder = "./temp",

        [Parameter(Mandatory = $false, Position = 1)]
        [ValidateNotNull()]
        [PSCustomObject]
        $Def
    )

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

        New-Item -ItemType Directory -Force -Path $Folder | Out-Null
        if (-not $PSBoundParameters.ContainsKey('Def')) {
            $Def = Get-MetricDefinitions
        }
        $File = Join-Path -Path $Folder -ChildPath "$(Get-Date -f 'yyyy-MM-dd-hhmmss').csv"

        # If patients are change to a status of "Removed" then "reset" by deleting the needs tracking file.
        # If patients come back on service as "Pending - Activation" or "Active" then a new needs file will be generated.
        @(Get-Patients -Status "Removed") | ForEach-Object {
            $fileName = "$($_.cdrPatient.resource.id).json"
            $PatientFile = Join-Path -Path $Folder -ChildPath $fileName
            if ((Test-Path $PatientFile -PathType Leaf)) {
                Remove-Item -Path $PatientFile
                Write-Information "Removed patient needs file $($PatientFile)"
            }
        }

        # Only retrieve the statuses of 'Active' and 'Pending - Activation'
        $patients = @(Get-Patients -Status "Active") + @(Get-Patients -Status "Pending - Activation")
        $needs = Get-NewPatientDeviceNeeds -Def $Def -Folder $Folder -Patients $patients
        if ($needs) {
            Write-Information "$(Get-Date -f 'hh:mm:ss') Found $($needs.length) new device need(s)"
            $needs | Select-Object -Property Site, FamilyName, GivenName, Needs, DeliveryChannel, TierOfService, Preferences, MRN, City, State, Address, PostalCode, District, Country, HomePhone, MobilePhone, CdrId | Export-Csv -Path $File -NoTypeInformation | Out-Null
            Write-Output $File
        }
    }
}