Public/Organizations/Get-UKGOrganization.ps1

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

    .DESCRIPTION
        Retrieves organization data by ID or lists all organizations with pagination support.

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

    .PARAMETER All
        Retrieves all organizations.

    .PARAMETER Count
        Returns only the count of organizations (HEAD request).

    .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-UKGOrganization -Id "org123"

    .EXAMPLE
        Get-UKGOrganization -All

    .EXAMPLE
        Get-UKGOrganization -Count

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

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

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

        [Parameter(Mandatory, ParameterSetName = 'Count')]
        [switch]$Count,

        [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 = "/organizations/$Id"
                $response = Invoke-UKGRequest -Endpoint $endpoint -Method GET -QueryParameters $queryParams
                if ($response) {
                    $response.PSObject.TypeNames.Insert(0, 'UKG.Organization')
                }
                return $response
            }

            'Count' {
                $headers = Invoke-UKGRequest -Endpoint '/organizations' -Method HEAD
                if ($headers.ContainsKey('X-Total-Count')) {
                    return [int]$headers['X-Total-Count']
                }
                return 0
            }

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

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

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

                        return $response.Data
                    }
                }
            }
        }
    }
}

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

    .DESCRIPTION
        Updates organization properties using either PUT (full replacement) or PATCH (partial update).

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

    .PARAMETER Name
        The organization name.

    .PARAMETER Code
        The organization code.

    .PARAMETER ParentId
        The ID of the parent organization.

    .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-UKGOrganization -Id "org123" -Name "Engineering Department"

    .EXAMPLE
        Set-UKGOrganization -Id "org123" -Properties @{ code = "ENG"; parent_id = "org001" }

    .OUTPUTS
        UKG.Organization object representing the updated organization.
    #>

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

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

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

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

        [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 ($Code) { $body['code'] = $Code }
            if ($ParentId) { $body['parent_id'] = $ParentId }

            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 ($method)")) {
            $response = Invoke-UKGRequest -Endpoint "/organizations/$Id" -Method $method -Body $body

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

            return $response
        }
    }
}