NN.Zisson.psm1

#Region './Private/Get-ZiAccessToken.ps1' 0
function Get-ZiAccessToken {
    [CmdletBinding()]
    param (
        $AccessTokenPath = "$env:USERPROFILE\.creds\Zisson\app2\ZissonAccessToken.xml"
    )

    process {
        if (!(Test-Path -Path $AccessTokenPath)) {
            New-ZiAccessToken
        }

        $TimeTillTokenExpiry = (Import-Clixml -Path $AccessTokenPath).expiry_date - (Get-Date)
        
        #Check if there's less than 5 minutes till the current access token expires
        if (($TimeTillTokenExpiry.Minutes) -lt 5) {
            New-ZiAccessToken
        }

        (Import-Clixml -Path $AccessTokenPath).access_token | ConvertFrom-SecureString -AsPlainText
    }
}
#EndRegion './Private/Get-ZiAccessToken.ps1' 22
#Region './Private/Get-ZiLegacyAccessToken.ps1' 0
function Get-ZiLegacyAccessToken {
    [CmdletBinding()]
    param (
        [string]$AccessTokenPath = "$env:USERPROFILE\.creds\Zisson\app1\zissonAccessToken.xml"
    )

    process {
        if (!(Test-Path $AccessTokenPath)) {
            New-ZiLegacyAccessToken
        }
        
        Import-Clixml $AccessTokenPath | ConvertFrom-SecureString -AsPlainText
    }
}
#EndRegion './Private/Get-ZiLegacyAccessToken.ps1' 15
#Region './Private/Get-ZiLegacyEndpoint.ps1' 0
function Get-ZiLegacyEndpoint {
    [CmdletBinding()]
    param (
        [string]$EndpointPath = "$env:USERPROFILE\.creds\Zisson\app1\zissonEndpoint.xml"
    )

    process {
        if (!(Test-Path $EndpointPath)) {
            New-ZiLegacyEndpoint
        }

        Import-Clixml $EndpointPath
    }
}
#EndRegion './Private/Get-ZiLegacyEndpoint.ps1' 15
#Region './Public/Get-ZiGenericQueues.ps1' 0
function Get-ZiGenericQueues {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory)][string]$CustomerGuid, 
        [Parameter(ParameterSetName="List queues")][int]$QueueType,
        [Parameter(Mandatory,ParameterSetName="Get queue by id")][string]$QueueGuid
    )

    process {
        $Uri = "https://app2.zisson.com/web-api/v1/customers/$CustomerGuid/generic-queues"

        switch ($PsCmdlet.ParameterSetName) {
            "List queues" {
                #Parameters to exclude in Uri build
                $ParameterExclusion = @("CustomerGuid")

                #Build request Uri
                $PSBoundParameters.Keys.ForEach({
                    $Key = $_
                    $Value = $PSBoundParameters.$key
                    
                    #Check if parameter is excluded
                    if ($ParameterExclusion -contains $Key) {
                        return
                    }

                    #Check for "?" in Uri and set delimiter
                    if (!($Uri -replace "[^?]+")) {
                        $Delimiter = "?"
                    } else {
                        $Delimiter = "&"
                    }

                    $Uri = "$Uri$Delimiter$Key=$Value"
                })
            }
            "Get queue by id" {
                $Uri = "$Uri/$QueueGuid"
            }
        }

        $splat = @{
            "Method" = "GET"
            "Uri" = $Uri
            "Header" = @{
                "Authorization" = "Bearer $(Get-ZiAccessToken)"
                "Accept" = "application/json"
                "Content-Type" = "application/json"
            }
        }
        Invoke-RestMethod @splat
    }
}
#EndRegion './Public/Get-ZiGenericQueues.ps1' 54
#Region './Public/Get-ZiLegacyQueueCalls.ps1' 0
function Get-ZiLegacyQueueCalls {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory)][datetime]$StartDate,
        [Parameter(Mandatory)][datetime]$EndDate,
        [Parameter(Mandatory)][int]$QueueList
    )

    process {
        $Splat = @{
            "Method" = "GET"
            "Uri" = "$(Get-ZiLegacyEndpoint)/api/simple/QueueCallsAll"
            "Headers" = @{
                "Authorization" = "Basic $(Get-ZiLegacyAccessToken)"
            }
            "Body" = @{
                "start_date" = $StartDate
                "end_date"  = $EndDate
                "queue_list" = $QueueList
            }
        }
        $Result = Invoke-RestMethod @Splat
        $Result.QueueCallsAll
    }
}
#EndRegion './Public/Get-ZiLegacyQueueCalls.ps1' 26
#Region './Public/Get-ZiLegacyQueueChats.ps1' 0
function Get-ZiLegacyQueueChats {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory)][datetime]$StartDate,
        [Parameter(Mandatory)][datetime]$EndDate
    )

    process {
        $Splat = @{
            "Method" = "GET"
            "Uri" = "$(Get-ZiLegacyEndpoint)/api/simple/QueueChats"
            "Headers" = @{
                "Authorization" = "Basic $(Get-ZiLegacyAccessToken)"
            }
            "Body" = @{
                "start_date" = $StartDate
                "end_date"  = $EndDate
            }
        }
        $Result = Invoke-RestMethod @Splat
        $Result.QueueChats
    }
}
#EndRegion './Public/Get-ZiLegacyQueueChats.ps1' 24
#Region './Public/Get-ZiQueueLog.ps1' 0
function Get-ZiQueueLog {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory)][datetime]$FromDate,
        [Parameter(Mandatory)][datetime]$ToDate
    )

    process {
        $splat = @{
            "Method" = "POST"
            "Uri" = "https://app2.zisson.com/web-api/v1/queue-log/search"
            "Header" = @{
                "Authorization" = "Bearer $(Get-ZiAccessToken)"
                "Accept" = "application/json"
                "Content-Type" = "application/json"
            }
            "Body" = @{
                "fromDate" = $FromDate
                "toDate" = $ToDate
            } | ConvertTo-Json
        }
        Invoke-RestMethod @splat
    }
}
#EndRegion './Public/Get-ZiQueueLog.ps1' 25
#Region './Public/Get-ZiQueueStatus.ps1' 0
function Get-ZiQueueStatus {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory)][string]$QueueGuid
    )

    process {
        $splat = @{
            "Method" = "GET"
            "Uri" = "https://app2.zisson.com/external-api/v1/queuestatus/$QueueGuid"
            "Header" = @{
                "Authorization" = "Bearer $(Get-ZiAccessToken)"
                "Accept" = "application/json"
                "Content-Type" = "application/json"
            }
        }
        Invoke-RestMethod @splat
    }
}
#EndRegion './Public/Get-ZiQueueStatus.ps1' 20
#Region './Public/New-ZiAccessToken.ps1' 0
function New-ZiAccessToken {
    [CmdletBinding()]
    param (
        [int]$TokenTtl = "60",
        [string]$AccessTokenPath = "$env:USERPROFILE\.creds\Zisson\app2\ZissonAccessToken.xml",
        [string]$LatestJwtPath = "$env:USERPROFILE\.creds\Zisson\app2\ZissonLatestJwt.xml"
    )

    process {
        #Create folder to store credentials
        $AccessTokenDir = $AccessTokenPath.Substring(0, $AccessTokenPath.lastIndexOf('\'))
        if (!(Test-Path $AccessTokenDir)) {
            $null = New-Item -ItemType Directory $AccessTokenDir
        }

        #Create ApiKeyId file
        if (!(Test-Path $LatestJwtPath)) {
            while (!$LatestJwt) {
                $LatestJwt = Read-Host "Enter Zisson latest JWT object"
            }
            $LatestJwt | ConvertTo-SecureString -AsPlainText | Export-Clixml $LatestJwtPath
        }

        $LatestJwt = Import-Clixml $LatestJwtPath | ConvertFrom-SecureString -AsPlainText | ConvertFrom-Json

        #Request new accesstoken
        $splat = @{
            "Method" = "POST"
            "Uri" = "https://app2.zisson.com/web-api/v1/authenticate/refresh-token"
            "Header" = @{
                "Accept" = "application/json"
                "Content-Type" = "application/json"
            }
            "Body" = @{
                "id" = $LatestJwt.id
                "customerGuid" = $LatestJwt.customerGuid
                "refreshToken" = $LatestJwt.refreshToken
            } | ConvertTo-Json
        }
        $Result = Invoke-RestMethod @splat
        

        #Set up output
        @{
            "access_token" = $Result.jwt | ConvertTo-SecureString -AsPlainText
            "expiry_date" = (Get-Date).AddMinutes($TokenTtl)
        } | Export-Clixml -Path $AccessTokenPath
    }
}
#EndRegion './Public/New-ZiAccessToken.ps1' 50
#Region './Public/New-ZiLegacyAccessToken.ps1' 0
function New-ZiLegacyAccessToken {
    param (
        [string]$AccessTokenPath = "$env:USERPROFILE\.creds\Zisson\app1\zissonAccessToken.xml"
    )

    while (!$Username) {
        $Username = Read-Host "Enter the legacy Zisson API username"
    }

    while (!$Passwd) {
        $Passwd = Read-Host "Enter the legacy Zisson API password"
    }

    $AccessTokenDir = $AccessTokenPath.Substring(0, $AccessTokenPath.LastIndexOf('\'))
    if (!(Test-Path $AccessTokenDir)) {
        #Create parent folders of the access token file
        $null = New-Item -ItemType Directory $AccessTokenDir
    }

    #Encode credentials to Base64
    $Text = "$($Username):$($Passwd)"
    $Bytes = [Text.Encoding]::UTF8.GetBytes($Text)
    $AccessToken = [Convert]::ToBase64String($Bytes)

    #Create access token file
    $AccessToken | ConvertTo-SecureString -AsPlainText | Export-Clixml $AccessTokenPath
}
#EndRegion './Public/New-ZiLegacyAccessToken.ps1' 28
#Region './Public/New-ZiLegacyEndpoint.ps1' 0
function New-ZiLegacyEndpoint {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory,HelpMessage="Enter your Zisson domain suffix (no/com)")][ValidateSet("com","no")][string]$DomainSuffix,
        [string]$EndpointPath = "$env:USERPROFILE\.creds\Zisson\app1\zissonEndpoint.xml"
    )

    process {
        switch ($DomainSuffix) {
            no {
                $Endpoint = "https://api.zisson.no"
            }
            com {
                $Endpoint = "https://api.zisson.com"
            }
        }

        #Create parent folders of the access token file
        $EndpointDir = $EndpointPath.Substring(0, $EndpointPath.lastIndexOf('\'))
        if (!(Test-Path $EndpointDir)) {
            $null = New-Item -ItemType Directory $EndpointDir
        }

        $Endpoint | Export-Clixml $EndpointPath
    }
}
#EndRegion './Public/New-ZiLegacyEndpoint.ps1' 27