Public/Documents/Get-UKGEmployeeDocument.ps1

function Get-UKGEmployeeDocument {
    <#
    .SYNOPSIS
        Gets employee documents from the UKG HR Service Delivery API.

    .DESCRIPTION
        Retrieves documents associated with employees.

    .PARAMETER EmployeeId
        The ID of the employee to get documents for.

    .PARAMETER DocumentId
        The ID of a specific document to retrieve.

    .PARAMETER DocumentTypeId
        Filter by document type ID.

    .PARAMETER All
        Retrieves all employee documents.

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

    .PARAMETER Cursor
        Pagination cursor for retrieving a specific page.

    .EXAMPLE
        Get-UKGEmployeeDocument -EmployeeId "emp123"

    .EXAMPLE
        Get-UKGEmployeeDocument -EmployeeId "emp123" -DocumentTypeId "dt001"

    .EXAMPLE
        Get-UKGEmployeeDocument -All

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

    [CmdletBinding(DefaultParameterSetName = 'ByEmployee')]
    [OutputType([PSCustomObject])]
    param(
        [Parameter(ParameterSetName = 'ByEmployee', ValueFromPipelineByPropertyName)]
        [Parameter(ParameterSetName = 'ByDocument')]
        [string]$EmployeeId,

        [Parameter(Mandatory, ParameterSetName = 'ByDocument')]
        [string]$DocumentId,

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

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

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

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

    process {
        $queryParams = @{}

        switch ($PSCmdlet.ParameterSetName) {
            'ByDocument' {
                $endpoint = "/employee_documents/$DocumentId"
                $response = Invoke-UKGRequest -Endpoint $endpoint -Method GET
                if ($response) {
                    $response.PSObject.TypeNames.Insert(0, 'UKG.EmployeeDocument')
                }
                return $response
            }

            'ByEmployee' {
                $queryParams['per_page'] = $PerPage
                if ($Cursor) { $queryParams['cursor'] = $Cursor }
                if ($EmployeeId) { $queryParams['employee_id'] = $EmployeeId }
                if ($DocumentTypeId) { $queryParams['document_type_id'] = $DocumentTypeId }

                $response = Invoke-UKGRequest -Endpoint '/employee_documents' -Method GET -QueryParameters $queryParams -ReturnHeaders
                if ($response.Data) {
                    foreach ($doc in $response.Data) {
                        $doc.PSObject.TypeNames.Insert(0, 'UKG.EmployeeDocument')
                    }

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

                    return $response.Data
                }
            }

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

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

function New-UKGEmployeeDocument {
    <#
    .SYNOPSIS
        Creates a new employee document record in the UKG HR Service Delivery system.

    .DESCRIPTION
        Creates a document record associated with an employee. Use Send-UKGDocument to upload files.

    .PARAMETER EmployeeId
        The ID of the employee to associate the document with.

    .PARAMETER DocumentTypeId
        The ID of the document type.

    .PARAMETER Title
        Title for the document.

    .PARAMETER Description
        Description of the document.

    .PARAMETER Properties
        Hashtable of additional properties.

    .PARAMETER InputObject
        A hashtable or PSCustomObject containing the document data.

    .EXAMPLE
        New-UKGEmployeeDocument -EmployeeId "emp123" -DocumentTypeId "dt001" -Title "Employment Contract"

    .OUTPUTS
        UKG.EmployeeDocument object representing the created document.
    #>

    [CmdletBinding(SupportsShouldProcess, DefaultParameterSetName = 'Properties')]
    [OutputType([PSCustomObject])]
    param(
        [Parameter(Mandatory, ParameterSetName = 'Properties')]
        [ValidateNotNullOrEmpty()]
        [string]$EmployeeId,

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

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

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

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

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

    process {
        $body = @{}

        if ($PSCmdlet.ParameterSetName -eq 'InputObject') {
            if ($InputObject -is [hashtable]) {
                $body = $InputObject.Clone()
            }
            else {
                foreach ($prop in $InputObject.PSObject.Properties) {
                    $body[$prop.Name] = $prop.Value
                }
            }
        }
        else {
            $body['employee_id'] = $EmployeeId
            if ($DocumentTypeId) { $body['document_type_id'] = $DocumentTypeId }
            if ($Title) { $body['title'] = $Title }
            if ($Description) { $body['description'] = $Description }

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

        $displayName = if ($body['title']) { $body['title'] } else { "Document for $EmployeeId" }

        if ($PSCmdlet.ShouldProcess($displayName, 'Create Employee Document')) {
            $response = Invoke-UKGRequest -Endpoint '/employee_documents' -Method POST -Body $body

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

            return $response
        }
    }
}