Public/Import-JoinableCollectionMemberBatch.ps1

<#
.SYNOPSIS
    Imports a batch of joinable collection member IDs into the Fortytwo Collections API.

.DESCRIPTION
    This function imports a batch of joinable collection member IDs into the Fortytwo Collections API.

#>

function Import-JoinableCollectionMemberBatch {
    [CmdletBinding(SupportsShouldProcess = $true)]
    param (
        [Alias("CollectionId")]
        [Parameter(Mandatory = $true)]
        [ValidatePattern("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$", ErrorMessage = "Id must be a valid GUID.")] # Is a guid connector object id
        [string] $Id,

        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [string[]]$Members
    )

    process {
        # TODO: Add validation for the Members array to ensure it meets the required structure and properties.
        $URI = $Script:APIRoot -f "collections-joinable", "/$Id/results/members"
        $Body = @{ coreIdentityIds = @($Members) } | ConvertTo-Json -Depth 10

        Write-Verbose "Importing Joinable Collection Member Batch at URI: $URI"

        $Result = Invoke-RestMethod -Method Post -Uri $URI -Headers (Get-CollectionHeader) -Body $Body -ContentType "application/json" -StatusCodeVariable StatusCode

        if ($Result.IsSuccess) {
            Write-Verbose "Successfully imported Joinable Collection Member Batch with Id: $($Result.Data.Id)"
        }
        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 import Joinable Collection Member Batch. StatusCode: $StatusCode"
                }
            }
            throw $($Result.Errors -join '; ')
        }
        
    }
}