Public/New-PatientDeviceNeedsCsv.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
<#
    .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 = "$($_.patient.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
        }
    }
}