private async Task HandleCoinsIncrementActionAsync(Data.Entities.Order order) { var user = order.User; var product = order.Product; if (user != null && product != null && product.Type == ProductEnum.COIN_OFFER) { await _userStatisticsService.UpdateTotalCoinsAsync(user, product.Value); } // TODO: Evaluate with Frontend devs y they need a PN with the success payment completed }
public async Task <RewardHistory> CreateRewardAsync(CreateRewardRequest rewardRequest) { // validate target user exists var existUser = await _uow.UserRepository.GetAsync(rewardRequest.UserId); if (existUser == null) { throw new NotFoundException(ExceptionConstants.NOT_FOUND, "User"); } // get/validate-exist reward type var rewardCategory = await _rewardCategoryService.GetRewardCategoryByCategoryAsync(rewardRequest.RewardCategoryEnum); var rewardAcomulate = await GetOrCreateRewardAcomulate(existUser, rewardCategory); var allowedPoints = 0; if (rewardCategory.Category == RewardCategoryEnum.SOLO_QUESTION_ANSWERED) { var data = JsonConvert.DeserializeObject <RewardHistoryData>(rewardRequest.Data); var userAnswer = JsonConvert.DeserializeObject <UserSoloAnswerResponse>(data.Entity1); allowedPoints = userAnswer.Points; } else { allowedPoints = await AllowedPointsAsync(rewardCategory, rewardAcomulate, rewardRequest.IsPlus); } if (allowedPoints == 0) { return(null); } var rewardPoints = rewardRequest.IsPlus ? rewardCategory.PointsToIncrement : rewardCategory.PointsToDecrement; if (rewardCategory.Category == RewardCategoryEnum.SOLO_QUESTION_ANSWERED) { rewardPoints = allowedPoints; } var dbReward = new RewardHistory(); dbReward.User = existUser; dbReward.RewardCategory = rewardCategory; dbReward.IsPlus = rewardRequest.IsPlus; dbReward.Points = allowedPoints; dbReward.RewardPoints = rewardPoints; dbReward.Data = rewardRequest.Data; dbReward.CreatedAt = DateTime.UtcNow; await _uow.RewardHistoryRepository.AddAsync(dbReward); allowedPoints = rewardRequest.IsPlus ? allowedPoints : allowedPoints * -1; rewardAcomulate.Points = rewardAcomulate.Points + allowedPoints; rewardAcomulate.ModifiedAt = DateTime.UtcNow; await _uow.RewardAcumulateRepository.UpdateAsync(rewardAcomulate, rewardAcomulate.Id); await _userStatisticsService.UpdateTotalPoints(existUser, allowedPoints); await _userStatisticsService.UpdateTotalCoinsAsync(existUser, allowedPoints); await _uow.CommitAsync(); return(dbReward); }