Functions/Get-SyncMSPCompleteCustomersScriptBlocks.ps1

<#
.SYNOPSIS
    This function returns the script blocks used to sync MSPComplete customers.
#>

function Get-SyncMSPCompleteCustomersScriptBlocks {
    # Return the script blocks
    return @{
        UnArchiveEntity = {
            # Retrieve the archived customer (if it exists)
            $archivedCustomer = Get-BT_Customer -Ticket $mspcObject.Ticket -CompanyName $entity.Expected.CompanyName `
                -WorkgroupId $mspcObject.Workgroup.Id -IsArchived $true -IsDeleted $false -Environment $environment

            # The archived customer doesn't exist
            if (!$archivedCustomer) {
                return
            }

            # If there are multiple archived customers, use the first one
            if ($archivedCustomer.length -gt 1) {
                $archivedCustomer = $archivedCustomer[0]
            }

            # Un-archive the customer
            $unArchivedCustomer = Set-BT_Customer -Ticket $mspcObject.Ticket -Customer $archivedCustomer `
                -IsArchived $false -Environment $environment
            if (!$unArchivedCustomer) {
                throw "Failed to un-archive previously archived customer."
            }
            "success"
        }
        CreateEntity = {
            # Create hash table for params
            $addBTCustomerParams = @{
                Ticket      = $mspcObject.Ticket
                Environment = $environment
            }

            # Add built-in properties to the params
            Get-MSPCompleteCustomerPropertyList | ForEach-Object -Process {
                if ($entity.Expected.$_) {
                    $addBTCustomerParams.Add($_, $entity.Expected.$_)
                }
            }

            # Create the customer
            $newCustomer = Add-BT_Customer @addBTCustomerParams
            if (!$newCustomer) {
                throw "Failed to create customer."
            }
            "success"
        }
        CompareEntities = {
            Compare-MSPCompleteCustomer -ReferenceCustomer $entity.Expected -ComparisonCustomer $entity.Current 2>$null
        }
        UpdateEntity = {
            # Create hash table for params
            $setBTCustomerParams = @{
                Ticket      = $mspcObject.Ticket
                Customer    = $entity.Current
                Environment = $environment
            }

            # Add built-in properties to the params
            Get-MSPCompleteCustomerPropertyList | ForEach-Object -Process {
                if ($entity.Expected.$_) {
                    $setBTCustomerParams.Add($_, $entity.Expected.$_)
                }
            }

            # Update the customer
            $updatedCustomer = Set-BT_Customer @setBTCustomerParams
            if (!$updatedCustomer) {
                throw "Failed to update customer."
            }
            "success"
        }
        ArchiveEntity = {
            # Archive the customer
            $archivedCustomer = Set-BT_Customer -Ticket $mspcObject.Ticket -Customer $entity.Current `
                -WorkgroupId $mspcObject.Workgroup.Id -IsArchived $true -Environment $environment
            if (!$archivedCustomer) {
                throw "Failed to archive customer."
            }
            "success"
        }
        DeleteEntity = {
            # Delete the customer
            Remove-BT_Customer -Ticket $mspcObject.Ticket -Id $entity.Current.Id `
                -Force -Environment $environment
            "success"
        }
    }
}