public virtual void HandleSupportItemEvent(SupportItemDto supportItemDto, TenantInfo tenantInfo)
        {
            // Add Cross-Tenant Permissions between Vendors and Customers based on the new SupportItem
            //     via LeaseItems for the relevant Asset
            //           Permissions are for Incidents, WorkOrders, Assets and Users

            List <string> entities = new List <string>()
            {
                "Incident", "WorkOrder", "Asset", "User"
            };

            SupportContract supportContract = _supportContractRepository.Get((int)supportItemDto.SupportContractId);
            Vendor          vendor          = _vendorRepository.Get((int)supportContract.VendorId);
            int             vendorTenantId  = (int)vendor.TenantId;

            List <int> leaseAgreementIds = _leaseItemRepository.GetAll().Where(s => s.AssetId == supportItemDto.AssetId).Select(s => (int)s.LeaseAgreementId).ToList();
            List <int> customerIds       = _leaseAgreementRepository.GetAll().Where(l => leaseAgreementIds.Contains(l.Id)).Select(l => (int)l.CustomerId).Distinct().ToList();
            List <int> customerTenantIds = _customerRepository.GetAll().Where(c => customerIds.Contains(c.Id)).Select(c => (int)c.TenantId).Distinct().ToList();

            bool clearCache = false;

            var allCtps = _crossTenantPermissionRepository.GetAllList();

            foreach (var entity in entities)
            {
                foreach (var customerTenantId in customerTenantIds)
                {
                    // Add the Customer Tenant Ids to the Vendors' CTPs

                    var existingVendorPermission = allCtps.Where(p => p.TenantId == vendorTenantId && p.EntityType == entity).FirstOrDefault();

                    if (existingVendorPermission != null)
                    {
                        var existingIds = existingVendorPermission.Tenants.Split(',').ToList().Select(int.Parse).ToList();
                        if (!existingIds.Contains(customerTenantId))
                        {
                            existingIds.Add(customerTenantId);

                            var ctp = _crossTenantPermissionRepository.GetAll().Where(p => p.TenantId == vendorTenantId && p.EntityType == entity).FirstOrDefault();
                            ctp.Tenants = string.Join(",", existingIds);
                            _crossTenantPermissionRepository.Update(ctp);
                            clearCache = true;
                        }
                    }
                    else
                    {
                        CrossTenantPermission ctp = new CrossTenantPermission()
                        {
                            EntityType  = entity,
                            TenantId    = vendorTenantId,
                            Tenants     = customerTenantId.ToString(),
                            TenantType  = "V",
                            TenantRefId = vendorTenantId
                        };

                        _crossTenantPermissionRepository.Insert(ctp);
                        clearCache = true;
                    }

                    // Add the Vendor Tenant Id to this Customer' CTPs

                    var existingCustomerPermission = allCtps.Where(p => p.TenantId == customerTenantId && p.EntityType == entity).FirstOrDefault();

                    if (existingCustomerPermission != null)
                    {
                        var existingIds = existingCustomerPermission.Tenants.Split(',').ToList().Select(int.Parse).ToList();
                        if (!existingIds.Contains(vendorTenantId))
                        {
                            existingIds.Add(vendorTenantId);

                            var ctp = _crossTenantPermissionRepository.GetAll().Where(p => p.TenantId == customerTenantId && p.EntityType == entity).FirstOrDefault();
                            ctp.Tenants = string.Join(",", existingIds);
                            _crossTenantPermissionRepository.Update(ctp);
                            clearCache = true;
                        }
                    }
                    else
                    {
                        CrossTenantPermission ctp = new CrossTenantPermission()
                        {
                            EntityType  = entity,
                            TenantId    = customerTenantId,
                            Tenants     = vendorTenantId.ToString(),
                            TenantType  = "C",
                            TenantRefId = customerTenantId
                        };

                        _crossTenantPermissionRepository.Insert(ctp);
                        clearCache = true;
                    }
                }
            }

            if (clearCache)
            {
                _cacheManager.GetCrossTenantPermissionCache().Clear();
            }
        }
        public virtual void HandleLeaseAgreementEvent(LeaseAgreementDto leaseAgreementDto, TenantInfo tenantInfo)
        {
            // Add Cross-Tenant Permissions between Asset Owners and Customer based on the new Lease Agreement
            //      Permissions are for Incidents, WorkOrders, Estimates, Assets and Users

            int assetOwnerTenantId = (int)_assetOwnerRepository.Get((int)leaseAgreementDto.AssetOwnerId).TenantId;
            int customerTenantId   = (int)_customerRepository.Get((int)leaseAgreementDto.CustomerId).TenantId;

            List <string> entities = new List <string>()
            {
                "Incident", "WorkOrder", "Estimate", "Asset", "User"
            };
            List <int> tenantIds = new List <int> {
                assetOwnerTenantId, customerTenantId
            };

            var allCtps = _crossTenantPermissionRepository.GetAllList();

            bool clearCache = false;

            foreach (var entity in entities)
            {
                foreach (var tenantId in tenantIds)
                {
                    int    otherTenantId = (tenantId == tenantIds.Last()) ? tenantIds.First() : tenantIds.Last();
                    string tenantType    = (tenantId == tenantIds.First()) ? "A" : "C";

                    var existingPermission = allCtps.Where(p => p.TenantId == tenantId && p.EntityType == entity).FirstOrDefault();

                    if (existingPermission != null)
                    {
                        var existingIds = existingPermission.Tenants.Split(',').ToList().Select(int.Parse).ToList();
                        if (!existingIds.Contains(otherTenantId))
                        {
                            existingIds.Add(otherTenantId);

                            var ctp = _crossTenantPermissionRepository.GetAll().Where(p => p.TenantId == tenantId && p.EntityType == entity).FirstOrDefault();
                            ctp.Tenants = string.Join(",", existingIds);
                            _crossTenantPermissionRepository.Update(ctp);
                            clearCache = true;
                        }
                    }
                    else
                    {
                        CrossTenantPermission ctp = new CrossTenantPermission()
                        {
                            EntityType  = entity,
                            TenantId    = tenantId,
                            Tenants     = otherTenantId.ToString(),
                            TenantType  = tenantType,
                            TenantRefId = tenantId
                        };

                        _crossTenantPermissionRepository.Insert(ctp);
                        clearCache = true;
                    }
                }
            }
            if (clearCache)
            {
                _cacheManager.GetCrossTenantPermissionCache().Clear();
            }
        }