public async Task <bool> Handle(DeleteSecuredDealLinkCommand message, CancellationToken cancellationToken)
        {
            var longApplicationNumber         = long.Parse(message.ApplicationNumber);
            CollateralRequirement requirement = _arrangementRequestRepository.GetCollateralRequirementById(longApplicationNumber, message.ArrangementRequestId, message.CollateralRequirementId);
            var result = false;

            try
            {
                var remainingDeals = requirement.SecuredDealLinks.Where
                                         (x => !x.ArrangementRequestId.Equals(message.ArrangementRequestId) ||
                                         !x.ApplicationNumber.Equals(message.ApplicationNumber) ||
                                         !x.ArrangementNumber.Equals(message.ArrangementNumber)).ToList();
                _logger.LogDebug("Number of remaining secure deal links is {SecureDealLinkCount}", remainingDeals.Count());
                requirement.SecuredDealLinks = remainingDeals;
                _arrangementRequestRepository.UpdateCollateralRequirement(requirement);
                result = await _arrangementRequestRepository.UnitOfWork.SaveEntitiesAsync();

                var msgBuilder =
                    _messageEventFactory.CreateBuilder("offer", "secured-deal-link-changed")
                    .AddHeaderProperty("application-number", message.ApplicationNumber)
                    .AddHeaderProperty("username", "ALL");

                _bus.Publish(msgBuilder.Build());
            }
            catch (Exception e)
            {
                _logger.LogError(e, "An error occurred while removing deal link from collateral requirement.");
            }
            return(result);
        }
        public async Task <bool?> Handle(UpdateCollateralRequirementCommand message, CancellationToken cancellationToken)
        {
            var longApplicationNumber = long.Parse(message.ApplicationNumber);
            var collateralRequirement = AutoMapper.Mapper.Map <CollateralRequirement>(message);

            _arrangementRequestRepository.UpdateCollateralRequirement(collateralRequirement);
            return(await _arrangementRequestRepository.UnitOfWork.SaveEntitiesAsync());
        }
        public async Task <bool> Handle(AddSecuredDealLinkCommand message, CancellationToken cancellationToken)
        {
            var longApplicationNumber               = long.Parse(message.ApplicationNumber);
            CollateralRequirement  requirement      = _arrangementRequestRepository.GetCollateralRequirementById(longApplicationNumber, message.ArrangementRequestId, message.CollateralRequirementId);
            List <SecuredDealLink> securedDealLinks = requirement?.SecuredDealLinks ?? new List <SecuredDealLink>();
            ArrangementRequest     request          = _arrangementRequestRepository.GetArrangementRequest(longApplicationNumber, message.ArrangementRequestId);

            if (request is FinanceServiceArrangementRequest finRequest)
            {
                CurrencyConverter currencyConverter = new CurrencyConverter();
                var currencyConversionMethod        = _configurationService.GetEffective("offer/currency-conversion-method").Result;
                var pledgedValueInLoanCurrency      = currencyConverter.CurrencyConvert(message.PledgedValueInCollateralCurrency, message.ArrangementCurrency, finRequest.Currency, DateTime.Today.ToString("o", CultureInfo.InvariantCulture), currencyConversionMethod);

                var existingSecuredDealLink = securedDealLinks.Where
                                                  (x => x.ArrangementRequestId.Equals(message.ArrangementRequestId) &&
                                                  x.ApplicationNumber.Equals(message.ApplicationNumber) &&
                                                  x.ArrangementNumber.Equals(message.ArrangementNumber)).FirstOrDefault();

                if (existingSecuredDealLink != null)
                {
                    existingSecuredDealLink.PledgedValueInCollateralCurrency = message.PledgedValueInCollateralCurrency;
                    existingSecuredDealLink.PledgedValueInLoanCurrency       = pledgedValueInLoanCurrency;
                    requirement.SecuredDealLinks = securedDealLinks;
                }
                else
                {
                    securedDealLinks.Add(new SecuredDealLink
                    {
                        ApplicationNumber                = message.ApplicationNumber,
                        ArrangementNumber                = message.ArrangementNumber,
                        ArrangementRequestId             = message.ArrangementRequestId,
                        PledgedValueInCollateralCurrency = message.PledgedValueInCollateralCurrency,
                        PledgedValueInLoanCurrency       = pledgedValueInLoanCurrency
                    });
                    requirement.SecuredDealLinks = securedDealLinks;
                }
                _arrangementRequestRepository.UpdateCollateralRequirement(requirement);
                var result = await _arrangementRequestRepository.UnitOfWork.SaveEntitiesAsync();

                var msgBuilder =
                    _messageEventFactory.CreateBuilder("offer", "secured-deal-link-changed")
                    .AddHeaderProperty("application-number", message.ApplicationNumber)
                    .AddHeaderProperty("username", "ALL");

                _bus.Publish(msgBuilder.Build());
                return(result);
            }
            else
            {
                return(false);
            }
        }