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); } }
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); } }