public async Task <IActionResult> Post([FromBody] EbaRequestInfo request) { try { if (!ModelState.IsValid) { return(this.ApiErrorMessage400BadRequest(ModelState)); } var jwtPayloadInfo = this.GetJwtPayloadInfo(); var userProfileId = await _bl.GetCachedUserId_byExternalReferenceIdAsync(jwtPayloadInfo.ExtReferenceId); if (userProfileId == null) { return(this.ApiErrorMessage400BadRequestUserIdInTokenNotFound(jwtPayloadInfo.ExtReferenceId)); } var bl_dto = new BL_EbaTransactionRequest { Comment = request.Comment, PointsToDeposit = request.PointsToDeposit, FromUserprofileId = userProfileId, }; await _bl.SetEbaTransactionAsync(bl_dto); return(this.ApiPostMessage204NotContent()); } catch (BusinessLogicException ex) { HttpContext.Items.Add("ex", ex); //For instrumentation return(this.ApiErrorMessage400BadRequest(ex)); } catch (Exception ex) { HttpContext.Items.Add("ex", ex); //For instrumentation return(StatusCode(StatusCodes.Status500InternalServerError)); } }
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); } } }