private static long?InternalGetMoodleUserId ( string cpf, Modalidade modalidade, GetUserByUsernameClient client = null, HttpClient httpClient = null ) { long?moodleUserId = MoodleFromToCache.GetCachedMoodleUser(modalidade.IdModalidade, cpf); if (moodleUserId.HasValue) { return(moodleUserId.Value); } GetUserByUsernameClient getClient = client; if (getClient == null) { getClient = new GetUserByUsernameClient(); } if (httpClient != null) { getClient.AddHttpClient(httpClient); } getClient.AddBaseUrl(modalidade.MoodleUrl) .AddServiceUrl(modalidade.MoodleServiceUrl) .AddToken(modalidade.MoodleGetInfoServiceToken); GetByUsernameRequest request = new GetByUsernameRequest() { Username = cpf.DesformatarCpf() }; Task <UserResponse> task = getClient.Post(request); task.Wait(); UserResponse response = task.Result; if (response?.Id > 0) { MoodleFromToCache.AddUser(modalidade.IdModalidade, cpf, response.Id); } return(response?.Id); }
protected override void ProcessItem(Professor item, AbstractMoodleServiceClient createClient, AbstractMoodleServiceClient verifyClient) { try { long?cachedMoodleId = MoodleFromToCache.GetCachedMoodleUser(ModalidadeAtual.IdModalidade, item.ProfessorCpf); if (cachedMoodleId.HasValue) { LastUrl = "cached_value"; var reason = new NotImportedReason <Professor>() { Data = item, Url = LastUrl, Reason = $"Professor [{item.ProfessorCpf} | {item.ProfessorNome}] já está adicionado ao MOODLE ({LastUrl})." }; Result.NotImported.Enqueue(reason); Log(reason.Reason); return; } UserResponse exists = VerifyIfExists(verifyClient, item.ProfessorCpf); if (exists?.Id > 0) { MoodleFromToCache.AddUser(ModalidadeAtual.IdModalidade, item.ProfessorCpf, exists.Id); var reason = new NotImportedReason <Professor>() { Data = item, Url = LastUrl, Reason = $"Professor [{item.ProfessorCpf} | {item.ProfessorNome}] já está adicionado ao MOODLE ({LastUrl})." }; Result.NotImported.Enqueue(reason); Log(reason.Reason); return; } if (item.AtivoProfessor) { UserResponse response = SendItem(createClient, item); ImportedResult <Professor, UserResponse> importedResult = new ImportedResult <Professor, UserResponse>() { Date = DateTime.Now, Data = item, Url = LastUrl, Result = response, Active = true, }; Result.ImportedSuccessfully.Enqueue(importedResult); Log($"Professor [{item.ProfessorCpf} | {item.ProfessorNome}] adicionado."); MoodleFromToCache.AddUser(ModalidadeAtual.IdModalidade, item.ProfessorCpf, response.Id); } else { SuspendedUserResult suspendedUserResult = this.SuspendItem(item, ModalidadeAtual, createClient.GetUnderlyingHttpClient()); if (!suspendedUserResult.MoodleId.HasValue) { throw new MoodleDataNotExistsException($"Tentativa de suspender usuário falhou. O professor [{item.ProfessorCpf} | {item.ProfessorNome}] não está cadastrado no MOODLE"); } var nome = item.ProfessorNome; var matricula = item.ProfessorMatricula.FormatarMatricula(); if (item.ProfessorEmail == null) { item.ProfessorEmail = ""; } UserResponse response = new UserResponse() { Email = item.ProfessorEmail.TratarEmail(item.ProfessorMatricula), Id = suspendedUserResult.MoodleId.Value, Fullname = nome, Username = item.ProfessorCpf.DesformatarCpf() }; ImportedResult <Professor, UserResponse> importedResult = new ImportedResult <Professor, UserResponse>() { Date = DateTime.Now, Data = item, Url = suspendedUserResult.LastUrl, Result = response, Active = false }; Result.ImportedSuccessfully.Enqueue(importedResult); Log($"Professor [{item.ProfessorCpf} | {item.ProfessorNome}] SUSPENSO."); MoodleFromToCache.AddUser(ModalidadeAtual.IdModalidade, item.ProfessorCpf, suspendedUserResult.MoodleId.Value); } } catch (MoodleDataNotExistsException mex) { var reason = new NotImportedReason <Professor>() { Data = item, Url = LastUrl, Reason = mex.Message }; Result.NotImported.Enqueue(reason); Log(reason.Reason); } catch (AggregateException agex) { var exception = agex.InnerExceptions[0]; var reason = new NotImportedReason <Professor>() { Data = item, Url = LastUrl, Reason = exception.Message, Exception = exception }; Result.NotImported.Enqueue(reason); Log(reason.Reason); } catch (Exception ex) { var reason = new NotImportedReason <Professor>() { Data = item, Url = LastUrl, Exception = ex, Reason = ex.Message }; Result.NotImported.Enqueue(reason); Log(reason.Reason); } }