public bool AssociateCustomerAsset(AssociateCustomerAssetEvent associateCustomerAsset) { try { var customer = GetCustomer(associateCustomerAsset.CustomerUID); if (customer?.CustomerID > 0) { Enum.TryParse(associateCustomerAsset.RelationType, true, out CustomerEnum.RelationType relationType); var messages = CustomerTopics ?.Select(topic => new KafkaMessage { Key = associateCustomerAsset.CustomerUID.ToString(), Message = new { AssociateCustomerAssetEvent = new { associateCustomerAsset.CustomerUID, associateCustomerAsset.AssetUID, RelationType = relationType.ToString(), associateCustomerAsset.ActionUTC, associateCustomerAsset.ReceivedUTC } }, Topic = topic }) ?.ToList(); var assetCustomer = new DbAssetCustomer { Fk_CustomerUID = associateCustomerAsset.CustomerUID, Fk_AssetUID = associateCustomerAsset.AssetUID, fk_AssetRelationTypeID = (int)relationType, LastCustomerUTC = DateTime.UtcNow }; var actions = new List <Action>() { () => transaction.Upsert(assetCustomer), () => transaction.Publish(messages) }; return(transaction.Execute(actions)); } logger.LogInformation($"Skipping the CustomerAsset Association as the required customeruid" + $" {associateCustomerAsset.CustomerUID} has not been received yet."); return(false); } catch (Exception ex) { logger.LogError($"Error while associating asset to customer : {ex.Message}, {ex.StackTrace}"); throw; } }
public static AssociateCustomerAssetEvent GetDefaultValidAssociateCustomerAssetServiceRequest() { AssociateCustomerAssetEvent defaultValidAssociateCustomerAssetServiceModel = new AssociateCustomerAssetEvent(); defaultValidAssociateCustomerAssetServiceModel.CustomerUID = customerServiceSupport.CreateCustomerModel.CustomerUID; defaultValidAssociateCustomerAssetServiceModel.AssetUID = Guid.NewGuid(); defaultValidAssociateCustomerAssetServiceModel.RelationType = "Dealer"; defaultValidAssociateCustomerAssetServiceModel.ActionUTC = DateTime.UtcNow; defaultValidAssociateCustomerAssetServiceModel.ReceivedUTC = null; return(defaultValidAssociateCustomerAssetServiceModel); }
public void AssociateCustomerAsset_ValidPayload_ExpectedTransactionStatus( bool hasValidCustomer, bool transactionStatus, int upsertCalls, int publishCalls, bool hasException, string relType) { //Arrange var customerUid = Guid.NewGuid(); var assetUid = Guid.NewGuid(); var assetCustomerEvent = new AssociateCustomerAssetEvent { CustomerUID = customerUid, AssetUID = assetUid, RelationType = relType, ActionUTC = DateTime.UtcNow, ReceivedUTC = DateTime.UtcNow }; DbCustomer customerData = hasValidCustomer ? new DbCustomer() { CustomerID = 109 } : null; transaction.Get <DbCustomer>(Arg.Any <string>()).Returns(new List <DbCustomer> { customerData }); transaction.Execute(Arg.Any <List <Action> >()) .Returns(a => { if (hasException) { a.Arg <List <Action> >().ForEach(action => action.Returns(e => throw new Exception())); return(false); } else { a.Arg <List <Action> >().ForEach(action => action.Invoke()); return(true); } }); //Act var resultData = customerAssetService.AssociateCustomerAsset(assetCustomerEvent); //Assert Assert.Equal(transactionStatus, resultData); transaction.Received(upsertCalls).Upsert( Arg.Is <DbAssetCustomer>(assetCust => ValidateCustomerAssetObject(assetCustomerEvent, assetCust))); transaction.Received(publishCalls).Publish( Arg.Is <List <KafkaMessage> >(messages => messages .TrueForAll(m => ValidateCustomerAssetKafkaObject(false, m, assetCustomerEvent)))); }
public IActionResult AssociateCustomerAsset([FromBody] AssociateCustomerAssetEvent customerAsset) { try { customerAsset.ReceivedUTC = DateTime.UtcNow; if (customerService.GetCustomer(customerAsset.CustomerUID) == null) { logger.LogInformation(Messages.CustomerDoesntExist); return(BadRequest(Messages.CustomerDoesntExist)); } if (!Enum.TryParse(customerAsset.RelationType, true, out RelationType assetCustomerRelationShipType)) { logger.LogInformation(Messages.InvalidRelationshipType); return(BadRequest(Messages.InvalidRelationshipType)); } var customerAssetInDb = customerAssetService .GetAssetCustomerByRelationType(customerAsset.CustomerUID, customerAsset.AssetUID, (int)assetCustomerRelationShipType); if (customerAssetInDb != null) { logger.LogInformation(string.Format(Messages.DuplicateCARequested, customerAsset.CustomerUID.ToString(), customerAsset.AssetUID.ToString())); return(Conflict(Messages.AssociationAlreadyExists)); } if (customerAssetService.AssociateCustomerAsset(customerAsset)) { 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)); } }