Exemplo n.º 1
0
        public void Test_Purchase_Is_Created_With_Correct_Data()
        {
            _purchaseCreate.Discount   = 20;
            _purchaseCreate.GrandTotal = Convert.ToDecimal(90.4);
            _purchaseCreate.Total      = 100;


            _supplierRepo.Setup(a => a.GetById(It.IsAny <long>())).ReturnsAsync(_supplier);

            var PurchaseDetail = new List <PurchaseDetailCreateDTO>();

            _purchaseCreate.PurchaseDetails = new List <PurchaseDetailCreateDTO>()
            {
                new PurchaseDetailCreateDTO()
                {
                    PchId  = 1,
                    Qty    = 10,
                    Amount = 100,
                    Rate   = 10,
                }
            };
            _purchaseCreate.PurchaseDetails = PurchaseDetail;

            _purchaseService.Create(_purchaseCreate);

            _purchaseRepo.Verify(a => a.InsertAsync(It.Is <Purchase>(b =>

                                                                     b.Discount.Equals(_purchaseCreate.Discount) /*&&*/
                                                                                                                 //b.GrandTotal.Equals(_purchaseCreate.GrandTotal) &&
                                                                                                                 //b.Total.Equals(_purchaseCreate.Total) &&
                                                                                                                 //b.Vat.Equals(_purchaseCreate.Vat) &&
                                                                                                                 //b.Suppliers.Equals(_supplier) &&
                                                                                                                 //b.PurchaseDetails.First().Equals(_purchaseCreate.PurchaseDetails.First())


                                                                     )), Times.Once);
        }
Exemplo n.º 2
0
        public async Task <ActionResult <Purchase> > PurchaseCurrency(PurchaseDto purchaseDto)
        {
            try
            {
                double exchange            = 0;
                double totalAmountPurchase = 0;

                if (purchaseDto == null)
                {
                    _logger.LogError("Purchase object sent from client is null.");
                    return(BadRequest("Purchase object is null"));
                }

                if (!ModelState.IsValid)
                {
                    _logger.LogError("Invalid purchase object sent from client.");

                    return(BadRequest("Invalid model object"));
                }

                var purchase = new Purchase
                {
                    UserId   = purchaseDto.userId,
                    Amount   = double.Parse(purchaseDto.amount),
                    Currency = purchaseDto.currency.ToLower(),
                    Month    = DateTime.Today.Month,
                    Year     = DateTime.Today.Year
                };

                // valid amount
                if (purchase.Amount <= 0)
                {
                    _logger.LogError("Invalid purchase amount sent from client.");

                    return(NotFound(new NotFoundError("Invalid amount")));
                }

                // check if is a valid currency.
                if (purchase.Currency != "dolar" && purchase.Currency != "real")
                {
                    _logger.LogError("Invalid purchase currency sent from client.");

                    return(NotFound(new NotFoundError("The currency was not found")));
                }

                // get all purchase for this user in this month with this currency.
                var purchaseByMonthList = _purchaseService.GetByUserIdAndMonthAndCurrency(purchase);

                foreach (var purchaseByMonth in purchaseByMonthList)
                {
                    totalAmountPurchase = totalAmountPurchase + purchaseByMonth.Amount;
                }

                // check if exceeds the minimal amount.
                if ((totalAmountPurchase >= 200 && purchase.Currency == "dolar") || (totalAmountPurchase >= 300 && purchase.Currency == "real"))
                {
                    _logger.LogError("Invalid purchase because the user exceeds monthly purchase limit.");

                    return(NotFound(new NotFoundError("The user exceeds monthly purchase limit")));
                }

                var exchangeResponse = await _exchangeController.ExchangeRate(purchase.Currency);

                if (exchangeResponse is OkObjectResult exchangeRate && exchangeRate.Value != null)
                {
                    // get the exchange rate sold.
                    var sold = exchangeRate.Value.GetType().GetProperty("sold").GetValue(exchangeRate.Value).ToString();

                    exchange            = double.Parse(sold);
                    purchase.Amount     = purchase.Amount / exchange;
                    totalAmountPurchase = totalAmountPurchase + purchase.Amount;

                    if ((totalAmountPurchase >= 200 && purchase.Currency == "dolar") || (totalAmountPurchase >= 300 && purchase.Currency == "real"))
                    {
                        _logger.LogError("Invalid purchase because the user exceeds monthly purchase limit.");

                        return(NotFound(new NotFoundError("The user exceeds monthly purchase limit")));
                    }

                    _purchaseService.Create(purchase);

                    _logger.LogInformation($"The creation of a purchase was successfully.");

                    return(CreatedAtRoute("GetPurchase", new { id = purchase.Id }, purchase));
                }

                return(BadRequest(new InternalServerError("The external service don't response")));
            }
            catch (Exception ex)
            {
                _logger.LogError($"Faild Create purchase. Message: {ex.Message}");

                return(BadRequest(new InternalServerError()));
            }
        }