예제 #1
0
        public async Task <Voucher> UpdateGiftVoucherAmount(string code, long amount)
        {
            try
            {
                string encryptedCode = CodeGenerator.Encrypt(code);
                var    voucher       = await GetVoucherByCode(encryptedCode);

                Gift giftVoucher = await _giftVoucherService.GetGiftVoucher(voucher); //returning a gift voucher

                var previousAmount = giftVoucher.GiftAmount;

                giftVoucher.GiftAmount  += amount;
                giftVoucher.GiftBalance += amount;


                //log the event
                var updatedEvent = new VoucherUpdatedEvent()
                {
                    EventId         = Guid.NewGuid(),
                    EventTime       = DateTime.Now,
                    MerchantId      = voucher.MerchantId,
                    Message         = "Update performed on voucher",
                    VoucherCode     = voucher.Code,
                    VoucherType     = voucher.VoucherType,
                    PropertyUpdated = new PropertyUpdated()
                    {
                        PropertyName  = "GiftAmount",
                        PreviousValue = previousAmount,
                        NewValue      = giftVoucher.GiftAmount
                    }
                };

                _logger.LogInformation("Updated a voucher: {@UpdateEvent}", updatedEvent);
                await _giftVoucherService.UpdateGiftVoucher(giftVoucher); //persist the change

                string decryptedCode = CodeGenerator.Decrypt(voucher.Code);
                voucher.Code = decryptedCode;

                return(voucher);
            }

            catch (VoucherUpdateException ex)
            {
                var updatedFailedEvent = new VoucherUpdateFailedEvent()
                {
                    EventId          = Guid.NewGuid(), EventTime = DateTime.Now, VoucherType = VoucherType.GIFT.ToString(),
                    Message          = "Update operation failed for voucher", VoucherCode = code, FailureReason = ex.Message,
                    PropertyToUpdate = new PropertyUpdated()
                    {
                        PropertyName = "GiftBalance", NewValue = amount
                    }
                };

                _logger.LogError("Error Updating a gift: {@UpdateFailedEvent}", updatedFailedEvent);

                _logger.LogDebug(ex, "Could not perform update operation on voucher with {Code}", code);
                return(null);
            }
        }
예제 #2
0
        public async Task <long?> ActivateOrDeactivateVoucher(string code)
        {
            try
            {
                //get the voucher that is to be updated
                string encryptedCode = CodeGenerator.Encrypt(code);
                var    voucher       = await GetVoucherByCode(encryptedCode);

                voucher.VoucherStatus = voucher.VoucherStatus.ToUpper() == "ACTIVE" ? "INACTIVE" : "ACTIVE";


                long recordsAffected = await _baseRepository.UpdateVoucherStatusByCodeAsync(voucher);

                //log the event
                if (voucher.VoucherStatus.ToUpper() == "ACTIVE")
                {
                    var updatedEvent = new VoucherDeactivatedEvent()
                    {
                        EventId     = Guid.NewGuid(), EventTime = DateTime.Now, MerchantId = voucher.MerchantId,
                        Message     = "Voucher was deactivated", VoucherCode = voucher.Code,
                        VoucherType = voucher.VoucherType
                    };
                    _logger.LogInformation("Deactivated a voucher: {@Event}", updatedEvent);
                }

                if (voucher.VoucherStatus.ToUpper() == "INACTIVE")
                {
                    var updatedEvent = new VoucherReactivatedEvent()
                    {
                        EventId     = Guid.NewGuid(), EventTime = DateTime.Now, MerchantId = voucher.MerchantId,
                        Message     = "Voucher was Activated", VoucherCode = voucher.Code,
                        VoucherType = voucher.VoucherType
                    };
                    _logger.LogInformation("Activated a voucher: {@Event}", updatedEvent);
                }
                ;

                return(recordsAffected);
            }

            catch (Exception ex)
            {
                //log the event

                var updatedFailedEvent = new VoucherUpdateFailedEvent()
                {
                    EventId       = Guid.NewGuid(), EventTime = DateTime.Now,
                    Message       = "Could not perform update on voucher", VoucherCode = code,
                    FailureReason = ex.Message
                };

                _logger.LogInformation("Updated a voucher: {@Event}", updatedFailedEvent);


                _logger.LogError(ex, "Could not perform activate or deactivate operation on voucher with {Code}", code);
            }
            return(null);
        }
예제 #3
0
        public async Task <long?> UpdateVoucherExpiryDate(string code, DateTime newDate)
        {
            try
            {
                //get the voucher that is to be updated
                string encryptedCode = CodeGenerator.Encrypt(code);
                var    voucher       = await GetVoucherByCode(encryptedCode);

                var oldDate = voucher.ExpiryDate;
                voucher.ExpiryDate = newDate;
                var recordsAffected = await _baseRepository.UpdateVoucherExpiryDateByCodeAsync(voucher);

                var updatedEvent = new VoucherUpdatedEvent()
                {
                    EventId     = Guid.NewGuid(), EventTime = DateTime.Now, MerchantId = voucher.MerchantId,
                    Message     = "Update performed on voucher", VoucherCode = voucher.Code,
                    VoucherType = voucher.VoucherType, PropertyUpdated = new PropertyUpdated()
                    {
                        PropertyName = "ExpiryDate", PreviousValue = oldDate, NewValue = voucher.ExpiryDate
                    }
                };

                _logger.LogInformation("Voucher expiry date updated: {@ExpiryUpdateEvent}", updatedEvent);
                return(recordsAffected);
            }
            catch (VoucherUpdateException ex)
            {
                var updateFailedEvent = new VoucherUpdateFailedEvent()
                {
                    EventId       = Guid.NewGuid(), EventTime = DateTime.Now,
                    Message       = "Update performed on voucher", VoucherCode = code,
                    FailureReason = ex.Message, PropertyToUpdate = new PropertyUpdated()
                    {
                        PropertyName = "ExpiryDate", NewValue = newDate
                    }
                };
                _logger.LogError("Failed to update Voucher: {@ExpiryUpdateFailedEvent}", updateFailedEvent);
                _logger.LogDebug(ex, "Could not perform update operation on voucher with {Code}", code);
                return(null);
            }
        }