Public/Set-CriteriaCollection.ps1

<#
.SYNOPSIS
    Updates an existing criteria collection in the Fortytwo Collections API.

.DESCRIPTION
    This function updates an existing criteria collection in the Fortytwo Collections API.

#>

function Set-CriteriaCollection {
    [CmdletBinding(SupportsShouldProcess = $true)]
    param (
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [System.Collections.Hashtable]$Collection,
        [Parameter(Mandatory = $false)]
        [string]$Id = $null
    )

    process {
        # Test that the collection has members
        if (!$Collection.ContainsKey("id") -or [string]::IsNullOrEmpty($Collection.id)) {
            if ([string]::IsNullOrEmpty($Id)) {
                throw "The collection object must contain a valid 'id' property or the 'Id' parameter must be provided."
            } else {
                $Collection["id"] = $Id
            }
        }

        $URI = $Script:APIRoot -f "collections-criteria", "/$($Collection.id)"
        $Body = $Collection | Select-Object -ExcludeProperty id | ConvertTo-Json -Depth 10

        Write-Verbose "Updating Criteria Collection with at URI: $URI"

        $Result = Invoke-RestMethod -Method Put -Uri $URI -Headers (Get-CollectionHeader) -Body $Body -ContentType "application/json"
        
        if ($Result.IsSuccess) {
            Write-Verbose "Successfully updated Criteria Collection"
        }
        else {
            throw "Failed to update Criteria Collection: $($Result.Errors -join '; ')"
        }
    }
}