Exemplo n.º 1
0
        public async Task VoucherManagementManager_GetVoucherByCode_VoucherRetrieved()
        {
            EstateReportingGenericContext context = await this.GetContext(Guid.NewGuid().ToString("N"), TestDatabaseType.InMemory);

            await context.Vouchers.AddAsync(new Voucher
            {
                EstateId    = TestData.EstateId,
                VoucherId   = TestData.VoucherId,
                VoucherCode = TestData.VoucherCode
            });

            await context.SaveChangesAsync(CancellationToken.None);

            var dbContextFactory = this.GetMockDbContextFactory();

            dbContextFactory.Setup(d => d.GetContext(It.IsAny <Guid>(), It.IsAny <CancellationToken>())).ReturnsAsync(context);

            Mock <IAggregateRepository <VoucherAggregate, DomainEventRecord.DomainEvent> > voucherAggregateRepository = new Mock <IAggregateRepository <VoucherAggregate, DomainEventRecord.DomainEvent> >();

            voucherAggregateRepository.Setup(v => v.GetLatestVersion(It.IsAny <Guid>(), It.IsAny <CancellationToken>())).ReturnsAsync(TestData.GetVoucherAggregateWithRecipientMobile);

            VoucherManagementManager manager = new VoucherManagementManager(dbContextFactory.Object, voucherAggregateRepository.Object);

            Models.Voucher voucher = await manager.GetVoucherByCode(TestData.EstateId, TestData.VoucherCode, CancellationToken.None);

            voucher.ShouldNotBeNull();
        }
        /// <summary>
        /// Gets the voucher operator.
        /// </summary>
        /// <param name="voucherModel">The voucher model.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns></returns>
        private async Task <String> GetVoucherOperator(Voucher voucherModel,
                                                       CancellationToken cancellationToken)
        {
            EstateReportingGenericContext context = await this.DbContextFactory.GetContext(voucherModel.EstateId, cancellationToken);

            Transaction transaction = await context.Transactions.SingleOrDefaultAsync(t => t.TransactionId == voucherModel.TransactionId);

            Contract contract = await context.Contracts.SingleOrDefaultAsync(c => c.ContractId == transaction.ContractId);

            return(contract.Description);
        }
        /// <summary>
        /// Handles the specific domain event.
        /// </summary>
        /// <param name="domainEvent">The domain event.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        private async Task HandleSpecificDomainEvent(VoucherIssuedEvent domainEvent,
                                                     CancellationToken cancellationToken)
        {
            // Get the voucher aggregate
            VoucherAggregate voucherAggregate = await this.VoucherAggregateRepository.GetLatestVersion(domainEvent.AggregateId, cancellationToken);

            Voucher voucherModel = voucherAggregate.GetVoucher();

            this.TokenResponse = await this.GetToken(cancellationToken);

            if (string.IsNullOrEmpty(voucherModel.RecipientEmail) == false)
            {
                String message = await this.GetEmailVoucherMessage(voucherModel, cancellationToken);

                SendEmailRequest request = new SendEmailRequest
                {
                    Body = message,
                    ConnectionIdentifier = domainEvent.EstateId,
                    FromAddress          = "*****@*****.**",                   // TODO: lookup from config
                    IsHtml      = true,
                    MessageId   = domainEvent.EventId,
                    Subject     = "Voucher Issue",
                    ToAddresses = new List <String>
                    {
                        voucherModel.RecipientEmail
                    }
                };

                await this.MessagingServiceClient.SendEmail(this.TokenResponse.AccessToken, request, cancellationToken);
            }

            if (String.IsNullOrEmpty(voucherModel.RecipientMobile) == false)
            {
                String message = await this.GetSMSVoucherMessage(voucherModel, cancellationToken);

                SendSMSRequest request = new SendSMSRequest
                {
                    ConnectionIdentifier = domainEvent.EstateId,
                    Destination          = domainEvent.RecipientMobile,
                    Message   = message,
                    MessageId = domainEvent.EventId,
                    Sender    = "Your Voucher"
                };

                await this.MessagingServiceClient.SendSMS(this.TokenResponse.AccessToken, request, cancellationToken);
            }
        }
        /// <summary>
        /// Gets the SMS voucher message.
        /// </summary>
        /// <param name="voucherModel">The voucher model.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns></returns>
        private async Task <String> GetSMSVoucherMessage(Voucher voucherModel,
                                                         CancellationToken cancellationToken)
        {
            IDirectoryInfo path = this.FileSystem.Directory.GetParent(Assembly.GetExecutingAssembly().Location);

            String fileData = await this.FileSystem.File.ReadAllTextAsync($"{path}/VoucherMessages/VoucherSMS.txt", cancellationToken);

            PropertyInfo[] voucherProperties = voucherModel.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);

            // Do the replaces for the transaction
            foreach (PropertyInfo propertyInfo in voucherProperties)
            {
                fileData = fileData.Replace($"[{propertyInfo.Name}]", propertyInfo.GetValue(voucherModel)?.ToString());
            }

            String voucherOperator = await this.GetVoucherOperator(voucherModel, cancellationToken);

            fileData = fileData.Replace("[OperatorIdentifier]", voucherOperator);

            return(fileData);
        }