Beispiel #1
0
        public void UpdateAccount_PublishUpsertException_TransactionException()
        {
            //Arrange
            var accountUid    = Guid.NewGuid();
            var accountDetail = new DbAccount
            {
                CustomerAccountID    = 1,
                fk_ChildCustomerUID  = Guid.NewGuid(),
                fk_ParentCustomerUID = Guid.NewGuid(),
                CustomerAccountUID   = accountUid
            };
            var updateEvent = new UpdateCustomerEvent
            {
                CustomerUID         = accountUid,
                BSSID               = "BSS02",
                CustomerName        = "CUS02",
                NetworkCustomerCode = "NCC02",
                DealerAccountCode   = "DAC02",
                ActionUTC           = DateTime.UtcNow,
                ReceivedUTC         = DateTime.UtcNow
            };

            transaction.Execute(Arg.Any <List <Action> >()).Returns(a =>
            {
                throw new Exception();
            });

            //Act
            Assert.Throws <Exception>(() => accountService.UpdateAccount(updateEvent, accountDetail));

            //Assert
            transaction.Received(0).Upsert(Arg.Any <DbAccount>());
            transaction.Received(0).Publish(Arg.Any <KafkaMessage>());
        }
Beispiel #2
0
        public bool UpdateAccount(UpdateCustomerEvent updateAccount, DbAccount accountDetails)
        {
            try
            {
                var accountEvent = new AccountEvent
                {
                    AccountName          = updateAccount.CustomerName,
                    AccountUID           = updateAccount.CustomerUID,
                    Action               = Operation.Update.ToString(),
                    BSSID                = updateAccount.BSSID,
                    DealerAccountCode    = updateAccount.DealerAccountCode,
                    NetworkCustomerCode  = updateAccount.NetworkCustomerCode,
                    fk_ParentCustomerUID = accountDetails.fk_ParentCustomerUID ?? null,
                    fk_ChildCustomerUID  = accountDetails.fk_ChildCustomerUID ?? null,
                    ActionUTC            = updateAccount.ActionUTC,
                    ReceivedUTC          = updateAccount.ReceivedUTC
                };

                if (!FieldHelper.IsValidValuesFilled(accountEvent, accountDetails, logger))
                {
                    logger.LogError("DB Object expects typeOf IDbTable");
                }

                FieldHelper.ReplaceEmptyFieldsByNull(accountEvent);

                var message = new KafkaMessage
                {
                    Key     = updateAccount.CustomerUID.ToString(),
                    Topic   = AccountTopic,
                    Message = new { AccountEvent = accountEvent }
                };

                var accountObj = new DbAccount
                {
                    CustomerAccountID  = accountDetails.CustomerAccountID,
                    CustomerAccountUID = accountEvent.AccountUID,
                    BSSID                = accountEvent.BSSID,
                    AccountName          = accountEvent.AccountName,
                    NetworkCustomerCode  = accountEvent.NetworkCustomerCode,
                    DealerAccountCode    = accountEvent.DealerAccountCode,
                    fk_ChildCustomerUID  = accountDetails.fk_ChildCustomerUID ?? null,
                    fk_ParentCustomerUID = accountDetails.fk_ParentCustomerUID ?? null,
                    RowUpdatedUTC        = DateTime.UtcNow
                };

                var actions = new List <Action>()
                {
                    () => transaction.Upsert(accountObj),
                    () => transaction.Publish(message)
                };
                return(transaction.Execute(actions));
            }
            catch (Exception ex)
            {
                logger.LogError($"Error while updating account customer : {ex.Message}, {ex.StackTrace}");
                throw;
            }
        }
 protected async Task UpdateButtonClicked()
 {
     if (addingNewCustomer)
     {
         addingNewCustomer = false;
         await AddCustomerEvent.InvokeAsync(SelectedCustomer);
     }
     else
     {
         await UpdateCustomerEvent.InvokeAsync(SelectedCustomer);
     }
 }
Beispiel #4
0
 private bool IsInvalidUpdateCustomer(UpdateCustomerEvent customer)
 {
     return(string.IsNullOrEmpty(customer.CustomerName) &&
            string.IsNullOrEmpty(customer.BSSID) &&
            string.IsNullOrEmpty(customer.DealerAccountCode) &&
            string.IsNullOrEmpty(customer.DealerNetwork) &&
            string.IsNullOrEmpty(customer.NetworkCustomerCode) &&
            string.IsNullOrEmpty(customer.NetworkDealerCode) &&
            string.IsNullOrEmpty(customer.PrimaryContactEmail) &&
            string.IsNullOrEmpty(customer.FirstName) &&
            string.IsNullOrEmpty(customer.LastName) &&
            !customer.IsActive.HasValue);
 }
 public void Handle(UpdateCustomerEvent e)
 {
     using (var conn = _configuration.CreateConnection())
     {
         conn.Open();
         var cmd = conn.CreateCommand();
         cmd.CommandText = "update Customer set name = @name, email = @email where username = @username";
         cmd.AddWithValue("@name", e.Name);
         cmd.AddWithValue("@email", e.Email);
         cmd.AddWithValue("@username", e.AggregateId);
         cmd.ExecuteNonQuery();
     }
 }
Beispiel #6
0
        public void UpdateCustomer_ValidPayload_TransactionSuccess(
            string customerType, bool hasActive, bool?isActive)
        {
            //Arrange
            var customerEvent = new UpdateCustomerEvent
            {
                CustomerName        = $"{customerType}01",
                CustomerUID         = Guid.NewGuid(),
                BSSID               = "BSS01",
                DealerNetwork       = "None",
                NetworkDealerCode   = "NDC01",
                PrimaryContactEmail = $"{customerType}[email protected]",
                FirstName           = $"{customerType}FN01",
                LastName            = $"{customerType}LN01",
                ActionUTC           = DateTime.UtcNow,
                ReceivedUTC         = DateTime.UtcNow
            };
            var custDetail = new DbCustomer
            {
                CustomerID        = 1,
                CustomerUID       = customerEvent.CustomerUID,
                fk_CustomerTypeID = 0,
                IsActive          = true
            };

            if (hasActive)
            {
                customerEvent.IsActive = isActive;
            }

            transaction.Execute(Arg.Any <List <Action> >())
            .Returns(a =>
            {
                a.Arg <List <Action> >().ForEach(action => action.Invoke());
                return(true);
            });

            //Act
            var resultData = customerService.UpdateCustomer(customerEvent, custDetail);

            //Assert
            Assert.True(resultData);
            transaction.Received(1).Upsert(
                Arg.Is <DbCustomer>(o =>
                                    ValidateCustomerObject(customerEvent, o, customerType, true, hasActive, custDetail)));
            transaction.Received(1).Publish(Arg.Is <List <KafkaMessage> >(messages =>
                                                                          messages.TrueForAll(m =>
                                                                                              ValidateCustomerKafkaObject(customerEvent, m, customerType, true, custDetail))));
        }
Beispiel #7
0
        public static UpdateCustomerEvent GetDefaultValidCustomerServiceUpdateRequest()
        {
            UpdateCustomerEvent defaultValidCustomerServiceUpdateModel = new UpdateCustomerEvent();

            defaultValidCustomerServiceUpdateModel.CustomerName        = "AutoTestAPICreateCustomerName" + DateTime.UtcNow.ToString("yyyyMMddhhmmss");
            defaultValidCustomerServiceUpdateModel.BSSID               = "BSS" + DateTime.UtcNow.ToString("yyyyMMddhhmmss");
            defaultValidCustomerServiceUpdateModel.DealerNetwork       = "AutoTestAPIDealerNetwork" + DateTime.UtcNow.ToString("yyyyMMddhhmmss");
            defaultValidCustomerServiceUpdateModel.NetworkDealerCode   = "AutoTestAPINetworkDealerCode" + DateTime.UtcNow.ToString("yyyyMMddhhmmss");
            defaultValidCustomerServiceUpdateModel.NetworkCustomerCode = "AutoTestAPINetworkCustomerCode" + DateTime.UtcNow.ToString("yyyyMMddhhmmss");
            defaultValidCustomerServiceUpdateModel.DealerAccountCode   = "AutoTestAPIDealerAccountCode" + DateTime.UtcNow.ToString("yyyyMMddhhmmss");
            defaultValidCustomerServiceUpdateModel.CustomerUID         = customerServiceSupport.CreateCustomerModel.CustomerUID;
            defaultValidCustomerServiceUpdateModel.ActionUTC           = DateTime.UtcNow;
            defaultValidCustomerServiceUpdateModel.ReceivedUTC         = null;
            defaultValidCustomerServiceUpdateModel.PrimaryContactEmail = "AutoTestAPIEmail" + DateTime.UtcNow.ToString("yyyyMMddhhmmss") + "@trimble.com";
            defaultValidCustomerServiceUpdateModel.FirstName           = "AutoTestAPIFirstName" + DateTime.UtcNow.ToString("yyyyMMddhhmmss");
            defaultValidCustomerServiceUpdateModel.LastName            = "AutoTestAPILastName" + DateTime.UtcNow.ToString("yyyyMMddhhmmss");
            return(defaultValidCustomerServiceUpdateModel);
        }
Beispiel #8
0
        public void UpdateCustomer_PublishUpsertException_TransactionException()
        {
            //Arrange
            var customerEvent = new UpdateCustomerEvent
            {
                CustomerName        = "CUS01",
                CustomerUID         = Guid.NewGuid(),
                BSSID               = "BSS01",
                DealerNetwork       = "None",
                NetworkDealerCode   = "NDC01",
                PrimaryContactEmail = "*****@*****.**",
                FirstName           = "FN01",
                LastName            = "LN01",
                ActionUTC           = DateTime.UtcNow,
                ReceivedUTC         = DateTime.UtcNow
            };
            var custDetail = new DbCustomer
            {
                CustomerID        = 1,
                CustomerUID       = customerEvent.CustomerUID,
                fk_CustomerTypeID = 0,
                IsActive          = true
            };

            transaction.Execute(Arg.Any <List <Action> >())
            .Returns(a =>
            {
                throw new Exception();
            });

            //Act
            Assert.Throws <Exception>(() => customerService.UpdateCustomer(customerEvent, custDetail));

            //Assert

            transaction.DidNotReceive().Upsert(Arg.Any <DbCustomer>());
            transaction.DidNotReceive().Publish(Arg.Any <List <KafkaMessage> >());
        }
Beispiel #9
0
        public IActionResult UpdateCustomer([FromBody] UpdateCustomerEvent customer)
        {
            try
            {
                customer.ReceivedUTC = DateTime.UtcNow;
                var customerDetails = customerService.GetCustomer(customer.CustomerUID);
                var accountDetails  = accountService.GetAccount(customer.CustomerUID);
                if (customerDetails == null && accountDetails == null)
                {
                    logger.LogInformation(Messages.CustomerDoesntExist);
                    return(BadRequest(Messages.CustomerDoesntExist));
                }

                if (IsInvalidUpdateCustomer(customer))
                {
                    logger.LogInformation(Messages.NoDataToUpdate);
                    return(BadRequest(Messages.NoDataToUpdate));
                }

                if (accountDetails != null && accountService.UpdateAccount(customer, accountDetails))
                {
                    return(Ok());
                }

                if (customerDetails != null && customerService.UpdateCustomer(customer, customerDetails))
                {
                    return(Ok());
                }

                logger.LogWarning(Messages.UnableToSaveToDb);
                return(BadRequest(Messages.UnableToSaveToDb));
            }
            catch (Exception ex)
            {
                logger.LogError(string.Format(Messages.ExceptionOccured, ex.Message, ex.StackTrace));
                return(StatusCode((int)HttpStatusCode.InternalServerError, ex.Message));
            }
        }
Beispiel #10
0
        public void UpdateAccount_ValidPayload_TransactionSuccess(bool isParentNull, bool isChildNull)
        {
            //Arrange
            var accountUid    = Guid.NewGuid();
            var accountDetail = new DbAccount
            {
                CustomerAccountID    = 1,
                fk_ChildCustomerUID  = isChildNull ? (Guid?)null : Guid.NewGuid(),
                fk_ParentCustomerUID = isParentNull ? (Guid?)null : Guid.NewGuid(),
                CustomerAccountUID   = accountUid
            };
            var updateEvent = new UpdateCustomerEvent
            {
                CustomerUID         = accountUid,
                BSSID               = "BSS02",
                CustomerName        = "CUS02",
                NetworkCustomerCode = "NCC02",
                DealerAccountCode   = "DAC02",
                ActionUTC           = DateTime.UtcNow,
                ReceivedUTC         = DateTime.UtcNow
            };

            transaction.Execute(Arg.Any <List <Action> >()).Returns(a =>
            {
                a.Arg <List <Action> >().ForEach(action => action.Invoke());
                return(true);
            });

            //Act
            var resultData = accountService.UpdateAccount(updateEvent, accountDetail);

            //Assert
            Assert.True(resultData);
            transaction.Received(1).Upsert(
                Arg.Is <DbAccount>(account => ValidateAccountObject(updateEvent, account, true, accountDetail)));
            transaction.Received(1).Publish(
                Arg.Is <KafkaMessage>(m => ValidateAccountKafkaObject(updateEvent, m, true, accountDetail)));
        }
Beispiel #11
0
 public void Handle(UpdateCustomerEvent e)
 {
     this.Name  = e.Name;
     this.Email = e.Email;
 }