/// <summary> /// Assumes all data is Validated and Complete prior to calling this method, since it will replace the complete document if already exists, else create a new one. /// </summary> /// <param name="entity"></param> /// <returns></returns> public async Task <string> SetEbaPointsSentAsync(EbaPointsSent entity) { SetEntityType(entity); //Below logic was introduced 180908 to replace the read and the replace operation we used to follow prior var upsert = await client.UpsertDocumentAsync(UriFactory.CreateDocumentCollectionUri(_settings.DatabaseId, _settings.DefaultCollectionId), entity); return(upsert.Resource.Id); }
public async Task <string> SetEbaTransactionAsync(BL_EbaTransactionRequest bl_dto) { var utcNow = DateTimeOffset.UtcNow; //Read Existing one var existingItem = await _repo.GetEbaPointsSent_ByUserIdAsync(bl_dto.FromUserprofileId); if (existingItem == null) { var existingUser = await _repo.GetUserProfile_byIdAsync(bl_dto.FromUserprofileId); existingItem = new EbaPointsSent { DTCreated = utcNow }; existingItem.RelationshipId = existingUser.Relationship_Id; existingItem.UserprofileId = existingUser.Id; } //Map and Update Balances //Map incoming data to existing entity (need to be mapped this way to not overwrite new properties not part of incoming BL_UserProfile) existingItem.DTLastUpdated = utcNow; if (existingItem.Transactions == null) { existingItem.Transactions = new List <EbaPointsSent.EbaTransaction>(); } existingItem.Transactions.Insert(0, new EbaPointsSent.EbaTransaction { Comment = bl_dto.Comment, Key = Guid.NewGuid().ToString("D").ToUpper(), Points = bl_dto.PointsToDeposit, PostedDT = utcNow }); existingItem.TotalPoints = existingItem.TotalPoints + bl_dto.PointsToDeposit; //Validate - done after mapping to ensure the mapping logic not broken var settings = await GetBusinessLogicSettings(); if (validator.CanSetEbaPoints(existingItem, settings, out var validationResult)) { var idUpdatedOrCreated = await _repo.SetEbaPointsSentAsync(existingItem); await publishPushNotification(existingItem.UserprofileId, existingItem.Transactions.First().Key); return(idUpdatedOrCreated); } else { throw new BusinessLogicException(validationResult); } //local method async Task publishPushNotification(string currentUserId, string entityId) { var partnerUserId = await base.GetCachedPartnerUserId(currentUserId); if (string.IsNullOrEmpty(partnerUserId)) { LCLog.Information($"PushNotifications - for UserId:null - Unable to find Partner Id for Current User Id:{currentUserId}. Cannot proceed publising push notificaiton for '{BL_PushNotificationMessageTypeEnum.Type_EBA_Points_Sent}'"); } else { await _blAppCenter.PublishNotificationMessage(BL_PushNotificationMessageTypeEnum.Type_EBA_Points_Sent, partnerUserId, entityId); } } }
public bool CanSetEbaPoints(EbaPointsSent entity, BL_Settings settings, out ValidationResult results) { results = new ValidationResult(); if (entity == null) { results.Failures.Add(new ValidationFailure { Code = ValidationErrorCode.EntityIsNull, DebugMessage = $"Entity: {nameof(entity)} is null", }); return(false); } if (string.IsNullOrEmpty(entity?.UserprofileId)) { results.Failures.Add(new ValidationFailure { Code = ValidationErrorCode.IdIsNull_UserProfileId, DebugMessage = $"Entity: {entity.GetType().Name} have Userprofile Id that is null", EntityJsonRepresentation = GetJson(entity), EntityIdInError = entity.Id, }); } if (string.IsNullOrEmpty(entity.RelationshipId)) { results.Failures.Add(new ValidationFailure { Code = ValidationErrorCode.IdIsNull, DebugMessage = $"Entity: {entity.GetType().Name} have Relationship that is null", EntityJsonRepresentation = GetJson(entity), EntityIdInError = entity.Id, }); } var mostRecentTransaction = entity?.Transactions?.FirstOrDefault(); if (mostRecentTransaction?.PostedDT < DateTimeOffset.UtcNow.AddSeconds(-5) || mostRecentTransaction?.PostedDT > DateTimeOffset.UtcNow.AddSeconds(5)) { results.Failures.Add(new ValidationFailure { Code = ValidationErrorCode.EbaPointPostingDTInvalid, DebugMessage = $"Entity: {entity.GetType().Name} have a transaction posting DT that is off more than 5 seconds", EntityJsonRepresentation = GetJson(entity), EntityIdInError = entity.Id, }); } if (string.IsNullOrWhiteSpace(mostRecentTransaction?.Comment)) { results.Failures.Add(new ValidationFailure { Code = ValidationErrorCode.EbaPointCommentEmpty, DebugMessage = $"Entity: {entity.GetType().Name} have a transaction Comment that is empty", EntityJsonRepresentation = GetJson(entity), EntityIdInError = entity.Id, }); } if (mostRecentTransaction?.Comment?.Length < 3) { results.Failures.Add(new ValidationFailure { Code = ValidationErrorCode.EbaPointCommentShorterThanMinLength, DebugMessage = $"Entity: {entity.GetType().Name} have a transaction Comment that is less than 3 characters", EntityJsonRepresentation = GetJson(entity), EntityIdInError = entity.Id, }); } if (mostRecentTransaction?.Comment?.Length > 1500) { results.Failures.Add(new ValidationFailure { Code = ValidationErrorCode.EbaPointCommentExceedMaxLength, DebugMessage = $"Entity: {entity.GetType().Name} have a transaction Comment that is longer than 1500", EntityJsonRepresentation = GetJson(entity), EntityIdInError = entity.Id, }); } if (string.IsNullOrWhiteSpace(mostRecentTransaction?.Key) || mostRecentTransaction?.Key != mostRecentTransaction?.Key?.ToUpper()) { results.Failures.Add(new ValidationFailure { Code = ValidationErrorCode.Empty, DebugMessage = $"Entity: {entity.GetType().Name} have a transaction Key which is invalid", EntityJsonRepresentation = GetJson(entity), EntityIdInError = entity.Id, }); } if (mostRecentTransaction?.Points == null || false == settings.EmotionalBankAccontPointOptions.ContainsKey(mostRecentTransaction.Points)) { results.Failures.Add(new ValidationFailure { Code = ValidationErrorCode.EbaPointValueNotWithinAllowedSet, DebugMessage = $"Entity: {entity.GetType().Name} have a transaction point value that is not within allowed set of valid points", EntityJsonRepresentation = GetJson(entity), EntityIdInError = entity.Id, }); } return(results.IsValid); }