Get-MealTicket.ps1

<#
.Synopsis
   The function returns claim, purchased and transferred amount of mealtickets for a person.
.DESCRIPTION
   The function returns claim, purchased and transferred amount of mealtickets for a person.
.PARAMETER URL
    Server API url.
.PARAMETER Token
    Authentication token for accessing API data. If you use the token for authentication, don't enter access key.
.PARAMETER AccessKey
    The AccessKey to get an authentication token for accessing API data. If you use the access key to get authentication token, don't enter token.
.PARAMETER PersonCode
    Person's personal identification code.
.PARAMETER PersonID
    Person's database identification id.
.PARAMETER Date
    Date to which data is returned. If not specified, the current date will be used.
.PARAMETER Simplified
    With parameter function returns a simplified object without nesting in the response.
.EXAMPLE
   Get-MealTicket -URL https://intranet.company.com/webtime12/api -AccessKey 56879065 -PersonCode "1045"
   Command retrives claim, purchased and transferred amount of mealtickets for a person identified by PersonCode with authentication via AccessKey
.EXAMPLE
   Get-MealTicket -URL https://intranet.company.com/webtime12/api -Token $MyToken -PersonCode "1045" -Simplified
   Command retrives claim, purchased and transferred amount of mealtickets for a person identified by PersonCode with authentication via Token,
   and then simplifies the acquired object.
#>

function Get-MealTicket {
   [CmdletBinding(DefaultParameterSetName='AccessKey')]
   param(
        [Parameter(Mandatory = $true)]
        [string]$URL,
        [Parameter(Mandatory = $true,ParameterSetName='Token')]
        [string]$Token,
        [Parameter(Mandatory = $true,ParameterSetName='AccessKey')]
        [string]$AccessKey,
        [Parameter(Mandatory = $false)]
        [string]$PersonCode,
        [Parameter(Mandatory = $false)]
        [string]$PersonID,
        [Parameter(Mandatory = $false)]
        [string]$Date = $(Get-Date -Format yyyy-MM-ddTHH:mm:ss.msZ),
        [Parameter(Mandatory = $false)]
        [switch]$Simplified
    )

    Process {

        if ($PSCmdlet.ParameterSetName -eq 'AccessKey') {
            $SchemeToken = Get-Token -URL $URL -AccessKey $AccessKey
            $Token = $SchemeToken.Scheme + " " + $SchemeToken.Token
            }

        $url = $url + "/MealTickets"

        $Body = @{
            date = $date;
            personcode = $PersonCode;
            personid = $PersonId
        }

        $json = $body | ConvertTo-Json
        Write-Verbose -Message "Send request to API endpoint $URL with access key $Token."
        $mealTicket = Invoke-RestMethod -Method Post -Uri $url -Headers @{ Authorization = $Token } -Body $json -ContentType 'application/json'

        If ($($Simplified.IsPresent)) {
            $smealTicket = New-Object -TypeName System.Collections.ArrayList
            Write-Verbose -Message "Parameter Simplified is present - simplification of the returned object."
            Foreach ($ticket in $($mealTicket)) {
                $mealt = New-Object PSObject
                Add-Member -InputObject $mealt -MemberType NoteProperty -Name Firstname -Value $($ticket.Person.Firstname)
                Add-Member -InputObject $mealt -MemberType NoteProperty -Name Surname -Value $($ticket.Person.Surname)
                Add-Member -InputObject $mealt -MemberType NoteProperty -Name DegreeBefore -Value $($ticket.Person.DegreeBefore)
                Add-Member -InputObject $mealt -MemberType NoteProperty -Name DegreeAfter -Value $($ticket.Person.DegreeAfter)
                Add-Member -InputObject $mealt -MemberType NoteProperty -Name PersonId -Value $($ticket.Person.Id)
                Add-Member -InputObject $mealt -MemberType NoteProperty -Name PersonCode -Value $($ticket.Person.Code)
                Add-Member -InputObject $mealt -MemberType NoteProperty -Name Ordinal -Value $($ticket.Person.Ordinal)
                Add-Member -InputObject $mealt -MemberType NoteProperty -Name Purchased -Value $($ticket.Purchased)
                Add-Member -InputObject $mealt -MemberType NoteProperty -Name Transferred -Value $($ticket.Transferred)
                Add-Member -InputObject $mealt -MemberType NoteProperty -Name Claim -Value $($ticket.Claim)

                $mt.Add($mealt) | Out-Null
                }
            Write-Verbose -Message "Return simplified object Meal Ticket."
            Write-Output $smealTicket
            }
        else {
            Write-Verbose -Message "Return object Meal Ticket."
            Write-Output $mealTicket
        }
    }
}