147 lines
5.2 KiB
C#
147 lines
5.2 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
||
using OpenRHCall.Models;
|
||
using PuppeteerSharp;
|
||
|
||
namespace OpenRHCall.Controllers
|
||
{
|
||
[ApiController]
|
||
[Route("[controller]")]
|
||
public class CallRHController : ControllerBase
|
||
{
|
||
private readonly ILogger<CallRHController> _logger;
|
||
|
||
public CallRHController(ILogger<CallRHController> logger)
|
||
{
|
||
_logger = logger;
|
||
}
|
||
|
||
[HttpPost(Name = "Create")]
|
||
public async Task<IActionResult> Create(CallRequest request)
|
||
{
|
||
if (request == null) return BadRequest();
|
||
if (string.IsNullOrEmpty(request.Nome)) return BadRequest();
|
||
if (string.IsNullOrEmpty(request.Email)) return BadRequest();
|
||
if (string.IsNullOrEmpty(request.TipoSolicitacao)) return BadRequest();
|
||
if (string.IsNullOrEmpty(request.Descricao)) return BadRequest();
|
||
if (string.IsNullOrEmpty(request.WhatsApp)) return BadRequest();
|
||
|
||
// Download and launch browser
|
||
await new BrowserFetcher().DownloadAsync();
|
||
|
||
#if DEBUG
|
||
var browser = await Puppeteer.LaunchAsync(new LaunchOptions
|
||
{
|
||
Headless = false
|
||
});
|
||
#else
|
||
var browser = await Puppeteer.LaunchAsync(GetLaunchOptions());
|
||
#endif
|
||
|
||
var page = await browser.NewPageAsync();
|
||
|
||
try
|
||
{
|
||
// Navigate to the form page - replace with your actual URL
|
||
await page.GoToAsync("https://app.pipefy.com/portals/efdbbe89-b25e-40e3-8026-8120b05c137c");
|
||
|
||
await page.WaitForSelectorAsync("a[data-title^='Solicita']");
|
||
|
||
await page.ClickAsync("a[data-title^='Solicita']");
|
||
|
||
var elementFrame = await page.WaitForSelectorAsync("#iFrameResizer0");
|
||
|
||
var frame = await elementFrame.ContentFrameAsync();
|
||
|
||
await frame.WaitForSelectorAsync("#publicForm-ShortText-nome_do_solicitante");
|
||
|
||
// Fill Nome Completo
|
||
await frame.TypeAsync("#publicForm-ShortText-nome_do_solicitante", request.Nome);
|
||
|
||
// Fill Email
|
||
await frame.TypeAsync("#publicForm-Email-email_do_solicitante", request.Email);
|
||
|
||
// Fill Celular
|
||
await frame.TypeAsync("#publicForm-ShortText-celular_com_whatsapp", request.WhatsApp);
|
||
|
||
// Select Tipo da solicita<74><61>o
|
||
await frame.WaitForSelectorAsync(".sc-ayeQl.ituYoD");
|
||
await frame.ClickAsync("[data-testid='select-field']");
|
||
|
||
await frame.WaitForSelectorAsync($"div[data-testid='list-item-content']:has(div[title='{request.TipoSolicitacao}'])",
|
||
new WaitForSelectorOptions { Visible = true });
|
||
|
||
await PressionarSetaAteOpcao(frame, page, request.TipoSolicitacao);
|
||
|
||
// Fill Descri<72><69>o
|
||
await frame.TypeAsync("#publicForm-LongText-informa_es_adicionais", request.Descricao);
|
||
|
||
//// Submit form
|
||
// Wait for submission to complete - adjust selector based on success indicator
|
||
await frame.ClickAsync("#submit-form-button");
|
||
await frame.WaitForNavigationAsync();
|
||
|
||
return Ok(new { message = "Solicita<74><61>o criada com sucesso!" });
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
return BadRequest(new { error = ex.Message });
|
||
}
|
||
finally
|
||
{
|
||
await browser.CloseAsync();
|
||
}
|
||
}
|
||
|
||
async Task PressionarSetaAteOpcao(IFrame frame, IPage page, string opcaoDesejada)
|
||
{
|
||
bool opcaoEncontrada = false;
|
||
int maxTentativas = 30; // Limite de seguran<61>a para n<>o ficar em loop infinito
|
||
int tentativa = 0;
|
||
|
||
while (!opcaoEncontrada && tentativa < maxTentativas)
|
||
{
|
||
// Pega o elemento selecionado atual
|
||
var elementoSelecionado = await frame.EvaluateExpressionAsync<string>(
|
||
"document.querySelector('div[data-focused=\"true\"] div[data-lib=\"lumen\"]')?.getAttribute('title')");
|
||
|
||
if (elementoSelecionado == opcaoDesejada)
|
||
{
|
||
opcaoEncontrada = true;
|
||
await page.Keyboard.PressAsync("Enter");
|
||
break;
|
||
}
|
||
|
||
await page.Keyboard.PressAsync("ArrowDown");
|
||
await Task.Delay(200);
|
||
tentativa++;
|
||
}
|
||
|
||
if (!opcaoEncontrada)
|
||
{
|
||
throw new Exception($"Op<4F><70>o {opcaoDesejada} n<>o encontrada ap<61>s {maxTentativas} tentativas");
|
||
}
|
||
}
|
||
|
||
private static LaunchOptions GetLaunchOptions()
|
||
{
|
||
return new LaunchOptions
|
||
{
|
||
Headless = true,
|
||
ExecutablePath = "/usr/bin/google-chrome-stable",
|
||
Args = new[]
|
||
{
|
||
"--no-sandbox",
|
||
"--disable-setuid-sandbox",
|
||
"--disable-dev-shm-usage",
|
||
"--disable-gpu"
|
||
},
|
||
DefaultViewport = new ViewPortOptions
|
||
{
|
||
Width = 1920,
|
||
Height = 1080
|
||
}
|
||
};
|
||
}
|
||
}
|
||
}
|