public void CreateAccountCustomerRelationShip_PublishUpsertException_TransactionFailure() { //Arrange var accountUid = Guid.NewGuid(); var accountEvent = new CreateCustomerRelationshipEvent { AccountCustomerUID = accountUid, ParentCustomerUID = Guid.NewGuid(), ChildCustomerUID = Guid.NewGuid(), ActionUTC = DateTime.UtcNow, ReceivedUTC = DateTime.UtcNow }; var accountDetail = new DbAccount { CustomerAccountID = 1, CustomerAccountUID = accountUid, AccountName = "ACC04", BSSID = "BSS04", DealerAccountCode = "DAC04", NetworkCustomerCode = "NCC04", }; transaction.Execute(Arg.Any <List <Action> >()).Returns(a => { throw new Exception(); }); //Act && Assert; Assert.Throws <Exception>(() => accountService.CreateAccountCustomerRelationShip(accountEvent, accountDetail)); transaction.Received(0).Upsert(Arg.Any <DbAccount>()); transaction.Received(0).Publish(Arg.Any <KafkaMessage>()); }
public static CreateCustomerRelationshipEvent GetDefaultValidCustomerRelationshipServiceCreateRequest() { CreateCustomerRelationshipEvent defaultValidCustomerRelationshipServiceCreateModel = new CreateCustomerRelationshipEvent(); defaultValidCustomerRelationshipServiceCreateModel.ParentCustomerUID = Guid.NewGuid(); defaultValidCustomerRelationshipServiceCreateModel.ChildCustomerUID = Guid.NewGuid(); defaultValidCustomerRelationshipServiceCreateModel.ActionUTC = DateTime.UtcNow; defaultValidCustomerRelationshipServiceCreateModel.ReceivedUTC = null; return(defaultValidCustomerRelationshipServiceCreateModel); }
public bool CreateAccountCustomerRelationShip(CreateCustomerRelationshipEvent customerRelationship, DbAccount accountDetails) { try { var message = new KafkaMessage { Key = customerRelationship.AccountCustomerUID.ToString(), Topic = AccountTopic, Message = new { AccountEvent = new AccountEvent() { AccountName = accountDetails.AccountName, AccountUID = accountDetails.CustomerAccountUID, Action = Operation.Update.ToString(), BSSID = accountDetails.BSSID, DealerAccountCode = accountDetails.DealerAccountCode, NetworkCustomerCode = accountDetails.NetworkCustomerCode, fk_ParentCustomerUID = customerRelationship.ParentCustomerUID, fk_ChildCustomerUID = customerRelationship.ChildCustomerUID, ActionUTC = customerRelationship.ActionUTC, ReceivedUTC = DateTime.UtcNow } } }; var accountobj = new DbAccount { CustomerAccountID = accountDetails.CustomerAccountID, CustomerAccountUID = accountDetails.CustomerAccountUID, BSSID = accountDetails.BSSID, AccountName = accountDetails.AccountName, NetworkCustomerCode = accountDetails.NetworkCustomerCode, DealerAccountCode = accountDetails.DealerAccountCode, fk_ChildCustomerUID = customerRelationship.ChildCustomerUID, fk_ParentCustomerUID = customerRelationship.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 creating account customer relationship : {ex.Message}, {ex.StackTrace}"); throw; } }
public IActionResult CreateCustomerRelationship(CreateCustomerRelationshipEvent customerRelationship) { try { var isSuccess = true; if (customerRelationship.ChildCustomerUID == Guid.Empty && customerRelationship.ParentCustomerUID == Guid.Empty || customerRelationship.ChildCustomerUID == Guid.Empty && customerRelationship.ParentCustomerUID == null) { logger.LogInformation(Messages.BothParentChildCustomerEmpty); return(BadRequest(Messages.BothParentChildCustomerEmpty)); } customerRelationship.ReceivedUTC = DateTime.UtcNow; if (customerRelationship.AccountCustomerUID != null) { var accountDetails = accountService.GetAccount(customerRelationship.AccountCustomerUID.Value); if (accountDetails != null) { isSuccess = accountService.CreateAccountCustomerRelationShip(customerRelationship, accountDetails); } } if (customerRelationship.ParentCustomerUID != customerRelationship.ChildCustomerUID) { var parentCustomerUID = customerRelationship.ParentCustomerUID ?? customerRelationship.ChildCustomerUID; List <DbCustomerRelationshipNode> customerRelationshipsInDb = customerService.GetCustomerRelationships( parentCustomerUID, customerRelationship.ChildCustomerUID); if (!customerRelationshipsInDb.Any()) { isSuccess = customerService.CreateCustomerRelationShip(customerRelationship); } } if (isSuccess) { return(Ok()); } return(StatusCode((int)HttpStatusCode.InternalServerError)); } catch (Exception ex) { logger.LogError(string.Format(Messages.ExceptionOccured, ex.Message, ex.StackTrace)); return(StatusCode((int)HttpStatusCode.InternalServerError, ex.Message)); } }
public void CreateAccountCustomerRelationShip_ValidPayload_TransactionSuccess(bool isParentNull) { //Arrange var accountUid = Guid.NewGuid(); var accountEvent = new CreateCustomerRelationshipEvent { AccountCustomerUID = accountUid, ParentCustomerUID = isParentNull ? (Guid?)null : Guid.NewGuid(), ChildCustomerUID = Guid.NewGuid(), ActionUTC = DateTime.UtcNow, ReceivedUTC = DateTime.UtcNow }; var accountDetail = new DbAccount { CustomerAccountID = 1, CustomerAccountUID = accountUid, AccountName = "ACC04", BSSID = "BSS04", DealerAccountCode = "DAC04", NetworkCustomerCode = "NCC04", }; transaction.Execute(Arg.Any <List <Action> >()).Returns(a => { a.Arg <List <Action> >().ForEach(action => action.Invoke()); return(true); }); //Act var resultData = accountService.CreateAccountCustomerRelationShip(accountEvent, accountDetail); //Assert Assert.True(resultData); transaction.Received(1).Upsert( Arg.Is <DbAccount>(account => ValidateCreateCustomerRelationshipAccountObject( false, null, accountEvent.ParentCustomerUID, accountEvent.ChildCustomerUID, account, accountDetail))); transaction.Received(1).Publish( Arg.Is <KafkaMessage>(message => ValidateCreateCustomerRelationshipAccountKafkaObject( null, accountEvent.ParentCustomerUID, accountEvent.ChildCustomerUID, message, accountDetail, accountEvent.ActionUTC))); }
public IActionResult CreateCustomer([FromBody] CreateCustomerEvent customer) { try { customer.ReceivedUTC = DateTime.UtcNow; if (!Enum.TryParse(customer.CustomerType.Trim(), true, out CustomerType customerType)) { logger.LogInformation(Messages.InvalidCustomerType); return(BadRequest(Messages.InvalidCustomerType)); } if (customerType != CustomerType.Account && customerService.GetCustomer(customer.CustomerUID) != null) { logger.LogInformation(Messages.CustomerAlreadyExists); return(BadRequest(Messages.CustomerAlreadyExists)); } if (customerType == CustomerType.Account && accountService.GetAccount(customer.CustomerUID) != null) { logger.LogInformation(Messages.AccountAlreadyExists); return(BadRequest(Messages.AccountAlreadyExists)); } if (customerType == CustomerType.Account && accountService.CreateAccount(customer)) { return(Ok()); } if (customerService.CreateCustomer(customer)) { //NOTE : Bug 91897 - Adding the customertype check from converted enum //using incoming payload since it fails to create relationship when //"CustomerType" is allowed to pass integer values. //TODO : The change will not be required once Newtonsoft json usage is removed //and datatype validation is stricter from the API user end. if (string.Equals(customerType.ToString(), CustomerType.Dealer.ToString(), StringComparison.OrdinalIgnoreCase)) { var createCustomerRelationshipEvent = new CreateCustomerRelationshipEvent { ParentCustomerUID = null, ChildCustomerUID = customer.CustomerUID, ActionUTC = DateTime.UtcNow }; var createCustomerRelationshipResponse = CreateCustomerRelationship(createCustomerRelationshipEvent); var responseData = (IStatusCodeActionResult)createCustomerRelationshipResponse; if (responseData != null && responseData.StatusCode != (int)HttpStatusCode.OK) { logger.LogInformation( $"Failed to create Customer Relationship Payload {createCustomerRelationshipEvent}"); } } 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)); } }