public void TransactionDomainEventHandler_VoucherGeneratedEvent_EventIsHandled()
        {
            VoucherGeneratedEvent voucherGeneratedEvent = TestData.VoucherGeneratedEvent;

            Mock <IEstateReportingRepository> estateReportingRepository = new Mock <IEstateReportingRepository>();

            TransactionDomainEventHandler eventHandler = new TransactionDomainEventHandler(estateReportingRepository.Object);

            Logger.Initialise(NullLogger.Instance);

            Should.NotThrow(async() => { await eventHandler.Handle(voucherGeneratedEvent, CancellationToken.None); });
        }
Exemplo n.º 2
0
 /// <summary>
 /// Plays the event.
 /// </summary>
 /// <param name="domainEvent">The domain event.</param>
 private void PlayEvent(VoucherGeneratedEvent domainEvent)
 {
     this.IsGenerated       = true;
     this.EstateId          = domainEvent.EstateId;
     this.GeneratedDateTime = domainEvent.GeneratedDateTime;
     this.ExpiryDate        = domainEvent.ExpiryDateTime;
     this.VoucherCode       = domainEvent.VoucherCode;
     this.Message           = domainEvent.Message;
     this.TransactionId     = domainEvent.TransactionId;
     this.Value             = domainEvent.Value;
     this.Balance           = 0;
 }
        public void VoucherGeneratedEvent_CanBeCreated_IsCreated()
        {
            VoucherGeneratedEvent voucherGeneratedEvent = new VoucherGeneratedEvent(TestData.VoucherId, TestData.EstateId,
                                                                                    TestData.TransactionId, TestData.IssuedDateTime,
                                                                                    TestData.OperatorIdentifier,
                                                                                    TestData.Value, TestData.VoucherCode, TestData.ExpiryDate,
                                                                                    TestData.Message);

            voucherGeneratedEvent.ShouldNotBeNull();
            voucherGeneratedEvent.AggregateId.ShouldBe(TestData.VoucherId);
            voucherGeneratedEvent.EventId.ShouldNotBe(Guid.Empty);
            voucherGeneratedEvent.VoucherId.ShouldBe(TestData.VoucherId);
            voucherGeneratedEvent.EstateId.ShouldBe(TestData.EstateId);
            voucherGeneratedEvent.OperatorIdentifier.ShouldBe(TestData.OperatorIdentifier);
            voucherGeneratedEvent.Value.ShouldBe(TestData.Value);
            voucherGeneratedEvent.VoucherCode.ShouldBe(TestData.VoucherCode);
            voucherGeneratedEvent.ExpiryDateTime.ShouldBe(TestData.ExpiryDate);
            voucherGeneratedEvent.Message.ShouldBe(TestData.Message);
            voucherGeneratedEvent.GeneratedDateTime.ShouldBe(TestData.IssuedDateTime);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Generates the specified operator identifier.
        /// </summary>
        /// <param name="operatorIdentifier">The operator identifier.</param>
        /// <param name="estateId">The estate identifier.</param>
        /// <param name="transactionId">The transaction identifier.</param>
        /// <param name="generatedDateTime">The generated date time.</param>
        /// <param name="value">The value.</param>
        public void Generate(String operatorIdentifier,
                             Guid estateId,
                             Guid transactionId,
                             DateTime generatedDateTime,
                             Decimal value)
        {
            Guard.ThrowIfInvalidDate(generatedDateTime, nameof(generatedDateTime));
            Guard.ThrowIfNullOrEmpty(operatorIdentifier, nameof(operatorIdentifier));
            Guard.ThrowIfInvalidGuid(transactionId, nameof(transactionId));
            Guard.ThrowIfInvalidGuid(estateId, nameof(estateId));
            Guard.ThrowIfNegative(value, nameof(value));
            Guard.ThrowIfZero(value, nameof(value));
            this.CheckIfVoucherAlreadyGenerated();

            // Do the generate process here...
            String   voucherCode    = this.GenerateVoucherCode();
            DateTime expiryDateTime = generatedDateTime.AddDays(30); // Default to a 30 day expiry for now...
            String   message        = string.Empty;

            VoucherGeneratedEvent voucherGeneratedEvent =
                new VoucherGeneratedEvent(this.AggregateId, estateId, transactionId, generatedDateTime, operatorIdentifier, value, voucherCode, expiryDateTime, message);

            this.ApplyAndAppend(voucherGeneratedEvent);
        }
Exemplo n.º 5
0
        public async Task <int?> CreateVoucher(VoucherRequest voucherRequest)
        {
            var numOfVouchersCreated = voucherRequest.NumbersOfVoucherToCreate;

            //let each voucher service handle its own creation
            try
            {
                voucherRequest.CreationDate = DateTime.Now;
                voucherRequest.Metadata     = Guid.NewGuid().ToString();


                if (voucherRequest.VoucherType.ToUpper() == "GIFT")
                {
                    Task.Run(() => _giftVoucherService.CreateGiftVoucher(voucherRequest));
                }

                else if (voucherRequest.VoucherType.ToUpper() == "DISCOUNT")
                {
                    Task.Run(() => _discountVoucherService.CreateDiscountVoucher(voucherRequest));
                }

                else
                {
                    Task.Run(() => _valueVoucherService.CreateValueVoucher(voucherRequest));
                }

                //TODO: Log the event (VoucherCreated)
                var voucherGeneratedEvent = new VoucherGeneratedEvent()
                {
                    EventId         = Guid.NewGuid(),
                    EventTime       = DateTime.Now,
                    MerchantId      = voucherRequest.MerchantId,
                    NumberGenerated = numOfVouchersCreated,
                    Message         = "New Vouchers created"
                };

                _logger.LogInformation("Created {Number}: vouchers for {Merchant} :{@Event}",
                                       numOfVouchersCreated, voucherRequest.MerchantId, voucherGeneratedEvent);

                return(numOfVouchersCreated);
            }

            catch (VoucherCreateException ex) //something happened handle it
                                              //if some error occurred and not all voucher could be created log the error
            {
                //Log the error
                var creationError = new VoucherGenerationFailedEvent()
                {
                    EventId          = Guid.NewGuid(),
                    EventTime        = DateTime.Now,
                    MerchantId       = voucherRequest.MerchantId,
                    FailureReason    = ex.Message,
                    Message          = "Failed to generate voucher",
                    VoucherType      = voucherRequest.VoucherType,
                    NumberToGenerate = voucherRequest.NumbersOfVoucherToCreate
                };
                _logger.LogError("Could not generate voucher: {@creationError}", creationError);
                _logger.LogDebug(ex, "An error occured while creating vouchers for {Merchant}", voucherRequest.MerchantId);
                return(null);
            }
        }
 /// <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(VoucherGeneratedEvent domainEvent,
                                              CancellationToken cancellationToken)
 {
     await this.EstateReportingRepository.AddGeneratedVoucher(domainEvent, cancellationToken);
 }
Exemplo n.º 7
0
        public async Task <int?> CreateVoucher(VoucherRequest voucherRequest)
        {
            var numOfVouchersCreated = 0;

            //let each voucher service handle its own creation
            try
            {
                voucherRequest.CreationDate = DateTime.Now;

                if (voucherRequest.VoucherType.ToUpper() == "GIFT")
                {
                    voucherRequest.CreationDate = DateTime.Now;
                    voucherRequest.Metadata     = Guid.NewGuid().ToString();

                    if (voucherRequest.VoucherType.ToUpper() == "GIFT")
                    {
                        numOfVouchersCreated += await _giftVoucherService.CreateGiftVoucher(voucherRequest);
                    }

                    else if (voucherRequest.VoucherType.ToUpper() == "DISCOUNT")
                    {
                        numOfVouchersCreated += await _discountVoucherService.CreateDiscountVoucher(voucherRequest);
                    }

                    else
                    {
                        numOfVouchersCreated += await _valueVoucherService.CreateValueVoucher(voucherRequest);
                    }

                    //TODO: Log the event (VoucherCreated)
                    var voucherGeneratedEvent = new VoucherGeneratedEvent()
                    {
                        EventId         = Guid.NewGuid(), EventTime = DateTime.Now, MerchantId = voucherRequest.MerchantId,
                        NumberGenerated = numOfVouchersCreated, Message = "New Vouchers created"
                    };

                    _logger.LogInformation("Created {Number}: vouchers for {Merchant} :{@Event}",
                                           numOfVouchersCreated, voucherRequest.MerchantId, voucherGeneratedEvent);

                    return(numOfVouchersCreated);
                }

                else if (voucherRequest.VoucherType.ToUpper() == "DISCOUNT")
                {
                    //TODO: Log the error event (VoucherGenerationFailed)
                    var voucherGeneratedEvent = new VoucherGeneratedEvent()
                    {
                        EventId         = Guid.NewGuid(), EventTime = DateTime.Now, MerchantId = voucherRequest.MerchantId,
                        NumberGenerated = numOfVouchersCreated, Message = "New Vouchers created"
                    };

                    _logger.LogInformation("Created {Number}: vouchers for {Merchant} :{@Event}",
                                           numOfVouchersCreated, voucherRequest.MerchantId, voucherGeneratedEvent);

                    //handle the error here; what should happen, try again or what
                    return(null);
                }

                else
                {
                    numOfVouchersCreated += await _valueVoucherService.CreateValueVoucher(voucherRequest);
                }

                //Log the event (Voucher Created)
                _logger.LogInformation("Created {Number}: vouchers for {Merchant}",
                                       numOfVouchersCreated, voucherRequest.MerchantId);

                return(numOfVouchersCreated);
            }
            catch (VoucherCreateException ex) //something happened handle it
                                              //if some error occurred and not all voucher could be created log the error
            {
                //Log the error
                _logger.LogError(ex, "An error occured while creating vouchers for {Merchant}", voucherRequest.MerchantId);
                return(null);
            }
        }