Wonky.Client/Wonky.Client/Local.Services/VatInfoLookupService.cs
2023-07-22 17:39:35 +02:00

138 lines
No EOL
4.9 KiB
C#

// Copyright (C) 2022 FCS Frede's Computer Services.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see [https://www.gnu.org/licenses/agpl-3.0.en.html]
//
using System.Text;
using System.Text.Json;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Primitives;
using Wonky.Client.Helpers;
using Wonky.Entity.Configuration;
using Wonky.Entity.Models;
using Wonky.Entity.Requests;
namespace Wonky.Client.Local.Services;
public class VatInfoLookupService
{
private readonly JsonSerializerOptions _options = new() { PropertyNameCaseInsensitive = true };
private readonly HttpClient _client;
private readonly ApiConfig _api;
public VatInfoLookupService(HttpClient client, IOptions<ApiConfig> apiConfig)
{
_client = client;
_api = apiConfig.Value;
}
public async Task<List<VirkRegInfo>> QueryVirkRegistry(VirkParams query)
{
Console.WriteLine(JsonSerializer.Serialize(query));
if(!ValidateCvrQuery(query))
throw new ArgumentException("CvrQuery does not validate");
var queryDictionary = new Dictionary<string, string>
{
["vatNumber"] = $"{query.VatNumber}",
["streetName"] = $"{query.StreetName}",
["houseNumber"] = $"{query.HouseNumber}",
["zipCode"] = $"{query.ZipCode}",
["entityName"] = $"{query.EntityName}"
};
var endpoint = WebUtils.QueryHelper($"{_api.ServiceVatDk}", queryDictionary);
var response = await _client.GetAsync(endpoint.ToString());
var content = await response.Content.ReadAsStringAsync();
Console.WriteLine(content);
var jsonResult = JsonSerializer.Deserialize<List<VirkRegInfo>>(content, _options);
if (string.IsNullOrWhiteSpace(content) || !jsonResult.Any())
return new List<VirkRegInfo>();
if (!string.IsNullOrWhiteSpace(query.VatNumber))
return jsonResult;
var result = jsonResult
//.Where(x => x.States[^1].State == "NORMAL")
.OrderBy(x => x.Name).ToList();
return result.Count == 0 ? new List<VirkRegInfo>() : result;
}
public async Task<List<VirkRegInfo>> QueryBrRegistry(string vatNumber)
{
var queryDictionary = new Dictionary<string, string>
{
["vatNumber"] = $"{vatNumber}"
};
var endpoint = WebUtils.QueryHelper($"{_api.ServiceVatNo}", queryDictionary);
var response = await _client.GetAsync(endpoint.ToString());
var content = await response.Content.ReadAsStringAsync();
var jsonResult = JsonSerializer.Deserialize<List<VirkRegInfo>>(content, _options);
if (string.IsNullOrWhiteSpace(content) || !jsonResult.Any())
return new List<VirkRegInfo>();
var result = jsonResult
//.Where(x => x.States[^1].State == "NORMAL")
.OrderBy(x => x.Name).ToList();
return result.Count == 0 ? new List<VirkRegInfo>() : result;
}
public async Task<List<VirkRegInfo>> QueryViesRegistry(string vatNumber)
{
var queryDictionary = new Dictionary<string, string>
{
["vatNumber"] = $"{vatNumber}"
};
var endpoint = WebUtils.QueryHelper($"{_api.ServiceVatEu}", queryDictionary);
var response = await _client.GetAsync(endpoint.ToString());
var content = await response.Content.ReadAsStringAsync();
var jsonResult = JsonSerializer.Deserialize<List<VirkRegInfo>>(content, _options);
if (string.IsNullOrWhiteSpace(content) || !jsonResult.Any()) return new List<VirkRegInfo>();
var result = jsonResult
//.Where(x => x.States[^1].State == "NORMAL")
.OrderBy(x => x.Name).ToList();
return result.Count == 0 ? new List<VirkRegInfo>() : result;
}
private static bool ValidateCvrQuery(VirkParams query)
{
if (!string.IsNullOrWhiteSpace(query.VatNumber))
return true;
if (!string.IsNullOrWhiteSpace(query.EntityName))
return true;
return !string.IsNullOrWhiteSpace(query.HouseNumber)
&& !string.IsNullOrWhiteSpace(query.StreetName)
&& !string.IsNullOrWhiteSpace(query.ZipCode);
}
}