Пример #1
0
        public async Task <IActionResult> GetSumToPayInvoices([FromBody] GetSumToPayInvoicesRequest model)
        {
            var validationResult = await ValidateForPayingInvoicesAsync(model);

            if (validationResult.HasError)
            {
                return(validationResult.ActionResult);
            }

            decimal sum;

            try
            {
                sum = await _invoiceService.GetSumInAssetForPayAsync(validationResult.Employee.MerchantId, validationResult.Invoices, validationResult.AssetForPay);
            }
            catch (InvalidOperationException ex)
            {
                _log.ErrorWithDetails(ex, model);

                return(BadRequest(ErrorResponse.Create(ex.Message)));
            }

            return(Ok(sum));
        }
 public Task <decimal> GetSumToPayInvoicesAsync(GetSumToPayInvoicesRequest model)
 {
     return(_runner.RunAsync(() => _invoicesApi.GetSumToPayInvoicesAsync(model)));
 }
Пример #3
0
        private async Task <(IReadOnlyList <Invoice> Invoices, string AssetForPay, Employee Employee, bool HasError, IActionResult ActionResult)> ValidateForPayingInvoicesAsync(GetSumToPayInvoicesRequest model)
        {
            IReadOnlyList <Invoice> invoices = null;
            string        assetForPay        = null;
            Employee      employee           = null;
            bool          hasError           = false;
            IActionResult actionResult       = null;

            try
            {
                employee = await _employeeService.GetByIdAsync(model.EmployeeId);

                // choose the asset for pay
                if (string.IsNullOrEmpty(model.AssetForPay))
                {
                    string baseAssetId = await _merchantSettingService.GetBaseAssetAsync(employee.MerchantId);

                    if (string.IsNullOrEmpty(baseAssetId))
                    {
                        throw new InvalidOperationException("BaseAsset for merchant is not found");
                    }

                    assetForPay = baseAssetId;
                }
                else
                {
                    assetForPay = model.AssetForPay;
                }

                invoices = await _invoiceService.ValidateForPayingInvoicesAsync(employee.MerchantId, model.InvoicesIds, model.AssetForPay);
            }
            catch (Exception ex)
            {
                hasError = true;
                _log.ErrorWithDetails(ex, model);

                switch (ex)
                {
                case EmployeeNotFoundException _:
                case MerchantNotFoundException _:
                case MerchantGroupNotFoundException _:
                case InvoiceNotFoundException _:
                    actionResult = NotFound(ErrorResponse.Create(ex.Message));
                    break;

                case InvoiceNotInsideGroupException _:
                case MerchantNotInvoiceClientException _:
                case AssetNotAvailableForMerchantException _:
                case InvalidOperationException _:
                    actionResult = BadRequest(ErrorResponse.Create(ex.Message));
                    break;

                default:
                    throw;
                }
            }

            return(invoices, assetForPay, employee, hasError, actionResult);
        }