Public/Get-AccountDependencies.ps1
|
function Get-AccountDependencies { <# .SYNOPSIS Gets all entities that reference a specific account. .DESCRIPTION Checks all billers, earnings, and transfers for references to the specified account. Returns a summary of dependencies to help with account deletion decisions and data integrity validation. .PARAMETER Id The ID of the account to check for dependencies. .PARAMETER Budget Optional budget name to target. Uses active budget if not specified. .PARAMETER DataPath Optional custom path for data storage. Overrides budget-based paths. .EXAMPLE Get-AccountDependencies -Id "acc-123" Shows all entities referencing account acc-123 .EXAMPLE $deps = Get-AccountDependencies -Id "acc-123" if ($deps.TotalCount -gt 0) { Write-Warning "Account has $($deps.TotalCount) dependencies" } .OUTPUTS PSCustomObject - Dependency information with Billers, Earnings, Transfers arrays and counts #> [CmdletBinding()] param( [Parameter(Mandatory, ValueFromPipelineByPropertyName)] [string]$Id, [Parameter()] [string]$Budget, [Parameter()] [string]$DataPath ) process { $resolvedPath = Resolve-DataPath -DataPath $DataPath -Budget $Budget if (-not $resolvedPath) { return } # Verify account exists $accounts = Read-EntityData -EntityType 'Account' -DataPath $resolvedPath $account = $accounts | Where-Object { $_.Id -eq $Id } if (-not $account) { Write-Error "Account with ID '$Id' not found." return } # Check billers $billers = Read-EntityData -EntityType 'Biller' -DataPath $resolvedPath $dependentBillers = @($billers | Where-Object { $_.AccountId -eq $Id }) # Check earnings $earnings = Read-EntityData -EntityType 'Earning' -DataPath $resolvedPath $dependentEarnings = @($earnings | Where-Object { $_.AccountId -eq $Id }) # Check transfers (both FromAccountId and ToAccountId) $transfers = Read-EntityData -EntityType 'Transfer' -DataPath $resolvedPath $dependentTransfers = @($transfers | Where-Object { $_.FromAccountId -eq $Id -or $_.ToAccountId -eq $Id }) # Separate transfers by direction $transfersFrom = @($transfers | Where-Object { $_.FromAccountId -eq $Id }) $transfersTo = @($transfers | Where-Object { $_.ToAccountId -eq $Id }) # Calculate total $totalCount = $dependentBillers.Count + $dependentEarnings.Count + $dependentTransfers.Count # Build result object $result = [PSCustomObject]@{ AccountId = $Id AccountName = $account.Name Billers = $dependentBillers BillerCount = $dependentBillers.Count Earnings = $dependentEarnings EarningCount = $dependentEarnings.Count Transfers = $dependentTransfers TransferCount = $dependentTransfers.Count TransfersFrom = $transfersFrom TransfersFromCount = $transfersFrom.Count TransfersTo = $transfersTo TransfersToCount = $transfersTo.Count TotalCount = $totalCount HasDependencies = ($totalCount -gt 0) } return $result } } |