public async Task TrainingPunctuationProcesses(User user, string trainingName, int trainingId) { var lastUserTrainingCompleted = await _trainingUserPointsRepository.CustomFind(x => x.User.Id == user.Id && x.ResultId == (int)TrainingResultEnum.Approved && x.TrainingDoneAt.Month == DateTime.Now.AddMonths(-1).Month && x.TrainingDoneAt.Year == DateTime.Now.AddMonths(-1).Year); var lastTraining = await _trainingRepository.CustomFind(x => x.CurrentMonth == DateTime.Now.AddMonths(-1).Month && x.CurrentYear == DateTime.Now.AddMonths(-1).Year && !x.hasTrainingMaterial && x.trainingKind == 2); var trainingValidation = lastTraining.Count() >= 2 ? 2 : 1; decimal point = 10; var lastPoint = (await _userPunctuationRepository.CustomFind(x => x.User.Id == user.Id && x.UserPunctuationSource.Id == (int)UserPunctuationSourceEnum.TrainingSaleman)).OrderBy(x => x.Id); if (lastUserTrainingCompleted.Count() >= trainingValidation) { var lastMonthToApply = lastPoint.Where(x => x.CurrentMonth == DateTime.Now.AddMonths(-1).Month&& x.CurrentYear == DateTime.Now.AddMonths(-1).Year).LastOrDefault(); var currentMonthToApply = lastPoint.Where(x => x.CurrentMonth == DateTime.Now.Month && x.CurrentYear == DateTime.Now.Year).LastOrDefault(); if (currentMonthToApply != null) { point = currentMonthToApply.Punctuation; } else if (lastMonthToApply != null && lastMonthToApply.Punctuation < 50) { point = lastMonthToApply.Punctuation + 10; } else if (lastMonthToApply != null && lastMonthToApply.Punctuation >= 50) { point = 50; } } _logger.Info($" Nova pontuacao inserida no mes {DateTime.Now.Month} de {DateTime.Now.Year} no valor {point} para o usuario com id {user.Id} "); _userPunctuationRepository.Save(new UserPunctuation { CurrentMonth = DateTime.Now.Month, CurrentYear = DateTime.Now.Year, CreatedAt = DateTime.Now, Description = trainingName, OperationType = 'C', Punctuation = point, ReferenceEntityId = trainingId, User = user, UserPunctuationSource = await _userPunctuationSourceRepository.GetById((int)UserPunctuationSourceEnum.TrainingSaleman) }); await _unitOfWork.CommitAsync(); }
public async Task <IEnumerable <UserPunctuation> > GetUserCredits(int userId, int currentMonth, int currentYear) => await _userPunctuationRepository.CustomFind(x => x.CurrentMonth == currentMonth && x.CurrentYear == currentYear && x.User.Id == userId && x.Punctuation > 0);
public async Task <RescueResponseDto <Order> > DoRescue(RescueRequestDto rescueRequestDto) { var user = await _userRepository.CustomFind(x => x.Cpf.Equals(rescueRequestDto.Cpf) && (x.UserStatus.Id == (int)UserStatusEnum.Active || x.UserStatus.Id == (int)UserStatusEnum.OnlyCatalog)); _logger.Info($"resgate para login {rescueRequestDto.Cpf} com codigo externo {rescueRequestDto.ExternalOrderId} - iniciada"); if (!user.Any()) { _logger.Info($"resgate para login {rescueRequestDto.Cpf} com codigo externo {rescueRequestDto.ExternalOrderId} - usuario invalido"); return(new RescueResponseDto <Order> { Success = false, Message = "usuario invalido" }); } var lastRescue = await _orderRepository.GetLast(x => x.User.Cpf.Equals(rescueRequestDto.Cpf), x => x.Id); if (lastRescue != null && (DateTime.Now - lastRescue.CreatedAt).TotalMinutes < 2) { _logger.Fatal($"resgate para login {rescueRequestDto.Cpf} com codigo externo {rescueRequestDto.ExternalOrderId} - suspeita de fraude por tentativa de gerar pedidos em um pequeno intervalo de tempo"); return(new RescueResponseDto <Order> { Success = false, Message = "suspeita de fraude por tentativa de gerar pedidos em um pequeno intervalo de tempo" }); } if (!rescueRequestDto.RescueRequestItems.Any()) { _logger.Info($"resgate para login {rescueRequestDto.Cpf} com codigo externo {rescueRequestDto.ExternalOrderId} - pedido não possue itens"); return(new RescueResponseDto <Order> { Success = false, Message = "pedido não possue itens" }); } var balance = (await _userPunctuationRepository.CustomFind(x => x.User.Id == user.First().Id)).Sum(x => x.Punctuation); var order = new Order(); if (balance < rescueRequestDto.Total) { _logger.Info($"autorizacao de pedido para login {rescueRequestDto.Cpf} com codigo externo {rescueRequestDto.ExternalOrderId} - autorizacao recusada saldo insuficiente"); return(new RescueResponseDto <Order> { Success = false, Message = "Saldo insuficiente" }); } order = new Order { Activated = true, ConversionRate = rescueRequestDto.ConversionRate, CreatedAt = DateTime.Now, ExternalOrderId = rescueRequestDto.ExternalOrderId, Freight = rescueRequestDto.Freight, Login = rescueRequestDto.Cpf, ForecastDate = rescueRequestDto.ForecastDate, OrderStatus = await _orderStatusRepository.GetById((int)OrderStatusEnum.Confirmed), OrderValue = rescueRequestDto.OrderValue, Total = rescueRequestDto.Total, User = user.First() }; _orderRepository.Save(order); await _unitOfWork.CommitAsync(); _userPunctuationRepository.Save(new UserPunctuation { CreatedAt = DateTime.Now, CurrentMonth = DateTime.Now.Month, CurrentYear = DateTime.Now.Year, Description = "Pedido Confirmado", OperationType = 'D', Punctuation = -1 * rescueRequestDto.Total, ReferenceEntityId = order.Id, UserPunctuationSource = await _userPunctuationSourceRepository.GetById((int)SourceUserPunctuationEnum.OrderConfirmed), User = user.First() }); foreach (var item in rescueRequestDto.RescueRequestItems) { _orderItemRepository.Save(new OrderItem { Activated = true, Ammout = item.Ammout, Category = item.Category, CodeItem = item.CodeItem, CreatedAt = DateTime.Now, Department = item.Department, ExternalOrderId = rescueRequestDto.ExternalOrderId, Order = order, Partner = item.Partner, ProductName = item.ProductName, TotalValue = item.TotalValue, UnitValue = item.UnitValue }); } var confirmSaved = await _unitOfWork.CommitAsync(); if (confirmSaved) { order.User = null; _logger.Info($"autorizacao de pedido para login {rescueRequestDto.Cpf} com codigo externo {rescueRequestDto.ExternalOrderId} - Pedido gerado com sucesso!"); return(new RescueResponseDto <Order> { Success = true, Message = "Pedido gerado com sucesso!", Data = new List <Order> { order } }); } else { _logger.Info($"autorizacao de pedido para login {rescueRequestDto.Cpf} com codigo externo {rescueRequestDto.ExternalOrderId} - autorizacao recusada ocorreu erro ao tentar gerar o pedido"); return(new RescueResponseDto <Order> { Success = false, Message = "ocorreu erro ao tentar gerar o pedido" }); } }