private async Task <ProcessESBResult> ValidateESBResponse(ProcessESBResponse esbRequest)
        {
            var docStatus = await _accountingInterfaceRepository.GetDocumentStatus(Convert.ToInt32(BusinessApplicationType.AX, CultureInfo.InvariantCulture), esbRequest.CompanyId, esbRequest.DocumentReference);

            if (docStatus != InterfaceStatus.None)
            {
                if (docStatus == InterfaceStatus.Completed)
                {
                    string message = "The document " + esbRequest.DocumentReference + " is in status 'Booked'";
                    processESBResult.Error     = message;
                    processESBResult.IsSuccess = false;
                }
                else
                {
                    processESBResult.IsSuccess = true;
                }
            }
            else
            {
                string message = "Invalid Document Reference";
                processESBResult.Error     = message;
                processESBResult.IsSuccess = false;
            }

            return(processESBResult);
        }
        public async Task <ProcessESBResult> Handle(ProcessESBResponse esbRequest, CancellationToken cancellationToken)
        {
            CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
            TextInfo    textInfo    = cultureInfo.TextInfo;
            var         companyDate = await _systemDateTimeService.GetCompanyDate(esbRequest.CompanyId);

            long sourceId = await _interfaceEventLogService.GetAccountingIdandCashIdbyDocumentReference(esbRequest.CompanyId, esbRequest.DocumentReference, (int)BusinessApplicationType.AX, (int)esbRequest.BusinessObjectType);

            var eventDto    = new EventDto(sourceId, esbRequest.DocumentReference, (int)esbRequest.BusinessObjectType, esbRequest.CompanyId);
            var eventStatus = await _interfaceEventLogService.FindEventAsync(eventDto);

            if (eventStatus != null)
            {
                var eventHistoryData = new EventHistory(eventStatus.EventId, "Callback message received from ESB", esbRequest.ESBMessage, null, null);
                await _interfaceEventLogService.CreateEventHistoryAsync(eventHistoryData, esbRequest.CompanyId);

                _unitOfWork.BeginTransaction();
                try
                {
                    var responseToESB = await ValidateESBResponse(esbRequest);

                    if (responseToESB.IsSuccess)
                    {
                        var processInterfaceData = new ProcessInterfaceDataChangeLogsRequest();
                        processInterfaceData.DocumentReference       = esbRequest.DocumentReference;
                        processInterfaceData.CompanyId               = esbRequest.CompanyId;
                        processInterfaceData.BusinessApplicationType = BusinessApplicationType.AX;
                        processInterfaceData.ESBMessage              = esbRequest.ESBMessage;
                        processInterfaceData.AcknowledgementId       = esbRequest.AckBusinessDocId;
                        processInterfaceData.TransactionDate         = esbRequest.TransactionDate.GetValueOrDefault() == default(DateTime) ? null : esbRequest.TransactionDate;
                        processInterfaceData.TimeStamp               = esbRequest.TimeStamp;
                        processInterfaceData.JournalNumber           = esbRequest.JournalNumber;
                        InterfaceStatus status;
                        if (Enum.TryParse(textInfo.ToTitleCase(esbRequest.ResponseStatus.ToLowerInvariant()), out status))
                        {
                            switch (status)
                            {
                            case InterfaceStatus.Completed:
                                await ProcessEventUpdate(eventStatus.EventId, InterfaceStatus.Completed, "ESB callback Message integration", esbRequest.ESBMessage, ResultCode.Ok.ToString(), null, esbRequest.CompanyId);
                                await ProcessStatusUpdate(processInterfaceData, InterfaceStatus.Completed, esbRequest.ResponseMessage);

                                break;

                            case InterfaceStatus.Error:
                                await ProcessEventUpdate(eventStatus.EventId, InterfaceStatus.Error, "ESB callback Message integration", esbRequest.ESBMessage, ResultCode.Error.ToString(), esbRequest.ResponseMessage, esbRequest.CompanyId);
                                await ProcessStatusUpdate(processInterfaceData, InterfaceStatus.Error, esbRequest.ResponseMessage);

                                break;

                            case InterfaceStatus.Rejected:
                                await ProcessEventUpdate(eventStatus.EventId, InterfaceStatus.Rejected, "ESB callback Message integration", esbRequest.ESBMessage, ResultCode.Error.ToString(), esbRequest.ResponseMessage, esbRequest.CompanyId);
                                await ProcessStatusUpdate(processInterfaceData, InterfaceStatus.Rejected, esbRequest.ResponseMessage);

                                break;
                            }
                        }
                    }

                    _unitOfWork.Commit();
                    _logger.LogInformation("Document with DocumentId {Atlas_AccountingDocumentReference} received status {Atlas_AccountingStatus} from Accounting System at {Atlas_DateTime}", esbRequest.DocumentReference, esbRequest.ResponseStatus, companyDate);
                    return(responseToESB);
                }
                catch
                {
                    _unitOfWork.Rollback();
                    throw;
                }
            }
            else
            {
                processESBResult.Error     = "Invalid Document Reference";
                processESBResult.IsSuccess = false;
                return(processESBResult);
            }
        }