Public/ScopingCriteria/New-JIMScopingCriteriaGroup.ps1

# Copyright (c) Tetron Limited. All rights reserved.
# Licensed under the Tetron Commercial License. See LICENSE file in the project root.

function New-JIMScopingCriteriaGroup {
    <#
    .SYNOPSIS
        Creates a new scoping criteria group for a Synchronisation Rule.
 
    .DESCRIPTION
        Creates a new scoping criteria group on an export Synchronisation Rule.
        Groups can be created at the root level or nested within an existing group.
        The group type determines how criteria within it are evaluated:
        - All: All criteria must match (AND logic)
        - Any: At least one criterion must match (OR logic)
 
    .PARAMETER SyncRuleId
        The unique identifier of the Synchronisation Rule.
 
    .PARAMETER ParentGroupId
        Optional. The ID of an existing group to nest this group within.
        If not specified, creates a root-level group.
 
    .PARAMETER Type
        The logical operator for this group: 'All' (AND) or 'Any' (OR).
        Defaults to 'All'.
 
    .PARAMETER Position
        Optional position/order for this group. Defaults to 0.
 
    .PARAMETER PassThru
        If specified, returns the created group object.
 
    .OUTPUTS
        If -PassThru is specified, returns the created scoping criteria group.
 
    .EXAMPLE
        New-JIMScopingCriteriaGroup -SyncRuleId 5 -Type All
 
        Creates a root-level criteria group with AND logic.
 
    .EXAMPLE
        New-JIMScopingCriteriaGroup -SyncRuleId 5 -ParentGroupId 10 -Type Any -PassThru
 
        Creates a child group with OR logic nested under group 10.
 
    .LINK
        Get-JIMScopingCriteria
        Set-JIMScopingCriteriaGroup
        Remove-JIMScopingCriteriaGroup
    #>

    [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Medium')]
    [OutputType([PSCustomObject])]
    param(
        [Parameter(Mandatory, ValueFromPipelineByPropertyName)]
        [int]$SyncRuleId,

        [Parameter()]
        [int]$ParentGroupId,

        [Parameter()]
        [ValidateSet('All', 'Any')]
        [string]$Type = 'All',

        [Parameter()]
        [int]$Position = 0,

        [switch]$PassThru
    )

    process {
        if (-not $script:JIMConnection) {
            Write-Error "You are not connected to JIM. Run Connect-JIM -Url <your JIM URL> to authenticate, then try again."
            return
        }

        $body = @{
            type = $Type
            position = $Position
        }

        $endpoint = if ($PSBoundParameters.ContainsKey('ParentGroupId')) {
            "/api/v1/synchronisation/sync-rules/$SyncRuleId/scoping-criteria/$ParentGroupId/child-groups"
        }
        else {
            "/api/v1/synchronisation/sync-rules/$SyncRuleId/scoping-criteria"
        }

        $target = if ($PSBoundParameters.ContainsKey('ParentGroupId')) {
            "Synchronisation Rule $SyncRuleId (under group $ParentGroupId)"
        }
        else {
            "Synchronisation Rule $SyncRuleId (root level)"
        }

        if ($PSCmdlet.ShouldProcess($target, "Create Scoping Criteria Group ($Type)")) {
            Write-Verbose "Creating scoping criteria group for Synchronisation Rule $SyncRuleId"

            try {
                $result = Invoke-JIMApi -Endpoint $endpoint -Method 'POST' -Body $body

                Write-Verbose "Created scoping criteria group ID: $($result.id)"

                if ($PassThru) {
                    $result | Add-Member -NotePropertyName 'SyncRuleId' -NotePropertyValue $SyncRuleId -PassThru -Force
                }
            }
            catch {
                Write-Error "Failed to create scoping criteria group: $_"
            }
        }
    }
}