public static SuspendedUserResult SuspendItem(this AbstractProxy proxy, object item, Modalidade modalidade, HttpClient httpClient) { SuspendedUserResult suspendedResult = new SuspendedUserResult() { MoodleId = null, LastUrl = "" }; UpdateUserClient client = new UpdateUserClient(); GetUserByUsernameClient getUserClient = new GetUserByUsernameClient(); client.AddHttpClient(httpClient); getUserClient.AddHttpClient(httpClient); proxy.BuildMoodleClient(client, MoodleTokenType.OfficialMoodleApiFunctions); proxy.BuildMoodleClient(getUserClient, MoodleTokenType.LocalMoodleExternalApiGetInfoToken); long?moodleId = null; if (item is Professor professor) { moodleId = professor?.GetMoodleUserId(modalidade, getUserClient, httpClient); } else if (item is Aluno aluno) { moodleId = aluno?.GetMoodleUserId(modalidade, getUserClient, httpClient); } if (!moodleId.HasValue) { return(suspendedResult); } UpdateUserRequest request = new UpdateUserRequest() { Id = moodleId.Value, Suspended = 1 }; Task <EmptyResponse> task = client.Post(request); task.Wait(); suspendedResult.LastUrl = client.LastUrl; suspendedResult.MoodleId = moodleId; return(suspendedResult); }
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); } }