206 lines
7.6 KiB
C#
206 lines
7.6 KiB
C#
using System.IO;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Newtonsoft.Json;
|
|
using PipefyAddCompanies.Core.Models;
|
|
|
|
namespace PipefyAddCompanies.Core.Services
|
|
{
|
|
public class PipefyService
|
|
{
|
|
private readonly HttpService _httpService;
|
|
private readonly string _pipeId;
|
|
private readonly string _table_id;
|
|
|
|
public PipefyService()
|
|
{
|
|
var config = new ConfigurationBuilder()
|
|
.SetBasePath(Directory.GetCurrentDirectory())
|
|
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
|
|
.Build();
|
|
|
|
_pipeId = config["Pipefy:PipeId"] ?? string.Empty;
|
|
_table_id = config["Pipefy:TableId"] ?? string.Empty;
|
|
|
|
_httpService = new HttpService();
|
|
}
|
|
|
|
// Método para verificar se a empresa já existe no Pipefy
|
|
public async Task<bool> VerificarRegistroEmpresaExistente(string nomeEmpresa)
|
|
{
|
|
var query = new
|
|
{
|
|
query = @"query GetRecords($table_id: ID!, $title: String!)
|
|
{
|
|
table_records(table_id: $table_id, first:50, search: {title: $title})
|
|
{
|
|
pageInfo{
|
|
hasNextPage
|
|
endCursor
|
|
}
|
|
edges{
|
|
node{
|
|
record_fields{
|
|
field {
|
|
id
|
|
}
|
|
value
|
|
array_value
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}",
|
|
variables = new
|
|
{
|
|
table_id = _table_id,
|
|
title = nomeEmpresa
|
|
}
|
|
};
|
|
|
|
var response = await _httpService.FazerRequisicaoApiAsync(JsonConvert.SerializeObject(query));
|
|
var test = response?.data?.table_records?.edges;
|
|
return test?.Count > 0;
|
|
}
|
|
// Método para criar um card no Pipefy
|
|
public async Task<string> CriarRecordNoPipefy(string nomeEmpresa)
|
|
{
|
|
var mutation = new
|
|
{
|
|
query = $@"
|
|
mutation($table_id: ID!, $field_id: ID!, $field_value: [UndefinedInput!], $title: String!)
|
|
{{
|
|
createTableRecord
|
|
(
|
|
input:
|
|
{{
|
|
table_id: $table_id
|
|
fields_attributes:
|
|
[
|
|
{{
|
|
field_id: $field_id,
|
|
field_value: $field_value
|
|
}}
|
|
]
|
|
title: $title
|
|
}}
|
|
)
|
|
{{
|
|
table_record{{
|
|
id
|
|
title
|
|
}}
|
|
}}
|
|
}}",
|
|
variables = new
|
|
{
|
|
table_id = _table_id,
|
|
field_id = "nome_da_empresa",
|
|
field_value = nomeEmpresa,
|
|
title = nomeEmpresa
|
|
}
|
|
};
|
|
|
|
var response = await _httpService.FazerRequisicaoApiAsync(JsonConvert.SerializeObject(mutation));
|
|
return response?.data?.createTableRecord?.table_record?.id ?? "";
|
|
}
|
|
// Método para criar um card no Pipefy
|
|
public async Task CriarCardNoPipefy(Empresa Empresa)
|
|
{
|
|
var mutation = $@"
|
|
mutation{{
|
|
createCard(
|
|
input:
|
|
{{
|
|
pipe_id: 303017662,
|
|
title: ""{Empresa.Nome}"",
|
|
fields_attributes:
|
|
[
|
|
{{
|
|
field_id: ""respons_vel"",
|
|
field_value:
|
|
[
|
|
302409638,
|
|
99999999
|
|
]
|
|
}}
|
|
{{
|
|
field_id: ""cidade_importa_o"",
|
|
field_value: {Empresa.Cidade}
|
|
}}
|
|
{{
|
|
field_id: ""estado_importa_o"",
|
|
field_value: {Empresa.Estado}
|
|
}}
|
|
{{
|
|
field_id: ""ramo_de_atividade"",
|
|
field_value: {Empresa.RamoAtividade}
|
|
}}
|
|
{{
|
|
field_id: ""coment_rios"",
|
|
field_value: {Empresa.Comentarios}
|
|
}}
|
|
{{
|
|
field_id: ""telefone_da_empresa_1"",
|
|
field_value: {Empresa.TelefoneEmpresa}
|
|
}}
|
|
{{
|
|
field_id: ""nome_do_respons_vel_por_energia"",
|
|
field_value: {Empresa.ResponsavelEnergia}
|
|
}}
|
|
{{
|
|
field_id: ""rea_do_respons_vel"",
|
|
field_value: {Empresa.AreaResponsavel}
|
|
}}
|
|
{{
|
|
field_id: ""telefone_do_respons_vel"",
|
|
field_value: {Empresa.TelefoneResponsavel}
|
|
}}
|
|
{{
|
|
field_id: ""celular_respons_vel"",
|
|
field_value: {Empresa.CelularResponsavel}
|
|
}}
|
|
{{
|
|
field_id: ""email_do_respons_vel"",
|
|
field_value: {Empresa.EmailResponsavel}
|
|
}}
|
|
{{
|
|
field_id: ""e_mail_2"",
|
|
field_value: {Empresa.Email2}
|
|
}}
|
|
{{
|
|
field_id: ""ambiente_de_contrata_o"",
|
|
field_value: {Empresa.Ambiente}
|
|
}}
|
|
{{
|
|
field_id: ""distribuidora"",
|
|
field_value: {Empresa.Distribuidora}
|
|
}}
|
|
{{
|
|
field_id: ""valor_da_fatura_1"",
|
|
field_value: {Empresa.ValorFatura}
|
|
}}
|
|
{{
|
|
field_id: ""empresa_bd"",
|
|
field_value: {Empresa.empresa_bd}
|
|
}}
|
|
{{
|
|
field_id: ""cidade_uf"",
|
|
field_value: {Empresa.cidade_uf}
|
|
}}
|
|
]
|
|
}}
|
|
)
|
|
{{
|
|
card
|
|
{{
|
|
id
|
|
}}
|
|
}}
|
|
}}";
|
|
|
|
await _httpService.FazerRequisicaoApiAsync(mutation);
|
|
}
|
|
}
|
|
}
|