public async Task <List <TransactionCreationResponse> > Handle(FxDealDocumentCreationCommand request, CancellationToken cancellationToken)
        {
            List <TransactionCreationResponse> fxDealDocumentCreationResponses = new List <TransactionCreationResponse>();

            foreach (var fxDeal in request.FxDealIds)
            {
                _unitOfWork.BeginTransaction();
                try
                {
                    TransactionCreationResponse dealCurrencyDocumentGenerationResponse = null;

                    IEnumerable <MasterData.Common.Entities.FxTradeType> fxTradeType = await _masterDataService.GetFxTradeTypes(request.Company);

                    var objectFxTradeType = fxDeal.FxTradeTypeId == null ? null :  fxTradeType.FirstOrDefault(x => x.FxTradeTypeId == fxDeal.FxTradeTypeId);

                    if (objectFxTradeType == null || !objectFxTradeType.IsNdf)
                    {
                        dealCurrencyDocumentGenerationResponse = await CreateFJDocumentDealCurrency(fxDeal.FxDealId, fxDeal.FxDealDocumentId, fxDeal.CurrencyCode, fxDeal.MaturityDate, request.IsReversal, request.Company);
                    }

                    var settlementCurrencyDocumentGenerationResponse = await CreateFJDocumentSettlementCurrency(fxDeal.FxDealId, fxDeal.FxSettlementDocumentId, fxDeal.SettlementCurrencyCode, fxDeal.MaturityDate, request.IsReversal, request.Company);

                    if (request.IsReversal == true)
                    {
                        await _fxDealRepository.DeleteFxDealAsync(fxDeal.FxDealId, request.Company);

                        _logger.LogWarning("Settled Fx deal with id {Atlas_FxDealId} deleted.", fxDeal.FxDealId);
                    }
                    else
                    {
                        await _fxDealRepository.UpdateSettleFxDeals(new List <long> {
                            fxDeal.FxDealId
                        }, request.Company);

                        _logger.LogWarning("Fx deal settled with id {Atlas_FxDealId}.", fxDeal.FxDealId);
                    }

                    _unitOfWork.Commit();

                    if (objectFxTradeType == null || !objectFxTradeType.IsNdf)
                    {
                        fxDealDocumentCreationResponses.Add(dealCurrencyDocumentGenerationResponse);
                    }

                    fxDealDocumentCreationResponses.Add(settlementCurrencyDocumentGenerationResponse);
                }
                catch
                {
                    _unitOfWork.Rollback();
                    throw;
                }
            }

            return(fxDealDocumentCreationResponses);
        }
Exemple #2
0
        public async Task <Unit> Handle(DeleteFxDealCommand request, CancellationToken cancellationToken)
        {
            _unitOfWork.BeginTransaction();
            try
            {
                // Load fx deal from DB
                FxDeal fxDeal = new FxDeal {
                    FxDealStatusId = (int)FxDealStatus.Open
                };

                if (fxDeal == null)
                {
                    throw new NotFoundException("fxDeal", request.FxDealId);
                }

                // Check fx deal status
                if (fxDeal.FxDealStatusId == (int)FxDealStatus.Open ||
                    (fxDeal.FxDealStatusId == (int)FxDealStatus.Settled && !fxDeal.Sections.Any()))
                {
                    await _fxDealRepository.DeleteFxDealAsync(request.FxDealId, request.Company);

                    if (fxDeal.FxDealStatusId == (int)FxDealStatus.Settled)
                    {
                        _logger.LogWarning("Settled Fx deal with id {Atlas_FxDealId} deleted.", request.FxDealId);
                    }
                    else
                    {
                        _logger.LogInformation("Fx deal with id {Atlas_FxDealId} deleted.", request.FxDealId);
                    }

                    _unitOfWork.Commit();
                }
                else
                {
                    throw new AtlasBusinessException("This Fx deal cannot be deleted.");
                }
            }
            catch
            {
                _unitOfWork.Rollback();
                throw;
            }

            return(Unit.Value);
        }