Public/Organizations/Get-UKGOrganizationGroup.ps1

function Get-UKGOrganizationGroup {
    <#
    .SYNOPSIS
        Gets organization group information from the UKG HR Service Delivery API.

    .DESCRIPTION
        Retrieves organization group data by ID or lists all organization groups.

    .PARAMETER Id
        The unique identifier of the organization group to retrieve.

    .PARAMETER All
        Retrieves all organization groups.

    .PARAMETER PerPage
        Number of results per page (1-100, default 25).

    .PARAMETER Cursor
        Pagination cursor for retrieving a specific page.

    .PARAMETER Fields
        Array of field names to include in the response.

    .EXAMPLE
        Get-UKGOrganizationGroup -Id "grp123"

    .EXAMPLE
        Get-UKGOrganizationGroup -All

    .OUTPUTS
        UKG.OrganizationGroup or array of UKG.OrganizationGroup objects.
    #>

    [CmdletBinding(DefaultParameterSetName = 'ById')]
    [OutputType([PSCustomObject])]
    param(
        [Parameter(Mandatory, ParameterSetName = 'ById', ValueFromPipelineByPropertyName)]
        [ValidateNotNullOrEmpty()]
        [Alias('GroupId')]
        [string]$Id,

        [Parameter(ParameterSetName = 'List')]
        [switch]$All,

        [Parameter(ParameterSetName = 'List')]
        [ValidateRange(1, 100)]
        [int]$PerPage = 25,

        [Parameter(ParameterSetName = 'List')]
        [string]$Cursor,

        [Parameter()]
        [string[]]$Fields
    )

    begin {
        $queryParams = @{}
        if ($Fields) {
            $queryParams['fields'] = $Fields -join ','
        }
    }

    process {
        switch ($PSCmdlet.ParameterSetName) {
            'ById' {
                $endpoint = "/organization_groups/$Id"
                $response = Invoke-UKGRequest -Endpoint $endpoint -Method GET -QueryParameters $queryParams
                if ($response) {
                    $response.PSObject.TypeNames.Insert(0, 'UKG.OrganizationGroup')
                }
                return $response
            }

            'List' {
                $queryParams['per_page'] = $PerPage
                if ($Cursor) {
                    $queryParams['cursor'] = $Cursor
                }

                if ($All -and -not $Cursor) {
                    $allGroups = Get-UKGAllPages -Endpoint '/organization_groups' -Method GET -QueryParameters $queryParams
                    foreach ($grp in $allGroups) {
                        $grp.PSObject.TypeNames.Insert(0, 'UKG.OrganizationGroup')
                    }
                    return $allGroups
                }
                else {
                    $response = Invoke-UKGRequest -Endpoint '/organization_groups' -Method GET -QueryParameters $queryParams -ReturnHeaders
                    if ($response.Data) {
                        foreach ($grp in $response.Data) {
                            $grp.PSObject.TypeNames.Insert(0, 'UKG.OrganizationGroup')
                        }

                        $pagination = Get-UKGNextPage -Headers $response.Headers
                        if ($pagination.NextCursor) {
                            Write-Verbose "Next cursor: $($pagination.NextCursor)"
                        }

                        return $response.Data
                    }
                }
            }
        }
    }
}

function Set-UKGOrganizationGroup {
    <#
    .SYNOPSIS
        Updates an existing organization group in the UKG HR Service Delivery system.

    .DESCRIPTION
        Updates organization group properties using either PUT or PATCH.

    .PARAMETER Id
        The unique identifier of the organization group to update.

    .PARAMETER Name
        The organization group name.

    .PARAMETER OrganizationIds
        Array of organization IDs in this group.

    .PARAMETER Properties
        Hashtable of properties to update.

    .PARAMETER InputObject
        A hashtable or PSCustomObject containing the properties to update.

    .PARAMETER Replace
        Use PUT method for full replacement instead of PATCH.

    .EXAMPLE
        Set-UKGOrganizationGroup -Id "grp123" -Name "Engineering Teams"

    .EXAMPLE
        Set-UKGOrganizationGroup -Id "grp123" -OrganizationIds @("org1", "org2", "org3")

    .OUTPUTS
        UKG.OrganizationGroup object representing the updated organization group.
    #>

    [CmdletBinding(SupportsShouldProcess, DefaultParameterSetName = 'Properties')]
    [OutputType([PSCustomObject])]
    param(
        [Parameter(Mandatory, ValueFromPipelineByPropertyName)]
        [ValidateNotNullOrEmpty()]
        [Alias('GroupId')]
        [string]$Id,

        [Parameter(ParameterSetName = 'Properties')]
        [string]$Name,

        [Parameter(ParameterSetName = 'Properties')]
        [string[]]$OrganizationIds,

        [Parameter(ParameterSetName = 'Properties')]
        [hashtable]$Properties,

        [Parameter(Mandatory, ParameterSetName = 'InputObject')]
        [object]$InputObject,

        [Parameter()]
        [switch]$Replace
    )

    process {
        $body = @{}

        if ($InputObject) {
            if ($InputObject -is [hashtable]) {
                $body = $InputObject.Clone()
            }
            else {
                foreach ($prop in $InputObject.PSObject.Properties) {
                    if ($prop.Name -notin @('id', 'created_at', 'updated_at', 'PSTypeName')) {
                        $body[$prop.Name] = $prop.Value
                    }
                }
            }
        }
        else {
            if ($Name) { $body['name'] = $Name }
            if ($OrganizationIds) { $body['organization_ids'] = $OrganizationIds }

            if ($Properties) {
                foreach ($key in $Properties.Keys) {
                    $body[$key] = $Properties[$key]
                }
            }
        }

        if ($body.Count -eq 0) {
            Write-Warning "No properties specified to update."
            return
        }

        $method = if ($Replace) { 'PUT' } else { 'PATCH' }

        if ($PSCmdlet.ShouldProcess($Id, "Update Organization Group ($method)")) {
            $response = Invoke-UKGRequest -Endpoint "/organization_groups/$Id" -Method $method -Body $body

            if ($response) {
                $response.PSObject.TypeNames.Insert(0, 'UKG.OrganizationGroup')
            }

            return $response
        }
    }
}