Public/Test-CriteriaCollectionMember.ps1

<#
.SYNOPSIS
    Tests the members of a criteria collection in the Fortytwo Collections API.

.DESCRIPTION
    This function tests the members of a criteria collection in the Fortytwo Collections API.

#>

function Test-CriteriaCollectionMember {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [System.Collections.Hashtable] $Collection
    )

    process {
        $Body = @{
            preview = $Collection
        } | ConvertTo-Json -Depth 10

        $URI = ($Script:APIRoot -f "collections-criteria", "/preview")
        Write-Verbose "Testing Criteria Collection Members with URI: $URI"
        $Result = Invoke-RestMethod -Method Post -Uri $URI -Headers (Get-CollectionHeader) -Body $Body -ContentType "application/json"
        
        if ($Result.IsSuccess) {
            Write-verbose "Successfully previewed Criteria Collection with $($Result.Data.Preview.Count) members."
            return $Result.Data
        }
        else {
            if(!$Result.Errors) {
                if ($StatusCode -eq 403) {
                    throw "Access denied. Please ensure that your access token has the necessary permissions to access Fortytwo Collections."
                }
                else {
                    throw "Failed to preview Criteria Collection. StatusCode: $StatusCode"
                }
            }
            throw "Failed to preview Criteria Collection: $($Result.Errors -join '; ')"
        }

    }
}