Public/Convert-CsvToMessages.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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
<# .SYNOPSIS Converts a CSV file to message objects for posting to salesforce APIs .DESCRIPTION The CSV file must contain the following columns: "Type" - either "Measurement" or "Survey" "MinuteOffset" - the offset in minutes from StartDate "Name" - Either the name of the measurement type or the name of the survey The additional columns required are determined by the value of each row in the Type and Name columns For Type="Survey": The "AnswerFile" column is required. It must specify the name of the file that contains the survey responses. See help on Get-SurveyResponsesFromCsv for a description of the format of this file. Example: "MinuteOffset","Type","Name","AnswerFile" "0","Survey","Blood Pressure Change Survey" For Type="Measurement": The Measurment name must match a measurement name eCC phecc__Measurement_Type__c table in the org A column can be added for each Metric name from the 'phecc__Metric__c' table in the org If the column is present the value in this column is used for the message And additional column is required to specify the units for the metric. The column must be named '<metric name>:units" The value must must be a valid unit for the metric from the 'phecc__Unit__c' table in the org Example: "MinuteOffset","Type","Name","Pulse (Average) - Wearable","Pulse (Average) - Wearable:units" "0","Measurement","Wearable","71","Bpm" .INPUTS Accepts the CsvFile to read .OUTPUTS An Array of PSCustomObjects that contains body and endpoints to call .PARAMETER CsvFile The CSV file to read sample data. Defaults to "data.csv" .PARAMETER Patient The patient object .PARAMETER StartDate The StartDate to start inserting the messages using the minute offset of each message .EXAMPLE C:\PS> .LINK .NOTES #> function Convert-CsvToMessages { [CmdletBinding()] [OutputType([PSCustomObject[]])] param( [Parameter(Mandatory = $false, Position = 0, ValueFromPipeline)] [ValidateNotNullOrEmpty()] [String] $CsvFile = "data.csv", [Parameter(Mandatory = $true, Position = 1)] [ValidateNotNull()] [PSCustomObject] $Patient, [Parameter(Mandatory = $false, Position = 2)] [System.DateTime] $StartDate ) 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)" $def = Get-MetricDefinitions $surveyDefs = Get-SurveyDefinitions if (-not $PSBoundParameters.ContainsKey('StartDate')) { $StartDate = Get-Date } $Start = Get-Date -Year $StartDate.year -Month $StartDate.month -Day $StartDate.Day -Hour $StartDate.Hour -Minute $StartDate.Minute -Second 0 -Millisecond 0 $Rows = Import-Csv -Path $CsvFile foreach ($Row in $Rows) { $DateTime = $Start.AddMinutes($Row.MinuteOffset) switch ($Row.Type) { "Measurement" { $measurement = $def.measurements | Where-Object { $_.Name -eq $Row.Name } if (-not $measurement) { throw "Measurement name $($Row.Name) not found. File $($CsvFile) is invalid." } $p = @{ PatientId = $Patient.sfPatient.Id DateTime = $DateTime DeviceType = $measurement.phecc__Device_Type_Code__c DeviceId = (New-Guid).Guid Gateway = (New-Guid).Guid GatewayDeviceId = (New-Guid).Guid ExternalObsId = (New-Guid).Guid } $observation = New-Observation @p $scalars = @() $measurementMetrics = $def.measurementMetrics | Where-Object { $_.phecc__Measurement_Type__c -eq $measurement.Id } if (-not $measurementMetrics) { throw "Metrics not found for measurement $($measurement.Name). File $($CsvFile) is invalid." } foreach ($measurementMetric in $measurementMetrics) { $metrics = $def.metrics | Where-Object { $_.Id -eq $measurementMetric.phecc__Metric__c } foreach ($metric in $metrics) { $valueToUse = $Row."$($metric.Name)" if ($valueToUse) { $metricUnits = $def.metricUnits | Where-Object { $_.phecc__Metric__c -eq $metric.Id } foreach ($metricUnit in $metricUnits) { $unitToUse = $Row."$($metric.Name):units" $unit = $def.units | Where-Object { $_.Id -eq $metricUnit.phecc__Unit__c -and $_.Name -eq $unitToUse } if ($unit) { $params = @{ MetricTypeCode = $metric.phecc__Metric_Type_Code__c UnitTypeCode = $unit.phecc__Unit_Type_Code__c Value = $valueToUse DateTime = $DateTime } $scalars += (New-Scalar @params) } } } } } $Body = New-ObservationScalars -Observation $observation -Scalars $scalars Write-Output @{ body = $Body endpoint = "/services/apexrest/phecc/scalarObservation/postObservationWithScalars" } } "Survey" { $p = @{ PatientId = $Patient.sfPatient.id DateTime = $DateTime DeviceType = (New-Guid).Guid DeviceId = (New-Guid).Guid Gateway = (New-Guid).Guid GatewayDeviceId = (New-Guid).Guid ExternalObsId = (New-Guid).Guid } $observation = New-Observation @p # The answer file should be in the same folder as the measurement file $AnswerFile = Join-Path (Get-Item $CsvFile | split-path -parent) $Row.AnswerFile $Body = Get-SurveyResponsesFromCsv -SurveyName $Row.Name -AnswerFile $AnswerFile -SurveyDefinitions $surveyDefs $Body.body.observation = $observation @{ body = $Body endpoint = "/services/apexrest/phecc/surveyResponseObservation/postObservationWithSurvey" } } } } } } |