From dc9fd6fd35e5ae6ed744ea2328a043556466e80b Mon Sep 17 00:00:00 2001 From: rulasg Date: Wed, 19 Mar 2025 11:19:38 +0100 Subject: [PATCH] feat: add Force parameter to functions and update tests for caching behavior --- Test/public/getstaccount.test.ps1 | 2 ++ public/getsfaccount.ps1 | 5 +++-- public/getsfopportunity.ps1 | 12 ++++++++++-- public/getsfuser.ps1 | 15 +++++++++++++-- public/sfDataQuery.ps1 | 16 ++++++++++------ public/transformations.ps1 | 5 +++-- 6 files changed, 41 insertions(+), 14 deletions(-) diff --git a/Test/public/getstaccount.test.ps1 b/Test/public/getstaccount.test.ps1 index f0f2fcd..40e681f 100644 --- a/Test/public/getstaccount.test.ps1 +++ b/Test/public/getstaccount.test.ps1 @@ -18,6 +18,7 @@ function Test_GetSfAccount{ $result = Get-SfAccount -SfUrl $url # Assert with cache + Assert-AreEqual -Expected "Hashtable" -Presented $result.GetType().BaseType.Name Assert-AreEqual -Expected $id -Presented $result.Id $path = Get-Mock_DatabaseStore | Join-Path -ChildPath $filename Assert-ItemExist -Path $path @@ -31,6 +32,7 @@ function Test_GetSfAccount{ $result = Get-SfAccount -SfUrl $url # Assert + Assert-AreEqual -Expected "Hashtable" -Presented $result.GetType().BaseType.Name Assert-AreEqual -Expected "0010V00002KIWkaQAH" -Presented $result.Id } diff --git a/public/getsfaccount.ps1 b/public/getsfaccount.ps1 index 308585d..92cc2a1 100644 --- a/public/getsfaccount.ps1 +++ b/public/getsfaccount.ps1 @@ -27,7 +27,8 @@ function Get-SfAccount{ [CmdletBinding()] param( [Parameter(Mandatory,Position=0)][string]$SfUrl, - [string]$AdditionalAttributes + [string]$AdditionalAttributes, + [switch]$Force ) # Extract Id from URL @@ -70,7 +71,7 @@ function Get-SfAccount{ } # Get object - $ret = Get-SfDataQuery -Type Account -Id $Id -Attributes $attributes + $ret = Get-SfDataQuery -Type Account -Id $Id -Attributes $attributes -Force:$Force # Transformations $ret = $ret | Edit-AttributeValueFromHTML ` diff --git a/public/getsfopportunity.ps1 b/public/getsfopportunity.ps1 index 9542913..475bf5a 100644 --- a/public/getsfopportunity.ps1 +++ b/public/getsfopportunity.ps1 @@ -10,6 +10,13 @@ The `Get-SfOpportunity` function extracts the Salesforce Opportunity ID from the .PARAMETER SfUrl The Salesforce URL of the Opportunity object to query. +.PARAMETER AdditionalAttributes +Additional attributes to retrieve from the Opportunity object. This parameter accepts a comma-separated string of attribute names. + +.PARAMETER Force +Forces the function to bypass any caching +when retrieving data. If this switch is set, the function will always query Salesforce for the latest data, regardless of any cached results. + .OUTPUTS The function returns a PowerShell object representing the queried Salesforce Opportunity data. If the query is unsuccessful or the object is not found, the function returns `$null`. @@ -25,7 +32,8 @@ function Get-SfOpportunity{ [CmdletBinding()] param( [Parameter(Mandatory,Position=0)][string]$SfUrl, - [string]$AdditionalAttributes + [string]$AdditionalAttributes, + [switch]$Force ) # Extract Id from URL @@ -68,7 +76,7 @@ function Get-SfOpportunity{ } # Get object - $ret = Get-SfDataQuery -Type opportunity -Id $Id -Attributes $attributes + $ret = Get-SfDataQuery -Type opportunity -Id $Id -Attributes $attributes -Force:$Force # Transformations diff --git a/public/getsfuser.ps1 b/public/getsfuser.ps1 index 1e3a977..099bef2 100644 --- a/public/getsfuser.ps1 +++ b/public/getsfuser.ps1 @@ -8,6 +8,16 @@ This function retrieves user information using the `sf data query` command. .PARAMETER Id The Salesforce user ID. +.PARAMETER AdditionalAttributes +Additional attributes to retrieve from the User object. This parameter accepts a comma-separated string of attribute names. + +.PARAMETER Force +Forces the function to bypass any caching +when retrieving data. If this switch is set, the function will always query Salesforce for the latest data, regardless of any cached results. + +.OUTPUTS +The function returns a PowerShell object representing the queried Salesforce User data. If the query is unsuccessful or the object is not found, the function returns `$null`. + .EXAMPLE Get-SfUser -Id "0050V000001Sv7XQAS" @@ -21,7 +31,8 @@ function Get-SfUser{ param( [Parameter(Mandatory,Position=0)] [string]$Id, - [string]$AdditionalAttributes + [string]$AdditionalAttributes, + [switch]$Force ) $attributes = @( @@ -41,7 +52,7 @@ function Get-SfUser{ } # Get object - $ret = Get-SfDataQuery -Type User -Id $Id -Attributes $attributes + $ret = Get-SfDataQuery -Type User -Id $Id -Attributes $attributes -Force:$Force # remove attributes # $ret.PsObject.Properties.Remove("attributes") diff --git a/public/sfDataQuery.ps1 b/public/sfDataQuery.ps1 index b4c9df2..0820d18 100644 --- a/public/sfDataQuery.ps1 +++ b/public/sfDataQuery.ps1 @@ -5,13 +5,17 @@ function Get-SfDataQuery{ param( [Parameter(Mandatory)][ValidateSet("Account", "User", "Opportunity")][string]$Type, [Parameter(Mandatory)][string]$Id, - [Parameter(Mandatory)][string[]]$Attributes + [Parameter(Mandatory)][string[]]$Attributes, + [switch]$Force ) - # Testcache first - $cacheKey = getcacheKey -Type $Type -Id $Id -Attributes $Attributes - if(Test-Database -Key $cacheKey){ - return Get-Database -Key $cacheKey + # avoid cache if Force is set + if(-Not $Force){ + # Testcache first + $cacheKey = getcacheKey -Type $Type -Id $Id -Attributes $Attributes + if(Test-Database -Key $cacheKey){ + return Get-Database -Key $cacheKey + } } $params = @{ @@ -22,7 +26,7 @@ function Get-SfDataQuery{ $result = Invoke-MyCommand -Command "sfDataQuery" -Param $params - $obj = $result | ConvertFrom-Json -Depth 10 + $obj = $result | ConvertFrom-Json -Depth 10 -asHashtable if($obj.status -ne 0){ throw "Status $($obj.status)" diff --git a/public/transformations.ps1 b/public/transformations.ps1 index 13a856f..65337b7 100644 --- a/public/transformations.ps1 +++ b/public/transformations.ps1 @@ -25,10 +25,11 @@ function Edit-AttributeValueFromHTML{ } # Add the new attribute - Add-Member -InputObject $ret -MemberType NoteProperty -Name $NewAttributeName -Value $value + $object.$NewAttributeName = $value + # Remove the original attribute if specified if ($RemoveOriginalAttribute) { - $Object.PsObject.Properties.Remove($AttributeName) + $Object.Remove($AttributeName) } return $Object