Example #1
0
        public async Task When_CurrencyConvertorClientReturnsValidResponse_Should_CallInsertOrUpdateCustomerPurchaseAmount()
        {
            //Arrange
            var purchaseModel = new VoucherPurchaseModel();

            var customerProfileServiceMock = new Mock <ICustomerProfileService>(MockBehavior.Strict);

            customerProfileServiceMock
            .Setup(x => x.InsertOrUpdateCustomerPurchaseAmount(It.IsAny <Guid>(), It.IsAny <decimal>()))
            .Returns(Task.CompletedTask);

            var currencyConvertorClientMock = new Mock <ICurrencyConvertorService>(MockBehavior.Strict);

            currencyConvertorClientMock.Setup(x => x.CovertToBaseCurrencyAsync(It.IsAny <decimal>(), It.IsAny <string>()))
            .ReturnsAsync((true, 1));

            var service = new VoucherOperationsService(customerProfileServiceMock.Object, currencyConvertorClientMock.Object, EmptyLogFactory.Instance);

            //Act
            await service.ProcessVoucherPurchaseEvent(purchaseModel);

            //Assert
            customerProfileServiceMock.Verify(
                x => x.InsertOrUpdateCustomerPurchaseAmount(It.IsAny <Guid>(), It.IsAny <decimal>()),
                Times.Once);
        }
        public async Task <(bool isSuccessful, string errorMessage)> ProcessVoucherPurchaseEvent(VoucherPurchaseModel voucherPurchaseModel)
        {
            var(isValidConversion, conversionAmount) = await _currencyConvertorService
                                                       .CovertToBaseCurrencyAsync(voucherPurchaseModel.Amount, voucherPurchaseModel.Currency);

            if (!isValidConversion)
            {
                return(false, $"'{voucherPurchaseModel.Currency}' cannot be converted to base currency.");
            }

            await _customerProfileService.InsertOrUpdateCustomerPurchaseAmount(voucherPurchaseModel.CustomerId, conversionAmount);

            return(true, string.Empty);
        }