Public/Convert-CsvToMessages.ps1
<# .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 $Body = Get-SurveyResponsesFromCsv -SurveyName $Row.Name -AnswerFile $Row.AnswerFile -SurveyDefinitions $surveyDefs $Body.body.observation = $observation @{ body = $Body endpoint = "/services/apexrest/phecc/surveyResponseObservation/postObservationWithSurvey" } } } } } } |