Public/MatchingRules/New-JIMSyncRuleMatchingRule.ps1

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

function New-JIMSyncRuleMatchingRule {
    <#
    .SYNOPSIS
        Creates a new Object Matching Rule on a Synchronisation Rule (advanced mode).
 
    .DESCRIPTION
        Creates a new Object Matching Rule on a specific Synchronisation Rule.
        This is used in advanced mode where matching rules are per-Synchronisation Rule
        rather than per-object type. The Metaverse Object Type is derived from
        the Synchronisation Rule automatically.
 
    .PARAMETER SyncRuleId
        The unique identifier of the Synchronisation Rule.
 
    .PARAMETER SourceAttributeId
        The Connected System attribute ID to use as the source for matching. Matched against
        -TargetMetaverseAttributeId, for both import and export matching.
 
    .PARAMETER TargetMetaverseAttributeId
        The Metaverse attribute ID to match against.
 
    .PARAMETER Order
        The evaluation order for this rule (lower values are evaluated first).
        If not specified, the rule will be added at the end.
 
    .PARAMETER CaseSensitive
        Whether the matching should be case-sensitive.
        When false (default), 'emp123' matches 'EMP123'.
        When true, 'emp123' does NOT match 'EMP123'.
 
    .PARAMETER PassThru
        If specified, returns the created Matching Rule object.
 
    .OUTPUTS
        If -PassThru is specified, returns the created Matching Rule object.
 
    .EXAMPLE
        New-JIMSyncRuleMatchingRule -SyncRuleId 5 -SourceAttributeId 25 -TargetMetaverseAttributeId 5
 
        Creates a matching rule on Synchronisation Rule 5 that maps CS attribute 25 to MV attribute 5.
 
    .LINK
        Get-JIMSyncRuleMatchingRule
        Set-JIMSyncRuleMatchingRule
        Remove-JIMSyncRuleMatchingRule
    #>

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

        [Parameter(Mandatory)]
        [int]$SourceAttributeId,

        [Parameter(Mandatory)]
        [int]$TargetMetaverseAttributeId,

        [Parameter()]
        [int]$Order,

        [Parameter()]
        [bool]$CaseSensitive,

        [switch]$PassThru
    )

    process {
        # Check connection first
        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
        }

        $source = @{
            order = 0
            connectedSystemAttributeId = $SourceAttributeId
        }

        $body = @{
            targetMetaverseAttributeId = $TargetMetaverseAttributeId
            sources = @($source)
        }

        if ($PSBoundParameters.ContainsKey('Order')) {
            $body.order = $Order
        }

        if ($PSBoundParameters.ContainsKey('CaseSensitive')) {
            $body.caseSensitive = $CaseSensitive
        }

        if ($PSCmdlet.ShouldProcess("Synchronisation Rule $SyncRuleId", "Create Matching Rule")) {
            Write-Verbose "Creating Matching Rule for Synchronisation Rule ID: $SyncRuleId"

            try {
                $result = Invoke-JIMApi -Endpoint "/api/v1/synchronisation/sync-rules/$SyncRuleId/matching-rules" -Method 'POST' -Body $body

                Write-Verbose "Created Matching Rule ID: $($result.id)"

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