Samples/ImportCallhomeDetails/lxco/lxcoContacts.ps1

# ------------------------------------------------------------------
# Lenovo Copyright
#
# (C) Copyright Lenovo 2024 - present.
#
# LIMITED AND RESTRICTED RIGHTS NOTICE:
# If data or software is delivered pursuant a General Services
# Administration (GSA) contract, use, reproduction, or disclosure
# is subject to restrictions set forth in Contract No. GS-35F-05925.
# ------------------------------------------------------------------

Import-Module LXCOPSTool

. $PSScriptRoot\..\InFile\device.ps1
. $PSScriptRoot\..\InFile\countries.ps1

$serviceContactsBase = "/api/v1/service/contacts"

function GetLXCOServiceContacts()
{
    param (
        [Parameter(Mandatory = $true)] $Connection
    )

    $uri = "$($serviceContactsBase)?offset=0&limit=10000"
    $result = Invoke-LXCORestMethod -Connection $Connection -Method Get -ResourceUrl $uri
    return $result
}

function UpdateLXCOServiceContacts()
{
    param (
        [Parameter(Mandatory = $true)] $Connection,
        [Parameter(Mandatory = $true)] [DeviceAddress] $Data
    )
    # create the request body
    $json = ConvertTo-Json -InputObject $Data.GetContactInfo() -Depth 2 -Compress
    # execute
    $result = Invoke-LXCORestMethod -Connection $Connection -Method Post -ResourceUrl $serviceContactsBase -RequestBody $json
    return $result
}

function UpdateContacts()
{
    param (
        [Parameter(Mandatory = $true)] $Connection,
        [Parameter(Mandatory = $true)] [string[]]$RequiredAddresses,
        [Parameter(Mandatory = $true)] [hashtable] $Addresses
    )

    # get existing contacts data from LXCO
    $contacts = ConvertFrom-Json -InputObject $(GetLXCOServiceContacts -Connection $Connection).ResponseBody
    $jsonContent =  GetCountriesList
    $existsCount = 0
    # - generate address from existing contacts
    # - verify if address from LXCO exists in $Addresses list
    foreach ($contact in $contacts.results)
    {
        $addr = [DeviceAddress]::new()
        $addr.city = $contact.city
        $addr.streetAddress = $contact.streetAddress
        $addr.stateProvince = $contact.stateProvince
        $addr.country = $contact.country
        $addr.countryName = GetCountryName -Abreviation $contact.Country -CountiesList $jsonContent
        $addr.postalCode = $contact.postalCode
        $addr.fullName = $contact.fullName
        $addr.contactEmail = $contact.contactEmail
        $addr.contactPhone = $contact.contactPhone

        $hash = $addr.GetAddressHash()
        if ($null -ne $Addresses[$hash])
        {
            $Addresses[$hash].exists = $true
            $Addresses[$hash].id = $addr.id
            $existsCount ++
        }
    }

    # update LXCO' contacts.
    $counter = 0
    $total = $RequiredAddresses.Count
    foreach ($aHash in $RequiredAddresses)
    {
        $addr = $Addresses[$aHash]
        if ($false -eq $addr.exists)
        {
            $addr.companyName = $Global:CompanyName
            $addr.preferredContactMethod = $Global:preferredContactMethod
        }
        Write-Host ("Creating address {0} / {1} - {2} => {3}" -f $counter++, $total, $existsCount, $addr)
        $result = UpdateLXCOServiceContacts -Connection $Connection -Data $addr
        $addr.id = $($($result.LocationHeaderValue) -split "/") | Select-Object -Last 1
    }
}

# EOF